]> sigrok.org Git - pulseview.git/blame - pv/view/marginwidget.cpp
MarginWidget: Moved in mouseReleaseEvent
[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) :
37 QWidget(&parent),
f4433aa9
JH
38 view_(parent),
39 dragging_(false)
c23b29d6 40{
e02e05c6 41 setAttribute(Qt::WA_NoSystemBackground, true);
dbfae3f1
JH
42 setFocusPolicy(Qt::ClickFocus);
43 setMouseTracking(true);
c23b29d6
JH
44}
45
786b7678
JH
46void MarginWidget::show_popup(const shared_ptr<ViewItem> &item)
47{
48 pv::widgets::Popup *const p = item->create_popup(this);
49 if (p)
50 p->show();
51}
52
6a2cc8dd
JH
53void MarginWidget::mouse_left_press_event(QMouseEvent *event)
54{
55 (void)event;
56
57 const bool ctrl_pressed =
58 QApplication::keyboardModifiers() & Qt::ControlModifier;
59
60 // Clear selection if control is not pressed and this item is unselected
61 if ((!mouse_down_item_ || !mouse_down_item_->selected()) &&
62 !ctrl_pressed)
63 clear_selection();
64
65 // Set the signal selection state if the item has been clicked
66 if (mouse_down_item_) {
67 if (ctrl_pressed)
68 mouse_down_item_->select(!mouse_down_item_->selected());
69 else
70 mouse_down_item_->select(true);
71 }
72
73 // Save the offsets of any signals which will be dragged
74 const auto items = this->items();
75 for (auto &i : items)
76 if (i->selected())
77 i->drag();
78
79 selection_changed();
80 update();
81}
82
e47b6988
JH
83void MarginWidget::mouse_left_release_event(QMouseEvent *event)
84{
85 assert(event);
86
87 auto items = this->items();
88 const bool ctrl_pressed =
89 QApplication::keyboardModifiers() & Qt::ControlModifier;
90
91 // Unselect everything if control is not pressed
92 const shared_ptr<ViewItem> mouse_over =
93 get_mouse_over_item(event->pos());
94
95 for (auto &i : items)
96 i->drag_release();
97
98 if (dragging_)
99 view_.restack_all_row_items();
100 else
101 {
102 if (!ctrl_pressed) {
103 for (shared_ptr<ViewItem> i : items)
104 if (mouse_down_item_ != i)
105 i->select(false);
106
107 if (mouse_down_item_)
108 show_popup(mouse_down_item_);
109 }
110 }
111
112 dragging_ = false;
113}
114
6a2cc8dd
JH
115void MarginWidget::mousePressEvent(QMouseEvent *event)
116{
117 assert(event);
118
119 mouse_down_point_ = event->pos();
120 mouse_down_item_ = get_mouse_over_item(event->pos());
121
122 if (event->button() & Qt::LeftButton)
123 mouse_left_press_event(event);
124}
125
e47b6988
JH
126void MarginWidget::mouseReleaseEvent(QMouseEvent *event)
127{
128 assert(event);
129 if (event->button() & Qt::LeftButton)
130 mouse_left_release_event(event);
131
132 mouse_down_item_ = nullptr;
133}
134
19552bb8
JH
135void MarginWidget::leaveEvent(QEvent*)
136{
137 mouse_point_ = QPoint(-1, -1);
138 update();
139}
140
2155b66b
JH
141void MarginWidget::contextMenuEvent(QContextMenuEvent *event)
142{
143 const shared_ptr<ViewItem> r = get_mouse_over_item(mouse_point_);
144 if (!r)
145 return;
146
147 QMenu *menu = r->create_context_menu(this);
148 if (menu)
149 menu->exec(event->globalPos());
150}
151
bb4a0e8e
JH
152void MarginWidget::keyPressEvent(QKeyEvent *e)
153{
154 assert(e);
155
156 if (e->key() == Qt::Key_Delete)
157 {
158 const auto items = this->items();
159 for (auto &i : items)
160 if (i->selected())
161 i->delete_pressed();
162 }
163}
164
84a0d458
JS
165void MarginWidget::clear_selection()
166{
5a6a4ce3
JH
167 const auto items = this->items();
168 for (auto &i : items)
169 i->select(false);
170 update();
84a0d458
JS
171}
172
c23b29d6
JH
173} // namespace view
174} // namespace pv