]> sigrok.org Git - pulseview.git/blame - pv/view/header.cpp
Added snap to grid drag behaviour
[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()));
1d8dca91
JH
57}
58
e3374498
JH
59boost::shared_ptr<pv::view::Signal> Header::get_mouse_over_signal(
60 const QPoint &pt)
61{
62 const int w = width();
63 const vector< shared_ptr<Signal> > &sigs =
64 _view.session().get_signals();
65
66 const int v_offset = _view.v_offset();
67 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
68 {
69 assert(s);
70
71 const QRect signal_heading_rect(
72 0, s->get_v_offset() - v_offset,
73 w, View::SignalHeight);
74
75 if(s->pt_in_label_rect(signal_heading_rect, pt))
76 return s;
77 }
78
79 return shared_ptr<Signal>();
80}
81
1d8dca91
JH
82void Header::paintEvent(QPaintEvent *event)
83{
84 const int w = width();
85 const vector< shared_ptr<Signal> > &sigs =
86 _view.session().get_signals();
87
88 QPainter painter(this);
89 painter.setRenderHint(QPainter::Antialiasing);
90
2e575351 91 const int v_offset = _view.v_offset();
1d8dca91
JH
92 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
93 {
94 assert(s);
95
a29bb7fb 96 const QRect signal_heading_rect(
2e575351
JH
97 0, s->get_v_offset() - v_offset,
98 w, View::SignalHeight);
a29bb7fb
JH
99
100 s->paint_label(painter, signal_heading_rect,
ed6f0f4f 101 s->pt_in_label_rect(signal_heading_rect, _mouse_point));
1d8dca91
JH
102 }
103
104 painter.end();
105}
106
e3374498
JH
107void Header::mousePressEvent(QMouseEvent *event)
108{
109 assert(event);
110
111 const vector< shared_ptr<Signal> > &sigs =
112 _view.session().get_signals();
113
114 if(~QApplication::keyboardModifiers() & Qt::ControlModifier) {
115 // Unselect all other signals because the Ctrl is not
116 // pressed
117 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
118 s->select(false);
119 }
120
121 // Select the signal if it has been clicked
122 const shared_ptr<Signal> mouse_over_signal =
123 get_mouse_over_signal(event->pos());
124 if(mouse_over_signal)
125 mouse_over_signal->select(!mouse_over_signal->selected());
126
54401bbb
JH
127 if(event->button() & Qt::LeftButton) {
128 _mouse_down_point = event->pos();
129
130 // Save the current signal offsets
131 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
132 _mouse_down_signal_offsets[s.get()] =
133 s->get_v_offset();
134 }
135
e3374498
JH
136 update();
137}
138
54401bbb
JH
139void Header::mouseReleaseEvent(QMouseEvent *event)
140{
141 assert(event);
142 if(event->button() == Qt::LeftButton)
143 _mouse_down_signal_offsets.clear();
144}
145
a29bb7fb
JH
146void Header::mouseMoveEvent(QMouseEvent *event)
147{
148 assert(event);
149 _mouse_point = event->pos();
54401bbb
JH
150
151 // Move the signals if we are dragging
152 if(!_mouse_down_signal_offsets.empty()) {
153 const vector< shared_ptr<Signal> > &sigs =
154 _view.session().get_signals();
155 const int delta = event->pos().y() - _mouse_down_point.y();
156
157 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
49153683
JH
158 if(s->selected()) {
159 const int y =
54401bbb 160 _mouse_down_signal_offsets[s.get()] +
49153683
JH
161 delta;
162 const int y_snap =
163 ((y + View::SignalSnapGridSize / 2) /
164 View::SignalSnapGridSize) *
165 View::SignalSnapGridSize;
166 s->set_v_offset(y_snap );
167 }
54401bbb
JH
168
169 signals_moved();
170 }
171
a29bb7fb
JH
172 update();
173}
174
175void Header::leaveEvent(QEvent *event)
176{
177 _mouse_point = QPoint(-1, -1);
178 update();
179}
180
49f8ff3f
JH
181void Header::contextMenuEvent(QContextMenuEvent *event)
182{
e3374498 183 const shared_ptr<Signal> s = get_mouse_over_signal(_mouse_point);
49f8ff3f 184
e3374498
JH
185 if(!s)
186 return;
49f8ff3f 187
e3374498
JH
188 QMenu menu(this);
189 menu.addAction(_action_set_name);
190 menu.addAction(_action_set_colour);
49f8ff3f 191
e3374498
JH
192 _context_signal = s;
193 menu.exec(event->globalPos());
194 _context_signal.reset();
49f8ff3f
JH
195}
196
197void Header::on_action_set_name_triggered()
198{
8d634081 199 shared_ptr<view::Signal> context_signal = _context_signal;
49f8ff3f
JH
200 if(!context_signal)
201 return;
202
203 const QString new_label = QInputDialog::getText(this, tr("Set Name"),
204 tr("Name"), QLineEdit::Normal, context_signal->get_name());
205
206 if(!new_label.isEmpty())
207 context_signal->set_name(new_label);
208}
209
b3b57abc
JH
210void Header::on_action_set_colour_triggered()
211{
8d634081 212 shared_ptr<view::Signal> context_signal = _context_signal;
b3b57abc
JH
213 if(!context_signal)
214 return;
215
216 const QColor new_colour = QColorDialog::getColor(
217 context_signal->get_colour(), this, tr("Set Colour"));
218
219 if(new_colour.isValid())
220 context_signal->set_colour(new_colour);
221}
222
1d8dca91
JH
223} // namespace view
224} // namespace pv