2 * This file is part of the sigrok project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "sigviewport.h"
23 #include "sigsession.h"
29 #include <QMouseEvent>
30 #include <QTextStream>
34 #include <boost/foreach.hpp>
36 using namespace boost;
39 const int SigViewport::SignalHeight = 50;
41 const int SigViewport::MinorTickSubdivision = 4;
42 const int SigViewport::ScaleUnits[3] = {1, 2, 5};
44 const QString SigViewport::SIPrefixes[9] =
45 {"f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G"};
46 const int SigViewport::FirstSIPrefixPower = -15;
48 SigViewport::SigViewport(SigView &parent) :
52 setMouseTracking(true);
53 setAutoFillBackground(false);
56 int SigViewport::get_total_height() const
59 BOOST_FOREACH(const shared_ptr<Signal> s,
60 _view._session.get_signals()) {
62 height += SignalHeight;
68 void SigViewport::initializeGL()
72 void SigViewport::resizeGL(int width, int height)
74 setup_viewport(width, height);
77 void SigViewport::paintEvent(QPaintEvent *event)
81 const vector< shared_ptr<Signal> > &sigs =
82 _view._session.get_signals();
84 // Prepare for OpenGL rendering
86 glMatrixMode(GL_MODELVIEW);
89 setup_viewport(width(), height());
91 qglClearColor(Qt::white);
92 glClear(GL_COLOR_BUFFER_BIT);
95 glEnable(GL_SCISSOR_TEST);
96 glScissor(SigView::LabelMarginWidth, 0, width(), height());
97 offset = SigView::RulerHeight - _view.v_offset();
98 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
102 const QRect signal_rect(SigView::LabelMarginWidth, offset,
103 width() - SigView::LabelMarginWidth, SignalHeight);
105 s->paint(*this, signal_rect, _view.scale(), _view.offset());
107 offset += SignalHeight;
110 glDisable(GL_SCISSOR_TEST);
112 // Prepare for QPainter rendering
113 glMatrixMode(GL_MODELVIEW);
116 QPainter painter(this);
117 painter.setRenderHint(QPainter::Antialiasing);
120 offset = SigView::RulerHeight - _view.v_offset();
121 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
125 const QRect label_rect(0, offset,
126 SigView::LabelMarginWidth, SignalHeight);
127 s->paint_label(painter, label_rect);
129 offset += SignalHeight;
133 paint_ruler(painter);
138 void SigViewport::mousePressEvent(QMouseEvent *event)
142 _mouse_down_point = event->pos();
143 _mouse_down_offset = _view.offset();
146 void SigViewport::mouseMoveEvent(QMouseEvent *event)
150 if(event->buttons() & Qt::LeftButton)
152 _view.set_scale_offset(_view.scale(),
154 (_mouse_down_point - event->pos()).x() *
159 void SigViewport::mouseReleaseEvent(QMouseEvent *event)
164 void SigViewport::wheelEvent(QWheelEvent *event)
167 _view.zoom(event->delta() / 120, event->x() -
168 SigView::LabelMarginWidth);
171 void SigViewport::setup_viewport(int width, int height)
173 glViewport(0, 0, (GLint)width, (GLint)height);
174 glMatrixMode(GL_PROJECTION);
176 glOrtho(0, width, height, 0, -1, 1);
177 glMatrixMode(GL_MODELVIEW);
180 void SigViewport::paint_ruler(QPainter &p)
182 const double MinSpacing = 80;
184 const double min_period = _view.scale() * MinSpacing;
186 const int order = (int)floorf(log10f(min_period));
187 const double order_decimal = pow(10, order);
190 double tick_period = 0.0f;
194 tick_period = order_decimal * ScaleUnits[unit++];
195 } while(tick_period < min_period && unit < countof(ScaleUnits));
197 const int prefix = (order - FirstSIPrefixPower) / 3;
199 assert(prefix < countof(SIPrefixes));
201 const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
202 Qt::AlignLeft | Qt::AlignTop, "8").height();
204 // Draw the tick marks
207 const double minor_tick_period = tick_period / MinorTickSubdivision;
208 const double first_major_division =
209 floor(_view.offset() / tick_period);
210 const double first_minor_division =
211 ceil(_view.offset() / minor_tick_period);
212 const double t0 = first_major_division * tick_period;
214 int division = (int)round(first_minor_division -
215 first_major_division * MinorTickSubdivision);
218 const double t = t0 + division * minor_tick_period;
219 const double x = (t - _view.offset()) / _view.scale() +
220 SigView::LabelMarginWidth;
225 if(division % MinorTickSubdivision == 0)
230 ts << (t / order_decimal) << SIPrefixes[prefix] << "s";
231 p.drawText(x, 0, 0, text_height, Qt::AlignCenter | Qt::AlignTop |
232 Qt::TextDontClip, s);
233 p.drawLine(x, text_height, x, SigView::RulerHeight);
239 (text_height + SigView::RulerHeight) / 2, x,
240 SigView::RulerHeight);