]>
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> | |
115a522a | 25 | #include <QMenu> |
819e2e95 JH |
26 | #include <QMouseEvent> |
27 | ||
28 | #include "ruler.hpp" | |
2acdb232 | 29 | #include "view.hpp" |
ccdd3ef5 | 30 | |
819e2e95 JH |
31 | #include <pv/util.hpp> |
32 | #include <pv/widgets/popup.hpp> | |
ccdd3ef5 | 33 | |
819f4c25 | 34 | using namespace Qt; |
f76af637 | 35 | |
819e2e95 JH |
36 | using std::shared_ptr; |
37 | using std::vector; | |
38 | ||
ccdd3ef5 JH |
39 | namespace pv { |
40 | namespace view { | |
41 | ||
249229ec | 42 | const float Ruler::RulerHeight = 2.5f; // x Text Height |
ccdd3ef5 | 43 | const int Ruler::MinorTickSubdivision = 4; |
ccdd3ef5 | 44 | |
415341a1 | 45 | const float Ruler::HoverArrowSize = 0.5f; // x Text Height |
b3a7c013 | 46 | |
ccdd3ef5 | 47 | Ruler::Ruler(View &parent) : |
e456e8e1 | 48 | MarginWidget(parent) |
ccdd3ef5 | 49 | { |
b3a7c013 JH |
50 | setMouseTracking(true); |
51 | ||
8dbbc7f0 | 52 | connect(&view_, SIGNAL(hover_point_changed()), |
b3a7c013 | 53 | this, SLOT(hover_point_changed())); |
ccdd3ef5 JH |
54 | } |
55 | ||
819e2e95 JH |
56 | void Ruler::clear_selection() |
57 | { | |
58 | const vector< shared_ptr<TimeItem> > items(view_.time_items()); | |
59 | for (auto &i : items) | |
60 | i->select(false); | |
61 | update(); | |
62 | } | |
63 | ||
a6c1726e JH |
64 | QSize Ruler::sizeHint() const |
65 | { | |
249229ec JH |
66 | const int text_height = calculate_text_height(); |
67 | return QSize(0, RulerHeight * text_height); | |
a6c1726e JH |
68 | } |
69 | ||
819e2e95 JH |
70 | QSize Ruler::extended_size_hint() const |
71 | { | |
f5b833c6 JH |
72 | QRectF max_rect; |
73 | std::vector< std::shared_ptr<TimeItem> > items(view_.time_items()); | |
74 | for (auto &i : items) | |
75 | max_rect = max_rect.united(i->label_rect(QRect())); | |
76 | return QSize(0, sizeHint().height() - max_rect.top() / 2 + | |
77 | ViewItem::HighlightRadius); | |
819e2e95 JH |
78 | } |
79 | ||
25a0d47c JH |
80 | shared_ptr<TimeItem> Ruler::get_mouse_over_item(const QPoint &pt) |
81 | { | |
82 | const vector< shared_ptr<TimeItem> > items(view_.time_items()); | |
83 | for (auto i = items.rbegin(); i != items.rend(); i++) | |
84 | if ((*i)->enabled() && (*i)->label_rect(rect()).contains(pt)) | |
85 | return *i; | |
86 | return nullptr; | |
87 | } | |
88 | ||
e314eca4 | 89 | void Ruler::paintEvent(QPaintEvent*) |
ccdd3ef5 | 90 | { |
f260e3bf | 91 | const int ValueMargin = 3; |
3f509c1f | 92 | |
ccdd3ef5 | 93 | QPainter p(this); |
ce6e73a8 | 94 | p.setRenderHint(QPainter::Antialiasing); |
ccdd3ef5 | 95 | |
361c560e JH |
96 | const double tick_period = view_.tick_period(); |
97 | const unsigned int prefix = view_.tick_prefix(); | |
1b5813fe | 98 | |
ccdd3ef5 | 99 | // Draw the tick marks |
d2caed8d | 100 | p.setPen(palette().color(foregroundRole())); |
ccdd3ef5 JH |
101 | |
102 | const double minor_tick_period = tick_period / MinorTickSubdivision; | |
103 | const double first_major_division = | |
8dbbc7f0 | 104 | floor(view_.offset() / tick_period); |
ccdd3ef5 | 105 | const double first_minor_division = |
8dbbc7f0 | 106 | ceil(view_.offset() / minor_tick_period); |
ccdd3ef5 JH |
107 | const double t0 = first_major_division * tick_period; |
108 | ||
109 | int division = (int)round(first_minor_division - | |
64b678ba | 110 | first_major_division * MinorTickSubdivision) - 1; |
f260e3bf | 111 | |
e456e8e1 | 112 | const int text_height = calculate_text_height(); |
249229ec | 113 | const int ruler_height = RulerHeight * text_height; |
e456e8e1 | 114 | const int major_tick_y1 = text_height + ValueMargin * 2; |
249229ec | 115 | const int minor_tick_y1 = (major_tick_y1 + ruler_height) / 2; |
f260e3bf | 116 | |
64b678ba | 117 | double x; |
ccdd3ef5 | 118 | |
64b678ba JH |
119 | do { |
120 | const double t = t0 + division * minor_tick_period; | |
8dbbc7f0 | 121 | x = (t - view_.offset()) / view_.scale(); |
ccdd3ef5 | 122 | |
333d5bbc | 123 | if (division % MinorTickSubdivision == 0) |
ccdd3ef5 JH |
124 | { |
125 | // Draw a major tick | |
e456e8e1 | 126 | p.drawText(x, ValueMargin, 0, text_height, |
f260e3bf | 127 | AlignCenter | AlignTop | TextDontClip, |
f0c9f81c | 128 | pv::util::format_time(t, prefix)); |
f260e3bf | 129 | p.drawLine(QPointF(x, major_tick_y1), |
249229ec | 130 | QPointF(x, ruler_height)); |
ccdd3ef5 JH |
131 | } |
132 | else | |
133 | { | |
134 | // Draw a minor tick | |
f260e3bf | 135 | p.drawLine(QPointF(x, minor_tick_y1), |
249229ec | 136 | QPointF(x, ruler_height)); |
ccdd3ef5 JH |
137 | } |
138 | ||
139 | division++; | |
64b678ba JH |
140 | |
141 | } while (x < width()); | |
ccdd3ef5 | 142 | |
b3a7c013 | 143 | // Draw the hover mark |
249229ec | 144 | draw_hover_mark(p, text_height); |
819e2e95 JH |
145 | |
146 | // The cursor labels are not drawn with the arrows exactly on the | |
147 | // bottom line of the widget, because then the selection shadow | |
148 | // would be clipped away. | |
09d5df11 | 149 | const QRect r = rect().adjusted(0, 0, 0, -ViewItem::HighlightRadius); |
819e2e95 JH |
150 | |
151 | // Draw the items | |
152 | const vector< shared_ptr<TimeItem> > items(view_.time_items()); | |
49028d6c JH |
153 | for (auto &i : items) { |
154 | const bool highlight = !dragging_ && | |
155 | i->label_rect(r).contains(mouse_point_); | |
156 | i->paint_label(p, r, highlight); | |
157 | } | |
819e2e95 JH |
158 | } |
159 | ||
160 | void Ruler::mouseMoveEvent(QMouseEvent *e) | |
161 | { | |
162 | mouse_point_ = e->pos(); | |
163 | ||
164 | if (!(e->buttons() & Qt::LeftButton)) | |
165 | return; | |
166 | ||
167 | if ((e->pos() - mouse_down_point_).manhattanLength() < | |
168 | QApplication::startDragDistance()) | |
169 | return; | |
170 | ||
171 | // Do the drag | |
172 | dragging_ = true; | |
173 | ||
23e75650 | 174 | const QPoint delta = e->pos() - mouse_down_point_; |
819e2e95 JH |
175 | const vector< shared_ptr<TimeItem> > items(view_.time_items()); |
176 | for (auto &i : items) | |
177 | if (i->dragging()) | |
23e75650 | 178 | i->drag_by(delta); |
819e2e95 JH |
179 | } |
180 | ||
181 | void Ruler::mousePressEvent(QMouseEvent *e) | |
182 | { | |
183 | if (e->buttons() & Qt::LeftButton) { | |
184 | mouse_down_point_ = e->pos(); | |
25a0d47c | 185 | mouse_down_item_ = get_mouse_over_item(e->pos()); |
819e2e95 JH |
186 | |
187 | clear_selection(); | |
188 | ||
819e2e95 JH |
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(), | |
09d5df11 | 206 | height() - ViewItem::HighlightRadius); |
819e2e95 JH |
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 | ||
115a522a JH |
231 | void Ruler::contextMenuEvent(QContextMenuEvent *event) |
232 | { | |
233 | const shared_ptr<TimeItem> r = get_mouse_over_item(mouse_point_); | |
234 | if (!r) | |
235 | return; | |
236 | ||
237 | QMenu *menu = r->create_context_menu(this); | |
238 | if (menu) | |
239 | menu->exec(event->globalPos()); | |
240 | } | |
241 | ||
819e2e95 JH |
242 | void Ruler::keyPressEvent(QKeyEvent *e) |
243 | { | |
244 | assert(e); | |
245 | ||
246 | if (e->key() == Qt::Key_Delete) | |
247 | { | |
248 | const vector< shared_ptr<TimeItem> > items(view_.time_items()); | |
249 | for (auto &i : items) | |
250 | if (i->selected()) | |
251 | i->delete_pressed(); | |
252 | } | |
ca4ec3ea JH |
253 | } |
254 | ||
249229ec | 255 | void Ruler::draw_hover_mark(QPainter &p, int text_height) |
b3a7c013 | 256 | { |
8dbbc7f0 | 257 | const int x = view_.hover_point().x(); |
333d5bbc | 258 | |
84a0d458 | 259 | if (x == -1) |
b3a7c013 JH |
260 | return; |
261 | ||
262 | p.setPen(QPen(Qt::NoPen)); | |
ad1d8e2b | 263 | p.setBrush(QBrush(palette().color(foregroundRole()))); |
b3a7c013 | 264 | |
249229ec | 265 | const int b = RulerHeight * text_height; |
415341a1 | 266 | const float hover_arrow_size = HoverArrowSize * text_height; |
b3a7c013 JH |
267 | const QPointF points[] = { |
268 | QPointF(x, b), | |
415341a1 JH |
269 | QPointF(x - hover_arrow_size, b - hover_arrow_size), |
270 | QPointF(x + hover_arrow_size, b - hover_arrow_size) | |
b3a7c013 JH |
271 | }; |
272 | p.drawPolygon(points, countof(points)); | |
273 | } | |
274 | ||
e456e8e1 | 275 | int Ruler::calculate_text_height() const |
819e2e95 | 276 | { |
c0096500 | 277 | return QFontMetrics(font()).ascent(); |
819e2e95 JH |
278 | } |
279 | ||
b3a7c013 JH |
280 | void Ruler::hover_point_changed() |
281 | { | |
282 | update(); | |
283 | } | |
284 | ||
ccdd3ef5 JH |
285 | } // namespace view |
286 | } // namespace pv |