]> sigrok.org Git - pulseview.git/blame - sigview.cpp
Initial implementation of labels
[pulseview.git] / sigview.cpp
CommitLineData
6fa02541
JH
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
009e1503 23#include "sigsession.h"
e3f65ace
JH
24#include "signal.h"
25
7cd5faf8
JH
26#include <QMouseEvent>
27
e3f65ace
JH
28#include <boost/foreach.hpp>
29
30using namespace boost;
31using namespace std;
32
33const int SigView::SignalHeight = 50;
3e46726a 34const int SigView::LabelMarginWidth = 70;
009e1503
JH
35
36SigView::SigView(SigSession &session, QWidget *parent) :
37 QGLWidget(parent),
3b18c57d 38 _session(session),
fb0d32cb 39 _scale(1e-6),
3b18c57d 40 _offset(0)
6fa02541 41{
009e1503
JH
42 connect(&_session, SIGNAL(dataUpdated()),
43 this, SLOT(dataUpdated()));
44
d7002724 45 setMouseTracking(true);
3e46726a 46 setAutoFillBackground(false);
d7002724
JH
47}
48
49void SigView::initializeGL()
50{
d7002724
JH
51}
52
53void SigView::resizeGL(int width, int height)
54{
3e46726a 55 setupViewport(width, height);
d7002724
JH
56}
57
3e46726a 58void SigView::paintEvent(QPaintEvent *event)
d7002724 59{
3e46726a 60 int offset;
e3f65ace 61
e3f65ace
JH
62 const vector< shared_ptr<Signal> > &sigs =
63 _session.get_signals();
3e46726a
JH
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;
e3f65ace
JH
98 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
99 {
100 assert(s);
3e46726a
JH
101
102 const QRect label_rect(0, offset,
103 LabelMarginWidth, SignalHeight);
104 s->paint_label(painter, label_rect);
105
106 offset += SignalHeight;
e3f65ace 107 }
3e46726a
JH
108
109 painter.end();
6fa02541 110}
009e1503
JH
111
112void SigView::dataUpdated()
113{
e3f65ace 114 update();
009e1503
JH
115}
116
7cd5faf8
JH
117void SigView::mouseMoveEvent(QMouseEvent *event)
118{
119 assert(event);
120}
121
122void SigView::mousePressEvent(QMouseEvent *event)
123{
124 assert(event);
125}
126
127void SigView::mouseReleaseEvent(QMouseEvent *event)
128{
129 assert(event);
130
fb0d32cb
JH
131 const double cursor_offset = _offset + _scale * (double)event->x();
132
7cd5faf8
JH
133 switch(event->button())
134 {
135 case Qt::LeftButton:
fb0d32cb 136 _scale *= 2.0 / 3.0;
7cd5faf8
JH
137 break;
138
139 case Qt::RightButton:
fb0d32cb 140 _scale *= 3.0 / 2.0;
7cd5faf8
JH
141 break;
142 }
143
fb0d32cb
JH
144 _offset = cursor_offset - _scale * (double)event->x();
145
3e46726a 146 update();
7cd5faf8
JH
147}
148
3e46726a
JH
149void 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}