PulseView  unreleased development snapshot
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, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <extdef.h>
21 
22 #include <algorithm>
23 #include <cassert>
24 
25 #include <QMenu>
26 #include <QPainter>
27 
28 #include "tracegroup.hpp"
29 
30 using std::any_of;
31 using std::pair;
32 using std::shared_ptr;
33 using std::vector;
34 
35 namespace pv {
36 namespace views {
37 namespace trace {
38 
39 const int TraceGroup::Padding = 8;
40 const int TraceGroup::Width = 12;
41 const int TraceGroup::LineThickness = 5;
42 const QColor TraceGroup::LineColor(QColor(0x55, 0x57, 0x53));
43 
45 {
46  owner_ = nullptr;
48 }
49 
50 bool TraceGroup::enabled() const
51 {
52  return any_of(child_items().begin(), child_items().end(),
53  [](const shared_ptr<ViewItem> &r) { return r->enabled(); });
54 }
55 
57 {
58  assert(owner_);
59  return owner_->session();
60 }
61 
63 {
64  assert(owner_);
65  return owner_->session();
66 }
67 
69 {
70  assert(owner_);
71  return owner_->view();
72 }
73 
74 const View* TraceGroup::view() const
75 {
76  assert(owner_);
77  return owner_->view();
78 }
79 
80 pair<int, int> TraceGroup::v_extents() const
81 {
83 }
84 
85 void TraceGroup::paint_label(QPainter &p, const QRect &rect, bool hover)
86 {
87  const QRectF r = label_rect(rect).adjusted(
88  LineThickness / 2.0, LineThickness / 2.0,
89  -LineThickness / 2.0, -LineThickness / 2.0);
90 
91  // Paint the label
92  const QPointF points[] = {
93  r.topRight(),
94  r.topLeft(),
95  r.bottomLeft(),
96  r.bottomRight()
97  };
98 
99  if (selected()) {
100  const QPen pen(highlight_pen());
101  p.setPen(QPen(pen.brush(), pen.width() + LineThickness,
102  Qt::SolidLine, Qt::SquareCap, Qt::RoundJoin));
103  p.setBrush(Qt::transparent);
104  p.drawPolyline(points, countof(points));
105  }
106 
107  p.setPen(QPen(QBrush(LineColor.darker()), LineThickness,
108  Qt::SolidLine, Qt::SquareCap, Qt::RoundJoin));
109  p.drawPolyline(points, countof(points));
110  p.setPen(QPen(QBrush(hover ? LineColor.lighter() : LineColor),
111  LineThickness - 2, Qt::SolidLine, Qt::SquareCap,
112  Qt::RoundJoin));
113  p.drawPolyline(points, countof(points));
114 }
115 
116 QRectF TraceGroup::label_rect(const QRectF &rect) const
117 {
118  QRectF child_rect;
119  for (const shared_ptr<ViewItem>& r : child_items())
120  if (r && r->enabled())
121  child_rect = child_rect.united(r->label_rect(rect));
122 
123  return QRectF(child_rect.x() - Width - Padding, child_rect.y(),
124  Width, child_rect.height());
125 }
126 
127 bool TraceGroup::pt_in_label_rect(int left, int right, const QPoint &point)
128 {
129  (void)left;
130  (void)right;
131  (void)point;
132 
133  return false;
134 }
135 
137 {
138  QMenu *const menu = new QMenu(parent);
139 
140  QAction *const ungroup = new QAction(tr("Ungroup"), this);
141 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
142  ungroup->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_U));
143 #else
144  ungroup->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_U));
145 #endif
146  connect(ungroup, SIGNAL(triggered()), this, SLOT(on_ungroup()));
147  menu->addAction(ungroup);
148 
149  return menu;
150 }
151 
153 {
154  (void)parent;
155  return nullptr;
156 }
157 
159 {
161 }
162 
163 unsigned int TraceGroup::depth() const
164 {
165  return owner_ ? owner_->depth() + 1 : 0;
166 }
167 
169 {
170  const vector<shared_ptr<TraceTreeItem>> items(trace_tree_child_items());
172 
173  for (const shared_ptr<TraceTreeItem>& r : items)
175 
176  owner_->remove_child_item(shared_from_this());
177 }
178 
180 {
181  ungroup();
182 }
183 
184 void TraceGroup::row_item_appearance_changed(bool label, bool content)
185 {
186  if (owner_)
187  owner_->row_item_appearance_changed(label, content);
188 }
189 
190 void TraceGroup::extents_changed(bool horz, bool vert)
191 {
192  if (owner_)
193  owner_->extents_changed(horz, vert);
194 }
195 
196 } // namespace trace
197 } // namespace views
198 } // namespace pv
int owner_visual_v_offset() const
Definition: tracegroup.cpp:158
vector< shared_ptr< TraceTreeItem > > trace_tree_child_items() const
static const int Padding
Definition: tracegroup.hpp:37
void row_item_appearance_changed(bool label, bool content)
Definition: tracegroup.cpp:184
virtual void extents_changed(bool horz, bool vert)=0
void remove_child_item(shared_ptr< TraceTreeItem > item)
pv::widgets::Popup * create_popup(QWidget *parent)
Definition: tracegroup.cpp:152
QRectF label_rect(const QRectF &rect) const
Definition: tracegroup.cpp:116
virtual int owner_visual_v_offset() const =0
void add_child_item(shared_ptr< TraceTreeItem > item)
unsigned int depth() const
Definition: tracegroup.cpp:163
static QPen highlight_pen()
Definition: viewitem.cpp:114
void extents_changed(bool horz, bool vert)
Definition: tracegroup.cpp:190
QMenu * create_header_context_menu(QWidget *parent)
Definition: tracegroup.cpp:136
static const int LineThickness
Definition: tracegroup.hpp:39
virtual const item_list & child_items() const
bool pt_in_label_rect(int left, int right, const QPoint &point)
Definition: tracegroup.cpp:127
virtual unsigned int depth() const =0
#define countof(x)
Definition: extdef.h:23
bool selected() const
Definition: viewitem.cpp:48
pair< int, int > v_extents() const
Definition: tracegroup.cpp:80
static const QColor LineColor
Definition: tracegroup.hpp:40
void paint_label(QPainter &p, const QRect &rect, bool hover)
Definition: tracegroup.cpp:85
virtual void row_item_appearance_changed(bool label, bool content)=0