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