]> sigrok.org Git - pulseview.git/blob - sigview.cpp
Added tango style highlights to labels
[pulseview.git] / sigview.cpp
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "sigview.h"
22
23 #include "sigsession.h"
24 #include "signal.h"
25
26 #include <QMouseEvent>
27
28 #include <boost/foreach.hpp>
29
30 using namespace boost;
31 using namespace std;
32
33 const int SigView::SignalHeight = 50;
34 const int SigView::LabelMarginWidth = 70;
35
36 SigView::SigView(SigSession &session, QWidget *parent) :
37         QGLWidget(parent),
38         _session(session),
39         _scale(1e-6),
40         _offset(0)
41 {
42         connect(&_session, SIGNAL(dataUpdated()),
43                 this, SLOT(dataUpdated()));
44
45         setMouseTracking(true);
46         setAutoFillBackground(false);
47 }
48
49 void SigView::initializeGL()
50 {
51 }
52
53 void SigView::resizeGL(int width, int height)
54 {
55         setupViewport(width, height);
56 }
57
58 void SigView::paintEvent(QPaintEvent *event)
59 {
60         int offset;
61
62         const vector< shared_ptr<Signal> > &sigs =
63                 _session.get_signals();
64
65         // Prepare for OpenGL rendering
66         makeCurrent();
67         glMatrixMode(GL_MODELVIEW);
68         glPushMatrix();
69
70         setupViewport(width(), height());
71
72         qglClearColor(Qt::white);
73         glClear(GL_COLOR_BUFFER_BIT);
74
75         // Plot the signal
76         offset = 0;
77         BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
78         {
79                 assert(s);
80
81                 const QRect signal_rect(LabelMarginWidth, offset,
82                         width() - LabelMarginWidth, SignalHeight);
83
84                 s->paint(*this, signal_rect, _scale, _offset);
85
86                 offset += SignalHeight;
87         }
88
89         // Prepare for QPainter rendering
90         glMatrixMode(GL_MODELVIEW);
91         glPopMatrix();
92
93         QPainter painter(this);
94         painter.setRenderHint(QPainter::Antialiasing);
95
96         // Paint the label
97         offset = 0;
98         BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
99         {
100                 assert(s);
101
102                 const QRect label_rect(0, offset,
103                         LabelMarginWidth, SignalHeight);
104                 s->paint_label(painter, label_rect);
105
106                 offset += SignalHeight;
107         }
108
109         painter.end();
110 }
111
112 void SigView::dataUpdated()
113 {
114         update();
115 }
116
117 void SigView::mouseMoveEvent(QMouseEvent *event)
118 {
119         assert(event);
120 }
121
122 void SigView::mousePressEvent(QMouseEvent *event)
123 {
124         assert(event);
125 }
126
127 void SigView::mouseReleaseEvent(QMouseEvent *event)
128 {
129         assert(event);
130
131         const double cursor_offset = _offset + _scale * (double)event->x();
132
133         switch(event->button())
134         {
135         case Qt::LeftButton:
136                 _scale *= 2.0 / 3.0;
137                 break;
138
139         case Qt::RightButton:
140                 _scale *= 3.0 / 2.0;
141                 break;
142         }
143
144         _offset = cursor_offset - _scale * (double)event->x();
145
146         update();
147 }
148
149 void SigView::setupViewport(int width, int height)
150 {
151         glViewport(0, 0, (GLint)width, (GLint)height);
152         glMatrixMode(GL_PROJECTION);
153         glLoadIdentity();
154         glOrtho(0, width, height, 0, -1, 1);
155         glMatrixMode(GL_MODELVIEW);
156 }