]> sigrok.org Git - pulseview.git/blob - pv/view/marginwidget.cpp
Added ViewWidget as a common ancestor of all view widgets
[pulseview.git] / pv / view / marginwidget.cpp
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
21 #include <QApplication>
22 #include <QMenu>
23 #include <QMouseEvent>
24
25 #include "view.hpp"
26
27 #include "marginwidget.hpp"
28
29 #include <pv/widgets/popup.hpp>
30
31 using std::shared_ptr;
32
33 namespace pv {
34 namespace view {
35
36 MarginWidget::MarginWidget(View &parent) :
37         ViewWidget(parent),
38         dragging_(false)
39 {
40         setAttribute(Qt::WA_NoSystemBackground, true);
41         setFocusPolicy(Qt::ClickFocus);
42         setMouseTracking(true);
43 }
44
45 void 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
52 void 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
82 void 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
114 void 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
125 void 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
134 void 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
156 void MarginWidget::leaveEvent(QEvent*)
157 {
158         mouse_point_ = QPoint(-1, -1);
159         update();
160 }
161
162 void 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
173 void 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
186 void MarginWidget::clear_selection()
187 {
188         const auto items = this->items();
189         for (auto &i : items)
190                 i->select(false);
191         update();
192 }
193
194 } // namespace view
195 } // namespace pv