]> sigrok.org Git - pulseview.git/blame - pv/view/marginwidget.cpp
MarginWidget: Moved in keyPressEvent
[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
83void 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
19552bb8
JH
94void MarginWidget::leaveEvent(QEvent*)
95{
96 mouse_point_ = QPoint(-1, -1);
97 update();
98}
99
2155b66b
JH
100void 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
bb4a0e8e
JH
111void MarginWidget::keyPressEvent(QKeyEvent *e)
112{
113 assert(e);
114
115 if (e->key() == Qt::Key_Delete)
116 {
117 const auto items = this->items();
118 for (auto &i : items)
119 if (i->selected())
120 i->delete_pressed();
121 }
122}
123
84a0d458
JS
124void MarginWidget::clear_selection()
125{
5a6a4ce3
JH
126 const auto items = this->items();
127 for (auto &i : items)
128 i->select(false);
129 update();
84a0d458
JS
130}
131
c23b29d6
JH
132} // namespace view
133} // namespace pv