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