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>
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 Ruler::Ruler(View &parent) :
49 setMouseTracking(true);
51 connect(&view_, SIGNAL(hover_point_changed()),
52 this, SLOT(hover_point_changed()));
55 void Ruler::clear_selection()
57 const vector< shared_ptr<TimeItem> > items(view_.time_items());
63 QSize Ruler::sizeHint() const
65 const int text_height = calculate_text_height();
66 return QSize(0, RulerHeight * text_height);
69 QSize Ruler::extended_size_hint() const
72 std::vector< std::shared_ptr<TimeItem> > items(view_.time_items());
74 max_rect = max_rect.united(i->label_rect(QRect()));
75 return QSize(0, sizeHint().height() - max_rect.top() / 2 +
76 ViewItem::HighlightRadius);
79 vector< shared_ptr<ViewItem> > Ruler::items()
81 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
82 return vector< shared_ptr<ViewItem> >(
83 time_items.begin(), time_items.end());
86 shared_ptr<ViewItem> Ruler::get_mouse_over_item(const QPoint &pt)
88 const vector< shared_ptr<TimeItem> > items(view_.time_items());
89 for (auto i = items.rbegin(); i != items.rend(); i++)
90 if ((*i)->enabled() && (*i)->label_rect(rect()).contains(pt))
95 void Ruler::paintEvent(QPaintEvent*)
97 const int ValueMargin = 3;
100 p.setRenderHint(QPainter::Antialiasing);
102 const double tick_period = view_.tick_period();
103 const unsigned int prefix = view_.tick_prefix();
105 // Draw the tick marks
106 p.setPen(palette().color(foregroundRole()));
108 const double minor_tick_period = tick_period / MinorTickSubdivision;
109 const double first_major_division =
110 floor(view_.offset() / tick_period);
111 const double first_minor_division =
112 ceil(view_.offset() / minor_tick_period);
113 const double t0 = first_major_division * tick_period;
115 int division = (int)round(first_minor_division -
116 first_major_division * MinorTickSubdivision) - 1;
118 const int text_height = calculate_text_height();
119 const int ruler_height = RulerHeight * text_height;
120 const int major_tick_y1 = text_height + ValueMargin * 2;
121 const int minor_tick_y1 = (major_tick_y1 + ruler_height) / 2;
126 const double t = t0 + division * minor_tick_period;
127 x = (t - view_.offset()) / view_.scale();
129 if (division % MinorTickSubdivision == 0)
132 p.drawText(x, ValueMargin, 0, text_height,
133 AlignCenter | AlignTop | TextDontClip,
134 pv::util::format_time(t, prefix));
135 p.drawLine(QPointF(x, major_tick_y1),
136 QPointF(x, ruler_height));
141 p.drawLine(QPointF(x, minor_tick_y1),
142 QPointF(x, ruler_height));
147 } while (x < width());
149 // Draw the hover mark
150 draw_hover_mark(p, text_height);
152 // The cursor labels are not drawn with the arrows exactly on the
153 // bottom line of the widget, because then the selection shadow
154 // would be clipped away.
155 const QRect r = rect().adjusted(0, 0, 0, -ViewItem::HighlightRadius);
158 const vector< shared_ptr<TimeItem> > items(view_.time_items());
159 for (auto &i : items) {
160 const bool highlight = !dragging_ &&
161 i->label_rect(r).contains(mouse_point_);
162 i->paint_label(p, r, highlight);
166 void Ruler::mouseMoveEvent(QMouseEvent *e)
168 mouse_point_ = e->pos();
170 if (!(e->buttons() & Qt::LeftButton))
173 if ((e->pos() - mouse_down_point_).manhattanLength() <
174 QApplication::startDragDistance())
180 const QPoint delta = e->pos() - mouse_down_point_;
181 const vector< shared_ptr<TimeItem> > items(view_.time_items());
182 for (auto &i : items)
187 void Ruler::mousePressEvent(QMouseEvent *e)
189 if (e->buttons() & Qt::LeftButton) {
190 mouse_down_point_ = e->pos();
191 mouse_down_item_ = get_mouse_over_item(e->pos());
195 if (mouse_down_item_) {
196 mouse_down_item_->select();
197 mouse_down_item_->drag();
204 void Ruler::mouseReleaseEvent(QMouseEvent *)
206 using pv::widgets::Popup;
208 if (!dragging_ && mouse_down_item_)
209 show_popup(mouse_down_item_);
212 mouse_down_item_.reset();
214 const vector< shared_ptr<TimeItem> > items(view_.time_items());
215 for (auto &i : items)
219 void Ruler::mouseDoubleClickEvent(QMouseEvent *e)
221 view_.add_flag(view_.offset() + ((double)e->x() + 0.5) * view_.scale());
224 void Ruler::contextMenuEvent(QContextMenuEvent *event)
226 const shared_ptr<ViewItem> r = get_mouse_over_item(mouse_point_);
230 QMenu *menu = r->create_context_menu(this);
232 menu->exec(event->globalPos());
235 void Ruler::keyPressEvent(QKeyEvent *e)
239 if (e->key() == Qt::Key_Delete)
241 const vector< shared_ptr<TimeItem> > items(view_.time_items());
242 for (auto &i : items)
248 void Ruler::draw_hover_mark(QPainter &p, int text_height)
250 const int x = view_.hover_point().x();
255 p.setPen(QPen(Qt::NoPen));
256 p.setBrush(QBrush(palette().color(foregroundRole())));
258 const int b = RulerHeight * text_height;
259 const float hover_arrow_size = HoverArrowSize * text_height;
260 const QPointF points[] = {
262 QPointF(x - hover_arrow_size, b - hover_arrow_size),
263 QPointF(x + hover_arrow_size, b - hover_arrow_size)
265 p.drawPolygon(points, countof(points));
268 int Ruler::calculate_text_height() const
270 return QFontMetrics(font()).ascent();
273 void Ruler::hover_point_changed()