]> sigrok.org Git - pulseview.git/blame_incremental - pv/view/header.cpp
ViewWidget: Moved in unified accept_drag
[pulseview.git] / pv / view / header.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2012 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 "header.hpp"
22#include "view.hpp"
23
24#include "signal.hpp"
25#include "tracegroup.hpp"
26
27#include <cassert>
28#include <algorithm>
29
30#include <boost/iterator/filter_iterator.hpp>
31
32#include <QApplication>
33#include <QMenu>
34#include <QMouseEvent>
35#include <QPainter>
36#include <QRect>
37
38#include <pv/session.hpp>
39#include <pv/widgets/popup.hpp>
40
41using boost::make_filter_iterator;
42using std::dynamic_pointer_cast;
43using std::max;
44using std::make_pair;
45using std::min;
46using std::pair;
47using std::shared_ptr;
48using std::stable_sort;
49using std::vector;
50
51namespace pv {
52namespace view {
53
54const int Header::Padding = 12;
55const int Header::BaselineOffset = 5;
56
57static bool item_selected(shared_ptr<RowItem> r)
58{
59 return r->selected();
60}
61
62Header::Header(View &parent) :
63 MarginWidget(parent)
64{
65 connect(&view_, SIGNAL(signals_moved()),
66 this, SLOT(on_signals_moved()));
67}
68
69QSize Header::sizeHint() const
70{
71 QRectF max_rect(-Padding, 0, Padding, 0);
72 for (auto &i : view_)
73 if (i->enabled())
74 max_rect = max_rect.united(i->label_rect(QRect()));
75 return QSize(max_rect.width() + Padding + BaselineOffset, 0);
76}
77
78QSize Header::extended_size_hint() const
79{
80 return sizeHint() + QSize(ViewItem::HighlightRadius, 0);
81}
82
83vector< shared_ptr<ViewItem> > Header::items()
84{
85 return vector< shared_ptr<ViewItem> >(view_.begin(), view_.end());
86}
87
88shared_ptr<ViewItem> Header::get_mouse_over_item(const QPoint &pt)
89{
90 const QRect r(0, 0, width() - BaselineOffset, height());
91 for (auto &i : view_)
92 if (i->enabled() && i->label_rect(r).contains(pt))
93 return i;
94 return shared_ptr<RowItem>();
95}
96
97void Header::drag_items(const QPoint &delta)
98{
99 RowItemOwner *item_owner = nullptr;
100 for (std::shared_ptr<RowItem> r : view_)
101 if (r->dragging()) {
102 item_owner = r->owner();
103 r->drag_by(delta);
104
105 // Ensure the trace is selected
106 r->select();
107 }
108
109 item_owner->restack_items();
110 for (const auto &r : *item_owner)
111 r->animate_to_layout_v_offset();
112 signals_moved();
113}
114
115void Header::paintEvent(QPaintEvent*)
116{
117 // The trace labels are not drawn with the arrows exactly on the
118 // left edge of the widget, because then the selection shadow
119 // would be clipped away.
120 const QRect rect(0, 0, width() - BaselineOffset, height());
121
122 vector< shared_ptr<RowItem> > row_items(
123 view_.begin(), view_.end());
124
125 stable_sort(row_items.begin(), row_items.end(),
126 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
127 return a->visual_v_offset() < b->visual_v_offset(); });
128
129 QPainter painter(this);
130 painter.setRenderHint(QPainter::Antialiasing);
131
132 for (const shared_ptr<RowItem> r : row_items)
133 {
134 assert(r);
135
136 const bool highlight = !dragging_ &&
137 r->label_rect(rect).contains(mouse_point_);
138 r->paint_label(painter, rect, highlight);
139 }
140
141 painter.end();
142}
143
144void Header::contextMenuEvent(QContextMenuEvent *event)
145{
146 const shared_ptr<ViewItem> r = get_mouse_over_item(mouse_point_);
147 if (!r)
148 return;
149
150 QMenu *menu = r->create_context_menu(this);
151 if (!menu)
152 menu = new QMenu(this);
153
154 if (std::count_if(view_.begin(), view_.end(), item_selected) > 1)
155 {
156 menu->addSeparator();
157
158 QAction *const group = new QAction(tr("Group"), this);
159 QList<QKeySequence> shortcuts;
160 shortcuts.append(QKeySequence(Qt::ControlModifier | Qt::Key_G));
161 group->setShortcuts(shortcuts);
162 connect(group, SIGNAL(triggered()), this, SLOT(on_group()));
163 menu->addAction(group);
164 }
165
166 menu->exec(event->globalPos());
167}
168
169void Header::keyPressEvent(QKeyEvent *e)
170{
171 assert(e);
172
173 MarginWidget::keyPressEvent(e);
174
175 if (e->key() == Qt::Key_G && e->modifiers() == Qt::ControlModifier)
176 on_group();
177 else if (e->key() == Qt::Key_U && e->modifiers() == Qt::ControlModifier)
178 on_ungroup();
179}
180
181void Header::on_signals_moved()
182{
183 update();
184}
185
186void Header::on_group()
187{
188 vector< shared_ptr<RowItem> > selected_items(
189 make_filter_iterator(item_selected, view_.begin(), view_.end()),
190 make_filter_iterator(item_selected, view_.end(), view_.end()));
191 stable_sort(selected_items.begin(), selected_items.end(),
192 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
193 return a->visual_v_offset() < b->visual_v_offset(); });
194
195 shared_ptr<TraceGroup> group(new TraceGroup());
196 shared_ptr<RowItem> mouse_down_item(
197 std::dynamic_pointer_cast<RowItem>(mouse_down_item_));
198 shared_ptr<RowItem> focus_item(
199 mouse_down_item ? mouse_down_item : selected_items.front());
200
201 assert(focus_item);
202 assert(focus_item->owner());
203 focus_item->owner()->add_child_item(group);
204
205 // Set the group v_offset here before reparenting
206 group->force_to_v_offset(focus_item->layout_v_offset() +
207 focus_item->v_extents().first);
208
209 for (size_t i = 0; i < selected_items.size(); i++) {
210 const shared_ptr<RowItem> &r = selected_items[i];
211 assert(r->owner());
212 r->owner()->remove_child_item(r);
213 group->add_child_item(r);
214
215 // Put the items at 1-pixel offsets, so that restack will
216 // stack them in the right order
217 r->set_layout_v_offset(i);
218 }
219}
220
221void Header::on_ungroup()
222{
223 bool restart;
224 do {
225 restart = false;
226 for (const shared_ptr<RowItem> r : view_) {
227 const shared_ptr<TraceGroup> tg =
228 dynamic_pointer_cast<TraceGroup>(r);
229 if (tg && tg->selected()) {
230 tg->ungroup();
231 restart = true;
232 break;
233 }
234 }
235 } while(restart);
236}
237
238} // namespace view
239} // namespace pv