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