]> sigrok.org Git - pulseview.git/blame - pv/view/header.cpp
Allow labels to be set to empty values
[pulseview.git] / pv / view / header.cpp
CommitLineData
1d8dca91 1/*
b3f22de0 2 * This file is part of the PulseView project.
1d8dca91
JH
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 "header.h"
22#include "view.h"
23
8d634081 24#include "signal.h"
51e77110 25#include "../sigsession.h"
1d8dca91
JH
26
27#include <assert.h>
28
29#include <boost/foreach.hpp>
30
e3374498 31#include <QApplication>
b3b57abc 32#include <QColorDialog>
49f8ff3f
JH
33#include <QInputDialog>
34#include <QMenu>
a29bb7fb 35#include <QMouseEvent>
1d8dca91
JH
36#include <QPainter>
37#include <QRect>
38
39using namespace boost;
40using namespace std;
41
42namespace pv {
43namespace view {
44
45Header::Header(View &parent) :
46 QWidget(&parent),
49f8ff3f 47 _view(parent),
b3b57abc
JH
48 _action_set_name(new QAction(tr("Set &Name..."), this)),
49 _action_set_colour(new QAction(tr("Set &Colour..."), this))
1d8dca91 50{
a29bb7fb 51 setMouseTracking(true);
49f8ff3f
JH
52
53 connect(_action_set_name, SIGNAL(triggered()),
54 this, SLOT(on_action_set_name_triggered()));
b3b57abc
JH
55 connect(_action_set_colour, SIGNAL(triggered()),
56 this, SLOT(on_action_set_colour_triggered()));
07204819
JH
57
58 connect(&_view, SIGNAL(signals_moved()),
59 this, SLOT(on_signals_moved()));
1d8dca91
JH
60}
61
e3374498
JH
62boost::shared_ptr<pv::view::Signal> Header::get_mouse_over_signal(
63 const QPoint &pt)
64{
65 const int w = width();
3868e5fa
JH
66 const vector< shared_ptr<Signal> > sigs(
67 _view.session().get_signals());
e3374498
JH
68
69 const int v_offset = _view.v_offset();
70 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
71 {
72 assert(s);
73
2658961b
JH
74 if (s->pt_in_label_rect(s->get_v_offset() - v_offset,
75 0, w, pt))
e3374498
JH
76 return s;
77 }
78
79 return shared_ptr<Signal>();
80}
81
e314eca4 82void Header::paintEvent(QPaintEvent*)
1d8dca91
JH
83{
84 const int w = width();
3868e5fa
JH
85 const vector< shared_ptr<Signal> > sigs(
86 _view.session().get_signals());
1d8dca91
JH
87
88 QPainter painter(this);
89 painter.setRenderHint(QPainter::Antialiasing);
90
2e575351 91 const int v_offset = _view.v_offset();
fdf5ff04 92 const bool dragging = !_drag_sigs.empty();
1d8dca91
JH
93 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
94 {
95 assert(s);
96
2658961b 97 const int y = s->get_v_offset() - v_offset;
fdf5ff04 98 const bool highlight = !dragging && s->pt_in_label_rect(
2658961b
JH
99 y, 0, w, _mouse_point);
100 s->paint_label(painter, y, w, highlight);
1d8dca91
JH
101 }
102
103 painter.end();
104}
105
e3374498
JH
106void Header::mousePressEvent(QMouseEvent *event)
107{
108 assert(event);
109
3868e5fa
JH
110 const vector< shared_ptr<Signal> > sigs(
111 _view.session().get_signals());
e3374498 112
333d5bbc 113 if (event->button() & Qt::LeftButton) {
54401bbb
JH
114 _mouse_down_point = event->pos();
115
da2bebfb 116 // Save the offsets of any signals which will be dragged
54401bbb 117 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
333d5bbc 118 if (s->selected())
da2bebfb
JH
119 _drag_sigs.push_back(
120 make_pair(s, s->get_v_offset()));
121 }
122
123 // Select the signal if it has been clicked
124 const shared_ptr<Signal> mouse_over_signal =
125 get_mouse_over_signal(event->pos());
333d5bbc
UH
126 if (mouse_over_signal) {
127 if (mouse_over_signal->selected())
da2bebfb
JH
128 mouse_over_signal->select(false);
129 else {
130 mouse_over_signal->select(true);
131
333d5bbc 132 if (~QApplication::keyboardModifiers() &
07204819
JH
133 Qt::ControlModifier)
134 _drag_sigs.clear();
135
da2bebfb 136 // Add the signal to the drag list
333d5bbc 137 if (event->button() & Qt::LeftButton)
07204819
JH
138 _drag_sigs.push_back(
139 make_pair(mouse_over_signal,
da2bebfb
JH
140 mouse_over_signal->get_v_offset()));
141 }
54401bbb
JH
142 }
143
333d5bbc 144 if (~QApplication::keyboardModifiers() & Qt::ControlModifier) {
07204819
JH
145 // Unselect all other signals because the Ctrl is not
146 // pressed
147 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
333d5bbc 148 if (s != mouse_over_signal)
07204819
JH
149 s->select(false);
150 }
151
e3374498
JH
152 update();
153}
154
54401bbb
JH
155void Header::mouseReleaseEvent(QMouseEvent *event)
156{
157 assert(event);
333d5bbc 158 if (event->button() == Qt::LeftButton) {
da2bebfb 159 _drag_sigs.clear();
4b192962
JH
160 _view.normalize_layout();
161 }
54401bbb
JH
162}
163
a29bb7fb
JH
164void Header::mouseMoveEvent(QMouseEvent *event)
165{
166 assert(event);
167 _mouse_point = event->pos();
54401bbb
JH
168
169 // Move the signals if we are dragging
333d5bbc 170 if (!_drag_sigs.empty()) {
54401bbb
JH
171 const int delta = event->pos().y() - _mouse_down_point.y();
172
333d5bbc 173 for (std::list<std::pair<boost::weak_ptr<Signal>,
da2bebfb
JH
174 int> >::iterator i = _drag_sigs.begin();
175 i != _drag_sigs.end(); i++) {
176 const boost::shared_ptr<Signal> sig((*i).first);
333d5bbc 177 if (sig) {
da2bebfb 178 const int y = (*i).second + delta;
49153683
JH
179 const int y_snap =
180 ((y + View::SignalSnapGridSize / 2) /
181 View::SignalSnapGridSize) *
182 View::SignalSnapGridSize;
da2bebfb
JH
183 sig->set_v_offset(y_snap);
184
185 // Ensure the signal is selected
186 sig->select();
49153683 187 }
da2bebfb
JH
188
189 }
54401bbb
JH
190
191 signals_moved();
192 }
193
a29bb7fb
JH
194 update();
195}
196
e314eca4 197void Header::leaveEvent(QEvent*)
a29bb7fb
JH
198{
199 _mouse_point = QPoint(-1, -1);
200 update();
201}
202
49f8ff3f
JH
203void Header::contextMenuEvent(QContextMenuEvent *event)
204{
e3374498 205 const shared_ptr<Signal> s = get_mouse_over_signal(_mouse_point);
49f8ff3f 206
333d5bbc 207 if (!s)
e3374498 208 return;
49f8ff3f 209
e3374498
JH
210 QMenu menu(this);
211 menu.addAction(_action_set_name);
212 menu.addAction(_action_set_colour);
49f8ff3f 213
e3374498
JH
214 _context_signal = s;
215 menu.exec(event->globalPos());
216 _context_signal.reset();
49f8ff3f
JH
217}
218
219void Header::on_action_set_name_triggered()
220{
2c982fc3
JH
221 bool ok = false;
222
8d634081 223 shared_ptr<view::Signal> context_signal = _context_signal;
333d5bbc 224 if (!context_signal)
49f8ff3f
JH
225 return;
226
227 const QString new_label = QInputDialog::getText(this, tr("Set Name"),
2c982fc3 228 tr("Name"), QLineEdit::Normal, context_signal->get_name(), &ok);
49f8ff3f 229
2c982fc3 230 if (ok)
49f8ff3f
JH
231 context_signal->set_name(new_label);
232}
233
b3b57abc
JH
234void Header::on_action_set_colour_triggered()
235{
8d634081 236 shared_ptr<view::Signal> context_signal = _context_signal;
333d5bbc 237 if (!context_signal)
b3b57abc
JH
238 return;
239
240 const QColor new_colour = QColorDialog::getColor(
241 context_signal->get_colour(), this, tr("Set Colour"));
242
333d5bbc 243 if (new_colour.isValid())
b3b57abc
JH
244 context_signal->set_colour(new_colour);
245}
246
07204819
JH
247void Header::on_signals_moved()
248{
249 update();
250}
251
252
1d8dca91
JH
253} // namespace view
254} // namespace pv