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 bool ViewWidget::mouse_down() const
91 return mouse_down_point_.x() != INT_MIN &&
92 mouse_down_point_.y() != INT_MIN;
95 void ViewWidget::drag_items(const QPoint &delta)
98 RowItemOwner *item_owner = nullptr;
99 for (std::shared_ptr<RowItem> r : view_)
101 item_owner = r->owner();
104 // Ensure the trace is selected
109 item_owner->restack_items();
110 for (const auto &r : *item_owner)
111 r->animate_to_layout_v_offset();
114 // Drag the time items
115 const vector< shared_ptr<TimeItem> > items(view_.time_items());
116 for (auto &i : items)
121 void ViewWidget::mouse_left_press_event(QMouseEvent *event)
125 const bool ctrl_pressed =
126 QApplication::keyboardModifiers() & Qt::ControlModifier;
128 // Clear selection if control is not pressed and this item is unselected
129 if ((!mouse_down_item_ || !mouse_down_item_->selected()) &&
133 // Set the signal selection state if the item has been clicked
134 if (mouse_down_item_) {
136 mouse_down_item_->select(!mouse_down_item_->selected());
138 mouse_down_item_->select(true);
141 // Save the offsets of any signals which will be dragged
142 const auto items = this->items();
143 for (auto &i : items)
151 void ViewWidget::mouse_left_release_event(QMouseEvent *event)
155 auto items = this->items();
156 const bool ctrl_pressed =
157 QApplication::keyboardModifiers() & Qt::ControlModifier;
159 // Unselect everything if control is not pressed
160 const shared_ptr<ViewItem> mouse_over =
161 get_mouse_over_item(event->pos());
163 for (auto &i : items)
167 view_.restack_all_row_items();
171 for (shared_ptr<ViewItem> i : items)
172 if (mouse_down_item_ != i)
175 if (mouse_down_item_)
176 item_clicked(mouse_down_item_);
180 item_dragging_ = false;
183 void ViewWidget::mousePressEvent(QMouseEvent *event)
187 mouse_down_point_ = event->pos();
188 mouse_down_item_ = get_mouse_over_item(event->pos());
190 if (event->button() & Qt::LeftButton)
191 mouse_left_press_event(event);
194 void ViewWidget::mouseReleaseEvent(QMouseEvent *event)
197 if (event->button() & Qt::LeftButton)
198 mouse_left_release_event(event);
200 mouse_down_point_ = QPoint(INT_MIN, INT_MIN);
201 mouse_down_item_ = nullptr;
204 void ViewWidget::mouseMoveEvent(QMouseEvent *event)
207 mouse_point_ = event->pos();
209 if (!(event->buttons() & Qt::LeftButton))
212 if ((event->pos() - mouse_down_point_).manhattanLength() <
213 QApplication::startDragDistance())
220 item_dragging_ = true;
221 drag_items(event->pos() - mouse_down_point_);
226 void ViewWidget::leaveEvent(QEvent*)
228 mouse_point_ = QPoint(-1, -1);