]> sigrok.org Git - pulseview.git/blob - pv/views/trace/flag.cpp
ea3cd609854c813783ffe6e406143b887915693a
[pulseview.git] / pv / views / trace / flag.cpp
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
60 QString Flag::get_text() const
61 {
62         QString s;
63
64         const shared_ptr<TimeItem> ref_item = view_.ruler()->get_reference_item();
65
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());
72
73         return s;
74 }
75
76 QRectF Flag::label_rect(const QRectF &rect) const
77 {
78         QRectF r;
79
80         const shared_ptr<TimeItem> ref_item = view_.ruler()->get_reference_item();
81
82         if (!ref_item || (ref_item.get() == this)) {
83                 r = TimeMarker::label_rect(rect);
84         } else {
85                 // TODO: Remove code duplication between here and cursor.cpp
86                 const float x = get_x();
87
88                 QFontMetrics m(QApplication::font());
89                 QSize text_size = m.boundingRect(get_text()).size();
90
91                 const QSizeF label_size(
92                         text_size.width() + LabelPadding.width() * 2,
93                         text_size.height() + LabelPadding.height() * 2);
94
95                 const float height = label_size.height();
96                 const float top =
97                         rect.height() - label_size.height() - TimeMarker::ArrowSize - 0.5f;
98
99                 const pv::util::Timestamp& delta = ref_item->delta(time_);
100
101                 if (delta >= 0)
102                         r = QRectF(x, top, label_size.width(), height);
103                 else
104                         r = QRectF(x - label_size.width(), top, label_size.width(), height);
105         }
106
107         return r;
108 }
109
110 pv::widgets::Popup* Flag::create_popup(QWidget *parent)
111 {
112         using pv::widgets::Popup;
113
114         Popup *const popup = TimeMarker::create_popup(parent);
115         popup->set_position(parent->mapToGlobal(
116                 drag_point(parent->rect())), Popup::Bottom);
117
118         QFormLayout *const form = (QFormLayout*)popup->layout();
119
120         QLineEdit *const text_edit = new QLineEdit(popup);
121         text_edit->setText(text_);
122
123         connect(text_edit, SIGNAL(textChanged(const QString&)),
124                 this, SLOT(on_text_changed(const QString&)));
125
126         form->insertRow(0, tr("Text"), text_edit);
127
128         return popup;
129 }
130
131 QMenu* Flag::create_header_context_menu(QWidget *parent)
132 {
133         QMenu *const menu = new QMenu(parent);
134
135         QAction *const del = new QAction(tr("Delete"), this);
136         del->setShortcuts(QKeySequence::Delete);
137         connect(del, SIGNAL(triggered()), this, SLOT(on_delete()));
138         menu->addAction(del);
139
140         QAction *const snap_disable = new QAction(tr("Disable snapping"), this);
141         snap_disable->setCheckable(true);
142         snap_disable->setChecked(snapping_disabled_);
143         connect(snap_disable, &QAction::toggled, this, [=](bool checked){snapping_disabled_ = checked;});
144         menu->addAction(snap_disable);
145
146         return menu;
147 }
148
149 void Flag::delete_pressed()
150 {
151         on_delete();
152 }
153
154 void Flag::on_delete()
155 {
156         view_.remove_flag(shared_ptr<Flag>(shared_from_this()));
157 }
158
159 void Flag::on_text_changed(const QString &text)
160 {
161         text_ = text;
162         view_.time_item_appearance_changed(true, false);
163 }
164
165 } // namespace trace
166 } // namespace views
167 } // namespace pv