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