PulseView  unreleased development snapshot
A Qt-based sigrok GUI
flag.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2014 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 "timemarker.hpp"
21 #include "view.hpp"
22 #include "ruler.hpp"
23 
24 #include <QColor>
25 #include <QFormLayout>
26 #include <QLineEdit>
27 #include <QMenu>
28 #include <QApplication>
29 
30 #include <libsigrokcxx/libsigrokcxx.hpp>
31 
32 #include <pv/widgets/popup.hpp>
33 
34 using std::enable_shared_from_this;
35 using std::shared_ptr;
36 
37 namespace pv {
38 namespace views {
39 namespace trace {
40 
41 const QColor Flag::FillColor(0x73, 0xD2, 0x16);
42 
43 Flag::Flag(View &view, const pv::util::Timestamp& time, const QString &text) :
44  TimeMarker(view, FillColor, time),
45  text_(text)
46 {
47 }
48 
49 Flag::Flag(const Flag &flag) :
50  TimeMarker(flag.view_, FillColor, flag.time_),
51  enable_shared_from_this<Flag>(flag)
52 {
53 }
54 
55 bool Flag::enabled() const
56 {
57  return true;
58 }
59 
64 QString Flag::get_display_text() const
65 {
66  QString s;
67 
68  const shared_ptr<TimeItem> ref_item = view_.ruler()->get_reference_item();
69 
70  if (!ref_item || (ref_item.get() == this))
71  s = text_;
72  else
74  ref_item->time(), ref_item->delta(time_),
76 
77  return s;
78 }
79 
83 QString Flag::get_text() const
84 {
85  return text_;
86 }
87 
88 void Flag::set_text(const QString &text)
89 {
90  text_ = text;
92 }
93 
94 QRectF Flag::label_rect(const QRectF &rect) const
95 {
96  QRectF r;
97 
98  const shared_ptr<TimeItem> ref_item = view_.ruler()->get_reference_item();
99 
100  if (!ref_item || (ref_item.get() == this)) {
101  r = TimeMarker::label_rect(rect);
102  } else {
103  // TODO: Remove code duplication between here and cursor.cpp
104  const float x = get_x();
105 
106  QFontMetrics m(QApplication::font());
107  QSize text_size = m.boundingRect(get_display_text()).size();
108 
109  const QSizeF label_size(
110  text_size.width() + LabelPadding.width() * 2,
111  text_size.height() + LabelPadding.height() * 2);
112 
113  const float height = label_size.height();
114  const float top =
115  rect.height() - label_size.height() - TimeMarker::ArrowSize - 0.5f;
116 
117  const pv::util::Timestamp& delta = ref_item->delta(time_);
118 
119  if (delta >= 0)
120  r = QRectF(x, top, label_size.width(), height);
121  else
122  r = QRectF(x - label_size.width(), top, label_size.width(), height);
123  }
124 
125  return r;
126 }
127 
129 {
130  using pv::widgets::Popup;
131 
132  Popup *const popup = TimeMarker::create_popup(parent);
133  popup->set_position(parent->mapToGlobal(
134  drag_point(parent->rect())), Popup::Bottom);
135 
136  QFormLayout *const form = (QFormLayout*)popup->layout();
137 
138  QLineEdit *const text_edit = new QLineEdit(popup);
139  text_edit->setText(text_);
140 
141  connect(text_edit, SIGNAL(textChanged(const QString&)),
142  this, SLOT(on_text_changed(const QString&)));
143 
144  form->insertRow(0, tr("Text"), text_edit);
145 
146  return popup;
147 }
148 
149 QMenu* Flag::create_header_context_menu(QWidget *parent)
150 {
151  QMenu *const menu = new QMenu(parent);
152 
153  QAction *const del = new QAction(tr("Delete"), this);
154  del->setShortcuts(QKeySequence::Delete);
155  connect(del, SIGNAL(triggered()), this, SLOT(on_delete()));
156  menu->addAction(del);
157 
158  QAction *const snap_disable = new QAction(tr("Disable snapping"), this);
159  snap_disable->setCheckable(true);
160  snap_disable->setChecked(snapping_disabled_);
161  connect(snap_disable, &QAction::toggled, this, [=](bool checked){snapping_disabled_ = checked;});
162  menu->addAction(snap_disable);
163 
164  return menu;
165 }
166 
168 {
169  on_delete();
170 }
171 
173 {
174  view_.remove_flag(shared_ptr<Flag>(shared_from_this()));
175 }
176 
177 void Flag::on_text_changed(const QString &text)
178 {
179  set_text(text);
180 }
181 
182 } // namespace trace
183 } // namespace views
184 } // namespace pv
const Ruler * ruler() const
Definition: view.cpp:491
shared_ptr< TimeItem > get_reference_item() const
Definition: ruler.cpp:201
util::TimeUnit time_unit() const
Definition: view.cpp:730
pv::util::Timestamp time_
Definition: timemarker.hpp:138
pv::util::SIPrefix tick_prefix() const
Definition: view.cpp:686
virtual QString get_display_text() const override
Definition: flag.cpp:64
virtual bool enabled() const override
Definition: flag.cpp:55
float get_x() const override
Definition: timemarker.cpp:74
void time_item_appearance_changed(bool label, bool content)
Definition: view.cpp:1699
m
Definition: CMakeCache.txt:621
Flag(View &view, const pv::util::Timestamp &time, const QString &text)
Definition: flag.cpp:43
static const QSizeF LabelPadding
Definition: viewitem.hpp:51
QRectF label_rect(const QRectF &rect) const override
Definition: timemarker.cpp:87
QRectF label_rect(const QRectF &rect) const override
Definition: flag.cpp:94
void remove_flag(shared_ptr< Flag > flag)
Definition: view.cpp:1025
virtual pv::widgets::Popup * create_popup(QWidget *parent) override
Definition: timemarker.cpp:179
virtual QString get_text() const override
Definition: flag.cpp:83
void on_text_changed(const QString &text)
Definition: flag.cpp:177
static QString format_time_with_distance(const pv::util::Timestamp &distance, const pv::util::Timestamp &t, pv::util::SIPrefix prefix=pv::util::SIPrefix::unspecified, pv::util::TimeUnit unit=pv::util::TimeUnit::Time, unsigned precision=0, bool sign=true)
Definition: ruler.cpp:84
QPoint drag_point(const QRect &rect) const override
Definition: timemarker.cpp:80
virtual void set_text(const QString &text) override
Definition: flag.cpp:88
x y t t *t x y t t t x y t t t x *y t *t t x *y t *t t x y t t t x y t t t x(y+z)
boost::multiprecision::number< boost::multiprecision::cpp_dec_float< 24 >, boost::multiprecision::et_off > Timestamp
Timestamp type providing yoctosecond resolution.
Definition: util.hpp:67
virtual const pv::util::Timestamp delta(const pv::util::Timestamp &other) const
Definition: timeitem.cpp:47
static const QColor FillColor
Definition: flag.hpp:44
virtual pv::widgets::Popup * create_popup(QWidget *parent) override
Definition: flag.cpp:128
static const int ArrowSize
Definition: timemarker.hpp:53
virtual QMenu * create_header_context_menu(QWidget *parent) override
Definition: flag.cpp:149
virtual void delete_pressed() override
Definition: flag.cpp:167
unsigned int tick_precision() const
Definition: view.cpp:699