]> sigrok.org Git - pulseview.git/blame - pv/view/marginwidget.cpp
Added ViewWidget as a common ancestor of all view widgets
[pulseview.git] / pv / view / marginwidget.cpp
CommitLineData
c23b29d6
JH
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2013 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
6a2cc8dd 21#include <QApplication>
2155b66b
JH
22#include <QMenu>
23#include <QMouseEvent>
24
2acdb232 25#include "view.hpp"
c23b29d6 26
2acdb232 27#include "marginwidget.hpp"
c23b29d6 28
786b7678
JH
29#include <pv/widgets/popup.hpp>
30
31using std::shared_ptr;
32
c23b29d6
JH
33namespace pv {
34namespace view {
35
36MarginWidget::MarginWidget(View &parent) :
40aca27e 37 ViewWidget(parent),
f4433aa9 38 dragging_(false)
c23b29d6 39{
e02e05c6 40 setAttribute(Qt::WA_NoSystemBackground, true);
dbfae3f1
JH
41 setFocusPolicy(Qt::ClickFocus);
42 setMouseTracking(true);
c23b29d6
JH
43}
44
786b7678
JH
45void MarginWidget::show_popup(const shared_ptr<ViewItem> &item)
46{
47 pv::widgets::Popup *const p = item->create_popup(this);
48 if (p)
49 p->show();
50}
51
6a2cc8dd
JH
52void MarginWidget::mouse_left_press_event(QMouseEvent *event)
53{
54 (void)event;
55
56 const bool ctrl_pressed =
57 QApplication::keyboardModifiers() & Qt::ControlModifier;
58
59 // Clear selection if control is not pressed and this item is unselected
60 if ((!mouse_down_item_ || !mouse_down_item_->selected()) &&
61 !ctrl_pressed)
62 clear_selection();
63
64 // Set the signal selection state if the item has been clicked
65 if (mouse_down_item_) {
66 if (ctrl_pressed)
67 mouse_down_item_->select(!mouse_down_item_->selected());
68 else
69 mouse_down_item_->select(true);
70 }
71
72 // Save the offsets of any signals which will be dragged
73 const auto items = this->items();
74 for (auto &i : items)
75 if (i->selected())
76 i->drag();
77
78 selection_changed();
79 update();
80}
81
e47b6988
JH
82void MarginWidget::mouse_left_release_event(QMouseEvent *event)
83{
84 assert(event);
85
86 auto items = this->items();
87 const bool ctrl_pressed =
88 QApplication::keyboardModifiers() & Qt::ControlModifier;
89
90 // Unselect everything if control is not pressed
91 const shared_ptr<ViewItem> mouse_over =
92 get_mouse_over_item(event->pos());
93
94 for (auto &i : items)
95 i->drag_release();
96
97 if (dragging_)
98 view_.restack_all_row_items();
99 else
100 {
101 if (!ctrl_pressed) {
102 for (shared_ptr<ViewItem> i : items)
103 if (mouse_down_item_ != i)
104 i->select(false);
105
106 if (mouse_down_item_)
107 show_popup(mouse_down_item_);
108 }
109 }
110
111 dragging_ = false;
112}
113
6a2cc8dd
JH
114void MarginWidget::mousePressEvent(QMouseEvent *event)
115{
116 assert(event);
117
118 mouse_down_point_ = event->pos();
119 mouse_down_item_ = get_mouse_over_item(event->pos());
120
121 if (event->button() & Qt::LeftButton)
122 mouse_left_press_event(event);
123}
124
e47b6988
JH
125void MarginWidget::mouseReleaseEvent(QMouseEvent *event)
126{
127 assert(event);
128 if (event->button() & Qt::LeftButton)
129 mouse_left_release_event(event);
130
131 mouse_down_item_ = nullptr;
132}
133
2f798236
JH
134void MarginWidget::mouseMoveEvent(QMouseEvent *event)
135{
136 assert(event);
137 mouse_point_ = event->pos();
138
139 if (!(event->buttons() & Qt::LeftButton))
140 return;
141
142 if ((event->pos() - mouse_down_point_).manhattanLength() <
143 QApplication::startDragDistance())
144 return;
145
146 if (!accept_drag())
147 return;
148
149 // Do the drag
150 dragging_ = true;
151 drag_items(event->pos() - mouse_down_point_);
152
153 update();
154}
155
19552bb8
JH
156void MarginWidget::leaveEvent(QEvent*)
157{
158 mouse_point_ = QPoint(-1, -1);
159 update();
160}
161
2155b66b
JH
162void MarginWidget::contextMenuEvent(QContextMenuEvent *event)
163{
164 const shared_ptr<ViewItem> r = get_mouse_over_item(mouse_point_);
165 if (!r)
166 return;
167
168 QMenu *menu = r->create_context_menu(this);
169 if (menu)
170 menu->exec(event->globalPos());
171}
172
bb4a0e8e
JH
173void MarginWidget::keyPressEvent(QKeyEvent *e)
174{
175 assert(e);
176
177 if (e->key() == Qt::Key_Delete)
178 {
179 const auto items = this->items();
180 for (auto &i : items)
181 if (i->selected())
182 i->delete_pressed();
183 }
184}
185
84a0d458
JS
186void MarginWidget::clear_selection()
187{
5a6a4ce3
JH
188 const auto items = this->items();
189 for (auto &i : items)
190 i->select(false);
191 update();
84a0d458
JS
192}
193
c23b29d6
JH
194} // namespace view
195} // namespace pv