]> sigrok.org Git - pulseview.git/blob - pv/view/marginwidget.cpp
e20e7b9f00afe664e72439969b73fe5894b6eca7
[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::mousePressEvent(QMouseEvent *event)
84 {
85         assert(event);
86
87         mouse_down_point_ = event->pos();
88         mouse_down_item_ = get_mouse_over_item(event->pos());
89
90         if (event->button() & Qt::LeftButton)
91                 mouse_left_press_event(event);
92 }
93
94 void MarginWidget::leaveEvent(QEvent*)
95 {
96         mouse_point_ = QPoint(-1, -1);
97         update();
98 }
99
100 void MarginWidget::contextMenuEvent(QContextMenuEvent *event)
101 {
102         const shared_ptr<ViewItem> r = get_mouse_over_item(mouse_point_);
103         if (!r)
104                 return;
105
106         QMenu *menu = r->create_context_menu(this);
107         if (menu)
108                 menu->exec(event->globalPos());
109 }
110
111 void MarginWidget::clear_selection()
112 {
113         const auto items = this->items();
114         for (auto &i : items)
115                 i->select(false);
116         update();
117 }
118
119 } // namespace view
120 } // namespace pv