]> sigrok.org Git - pulseview.git/blame - pv/view/ruler.cpp
ViewItem: Moved in paint_label
[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
a6c1726e 41const int Ruler::RulerHeight = 30;
ccdd3ef5 42const int Ruler::MinorTickSubdivision = 4;
ccdd3ef5 43
b3a7c013
JH
44const int Ruler::HoverArrowSize = 5;
45
819e2e95
JH
46const int Ruler::Padding = 20;
47const int Ruler::BaselineOffset = 5;
48
ccdd3ef5 49Ruler::Ruler(View &parent) :
819e2e95
JH
50 MarginWidget(parent),
51 text_height_(calculate_text_height())
ccdd3ef5 52{
b3a7c013
JH
53 setMouseTracking(true);
54
8dbbc7f0 55 connect(&view_, SIGNAL(hover_point_changed()),
b3a7c013 56 this, SLOT(hover_point_changed()));
ccdd3ef5
JH
57}
58
819e2e95
JH
59void Ruler::clear_selection()
60{
61 const vector< shared_ptr<TimeItem> > items(view_.time_items());
62 for (auto &i : items)
63 i->select(false);
64 update();
65}
66
a6c1726e
JH
67QSize Ruler::sizeHint() const
68{
69 return QSize(0, RulerHeight);
70}
71
819e2e95
JH
72QSize Ruler::extended_size_hint() const
73{
74 return QSize(0, RulerHeight +
75 (text_height_ + Padding + BaselineOffset) / 2);
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
819e2e95
JH
101 const int major_tick_y1 = text_height_ + ValueMargin * 2;
102 const int tick_y2 = RulerHeight;
f260e3bf
JH
103 const int minor_tick_y1 = (major_tick_y1 + tick_y2) / 2;
104
64b678ba 105 double x;
ccdd3ef5 106
64b678ba
JH
107 do {
108 const double t = t0 + division * minor_tick_period;
8dbbc7f0 109 x = (t - view_.offset()) / view_.scale();
ccdd3ef5 110
333d5bbc 111 if (division % MinorTickSubdivision == 0)
ccdd3ef5
JH
112 {
113 // Draw a major tick
819e2e95 114 p.drawText(x, ValueMargin, 0, text_height_,
f260e3bf 115 AlignCenter | AlignTop | TextDontClip,
f0c9f81c 116 pv::util::format_time(t, prefix));
f260e3bf
JH
117 p.drawLine(QPointF(x, major_tick_y1),
118 QPointF(x, tick_y2));
ccdd3ef5
JH
119 }
120 else
121 {
122 // Draw a minor tick
f260e3bf
JH
123 p.drawLine(QPointF(x, minor_tick_y1),
124 QPointF(x, tick_y2));
ccdd3ef5
JH
125 }
126
127 division++;
64b678ba
JH
128
129 } while (x < width());
ccdd3ef5 130
b3a7c013
JH
131 // Draw the hover mark
132 draw_hover_mark(p);
819e2e95
JH
133
134 // The cursor labels are not drawn with the arrows exactly on the
135 // bottom line of the widget, because then the selection shadow
136 // would be clipped away.
137 const QRect r = rect().adjusted(0, 0, 0, -BaselineOffset);
138
139 // Draw the items
140 const vector< shared_ptr<TimeItem> > items(view_.time_items());
49028d6c
JH
141 for (auto &i : items) {
142 const bool highlight = !dragging_ &&
143 i->label_rect(r).contains(mouse_point_);
144 i->paint_label(p, r, highlight);
145 }
819e2e95
JH
146}
147
148void Ruler::mouseMoveEvent(QMouseEvent *e)
149{
150 mouse_point_ = e->pos();
151
152 if (!(e->buttons() & Qt::LeftButton))
153 return;
154
155 if ((e->pos() - mouse_down_point_).manhattanLength() <
156 QApplication::startDragDistance())
157 return;
158
159 // Do the drag
160 dragging_ = true;
161
162 const int delta = e->pos().x() - mouse_down_point_.x();
163 const vector< shared_ptr<TimeItem> > items(view_.time_items());
164 for (auto &i : items)
165 if (i->dragging())
166 i->set_time(view_.offset() +
167 (i->drag_point().x() + delta - 0.5) *
168 view_.scale());
169}
170
171void Ruler::mousePressEvent(QMouseEvent *e)
172{
173 if (e->buttons() & Qt::LeftButton) {
174 mouse_down_point_ = e->pos();
175
176 mouse_down_item_.reset();
177
178 clear_selection();
179
180 const vector< shared_ptr<TimeItem> > items(view_.time_items());
181 for (auto i = items.rbegin(); i != items.rend(); i++)
182 if ((*i)->label_rect(rect()).contains(e->pos())) {
183 mouse_down_item_ = (*i);
184 break;
185 }
186
187 if (mouse_down_item_) {
188 mouse_down_item_->select();
189 mouse_down_item_->drag();
190 }
191
192 selection_changed();
193 }
194}
195
196void Ruler::mouseReleaseEvent(QMouseEvent *)
197{
198 using pv::widgets::Popup;
199
200 if (!dragging_ && mouse_down_item_) {
201 Popup *const p = mouse_down_item_->create_popup(&view_);
202 if (p) {
203 const QPoint arrpos(mouse_down_item_->get_x(),
204 height() - BaselineOffset);
205 p->set_position(mapToGlobal(arrpos), Popup::Bottom);
206 p->show();
207 }
208 }
209
210 dragging_ = false;
211 mouse_down_item_.reset();
212
213 const vector< shared_ptr<TimeItem> > items(view_.time_items());
214 for (auto &i : items)
215 i->drag_release();
216}
217
218void Ruler::leaveEvent(QEvent*)
219{
220 mouse_point_ = QPoint(-1, -1);
221 update();
222}
223
224void Ruler::mouseDoubleClickEvent(QMouseEvent *e)
225{
226 view_.add_flag(view_.offset() + ((double)e->x() + 0.5) * view_.scale());
227}
228
229void Ruler::keyPressEvent(QKeyEvent *e)
230{
231 assert(e);
232
233 if (e->key() == Qt::Key_Delete)
234 {
235 const vector< shared_ptr<TimeItem> > items(view_.time_items());
236 for (auto &i : items)
237 if (i->selected())
238 i->delete_pressed();
239 }
ca4ec3ea
JH
240}
241
b3a7c013
JH
242void Ruler::draw_hover_mark(QPainter &p)
243{
8dbbc7f0 244 const int x = view_.hover_point().x();
333d5bbc 245
84a0d458 246 if (x == -1)
b3a7c013
JH
247 return;
248
249 p.setPen(QPen(Qt::NoPen));
ad1d8e2b 250 p.setBrush(QBrush(palette().color(foregroundRole())));
b3a7c013 251
819e2e95 252 const int b = RulerHeight;
b3a7c013
JH
253 const QPointF points[] = {
254 QPointF(x, b),
255 QPointF(x - HoverArrowSize, b - HoverArrowSize),
256 QPointF(x + HoverArrowSize, b - HoverArrowSize)
257 };
258 p.drawPolygon(points, countof(points));
259}
260
819e2e95
JH
261int Ruler::calculate_text_height()
262{
263 QFontMetrics fm(font());
264 return fm.boundingRect(0, 0, INT_MAX, INT_MAX,
265 Qt::AlignLeft | Qt::AlignTop, "8").height();
266}
267
b3a7c013
JH
268void Ruler::hover_point_changed()
269{
270 update();
271}
272
ccdd3ef5
JH
273} // namespace view
274} // namespace pv