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
23 #include "sigsession.h"
28 #include <QMouseEvent>
32 #include <boost/foreach.hpp>
34 using namespace boost;
37 const int SigView::SignalHeight = 50;
38 const int SigView::LabelMarginWidth = 70;
39 const int SigView::RulerHeight = 30;
41 const int SigView::ScaleUnits[3] = {1, 2, 5};
43 SigView::SigView(SigSession &session, QWidget *parent) :
49 connect(&_session, SIGNAL(dataUpdated()),
50 this, SLOT(dataUpdated()));
52 setMouseTracking(true);
53 setAutoFillBackground(false);
56 void SigView::initializeGL()
60 void SigView::resizeGL(int width, int height)
62 setupViewport(width, height);
65 void SigView::paintEvent(QPaintEvent *event)
69 const vector< shared_ptr<Signal> > &sigs =
70 _session.get_signals();
72 // Prepare for OpenGL rendering
74 glMatrixMode(GL_MODELVIEW);
77 setupViewport(width(), height());
79 qglClearColor(Qt::white);
80 glClear(GL_COLOR_BUFFER_BIT);
84 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
88 const QRect signal_rect(LabelMarginWidth, offset,
89 width() - LabelMarginWidth, SignalHeight);
91 s->paint(*this, signal_rect, _scale, _offset);
93 offset += SignalHeight;
96 // Prepare for QPainter rendering
97 glMatrixMode(GL_MODELVIEW);
100 QPainter painter(this);
101 painter.setRenderHint(QPainter::Antialiasing);
104 offset = RulerHeight;
105 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
109 const QRect label_rect(0, offset,
110 LabelMarginWidth, SignalHeight);
111 s->paint_label(painter, label_rect);
113 offset += SignalHeight;
122 void SigView::dataUpdated()
127 void SigView::mouseMoveEvent(QMouseEvent *event)
132 void SigView::mousePressEvent(QMouseEvent *event)
137 void SigView::mouseReleaseEvent(QMouseEvent *event)
141 const double cursor_offset = _offset + _scale * (double)event->x();
143 switch(event->button())
149 case Qt::RightButton:
154 _offset = cursor_offset - _scale * (double)event->x();
159 void SigView::setupViewport(int width, int height)
161 glViewport(0, 0, (GLint)width, (GLint)height);
162 glMatrixMode(GL_PROJECTION);
164 glOrtho(0, width, height, 0, -1, 1);
165 glMatrixMode(GL_MODELVIEW);
168 void SigView::paintRuler(QPainter &p)
170 const double MinSpacing = 20;
172 double tick_period = 0.0f;
173 const double min_period = _scale * MinSpacing;
175 double order = 10e-15;
176 while(tick_period < min_period)
179 while(tick_period < min_period &&
180 unit < countof(ScaleUnits))
181 tick_period = order * ScaleUnits[unit++];
185 const double tick_seperation = tick_period / _scale;
187 p.setPen(Qt::transparent);
188 p.setBrush(QColor(0xC0, 0xC0, 0xC0));
189 p.drawRect(LabelMarginWidth, 0,
190 width() - LabelMarginWidth, RulerHeight);
194 const double offset_ticks = -_offset / tick_period;
195 double x = (offset_ticks - floor(offset_ticks)) *
196 tick_seperation + LabelMarginWidth;
199 p.drawLine(x, 0, x, RulerHeight);
200 x += tick_seperation;