MathSignal: Add config dialog
[pulseview.git] / pv / views / trace / flag.cpp
blobb0518b646d87c493d8c13de606570f8dccf92522
1 /*
2 * This file is part of the PulseView project.
4 * Copyright (C) 2014 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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/>.
20 #include "timemarker.hpp"
21 #include "view.hpp"
22 #include "ruler.hpp"
24 #include <QColor>
25 #include <QFormLayout>
26 #include <QLineEdit>
27 #include <QMenu>
28 #include <QApplication>
30 #include <libsigrokcxx/libsigrokcxx.hpp>
32 #include <pv/widgets/popup.hpp>
34 using std::enable_shared_from_this;
35 using std::shared_ptr;
37 namespace pv {
38 namespace views {
39 namespace trace {
41 const QColor Flag::FillColor(0x73, 0xD2, 0x16);
43 Flag::Flag(View &view, const pv::util::Timestamp& time, const QString &text) :
44 TimeMarker(view, FillColor, time),
45 text_(text)
49 Flag::Flag(const Flag &flag) :
50 TimeMarker(flag.view_, FillColor, flag.time_),
51 enable_shared_from_this<Flag>(flag)
55 bool Flag::enabled() const
57 return true;
60 QString Flag::get_text() const
62 QString s;
64 const shared_ptr<TimeItem> ref_item = view_.ruler()->get_reference_item();
66 if (!ref_item || (ref_item.get() == this))
67 s = text_;
68 else
69 s = Ruler::format_time_with_distance(
70 ref_item->time(), ref_item->delta(time_),
71 view_.tick_prefix(), view_.time_unit(), view_.tick_precision());
73 return s;
76 void Flag::set_text(const QString &text)
78 text_ = text;
79 view_.time_item_appearance_changed(true, false);
82 QRectF Flag::label_rect(const QRectF &rect) const
84 QRectF r;
86 const shared_ptr<TimeItem> ref_item = view_.ruler()->get_reference_item();
88 if (!ref_item || (ref_item.get() == this)) {
89 r = TimeMarker::label_rect(rect);
90 } else {
91 // TODO: Remove code duplication between here and cursor.cpp
92 const float x = get_x();
94 QFontMetrics m(QApplication::font());
95 QSize text_size = m.boundingRect(get_text()).size();
97 const QSizeF label_size(
98 text_size.width() + LabelPadding.width() * 2,
99 text_size.height() + LabelPadding.height() * 2);
101 const float height = label_size.height();
102 const float top =
103 rect.height() - label_size.height() - TimeMarker::ArrowSize - 0.5f;
105 const pv::util::Timestamp& delta = ref_item->delta(time_);
107 if (delta >= 0)
108 r = QRectF(x, top, label_size.width(), height);
109 else
110 r = QRectF(x - label_size.width(), top, label_size.width(), height);
113 return r;
116 pv::widgets::Popup* Flag::create_popup(QWidget *parent)
118 using pv::widgets::Popup;
120 Popup *const popup = TimeMarker::create_popup(parent);
121 popup->set_position(parent->mapToGlobal(
122 drag_point(parent->rect())), Popup::Bottom);
124 QFormLayout *const form = (QFormLayout*)popup->layout();
126 QLineEdit *const text_edit = new QLineEdit(popup);
127 text_edit->setText(text_);
129 connect(text_edit, SIGNAL(textChanged(const QString&)),
130 this, SLOT(on_text_changed(const QString&)));
132 form->insertRow(0, tr("Text"), text_edit);
134 return popup;
137 QMenu* Flag::create_header_context_menu(QWidget *parent)
139 QMenu *const menu = new QMenu(parent);
141 QAction *const del = new QAction(tr("Delete"), this);
142 del->setShortcuts(QKeySequence::Delete);
143 connect(del, SIGNAL(triggered()), this, SLOT(on_delete()));
144 menu->addAction(del);
146 QAction *const snap_disable = new QAction(tr("Disable snapping"), this);
147 snap_disable->setCheckable(true);
148 snap_disable->setChecked(snapping_disabled_);
149 connect(snap_disable, &QAction::toggled, this, [=](bool checked){snapping_disabled_ = checked;});
150 menu->addAction(snap_disable);
152 return menu;
155 void Flag::delete_pressed()
157 on_delete();
160 void Flag::on_delete()
162 view_.remove_flag(shared_ptr<Flag>(shared_from_this()));
165 void Flag::on_text_changed(const QString &text)
167 set_text(text);
170 } // namespace trace
171 } // namespace views
172 } // namespace pv