]> sigrok.org Git - pulseview.git/blob - pv/view/header.cpp
Added palette of colours for different decode annotation types
[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 <QMenu>
33 #include <QMouseEvent>
34 #include <QPainter>
35 #include <QRect>
36
37 using namespace boost;
38 using namespace std;
39
40 namespace pv {
41 namespace view {
42
43 Header::Header(View &parent) :
44         MarginWidget(parent)
45 {
46         setMouseTracking(true);
47
48         connect(&_view.session(), SIGNAL(signals_changed()),
49                 this, SLOT(on_signals_changed()));
50
51         connect(&_view, SIGNAL(signals_moved()),
52                 this, SLOT(on_signals_moved()));
53 }
54
55 shared_ptr<Trace> Header::get_mouse_over_trace(const QPoint &pt)
56 {
57         const int w = width();
58         const vector< shared_ptr<Trace> > traces(_view.get_traces());
59
60         BOOST_FOREACH(const shared_ptr<Trace> t, traces)
61         {
62                 assert(t);
63                 if (t->pt_in_label_rect(0, w, pt))
64                         return t;
65         }
66
67         return shared_ptr<Trace>();
68 }
69
70 void Header::clear_selection()
71 {
72         const vector< shared_ptr<Trace> > traces(_view.get_traces());
73         BOOST_FOREACH(const shared_ptr<Trace> t, traces) {
74                 assert(t);
75                 t->select(false);
76         }
77
78         update();
79 }
80
81 void Header::paintEvent(QPaintEvent*)
82 {
83         const int w = width();
84         const vector< shared_ptr<Trace> > traces(_view.get_traces());
85
86         QPainter painter(this);
87         painter.setRenderHint(QPainter::Antialiasing);
88
89         const bool dragging = !_drag_traces.empty();
90         BOOST_FOREACH(const shared_ptr<Trace> t, traces)
91         {
92                 assert(t);
93
94                 const bool highlight = !dragging && t->pt_in_label_rect(
95                         0, w, _mouse_point);
96                 t->paint_label(painter, w, highlight);
97         }
98
99         painter.end();
100 }
101
102 void Header::mousePressEvent(QMouseEvent *event)
103 {
104         assert(event);
105
106         const vector< shared_ptr<Trace> > traces(_view.get_traces());
107
108         if (event->button() & Qt::LeftButton) {
109                 _mouse_down_point = event->pos();
110
111                 // Save the offsets of any signals which will be dragged
112                 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
113                         if (t->selected())
114                                 _drag_traces.push_back(
115                                         make_pair(t, t->get_v_offset()));
116         }
117
118         // Select the signal if it has been clicked
119         const shared_ptr<Trace> mouse_over_trace =
120                 get_mouse_over_trace(event->pos());
121         if (mouse_over_trace) {
122                 if (mouse_over_trace->selected())
123                         mouse_over_trace->select(false);
124                 else {
125                         mouse_over_trace->select(true);
126
127                         if (~QApplication::keyboardModifiers() &
128                                 Qt::ControlModifier)
129                                 _drag_traces.clear();
130
131                         // Add the signal to the drag list
132                         if (event->button() & Qt::LeftButton)
133                                 _drag_traces.push_back(
134                                         make_pair(mouse_over_trace,
135                                         mouse_over_trace->get_v_offset()));
136                 }
137         }
138
139         if (~QApplication::keyboardModifiers() & Qt::ControlModifier) {
140                 // Unselect all other signals because the Ctrl is not
141                 // pressed
142                 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
143                         if (t != mouse_over_trace)
144                                 t->select(false);
145         }
146
147         selection_changed();
148         update();
149 }
150
151 void Header::mouseReleaseEvent(QMouseEvent *event)
152 {
153         assert(event);
154         if (event->button() == Qt::LeftButton) {
155                 _drag_traces.clear();
156                 _view.normalize_layout();
157         }
158 }
159
160 void Header::mouseMoveEvent(QMouseEvent *event)
161 {
162         assert(event);
163         _mouse_point = event->pos();
164
165         // Move the signals if we are dragging
166         if (!_drag_traces.empty()) {
167                 const int delta = event->pos().y() - _mouse_down_point.y();
168
169                 for (std::list<std::pair<boost::weak_ptr<Trace>,
170                         int> >::iterator i = _drag_traces.begin();
171                         i != _drag_traces.end(); i++) {
172                         const boost::shared_ptr<Trace> trace((*i).first);
173                         if (trace) {
174                                 const int y = (*i).second + delta;
175                                 const int y_snap =
176                                         ((y + View::SignalSnapGridSize / 2) /
177                                                 View::SignalSnapGridSize) *
178                                                 View::SignalSnapGridSize;
179                                 trace->set_v_offset(y_snap);
180
181                                 // Ensure the trace is selected
182                                 trace->select();
183                         }
184                         
185                 }
186
187                 signals_moved();
188         }
189
190         update();
191 }
192
193 void Header::leaveEvent(QEvent*)
194 {
195         _mouse_point = QPoint(-1, -1);
196         update();
197 }
198
199 void Header::contextMenuEvent(QContextMenuEvent *event)
200 {
201         const shared_ptr<Trace> t = get_mouse_over_trace(_mouse_point);
202
203         if (t)
204                 t->create_context_menu(this)->exec(event->globalPos());
205 }
206
207 void Header::on_signals_changed()
208 {
209         const vector< shared_ptr<Trace> > traces(_view.get_traces());
210         BOOST_FOREACH(shared_ptr<Trace> t, traces) {
211                 assert(t);
212                 connect(t.get(), SIGNAL(text_changed()), this, SLOT(update()));
213         }
214 }
215
216 void Header::on_signals_moved()
217 {
218         update();
219 }
220
221
222 } // namespace view
223 } // namespace pv