2 * This file is part of the PulseView project.
4 * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
21 #include <QApplication>
23 #include <QMouseEvent>
27 #include "marginwidget.hpp"
29 #include <pv/widgets/popup.hpp>
31 using std::shared_ptr;
36 MarginWidget::MarginWidget(View &parent) :
40 setAttribute(Qt::WA_NoSystemBackground, true);
41 setFocusPolicy(Qt::ClickFocus);
42 setMouseTracking(true);
45 void MarginWidget::show_popup(const shared_ptr<ViewItem> &item)
47 pv::widgets::Popup *const p = item->create_popup(this);
52 void MarginWidget::mouse_left_press_event(QMouseEvent *event)
56 const bool ctrl_pressed =
57 QApplication::keyboardModifiers() & Qt::ControlModifier;
59 // Clear selection if control is not pressed and this item is unselected
60 if ((!mouse_down_item_ || !mouse_down_item_->selected()) &&
64 // Set the signal selection state if the item has been clicked
65 if (mouse_down_item_) {
67 mouse_down_item_->select(!mouse_down_item_->selected());
69 mouse_down_item_->select(true);
72 // Save the offsets of any signals which will be dragged
73 const auto items = this->items();
82 void MarginWidget::mouse_left_release_event(QMouseEvent *event)
86 auto items = this->items();
87 const bool ctrl_pressed =
88 QApplication::keyboardModifiers() & Qt::ControlModifier;
90 // Unselect everything if control is not pressed
91 const shared_ptr<ViewItem> mouse_over =
92 get_mouse_over_item(event->pos());
98 view_.restack_all_row_items();
102 for (shared_ptr<ViewItem> i : items)
103 if (mouse_down_item_ != i)
106 if (mouse_down_item_)
107 show_popup(mouse_down_item_);
114 void MarginWidget::mousePressEvent(QMouseEvent *event)
118 mouse_down_point_ = event->pos();
119 mouse_down_item_ = get_mouse_over_item(event->pos());
121 if (event->button() & Qt::LeftButton)
122 mouse_left_press_event(event);
125 void MarginWidget::mouseReleaseEvent(QMouseEvent *event)
128 if (event->button() & Qt::LeftButton)
129 mouse_left_release_event(event);
131 mouse_down_item_ = nullptr;
134 void MarginWidget::mouseMoveEvent(QMouseEvent *event)
137 mouse_point_ = event->pos();
139 if (!(event->buttons() & Qt::LeftButton))
142 if ((event->pos() - mouse_down_point_).manhattanLength() <
143 QApplication::startDragDistance())
151 drag_items(event->pos() - mouse_down_point_);
156 void MarginWidget::leaveEvent(QEvent*)
158 mouse_point_ = QPoint(-1, -1);
162 void MarginWidget::contextMenuEvent(QContextMenuEvent *event)
164 const shared_ptr<ViewItem> r = get_mouse_over_item(mouse_point_);
168 QMenu *menu = r->create_context_menu(this);
170 menu->exec(event->globalPos());
173 void MarginWidget::keyPressEvent(QKeyEvent *e)
177 if (e->key() == Qt::Key_Delete)
179 const auto items = this->items();
180 for (auto &i : items)
186 void MarginWidget::clear_selection()
188 const auto items = this->items();
189 for (auto &i : items)