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