]> sigrok.org Git - pulseview.git/blame - pv/view/ruler.cpp
MarginWidget: Moved in clear_selection
[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 30#include <pv/util.hpp>
ccdd3ef5 31
819f4c25 32using namespace Qt;
f76af637 33
819e2e95
JH
34using std::shared_ptr;
35using std::vector;
36
ccdd3ef5
JH
37namespace pv {
38namespace view {
39
249229ec 40const float Ruler::RulerHeight = 2.5f; // x Text Height
ccdd3ef5 41const int Ruler::MinorTickSubdivision = 4;
ccdd3ef5 42
415341a1 43const float Ruler::HoverArrowSize = 0.5f; // x Text Height
b3a7c013 44
ccdd3ef5 45Ruler::Ruler(View &parent) :
e456e8e1 46 MarginWidget(parent)
ccdd3ef5 47{
b3a7c013
JH
48 setMouseTracking(true);
49
8dbbc7f0 50 connect(&view_, SIGNAL(hover_point_changed()),
b3a7c013 51 this, SLOT(hover_point_changed()));
ccdd3ef5
JH
52}
53
a6c1726e
JH
54QSize Ruler::sizeHint() const
55{
249229ec
JH
56 const int text_height = calculate_text_height();
57 return QSize(0, RulerHeight * text_height);
a6c1726e
JH
58}
59
819e2e95
JH
60QSize Ruler::extended_size_hint() const
61{
f5b833c6
JH
62 QRectF max_rect;
63 std::vector< std::shared_ptr<TimeItem> > items(view_.time_items());
64 for (auto &i : items)
65 max_rect = max_rect.united(i->label_rect(QRect()));
66 return QSize(0, sizeHint().height() - max_rect.top() / 2 +
67 ViewItem::HighlightRadius);
819e2e95
JH
68}
69
3e124bee
JH
70vector< shared_ptr<ViewItem> > Ruler::items()
71{
72 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
73 return vector< shared_ptr<ViewItem> >(
74 time_items.begin(), time_items.end());
75}
76
6871ee9f 77shared_ptr<ViewItem> Ruler::get_mouse_over_item(const QPoint &pt)
25a0d47c
JH
78{
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))
82 return *i;
83 return nullptr;
84}
85
e314eca4 86void Ruler::paintEvent(QPaintEvent*)
ccdd3ef5 87{
f260e3bf 88 const int ValueMargin = 3;
3f509c1f 89
ccdd3ef5 90 QPainter p(this);
ce6e73a8 91 p.setRenderHint(QPainter::Antialiasing);
ccdd3ef5 92
361c560e
JH
93 const double tick_period = view_.tick_period();
94 const unsigned int prefix = view_.tick_prefix();
1b5813fe 95
ccdd3ef5 96 // Draw the tick marks
d2caed8d 97 p.setPen(palette().color(foregroundRole()));
ccdd3ef5
JH
98
99 const double minor_tick_period = tick_period / MinorTickSubdivision;
100 const double first_major_division =
8dbbc7f0 101 floor(view_.offset() / tick_period);
ccdd3ef5 102 const double first_minor_division =
8dbbc7f0 103 ceil(view_.offset() / minor_tick_period);
ccdd3ef5
JH
104 const double t0 = first_major_division * tick_period;
105
106 int division = (int)round(first_minor_division -
64b678ba 107 first_major_division * MinorTickSubdivision) - 1;
f260e3bf 108
e456e8e1 109 const int text_height = calculate_text_height();
249229ec 110 const int ruler_height = RulerHeight * text_height;
e456e8e1 111 const int major_tick_y1 = text_height + ValueMargin * 2;
249229ec 112 const int minor_tick_y1 = (major_tick_y1 + ruler_height) / 2;
f260e3bf 113
64b678ba 114 double x;
ccdd3ef5 115
64b678ba
JH
116 do {
117 const double t = t0 + division * minor_tick_period;
8dbbc7f0 118 x = (t - view_.offset()) / view_.scale();
ccdd3ef5 119
333d5bbc 120 if (division % MinorTickSubdivision == 0)
ccdd3ef5
JH
121 {
122 // Draw a major tick
e456e8e1 123 p.drawText(x, ValueMargin, 0, text_height,
f260e3bf 124 AlignCenter | AlignTop | TextDontClip,
f0c9f81c 125 pv::util::format_time(t, prefix));
f260e3bf 126 p.drawLine(QPointF(x, major_tick_y1),
249229ec 127 QPointF(x, ruler_height));
ccdd3ef5
JH
128 }
129 else
130 {
131 // Draw a minor tick
f260e3bf 132 p.drawLine(QPointF(x, minor_tick_y1),
249229ec 133 QPointF(x, ruler_height));
ccdd3ef5
JH
134 }
135
136 division++;
64b678ba
JH
137
138 } while (x < width());
ccdd3ef5 139
b3a7c013 140 // Draw the hover mark
249229ec 141 draw_hover_mark(p, text_height);
819e2e95
JH
142
143 // The cursor labels are not drawn with the arrows exactly on the
144 // bottom line of the widget, because then the selection shadow
145 // would be clipped away.
09d5df11 146 const QRect r = rect().adjusted(0, 0, 0, -ViewItem::HighlightRadius);
819e2e95
JH
147
148 // Draw the items
149 const vector< shared_ptr<TimeItem> > items(view_.time_items());
49028d6c
JH
150 for (auto &i : items) {
151 const bool highlight = !dragging_ &&
152 i->label_rect(r).contains(mouse_point_);
153 i->paint_label(p, r, highlight);
154 }
819e2e95
JH
155}
156
157void Ruler::mouseMoveEvent(QMouseEvent *e)
158{
159 mouse_point_ = e->pos();
160
161 if (!(e->buttons() & Qt::LeftButton))
162 return;
163
164 if ((e->pos() - mouse_down_point_).manhattanLength() <
165 QApplication::startDragDistance())
166 return;
167
168 // Do the drag
169 dragging_ = true;
170
23e75650 171 const QPoint delta = e->pos() - mouse_down_point_;
819e2e95
JH
172 const vector< shared_ptr<TimeItem> > items(view_.time_items());
173 for (auto &i : items)
174 if (i->dragging())
23e75650 175 i->drag_by(delta);
819e2e95
JH
176}
177
178void Ruler::mousePressEvent(QMouseEvent *e)
179{
180 if (e->buttons() & Qt::LeftButton) {
181 mouse_down_point_ = e->pos();
25a0d47c 182 mouse_down_item_ = get_mouse_over_item(e->pos());
819e2e95
JH
183
184 clear_selection();
185
819e2e95
JH
186 if (mouse_down_item_) {
187 mouse_down_item_->select();
188 mouse_down_item_->drag();
189 }
190
191 selection_changed();
192 }
193}
194
195void Ruler::mouseReleaseEvent(QMouseEvent *)
196{
197 using pv::widgets::Popup;
198
786b7678
JH
199 if (!dragging_ && mouse_down_item_)
200 show_popup(mouse_down_item_);
819e2e95
JH
201
202 dragging_ = false;
203 mouse_down_item_.reset();
204
205 const vector< shared_ptr<TimeItem> > items(view_.time_items());
206 for (auto &i : items)
207 i->drag_release();
208}
209
819e2e95
JH
210void Ruler::mouseDoubleClickEvent(QMouseEvent *e)
211{
212 view_.add_flag(view_.offset() + ((double)e->x() + 0.5) * view_.scale());
213}
214
215void Ruler::keyPressEvent(QKeyEvent *e)
216{
217 assert(e);
218
219 if (e->key() == Qt::Key_Delete)
220 {
221 const vector< shared_ptr<TimeItem> > items(view_.time_items());
222 for (auto &i : items)
223 if (i->selected())
224 i->delete_pressed();
225 }
ca4ec3ea
JH
226}
227
249229ec 228void Ruler::draw_hover_mark(QPainter &p, int text_height)
b3a7c013 229{
8dbbc7f0 230 const int x = view_.hover_point().x();
333d5bbc 231
84a0d458 232 if (x == -1)
b3a7c013
JH
233 return;
234
235 p.setPen(QPen(Qt::NoPen));
ad1d8e2b 236 p.setBrush(QBrush(palette().color(foregroundRole())));
b3a7c013 237
249229ec 238 const int b = RulerHeight * text_height;
415341a1 239 const float hover_arrow_size = HoverArrowSize * text_height;
b3a7c013
JH
240 const QPointF points[] = {
241 QPointF(x, b),
415341a1
JH
242 QPointF(x - hover_arrow_size, b - hover_arrow_size),
243 QPointF(x + hover_arrow_size, b - hover_arrow_size)
b3a7c013
JH
244 };
245 p.drawPolygon(points, countof(points));
246}
247
e456e8e1 248int Ruler::calculate_text_height() const
819e2e95 249{
c0096500 250 return QFontMetrics(font()).ascent();
819e2e95
JH
251}
252
b3a7c013
JH
253void Ruler::hover_point_changed()
254{
255 update();
256}
257
ccdd3ef5
JH
258} // namespace view
259} // namespace pv