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