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