]> sigrok.org Git - pulseview.git/blame - pv/view/ruler.cpp
TimeMarker: Simplified label_rect
[pulseview.git] / pv / view / ruler.cpp
CommitLineData
ccdd3ef5 1/*
b3f22de0 2 * This file is part of the PulseView project.
ccdd3ef5
JH
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
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.
10 *
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.
15 *
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
19 */
20
819e2e95 21#include <extdef.h>
ca4ec3ea 22
819e2e95
JH
23#include <QApplication>
24#include <QFontMetrics>
25#include <QMouseEvent>
26
27#include "ruler.hpp"
2acdb232 28#include "view.hpp"
ccdd3ef5 29
819e2e95
JH
30#include <pv/util.hpp>
31#include <pv/widgets/popup.hpp>
ccdd3ef5 32
819f4c25 33using namespace Qt;
f76af637 34
819e2e95
JH
35using std::shared_ptr;
36using std::vector;
37
ccdd3ef5
JH
38namespace pv {
39namespace view {
40
249229ec 41const float Ruler::RulerHeight = 2.5f; // x Text Height
ccdd3ef5 42const int Ruler::MinorTickSubdivision = 4;
ccdd3ef5 43
415341a1 44const float Ruler::HoverArrowSize = 0.5f; // x Text Height
b3a7c013 45
819e2e95 46const int Ruler::Padding = 20;
819e2e95 47
ccdd3ef5 48Ruler::Ruler(View &parent) :
e456e8e1 49 MarginWidget(parent)
ccdd3ef5 50{
b3a7c013
JH
51 setMouseTracking(true);
52
8dbbc7f0 53 connect(&view_, SIGNAL(hover_point_changed()),
b3a7c013 54 this, SLOT(hover_point_changed()));
ccdd3ef5
JH
55}
56
819e2e95
JH
57void Ruler::clear_selection()
58{
59 const vector< shared_ptr<TimeItem> > items(view_.time_items());
60 for (auto &i : items)
61 i->select(false);
62 update();
63}
64
a6c1726e
JH
65QSize Ruler::sizeHint() const
66{
249229ec
JH
67 const int text_height = calculate_text_height();
68 return QSize(0, RulerHeight * text_height);
a6c1726e
JH
69}
70
819e2e95
JH
71QSize Ruler::extended_size_hint() const
72{
e456e8e1 73 const int text_height = calculate_text_height();
249229ec 74 return QSize(0, RulerHeight * text_height +
09d5df11 75 (text_height + Padding + ViewItem::HighlightRadius) / 2);
819e2e95
JH
76}
77
e314eca4 78void Ruler::paintEvent(QPaintEvent*)
ccdd3ef5 79{
f260e3bf 80 const int ValueMargin = 3;
3f509c1f 81
ccdd3ef5 82 QPainter p(this);
ce6e73a8 83 p.setRenderHint(QPainter::Antialiasing);
ccdd3ef5 84
361c560e
JH
85 const double tick_period = view_.tick_period();
86 const unsigned int prefix = view_.tick_prefix();
1b5813fe 87
ccdd3ef5 88 // Draw the tick marks
d2caed8d 89 p.setPen(palette().color(foregroundRole()));
ccdd3ef5
JH
90
91 const double minor_tick_period = tick_period / MinorTickSubdivision;
92 const double first_major_division =
8dbbc7f0 93 floor(view_.offset() / tick_period);
ccdd3ef5 94 const double first_minor_division =
8dbbc7f0 95 ceil(view_.offset() / minor_tick_period);
ccdd3ef5
JH
96 const double t0 = first_major_division * tick_period;
97
98 int division = (int)round(first_minor_division -
64b678ba 99 first_major_division * MinorTickSubdivision) - 1;
f260e3bf 100
e456e8e1 101 const int text_height = calculate_text_height();
249229ec 102 const int ruler_height = RulerHeight * text_height;
e456e8e1 103 const int major_tick_y1 = text_height + ValueMargin * 2;
249229ec 104 const int minor_tick_y1 = (major_tick_y1 + ruler_height) / 2;
f260e3bf 105
64b678ba 106 double x;
ccdd3ef5 107
64b678ba
JH
108 do {
109 const double t = t0 + division * minor_tick_period;
8dbbc7f0 110 x = (t - view_.offset()) / view_.scale();
ccdd3ef5 111
333d5bbc 112 if (division % MinorTickSubdivision == 0)
ccdd3ef5
JH
113 {
114 // Draw a major tick
e456e8e1 115 p.drawText(x, ValueMargin, 0, text_height,
f260e3bf 116 AlignCenter | AlignTop | TextDontClip,
f0c9f81c 117 pv::util::format_time(t, prefix));
f260e3bf 118 p.drawLine(QPointF(x, major_tick_y1),
249229ec 119 QPointF(x, ruler_height));
ccdd3ef5
JH
120 }
121 else
122 {
123 // Draw a minor tick
f260e3bf 124 p.drawLine(QPointF(x, minor_tick_y1),
249229ec 125 QPointF(x, ruler_height));
ccdd3ef5
JH
126 }
127
128 division++;
64b678ba
JH
129
130 } while (x < width());
ccdd3ef5 131
b3a7c013 132 // Draw the hover mark
249229ec 133 draw_hover_mark(p, text_height);
819e2e95
JH
134
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.
09d5df11 138 const QRect r = rect().adjusted(0, 0, 0, -ViewItem::HighlightRadius);
819e2e95
JH
139
140 // Draw the items
141 const vector< shared_ptr<TimeItem> > items(view_.time_items());
49028d6c
JH
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);
146 }
819e2e95
JH
147}
148
149void Ruler::mouseMoveEvent(QMouseEvent *e)
150{
151 mouse_point_ = e->pos();
152
153 if (!(e->buttons() & Qt::LeftButton))
154 return;
155
156 if ((e->pos() - mouse_down_point_).manhattanLength() <
157 QApplication::startDragDistance())
158 return;
159
160 // Do the drag
161 dragging_ = true;
162
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)
166 if (i->dragging())
167 i->set_time(view_.offset() +
168 (i->drag_point().x() + delta - 0.5) *
169 view_.scale());
170}
171
172void Ruler::mousePressEvent(QMouseEvent *e)
173{
174 if (e->buttons() & Qt::LeftButton) {
175 mouse_down_point_ = e->pos();
176
177 mouse_down_item_.reset();
178
179 clear_selection();
180
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);
185 break;
186 }
187
188 if (mouse_down_item_) {
189 mouse_down_item_->select();
190 mouse_down_item_->drag();
191 }
192
193 selection_changed();
194 }
195}
196
197void Ruler::mouseReleaseEvent(QMouseEvent *)
198{
199 using pv::widgets::Popup;
200
201 if (!dragging_ && mouse_down_item_) {
202 Popup *const p = mouse_down_item_->create_popup(&view_);
203 if (p) {
204 const QPoint arrpos(mouse_down_item_->get_x(),
09d5df11 205 height() - ViewItem::HighlightRadius);
819e2e95
JH
206 p->set_position(mapToGlobal(arrpos), Popup::Bottom);
207 p->show();
208 }
209 }
210
211 dragging_ = false;
212 mouse_down_item_.reset();
213
214 const vector< shared_ptr<TimeItem> > items(view_.time_items());
215 for (auto &i : items)
216 i->drag_release();
217}
218
219void Ruler::leaveEvent(QEvent*)
220{
221 mouse_point_ = QPoint(-1, -1);
222 update();
223}
224
225void Ruler::mouseDoubleClickEvent(QMouseEvent *e)
226{
227 view_.add_flag(view_.offset() + ((double)e->x() + 0.5) * view_.scale());
228}
229
230void Ruler::keyPressEvent(QKeyEvent *e)
231{
232 assert(e);
233
234 if (e->key() == Qt::Key_Delete)
235 {
236 const vector< shared_ptr<TimeItem> > items(view_.time_items());
237 for (auto &i : items)
238 if (i->selected())
239 i->delete_pressed();
240 }
ca4ec3ea
JH
241}
242
249229ec 243void Ruler::draw_hover_mark(QPainter &p, int text_height)
b3a7c013 244{
8dbbc7f0 245 const int x = view_.hover_point().x();
333d5bbc 246
84a0d458 247 if (x == -1)
b3a7c013
JH
248 return;
249
250 p.setPen(QPen(Qt::NoPen));
ad1d8e2b 251 p.setBrush(QBrush(palette().color(foregroundRole())));
b3a7c013 252
249229ec 253 const int b = RulerHeight * text_height;
415341a1 254 const float hover_arrow_size = HoverArrowSize * text_height;
b3a7c013
JH
255 const QPointF points[] = {
256 QPointF(x, b),
415341a1
JH
257 QPointF(x - hover_arrow_size, b - hover_arrow_size),
258 QPointF(x + hover_arrow_size, b - hover_arrow_size)
b3a7c013
JH
259 };
260 p.drawPolygon(points, countof(points));
261}
262
e456e8e1 263int Ruler::calculate_text_height() const
819e2e95 264{
c0096500 265 return QFontMetrics(font()).ascent();
819e2e95
JH
266}
267
b3a7c013
JH
268void Ruler::hover_point_changed()
269{
270 update();
271}
272
ccdd3ef5
JH
273} // namespace view
274} // namespace pv