]> sigrok.org Git - pulseview.git/blame - pv/views/trace/flag.cpp
Session: Fix issue #67 by improving error handling
[pulseview.git] / pv / views / trace / flag.cpp
CommitLineData
8914fe79
JH
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
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
8914fe79
JH
18 */
19
20#include "timemarker.hpp"
21#include "view.hpp"
710c2a18 22#include "ruler.hpp"
8914fe79
JH
23
24#include <QColor>
25#include <QFormLayout>
26#include <QLineEdit>
97430d77 27#include <QMenu>
710c2a18 28#include <QApplication>
8914fe79 29
fe3a1c21 30#include <libsigrokcxx/libsigrokcxx.hpp>
8914fe79
JH
31
32#include <pv/widgets/popup.hpp>
33
6f925ba9 34using std::enable_shared_from_this;
8914fe79
JH
35using std::shared_ptr;
36
37namespace pv {
f4e57597 38namespace views {
1573bf16 39namespace trace {
8914fe79 40
641574bc 41const QColor Flag::FillColor(0x73, 0xD2, 0x16);
8914fe79 42
60d9b99a 43Flag::Flag(View &view, const pv::util::Timestamp& time, const QString &text) :
641574bc 44 TimeMarker(view, FillColor, time),
8914fe79
JH
45 text_(text)
46{
47}
48
49Flag::Flag(const Flag &flag) :
641574bc 50 TimeMarker(flag.view_, FillColor, flag.time_),
6f925ba9 51 enable_shared_from_this<Flag>(flag)
8914fe79
JH
52{
53}
54
55bool Flag::enabled() const
56{
57 return true;
58}
59
ef6cbaa2
SA
60/**
61 * Returns the text used to display this flag item. This is not necessarily the
62 * name that the user has given it.
63 */
64QString Flag::get_display_text() const
8914fe79 65{
9f094349
SA
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
73 s = Ruler::format_time_with_distance(
710c2a18 74 ref_item->time(), ref_item->delta(time_),
75 view_.tick_prefix(), view_.time_unit(), view_.tick_precision());
9f094349
SA
76
77 return s;
710c2a18 78}
79
ef6cbaa2
SA
80/**
81 * Returns the text of this flag item, i.e. the user-editable name.
82 */
83QString Flag::get_text() const
84{
85 return text_;
86}
87
e887fe9e
SA
88void Flag::set_text(const QString &text)
89{
90 text_ = text;
91 view_.time_item_appearance_changed(true, false);
92}
93
710c2a18 94QRectF Flag::label_rect(const QRectF &rect) const
95{
9f094349
SA
96 QRectF r;
97
98 const shared_ptr<TimeItem> ref_item = view_.ruler()->get_reference_item();
710c2a18 99
9f094349
SA
100 if (!ref_item || (ref_item.get() == this)) {
101 r = TimeMarker::label_rect(rect);
710c2a18 102 } else {
103 // TODO: Remove code duplication between here and cursor.cpp
104 const float x = get_x();
105
106 QFontMetrics m(QApplication::font());
ef6cbaa2 107 QSize text_size = m.boundingRect(get_display_text()).size();
710c2a18 108
109 const QSizeF label_size(
110 text_size.width() + LabelPadding.width() * 2,
111 text_size.height() + LabelPadding.height() * 2);
9f094349 112
710c2a18 113 const float height = label_size.height();
9f094349
SA
114 const float top =
115 rect.height() - label_size.height() - TimeMarker::ArrowSize - 0.5f;
710c2a18 116
117 const pv::util::Timestamp& delta = ref_item->delta(time_);
118
119 if (delta >= 0)
9f094349 120 r = QRectF(x, top, label_size.width(), height);
710c2a18 121 else
9f094349 122 r = QRectF(x - label_size.width(), top, label_size.width(), height);
710c2a18 123 }
9f094349
SA
124
125 return r;
8914fe79
JH
126}
127
128pv::widgets::Popup* Flag::create_popup(QWidget *parent)
129{
786b7678
JH
130 using pv::widgets::Popup;
131
132 Popup *const popup = TimeMarker::create_popup(parent);
133 popup->set_position(parent->mapToGlobal(
a3d5a7c7 134 drag_point(parent->rect())), Popup::Bottom);
786b7678 135
8914fe79
JH
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
9e773fec 149QMenu* Flag::create_header_context_menu(QWidget *parent)
97430d77
JH
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
0aabc15a
MM
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
97430d77
JH
164 return menu;
165}
166
dbfae3f1 167void Flag::delete_pressed()
97430d77
JH
168{
169 on_delete();
170}
171
172void Flag::on_delete()
dbfae3f1
JH
173{
174 view_.remove_flag(shared_ptr<Flag>(shared_from_this()));
175}
176
8914fe79
JH
177void Flag::on_text_changed(const QString &text)
178{
e887fe9e 179 set_text(text);
8914fe79
JH
180}
181
1573bf16 182} // namespace trace
f4e57597 183} // namespace views
8914fe79 184} // namespace pv