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"
25 #include <QFormLayout>
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;
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),
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
60 QString Flag::get_text() const
62 const shared_ptr<TimeItem> ref_item = view_.get_reference_time_item();
63 if (ref_item == nullptr || ref_item.get() == this) {
66 return Ruler::format_time_with_distance(
67 ref_item->time(), ref_item->delta(time_),
68 view_.tick_prefix(), view_.time_unit(), view_.tick_precision());
72 QRectF Flag::label_rect(const QRectF &rect) const
74 const shared_ptr<TimeItem> ref_item = view_.get_reference_time_item();
75 if (ref_item == nullptr || ref_item.get() == this) {
76 return TimeMarker::label_rect(rect);
79 // TODO: Remove code duplication between here and cursor.cpp
80 const float x = get_x();
82 QFontMetrics m(QApplication::font());
83 QSize text_size = m.boundingRect(get_text()).size();
85 const QSizeF label_size(
86 text_size.width() + LabelPadding.width() * 2,
87 text_size.height() + LabelPadding.height() * 2);
88 const float top = rect.height() - label_size.height() -
89 TimeMarker::ArrowSize - 0.5f;
90 const float height = label_size.height();
92 const pv::util::Timestamp& delta = ref_item->delta(time_);
95 return QRectF(x, top, label_size.width(), height);
97 return QRectF(x - label_size.width(), top, label_size.width(), height);
101 pv::widgets::Popup* Flag::create_popup(QWidget *parent)
103 using pv::widgets::Popup;
105 Popup *const popup = TimeMarker::create_popup(parent);
106 popup->set_position(parent->mapToGlobal(
107 drag_point(parent->rect())), Popup::Bottom);
109 QFormLayout *const form = (QFormLayout*)popup->layout();
111 QLineEdit *const text_edit = new QLineEdit(popup);
112 text_edit->setText(text_);
114 connect(text_edit, SIGNAL(textChanged(const QString&)),
115 this, SLOT(on_text_changed(const QString&)));
117 form->insertRow(0, tr("Text"), text_edit);
122 QMenu* Flag::create_header_context_menu(QWidget *parent)
124 QMenu *const menu = new QMenu(parent);
126 QAction *const del = new QAction(tr("Delete"), this);
127 del->setShortcuts(QKeySequence::Delete);
128 connect(del, SIGNAL(triggered()), this, SLOT(on_delete()));
129 menu->addAction(del);
134 void Flag::delete_pressed()
139 void Flag::on_delete()
141 view_.remove_flag(shared_ptr<Flag>(shared_from_this()));
144 void Flag::on_text_changed(const QString &text)
147 view_.time_item_appearance_changed(true, false);