2 * This file is part of the PulseView project.
4 * Copyright (C) 2014 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>
22 #include <QMouseEvent>
24 #include "rowitem.hpp"
26 #include "viewwidget.hpp"
29 using std::shared_ptr;
35 ViewWidget::ViewWidget(View &parent) :
40 setFocusPolicy(Qt::ClickFocus);
41 setMouseTracking(true);
44 void ViewWidget::clear_selection()
46 const auto items = this->items();
52 void ViewWidget::item_clicked(const shared_ptr<ViewItem> &item)
57 bool ViewWidget::accept_drag() const
59 const vector< shared_ptr<TimeItem> > items(view_.time_items());
61 const bool any_row_items_selected = any_of(view_.begin(), view_.end(),
62 [](const shared_ptr<RowItem> &r) { return r->selected(); });
64 const bool any_time_items_selected = any_of(items.begin(), items.end(),
65 [](const shared_ptr<TimeItem> &i) { return i->selected(); });
67 if (any_row_items_selected && !any_time_items_selected)
69 // Check all the drag items share a common owner
70 RowItemOwner *item_owner = nullptr;
71 for (shared_ptr<RowItem> r : view_)
74 item_owner = r->owner();
75 else if(item_owner != r->owner())
81 else if (any_time_items_selected && !any_row_items_selected)
89 void ViewWidget::drag_items(const QPoint &delta)
92 RowItemOwner *item_owner = nullptr;
93 for (std::shared_ptr<RowItem> r : view_)
95 item_owner = r->owner();
98 // Ensure the trace is selected
103 item_owner->restack_items();
104 for (const auto &r : *item_owner)
105 r->animate_to_layout_v_offset();
108 // Drag the time items
109 const vector< shared_ptr<TimeItem> > items(view_.time_items());
110 for (auto &i : items)
115 void ViewWidget::mouse_left_press_event(QMouseEvent *event)
119 const bool ctrl_pressed =
120 QApplication::keyboardModifiers() & Qt::ControlModifier;
122 // Clear selection if control is not pressed and this item is unselected
123 if ((!mouse_down_item_ || !mouse_down_item_->selected()) &&
127 // Set the signal selection state if the item has been clicked
128 if (mouse_down_item_) {
130 mouse_down_item_->select(!mouse_down_item_->selected());
132 mouse_down_item_->select(true);
135 // Save the offsets of any signals which will be dragged
136 const auto items = this->items();
137 for (auto &i : items)
145 void ViewWidget::mouse_left_release_event(QMouseEvent *event)
149 auto items = this->items();
150 const bool ctrl_pressed =
151 QApplication::keyboardModifiers() & Qt::ControlModifier;
153 // Unselect everything if control is not pressed
154 const shared_ptr<ViewItem> mouse_over =
155 get_mouse_over_item(event->pos());
157 for (auto &i : items)
161 view_.restack_all_row_items();
165 for (shared_ptr<ViewItem> i : items)
166 if (mouse_down_item_ != i)
169 if (mouse_down_item_)
170 item_clicked(mouse_down_item_);
174 item_dragging_ = false;
177 void ViewWidget::mousePressEvent(QMouseEvent *event)
181 mouse_down_point_ = event->pos();
182 mouse_down_item_ = get_mouse_over_item(event->pos());
184 if (event->button() & Qt::LeftButton)
185 mouse_left_press_event(event);
188 void ViewWidget::mouseReleaseEvent(QMouseEvent *event)
191 if (event->button() & Qt::LeftButton)
192 mouse_left_release_event(event);
194 mouse_down_item_ = nullptr;
197 void ViewWidget::mouseMoveEvent(QMouseEvent *event)
200 mouse_point_ = event->pos();
202 if (!(event->buttons() & Qt::LeftButton))
205 if ((event->pos() - mouse_down_point_).manhattanLength() <
206 QApplication::startDragDistance())
213 item_dragging_ = true;
214 drag_items(event->pos() - mouse_down_point_);
219 void ViewWidget::leaveEvent(QEvent*)
221 mouse_point_ = QPoint(-1, -1);