]> sigrok.org Git - pulseview.git/blame - pv/view/ruler.cpp
MarginWidget: Added item() method
[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>
115a522a 25#include <QMenu>
819e2e95
JH
26#include <QMouseEvent>
27
28#include "ruler.hpp"
2acdb232 29#include "view.hpp"
ccdd3ef5 30
819e2e95 31#include <pv/util.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
ccdd3ef5 46Ruler::Ruler(View &parent) :
e456e8e1 47 MarginWidget(parent)
ccdd3ef5 48{
b3a7c013
JH
49 setMouseTracking(true);
50
8dbbc7f0 51 connect(&view_, SIGNAL(hover_point_changed()),
b3a7c013 52 this, SLOT(hover_point_changed()));
ccdd3ef5
JH
53}
54
819e2e95
JH
55void Ruler::clear_selection()
56{
57 const vector< shared_ptr<TimeItem> > items(view_.time_items());
58 for (auto &i : items)
59 i->select(false);
60 update();
61}
62
a6c1726e
JH
63QSize Ruler::sizeHint() const
64{
249229ec
JH
65 const int text_height = calculate_text_height();
66 return QSize(0, RulerHeight * text_height);
a6c1726e
JH
67}
68
819e2e95
JH
69QSize Ruler::extended_size_hint() const
70{
f5b833c6
JH
71 QRectF max_rect;
72 std::vector< std::shared_ptr<TimeItem> > items(view_.time_items());
73 for (auto &i : items)
74 max_rect = max_rect.united(i->label_rect(QRect()));
75 return QSize(0, sizeHint().height() - max_rect.top() / 2 +
76 ViewItem::HighlightRadius);
819e2e95
JH
77}
78
3e124bee
JH
79vector< shared_ptr<ViewItem> > Ruler::items()
80{
81 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
82 return vector< shared_ptr<ViewItem> >(
83 time_items.begin(), time_items.end());
84}
85
6871ee9f 86shared_ptr<ViewItem> Ruler::get_mouse_over_item(const QPoint &pt)
25a0d47c
JH
87{
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))
91 return *i;
92 return nullptr;
93}
94
e314eca4 95void Ruler::paintEvent(QPaintEvent*)
ccdd3ef5 96{
f260e3bf 97 const int ValueMargin = 3;
3f509c1f 98
ccdd3ef5 99 QPainter p(this);
ce6e73a8 100 p.setRenderHint(QPainter::Antialiasing);
ccdd3ef5 101
361c560e
JH
102 const double tick_period = view_.tick_period();
103 const unsigned int prefix = view_.tick_prefix();
1b5813fe 104
ccdd3ef5 105 // Draw the tick marks
d2caed8d 106 p.setPen(palette().color(foregroundRole()));
ccdd3ef5
JH
107
108 const double minor_tick_period = tick_period / MinorTickSubdivision;
109 const double first_major_division =
8dbbc7f0 110 floor(view_.offset() / tick_period);
ccdd3ef5 111 const double first_minor_division =
8dbbc7f0 112 ceil(view_.offset() / minor_tick_period);
ccdd3ef5
JH
113 const double t0 = first_major_division * tick_period;
114
115 int division = (int)round(first_minor_division -
64b678ba 116 first_major_division * MinorTickSubdivision) - 1;
f260e3bf 117
e456e8e1 118 const int text_height = calculate_text_height();
249229ec 119 const int ruler_height = RulerHeight * text_height;
e456e8e1 120 const int major_tick_y1 = text_height + ValueMargin * 2;
249229ec 121 const int minor_tick_y1 = (major_tick_y1 + ruler_height) / 2;
f260e3bf 122
64b678ba 123 double x;
ccdd3ef5 124
64b678ba
JH
125 do {
126 const double t = t0 + division * minor_tick_period;
8dbbc7f0 127 x = (t - view_.offset()) / view_.scale();
ccdd3ef5 128
333d5bbc 129 if (division % MinorTickSubdivision == 0)
ccdd3ef5
JH
130 {
131 // Draw a major tick
e456e8e1 132 p.drawText(x, ValueMargin, 0, text_height,
f260e3bf 133 AlignCenter | AlignTop | TextDontClip,
f0c9f81c 134 pv::util::format_time(t, prefix));
f260e3bf 135 p.drawLine(QPointF(x, major_tick_y1),
249229ec 136 QPointF(x, ruler_height));
ccdd3ef5
JH
137 }
138 else
139 {
140 // Draw a minor tick
f260e3bf 141 p.drawLine(QPointF(x, minor_tick_y1),
249229ec 142 QPointF(x, ruler_height));
ccdd3ef5
JH
143 }
144
145 division++;
64b678ba
JH
146
147 } while (x < width());
ccdd3ef5 148
b3a7c013 149 // Draw the hover mark
249229ec 150 draw_hover_mark(p, text_height);
819e2e95
JH
151
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.
09d5df11 155 const QRect r = rect().adjusted(0, 0, 0, -ViewItem::HighlightRadius);
819e2e95
JH
156
157 // Draw the items
158 const vector< shared_ptr<TimeItem> > items(view_.time_items());
49028d6c
JH
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);
163 }
819e2e95
JH
164}
165
166void Ruler::mouseMoveEvent(QMouseEvent *e)
167{
168 mouse_point_ = e->pos();
169
170 if (!(e->buttons() & Qt::LeftButton))
171 return;
172
173 if ((e->pos() - mouse_down_point_).manhattanLength() <
174 QApplication::startDragDistance())
175 return;
176
177 // Do the drag
178 dragging_ = true;
179
23e75650 180 const QPoint delta = e->pos() - mouse_down_point_;
819e2e95
JH
181 const vector< shared_ptr<TimeItem> > items(view_.time_items());
182 for (auto &i : items)
183 if (i->dragging())
23e75650 184 i->drag_by(delta);
819e2e95
JH
185}
186
187void Ruler::mousePressEvent(QMouseEvent *e)
188{
189 if (e->buttons() & Qt::LeftButton) {
190 mouse_down_point_ = e->pos();
25a0d47c 191 mouse_down_item_ = get_mouse_over_item(e->pos());
819e2e95
JH
192
193 clear_selection();
194
819e2e95
JH
195 if (mouse_down_item_) {
196 mouse_down_item_->select();
197 mouse_down_item_->drag();
198 }
199
200 selection_changed();
201 }
202}
203
204void Ruler::mouseReleaseEvent(QMouseEvent *)
205{
206 using pv::widgets::Popup;
207
786b7678
JH
208 if (!dragging_ && mouse_down_item_)
209 show_popup(mouse_down_item_);
819e2e95
JH
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
819e2e95
JH
219void Ruler::mouseDoubleClickEvent(QMouseEvent *e)
220{
221 view_.add_flag(view_.offset() + ((double)e->x() + 0.5) * view_.scale());
222}
223
115a522a
JH
224void Ruler::contextMenuEvent(QContextMenuEvent *event)
225{
6871ee9f 226 const shared_ptr<ViewItem> r = get_mouse_over_item(mouse_point_);
115a522a
JH
227 if (!r)
228 return;
229
230 QMenu *menu = r->create_context_menu(this);
231 if (menu)
232 menu->exec(event->globalPos());
233}
234
819e2e95
JH
235void Ruler::keyPressEvent(QKeyEvent *e)
236{
237 assert(e);
238
239 if (e->key() == Qt::Key_Delete)
240 {
241 const vector< shared_ptr<TimeItem> > items(view_.time_items());
242 for (auto &i : items)
243 if (i->selected())
244 i->delete_pressed();
245 }
ca4ec3ea
JH
246}
247
249229ec 248void Ruler::draw_hover_mark(QPainter &p, int text_height)
b3a7c013 249{
8dbbc7f0 250 const int x = view_.hover_point().x();
333d5bbc 251
84a0d458 252 if (x == -1)
b3a7c013
JH
253 return;
254
255 p.setPen(QPen(Qt::NoPen));
ad1d8e2b 256 p.setBrush(QBrush(palette().color(foregroundRole())));
b3a7c013 257
249229ec 258 const int b = RulerHeight * text_height;
415341a1 259 const float hover_arrow_size = HoverArrowSize * text_height;
b3a7c013
JH
260 const QPointF points[] = {
261 QPointF(x, b),
415341a1
JH
262 QPointF(x - hover_arrow_size, b - hover_arrow_size),
263 QPointF(x + hover_arrow_size, b - hover_arrow_size)
b3a7c013
JH
264 };
265 p.drawPolygon(points, countof(points));
266}
267
e456e8e1 268int Ruler::calculate_text_height() const
819e2e95 269{
c0096500 270 return QFontMetrics(font()).ascent();
819e2e95
JH
271}
272
b3a7c013
JH
273void Ruler::hover_point_changed()
274{
275 update();
276}
277
ccdd3ef5
JH
278} // namespace view
279} // namespace pv