]> sigrok.org Git - pulseview.git/blob - pv/view/marginwidget.cpp
MarginWidget: Moved in mouseReleaseEvent
[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         QWidget(&parent),
38         view_(parent),
39         dragging_(false)
40 {
41         setAttribute(Qt::WA_NoSystemBackground, true);
42         setFocusPolicy(Qt::ClickFocus);
43         setMouseTracking(true);
44 }
45
46 void 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
53 void 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
83 void 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
115 void 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
126 void 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
135 void MarginWidget::leaveEvent(QEvent*)
136 {
137         mouse_point_ = QPoint(-1, -1);
138         update();
139 }
140
141 void 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
152 void 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
165 void MarginWidget::clear_selection()
166 {
167         const auto items = this->items();
168         for (auto &i : items)
169                 i->select(false);
170         update();
171 }
172
173 } // namespace view
174 } // namespace pv