]> sigrok.org Git - pulseview.git/blob - pv/view/header.cpp
e50d851f686d315bc584f5e92510218b85f3ad08
[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
59 boost::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
82 void 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
91         const int v_offset = _view.v_offset();
92         BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
93         {
94                 assert(s);
95
96                 const QRect signal_heading_rect(
97                         0, s->get_v_offset() - v_offset,
98                         w, View::SignalHeight);
99
100                 s->paint_label(painter, signal_heading_rect,
101                         s->pt_in_label_rect(signal_heading_rect, _mouse_point));
102         }
103
104         painter.end();
105 }
106
107 void 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                 _drag_sigs.clear();
118                 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
119                         s->select(false);
120         }
121
122         if(event->button() & Qt::LeftButton) {
123                 _mouse_down_point = event->pos();
124
125                 // Save the offsets of any signals which will be dragged
126                 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
127                         if(s->selected())
128                                 _drag_sigs.push_back(
129                                         make_pair(s, s->get_v_offset()));
130         }
131
132         // Select the signal if it has been clicked
133         const shared_ptr<Signal> mouse_over_signal =
134                 get_mouse_over_signal(event->pos());
135         if(mouse_over_signal) {
136                 if(mouse_over_signal->selected())
137                         mouse_over_signal->select(false);
138                 else {
139                         mouse_over_signal->select(true);
140
141                         // Add the signal to the drag list
142                         _drag_sigs.push_back(
143                                 make_pair(mouse_over_signal,
144                                         mouse_over_signal->get_v_offset()));
145                 }
146         }
147
148         update();
149 }
150
151 void Header::mouseReleaseEvent(QMouseEvent *event)
152 {
153         assert(event);
154         if(event->button() == Qt::LeftButton)
155                 _drag_sigs.clear();
156 }
157
158 void Header::mouseMoveEvent(QMouseEvent *event)
159 {
160         assert(event);
161         _mouse_point = event->pos();
162
163         // Move the signals if we are dragging
164         if(!_drag_sigs.empty()) {
165                 const int delta = event->pos().y() - _mouse_down_point.y();
166
167                 for(std::list<std::pair<boost::weak_ptr<Signal>,
168                         int> >::iterator i = _drag_sigs.begin();
169                         i != _drag_sigs.end(); i++) {
170                         const boost::shared_ptr<Signal> sig((*i).first);
171                         if(sig) {
172                                 const int y = (*i).second + delta;
173                                 const int y_snap =
174                                         ((y + View::SignalSnapGridSize / 2) /
175                                                 View::SignalSnapGridSize) *
176                                                 View::SignalSnapGridSize;
177                                 sig->set_v_offset(y_snap);
178
179                                 // Ensure the signal is selected
180                                 sig->select();
181                         }
182                         
183                 }
184
185                 signals_moved();
186         }
187
188         update();
189 }
190
191 void Header::leaveEvent(QEvent *event)
192 {
193         _mouse_point = QPoint(-1, -1);
194         update();
195 }
196
197 void Header::contextMenuEvent(QContextMenuEvent *event)
198 {
199         const shared_ptr<Signal> s = get_mouse_over_signal(_mouse_point);
200
201         if(!s)
202                 return;
203
204         QMenu menu(this);
205         menu.addAction(_action_set_name);
206         menu.addAction(_action_set_colour);
207
208         _context_signal = s;
209         menu.exec(event->globalPos());
210         _context_signal.reset();
211 }
212
213 void Header::on_action_set_name_triggered()
214 {
215         shared_ptr<view::Signal> context_signal = _context_signal;
216         if(!context_signal)
217                 return;
218
219         const QString new_label = QInputDialog::getText(this, tr("Set Name"),
220                 tr("Name"), QLineEdit::Normal, context_signal->get_name());
221
222         if(!new_label.isEmpty())
223                 context_signal->set_name(new_label);
224 }
225
226 void Header::on_action_set_colour_triggered()
227 {
228         shared_ptr<view::Signal> context_signal = _context_signal;
229         if(!context_signal)
230                 return;
231
232         const QColor new_colour = QColorDialog::getColor(
233                 context_signal->get_colour(), this, tr("Set Colour"));
234
235         if(new_colour.isValid())
236                 context_signal->set_colour(new_colour);
237 }
238
239 } // namespace view
240 } // namespace pv