PulseView  0.3.0
A Qt-based sigrok GUI
tracegroup.cpp
Go to the documentation of this file.
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 
21 #include <extdef.h>
22 #include <assert.h>
23 
24 #include <algorithm>
25 
26 #include <QMenu>
27 #include <QPainter>
28 
29 #include "tracegroup.hpp"
30 
31 using std::pair;
32 using std::shared_ptr;
33 using std::vector;
34 
35 namespace pv {
36 namespace view {
37 
38 const int TraceGroup::Padding = 8;
39 const int TraceGroup::Width = 12;
40 const int TraceGroup::LineThickness = 5;
41 const QColor TraceGroup::LineColour(QColor(0x55, 0x57, 0x53));
42 
44 {
45  owner_ = nullptr;
47 }
48 
49 bool TraceGroup::enabled() const
50 {
51  return std::any_of(child_items().begin(), child_items().end(),
52  [](const shared_ptr<ViewItem> &r) { return r->enabled(); });
53 }
54 
56 {
57  assert(owner_);
58  return owner_->session();
59 }
60 
62 {
63  assert(owner_);
64  return owner_->session();
65 }
66 
68 {
69  assert(owner_);
70  return owner_->view();
71 }
72 
74 {
75  assert(owner_);
76  return owner_->view();
77 }
78 
79 pair<int, int> TraceGroup::v_extents() const
80 {
82 }
83 
84 void TraceGroup::paint_label(QPainter &p, const QRect &rect, bool hover)
85 {
86  const QRectF r = label_rect(rect).adjusted(
88  -LineThickness / 2, -LineThickness / 2);
89 
90  // Paint the label
91  const QPointF points[] = {
92  r.topRight(),
93  r.topLeft(),
94  r.bottomLeft(),
95  r.bottomRight()
96  };
97 
98  if (selected()) {
99  const QPen pen(highlight_pen());
100  p.setPen(QPen(pen.brush(), pen.width() + LineThickness,
101  Qt::SolidLine, Qt::SquareCap, Qt::RoundJoin));
102  p.setBrush(Qt::transparent);
103  p.drawPolyline(points, countof(points));
104  }
105 
106  p.setPen(QPen(QBrush(LineColour.darker()), LineThickness,
107  Qt::SolidLine, Qt::SquareCap, Qt::RoundJoin));
108  p.drawPolyline(points, countof(points));
109  p.setPen(QPen(QBrush(hover ? LineColour.lighter() : LineColour),
110  LineThickness - 2, Qt::SolidLine, Qt::SquareCap,
111  Qt::RoundJoin));
112  p.drawPolyline(points, countof(points));
113 }
114 
115 QRectF TraceGroup::label_rect(const QRectF &rect) const
116 {
117  QRectF child_rect;
118  for (const shared_ptr<ViewItem> r : child_items())
119  if (r && r->enabled())
120  child_rect = child_rect.united(r->label_rect(rect));
121 
122  return QRectF(child_rect.x() - Width - Padding, child_rect.y(),
123  Width, child_rect.height());
124 }
125 
126 bool TraceGroup::pt_in_label_rect(int left, int right, const QPoint &point)
127 {
128  (void)left;
129  (void)right;
130  (void)point;
131 
132  return false;
133 }
134 
135 QMenu* TraceGroup::create_context_menu(QWidget *parent)
136 {
137  QMenu *const menu = new QMenu(parent);
138 
139  QAction *const ungroup = new QAction(tr("Ungroup"), this);
140  ungroup->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_U));
141  connect(ungroup, SIGNAL(triggered()), this, SLOT(on_ungroup()));
142  menu->addAction(ungroup);
143 
144  return menu;
145 }
146 
148 {
149  (void)parent;
150  return nullptr;
151 }
152 
154 {
156 }
157 
159 {
160  vector<shared_ptr<TraceTreeItem>> items(trace_tree_child_items());
161 
162  // Sort by the centre line of the extents
163  stable_sort(items.begin(), items.end(),
164  [](const shared_ptr<TraceTreeItem> &a, const shared_ptr<TraceTreeItem> &b) {
165  const auto aext = a->v_extents();
166  const auto bext = b->v_extents();
167  return a->layout_v_offset() +
168  (aext.first + aext.second) / 2 <
169  b->layout_v_offset() +
170  (bext.first + bext.second) / 2;
171  });
172 
173  int total_offset = 0;
174  for (shared_ptr<TraceTreeItem> r : items) {
175  const pair<int, int> extents = r->v_extents();
176  if (extents.first == 0 && extents.second == 0)
177  continue;
178 
179  // We position disabled traces, so that they are close to the
180  // animation target positon should they be re-enabled
181  if (r->enabled())
182  total_offset += -extents.first;
183 
184  if (!r->dragging())
185  r->set_layout_v_offset(total_offset);
186 
187  if (r->enabled())
188  total_offset += extents.second;
189  }
190 }
191 
192 unsigned int TraceGroup::depth() const
193 {
194  return owner_ ? owner_->depth() + 1 : 0;
195 }
196 
198 {
199  const vector<shared_ptr<TraceTreeItem>> items(trace_tree_child_items());
201 
202  for (shared_ptr<TraceTreeItem> r : items)
204 
205  owner_->remove_child_item(shared_from_this());
206 }
207 
209 {
210  ungroup();
211 }
212 
213 void TraceGroup::row_item_appearance_changed(bool label, bool content)
214 {
215  if (owner_)
216  owner_->row_item_appearance_changed(label, content);
217 }
218 
219 void TraceGroup::extents_changed(bool horz, bool vert)
220 {
221  if (owner_)
222  owner_->extents_changed(horz, vert);
223 }
224 
225 } // namespace view
226 } // namespace pv
virtual void row_item_appearance_changed(bool label, bool content)=0
void extents_changed(bool horz, bool vert)
Definition: tracegroup.cpp:219
std::vector< std::shared_ptr< TraceTreeItem > > trace_tree_child_items() const
std::pair< int, int > v_extents() const
int visual_v_offset() const
std::pair< int, int > v_extents() const
Definition: tracegroup.cpp:79
bool enabled() const
Definition: tracegroup.cpp:49
void row_item_appearance_changed(bool label, bool content)
Definition: tracegroup.cpp:213
static const int LineThickness
Definition: tracegroup.hpp:37
virtual void extents_changed(bool horz, bool vert)=0
pv::widgets::Popup * create_popup(QWidget *parent)
Definition: tracegroup.cpp:147
virtual const item_list & child_items() const
QMenu * create_context_menu(QWidget *parent)
Definition: tracegroup.cpp:135
bool selected() const
Definition: viewitem.cpp:42
static const QColor LineColour
Definition: tracegroup.hpp:38
virtual pv::view::View * view()=0
virtual pv::view::View * view()
Definition: tracegroup.cpp:67
void add_child_item(std::shared_ptr< TraceTreeItem > item)
static const int Width
Definition: tracegroup.hpp:36
#define countof(x)
Definition: extdef.h:24
QRectF label_rect(const QRectF &rect) const
Definition: tracegroup.cpp:115
int owner_visual_v_offset() const
Definition: tracegroup.cpp:153
pv::Session & session()
Definition: tracegroup.cpp:55
void paint_label(QPainter &p, const QRect &rect, bool hover)
Definition: tracegroup.cpp:84
virtual unsigned int depth() const =0
unsigned int depth() const
Definition: tracegroup.cpp:192
TraceTreeItemOwner * owner_
virtual int owner_visual_v_offset() const =0
bool pt_in_label_rect(int left, int right, const QPoint &point)
Definition: tracegroup.cpp:126
static QPen highlight_pen()
Definition: viewitem.cpp:101
void remove_child_item(std::shared_ptr< TraceTreeItem > item)
virtual pv::Session & session()=0
static const int Padding
Definition: tracegroup.hpp:35