2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <QApplication>
24 #include <QFontMetrics>
25 #include <QMouseEvent>
30 #include <pv/util.hpp>
31 #include <pv/widgets/popup.hpp>
35 using std::shared_ptr;
41 const float Ruler::RulerHeight = 2.5f; // x Text Height
42 const int Ruler::MinorTickSubdivision = 4;
44 const float Ruler::HoverArrowSize = 0.5f; // x Text Height
46 const int Ruler::Padding = 20;
48 Ruler::Ruler(View &parent) :
51 setMouseTracking(true);
53 connect(&view_, SIGNAL(hover_point_changed()),
54 this, SLOT(hover_point_changed()));
57 void Ruler::clear_selection()
59 const vector< shared_ptr<TimeItem> > items(view_.time_items());
65 QSize Ruler::sizeHint() const
67 const int text_height = calculate_text_height();
68 return QSize(0, RulerHeight * text_height);
71 QSize Ruler::extended_size_hint() const
73 const int text_height = calculate_text_height();
74 return QSize(0, RulerHeight * text_height +
75 (text_height + Padding + ViewItem::HighlightRadius) / 2);
78 void Ruler::paintEvent(QPaintEvent*)
80 const int ValueMargin = 3;
83 p.setRenderHint(QPainter::Antialiasing);
85 const double tick_period = view_.tick_period();
86 const unsigned int prefix = view_.tick_prefix();
88 // Draw the tick marks
89 p.setPen(palette().color(foregroundRole()));
91 const double minor_tick_period = tick_period / MinorTickSubdivision;
92 const double first_major_division =
93 floor(view_.offset() / tick_period);
94 const double first_minor_division =
95 ceil(view_.offset() / minor_tick_period);
96 const double t0 = first_major_division * tick_period;
98 int division = (int)round(first_minor_division -
99 first_major_division * MinorTickSubdivision) - 1;
101 const int text_height = calculate_text_height();
102 const int ruler_height = RulerHeight * text_height;
103 const int major_tick_y1 = text_height + ValueMargin * 2;
104 const int minor_tick_y1 = (major_tick_y1 + ruler_height) / 2;
109 const double t = t0 + division * minor_tick_period;
110 x = (t - view_.offset()) / view_.scale();
112 if (division % MinorTickSubdivision == 0)
115 p.drawText(x, ValueMargin, 0, text_height,
116 AlignCenter | AlignTop | TextDontClip,
117 pv::util::format_time(t, prefix));
118 p.drawLine(QPointF(x, major_tick_y1),
119 QPointF(x, ruler_height));
124 p.drawLine(QPointF(x, minor_tick_y1),
125 QPointF(x, ruler_height));
130 } while (x < width());
132 // Draw the hover mark
133 draw_hover_mark(p, text_height);
135 // The cursor labels are not drawn with the arrows exactly on the
136 // bottom line of the widget, because then the selection shadow
137 // would be clipped away.
138 const QRect r = rect().adjusted(0, 0, 0, -ViewItem::HighlightRadius);
141 const vector< shared_ptr<TimeItem> > items(view_.time_items());
142 for (auto &i : items) {
143 const bool highlight = !dragging_ &&
144 i->label_rect(r).contains(mouse_point_);
145 i->paint_label(p, r, highlight);
149 void Ruler::mouseMoveEvent(QMouseEvent *e)
151 mouse_point_ = e->pos();
153 if (!(e->buttons() & Qt::LeftButton))
156 if ((e->pos() - mouse_down_point_).manhattanLength() <
157 QApplication::startDragDistance())
163 const int delta = e->pos().x() - mouse_down_point_.x();
164 const vector< shared_ptr<TimeItem> > items(view_.time_items());
165 for (auto &i : items)
167 i->set_time(view_.offset() +
168 (i->drag_point().x() + delta - 0.5) *
172 void Ruler::mousePressEvent(QMouseEvent *e)
174 if (e->buttons() & Qt::LeftButton) {
175 mouse_down_point_ = e->pos();
177 mouse_down_item_.reset();
181 const vector< shared_ptr<TimeItem> > items(view_.time_items());
182 for (auto i = items.rbegin(); i != items.rend(); i++)
183 if ((*i)->label_rect(rect()).contains(e->pos())) {
184 mouse_down_item_ = (*i);
188 if (mouse_down_item_) {
189 mouse_down_item_->select();
190 mouse_down_item_->drag();
197 void Ruler::mouseReleaseEvent(QMouseEvent *)
199 using pv::widgets::Popup;
201 if (!dragging_ && mouse_down_item_) {
202 Popup *const p = mouse_down_item_->create_popup(&view_);
204 const QPoint arrpos(mouse_down_item_->get_x(),
205 height() - ViewItem::HighlightRadius);
206 p->set_position(mapToGlobal(arrpos), Popup::Bottom);
212 mouse_down_item_.reset();
214 const vector< shared_ptr<TimeItem> > items(view_.time_items());
215 for (auto &i : items)
219 void Ruler::leaveEvent(QEvent*)
221 mouse_point_ = QPoint(-1, -1);
225 void Ruler::mouseDoubleClickEvent(QMouseEvent *e)
227 view_.add_flag(view_.offset() + ((double)e->x() + 0.5) * view_.scale());
230 void Ruler::keyPressEvent(QKeyEvent *e)
234 if (e->key() == Qt::Key_Delete)
236 const vector< shared_ptr<TimeItem> > items(view_.time_items());
237 for (auto &i : items)
243 void Ruler::draw_hover_mark(QPainter &p, int text_height)
245 const int x = view_.hover_point().x();
250 p.setPen(QPen(Qt::NoPen));
251 p.setBrush(QBrush(palette().color(foregroundRole())));
253 const int b = RulerHeight * text_height;
254 const float hover_arrow_size = HoverArrowSize * text_height;
255 const QPointF points[] = {
257 QPointF(x - hover_arrow_size, b - hover_arrow_size),
258 QPointF(x + hover_arrow_size, b - hover_arrow_size)
260 p.drawPolygon(points, countof(points));
263 int Ruler::calculate_text_height() const
265 return QFontMetrics(font()).ascent();
268 void Ruler::hover_point_changed()