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>
34 using std::shared_ptr;
40 const float Ruler::RulerHeight = 2.5f; // x Text Height
41 const int Ruler::MinorTickSubdivision = 4;
43 const float Ruler::HoverArrowSize = 0.5f; // x Text Height
45 Ruler::Ruler(View &parent) :
48 setMouseTracking(true);
50 connect(&view_, SIGNAL(hover_point_changed()),
51 this, SLOT(hover_point_changed()));
54 QSize Ruler::sizeHint() const
56 const int text_height = calculate_text_height();
57 return QSize(0, RulerHeight * text_height);
60 QSize Ruler::extended_size_hint() const
63 std::vector< std::shared_ptr<TimeItem> > items(view_.time_items());
65 max_rect = max_rect.united(i->label_rect(QRect()));
66 return QSize(0, sizeHint().height() - max_rect.top() / 2 +
67 ViewItem::HighlightRadius);
70 vector< shared_ptr<ViewItem> > Ruler::items()
72 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
73 return vector< shared_ptr<ViewItem> >(
74 time_items.begin(), time_items.end());
77 shared_ptr<ViewItem> Ruler::get_mouse_over_item(const QPoint &pt)
79 const vector< shared_ptr<TimeItem> > items(view_.time_items());
80 for (auto i = items.rbegin(); i != items.rend(); i++)
81 if ((*i)->enabled() && (*i)->label_rect(rect()).contains(pt))
86 bool Ruler::accept_drag() const
91 void Ruler::drag_items(const QPoint &delta)
93 const vector< shared_ptr<TimeItem> > items(view_.time_items());
99 void Ruler::paintEvent(QPaintEvent*)
101 const int ValueMargin = 3;
104 p.setRenderHint(QPainter::Antialiasing);
106 const double tick_period = view_.tick_period();
107 const unsigned int prefix = view_.tick_prefix();
109 // Draw the tick marks
110 p.setPen(palette().color(foregroundRole()));
112 const double minor_tick_period = tick_period / MinorTickSubdivision;
113 const double first_major_division =
114 floor(view_.offset() / tick_period);
115 const double first_minor_division =
116 ceil(view_.offset() / minor_tick_period);
117 const double t0 = first_major_division * tick_period;
119 int division = (int)round(first_minor_division -
120 first_major_division * MinorTickSubdivision) - 1;
122 const int text_height = calculate_text_height();
123 const int ruler_height = RulerHeight * text_height;
124 const int major_tick_y1 = text_height + ValueMargin * 2;
125 const int minor_tick_y1 = (major_tick_y1 + ruler_height) / 2;
130 const double t = t0 + division * minor_tick_period;
131 x = (t - view_.offset()) / view_.scale();
133 if (division % MinorTickSubdivision == 0)
136 p.drawText(x, ValueMargin, 0, text_height,
137 AlignCenter | AlignTop | TextDontClip,
138 pv::util::format_time(t, prefix));
139 p.drawLine(QPointF(x, major_tick_y1),
140 QPointF(x, ruler_height));
145 p.drawLine(QPointF(x, minor_tick_y1),
146 QPointF(x, ruler_height));
151 } while (x < width());
153 // Draw the hover mark
154 draw_hover_mark(p, text_height);
156 // The cursor labels are not drawn with the arrows exactly on the
157 // bottom line of the widget, because then the selection shadow
158 // would be clipped away.
159 const QRect r = rect().adjusted(0, 0, 0, -ViewItem::HighlightRadius);
162 const vector< shared_ptr<TimeItem> > items(view_.time_items());
163 for (auto &i : items) {
164 const bool highlight = !dragging_ &&
165 i->label_rect(r).contains(mouse_point_);
166 i->paint_label(p, r, highlight);
170 void Ruler::mouseDoubleClickEvent(QMouseEvent *e)
172 view_.add_flag(view_.offset() + ((double)e->x() + 0.5) * view_.scale());
175 void Ruler::draw_hover_mark(QPainter &p, int text_height)
177 const int x = view_.hover_point().x();
182 p.setPen(QPen(Qt::NoPen));
183 p.setBrush(QBrush(palette().color(foregroundRole())));
185 const int b = RulerHeight * text_height;
186 const float hover_arrow_size = HoverArrowSize * text_height;
187 const QPointF points[] = {
189 QPointF(x - hover_arrow_size, b - hover_arrow_size),
190 QPointF(x + hover_arrow_size, b - hover_arrow_size)
192 p.drawPolygon(points, countof(points));
195 int Ruler::calculate_text_height() const
197 return QFontMetrics(font()).ascent();
200 void Ruler::hover_point_changed()