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>
26 #include <QMouseEvent>
31 #include <pv/util.hpp>
32 #include <pv/widgets/popup.hpp>
36 using std::shared_ptr;
42 const float Ruler::RulerHeight = 2.5f; // x Text Height
43 const int Ruler::MinorTickSubdivision = 4;
45 const float Ruler::HoverArrowSize = 0.5f; // x Text Height
47 Ruler::Ruler(View &parent) :
50 setMouseTracking(true);
52 connect(&view_, SIGNAL(hover_point_changed()),
53 this, SLOT(hover_point_changed()));
56 void Ruler::clear_selection()
58 const vector< shared_ptr<TimeItem> > items(view_.time_items());
64 QSize Ruler::sizeHint() const
66 const int text_height = calculate_text_height();
67 return QSize(0, RulerHeight * text_height);
70 QSize Ruler::extended_size_hint() const
73 std::vector< std::shared_ptr<TimeItem> > items(view_.time_items());
75 max_rect = max_rect.united(i->label_rect(QRect()));
76 return QSize(0, sizeHint().height() - max_rect.top() / 2 +
77 ViewItem::HighlightRadius);
80 shared_ptr<TimeItem> Ruler::get_mouse_over_item(const QPoint &pt)
82 const vector< shared_ptr<TimeItem> > items(view_.time_items());
83 for (auto i = items.rbegin(); i != items.rend(); i++)
84 if ((*i)->enabled() && (*i)->label_rect(rect()).contains(pt))
89 void Ruler::paintEvent(QPaintEvent*)
91 const int ValueMargin = 3;
94 p.setRenderHint(QPainter::Antialiasing);
96 const double tick_period = view_.tick_period();
97 const unsigned int prefix = view_.tick_prefix();
99 // Draw the tick marks
100 p.setPen(palette().color(foregroundRole()));
102 const double minor_tick_period = tick_period / MinorTickSubdivision;
103 const double first_major_division =
104 floor(view_.offset() / tick_period);
105 const double first_minor_division =
106 ceil(view_.offset() / minor_tick_period);
107 const double t0 = first_major_division * tick_period;
109 int division = (int)round(first_minor_division -
110 first_major_division * MinorTickSubdivision) - 1;
112 const int text_height = calculate_text_height();
113 const int ruler_height = RulerHeight * text_height;
114 const int major_tick_y1 = text_height + ValueMargin * 2;
115 const int minor_tick_y1 = (major_tick_y1 + ruler_height) / 2;
120 const double t = t0 + division * minor_tick_period;
121 x = (t - view_.offset()) / view_.scale();
123 if (division % MinorTickSubdivision == 0)
126 p.drawText(x, ValueMargin, 0, text_height,
127 AlignCenter | AlignTop | TextDontClip,
128 pv::util::format_time(t, prefix));
129 p.drawLine(QPointF(x, major_tick_y1),
130 QPointF(x, ruler_height));
135 p.drawLine(QPointF(x, minor_tick_y1),
136 QPointF(x, ruler_height));
141 } while (x < width());
143 // Draw the hover mark
144 draw_hover_mark(p, text_height);
146 // The cursor labels are not drawn with the arrows exactly on the
147 // bottom line of the widget, because then the selection shadow
148 // would be clipped away.
149 const QRect r = rect().adjusted(0, 0, 0, -ViewItem::HighlightRadius);
152 const vector< shared_ptr<TimeItem> > items(view_.time_items());
153 for (auto &i : items) {
154 const bool highlight = !dragging_ &&
155 i->label_rect(r).contains(mouse_point_);
156 i->paint_label(p, r, highlight);
160 void Ruler::mouseMoveEvent(QMouseEvent *e)
162 mouse_point_ = e->pos();
164 if (!(e->buttons() & Qt::LeftButton))
167 if ((e->pos() - mouse_down_point_).manhattanLength() <
168 QApplication::startDragDistance())
174 const int delta = e->pos().x() - mouse_down_point_.x();
175 const vector< shared_ptr<TimeItem> > items(view_.time_items());
176 for (auto &i : items)
178 i->set_time(view_.offset() +
179 (i->drag_point().x() + delta - 0.5) *
183 void Ruler::mousePressEvent(QMouseEvent *e)
185 if (e->buttons() & Qt::LeftButton) {
186 mouse_down_point_ = e->pos();
187 mouse_down_item_ = get_mouse_over_item(e->pos());
191 if (mouse_down_item_) {
192 mouse_down_item_->select();
193 mouse_down_item_->drag();
200 void Ruler::mouseReleaseEvent(QMouseEvent *)
202 using pv::widgets::Popup;
204 if (!dragging_ && mouse_down_item_) {
205 Popup *const p = mouse_down_item_->create_popup(&view_);
207 const QPoint arrpos(mouse_down_item_->get_x(),
208 height() - ViewItem::HighlightRadius);
209 p->set_position(mapToGlobal(arrpos), Popup::Bottom);
215 mouse_down_item_.reset();
217 const vector< shared_ptr<TimeItem> > items(view_.time_items());
218 for (auto &i : items)
222 void Ruler::leaveEvent(QEvent*)
224 mouse_point_ = QPoint(-1, -1);
228 void Ruler::mouseDoubleClickEvent(QMouseEvent *e)
230 view_.add_flag(view_.offset() + ((double)e->x() + 0.5) * view_.scale());
233 void Ruler::contextMenuEvent(QContextMenuEvent *event)
235 const shared_ptr<TimeItem> r = get_mouse_over_item(mouse_point_);
239 QMenu *menu = r->create_context_menu(this);
241 menu->exec(event->globalPos());
244 void Ruler::keyPressEvent(QKeyEvent *e)
248 if (e->key() == Qt::Key_Delete)
250 const vector< shared_ptr<TimeItem> > items(view_.time_items());
251 for (auto &i : items)
257 void Ruler::draw_hover_mark(QPainter &p, int text_height)
259 const int x = view_.hover_point().x();
264 p.setPen(QPen(Qt::NoPen));
265 p.setBrush(QBrush(palette().color(foregroundRole())));
267 const int b = RulerHeight * text_height;
268 const float hover_arrow_size = HoverArrowSize * text_height;
269 const QPointF points[] = {
271 QPointF(x - hover_arrow_size, b - hover_arrow_size),
272 QPointF(x + hover_arrow_size, b - hover_arrow_size)
274 p.drawPolygon(points, countof(points));
277 int Ruler::calculate_text_height() const
279 return QFontMetrics(font()).ascent();
282 void Ruler::hover_point_changed()