]>
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 | 30 | #include <pv/util.hpp> |
ccdd3ef5 | 31 | |
819f4c25 | 32 | using namespace Qt; |
f76af637 | 33 | |
819e2e95 JH |
34 | using std::shared_ptr; |
35 | using std::vector; | |
36 | ||
ccdd3ef5 JH |
37 | namespace pv { |
38 | namespace view { | |
39 | ||
249229ec | 40 | const float Ruler::RulerHeight = 2.5f; // x Text Height |
ccdd3ef5 | 41 | const int Ruler::MinorTickSubdivision = 4; |
ccdd3ef5 | 42 | |
415341a1 | 43 | const float Ruler::HoverArrowSize = 0.5f; // x Text Height |
b3a7c013 | 44 | |
ccdd3ef5 | 45 | Ruler::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 | ||
819e2e95 JH |
54 | void 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 | ||
a6c1726e JH |
62 | QSize Ruler::sizeHint() const |
63 | { | |
249229ec JH |
64 | const int text_height = calculate_text_height(); |
65 | return QSize(0, RulerHeight * text_height); | |
a6c1726e JH |
66 | } |
67 | ||
819e2e95 JH |
68 | QSize Ruler::extended_size_hint() const |
69 | { | |
f5b833c6 JH |
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); | |
819e2e95 JH |
76 | } |
77 | ||
3e124bee JH |
78 | vector< 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 | ||
6871ee9f | 85 | shared_ptr<ViewItem> Ruler::get_mouse_over_item(const QPoint &pt) |
25a0d47c JH |
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 | ||
e314eca4 | 94 | void Ruler::paintEvent(QPaintEvent*) |
ccdd3ef5 | 95 | { |
f260e3bf | 96 | const int ValueMargin = 3; |
3f509c1f | 97 | |
ccdd3ef5 | 98 | QPainter p(this); |
ce6e73a8 | 99 | p.setRenderHint(QPainter::Antialiasing); |
ccdd3ef5 | 100 | |
361c560e JH |
101 | const double tick_period = view_.tick_period(); |
102 | const unsigned int prefix = view_.tick_prefix(); | |
1b5813fe | 103 | |
ccdd3ef5 | 104 | // Draw the tick marks |
d2caed8d | 105 | p.setPen(palette().color(foregroundRole())); |
ccdd3ef5 JH |
106 | |
107 | const double minor_tick_period = tick_period / MinorTickSubdivision; | |
108 | const double first_major_division = | |
8dbbc7f0 | 109 | floor(view_.offset() / tick_period); |
ccdd3ef5 | 110 | const double first_minor_division = |
8dbbc7f0 | 111 | ceil(view_.offset() / minor_tick_period); |
ccdd3ef5 JH |
112 | const double t0 = first_major_division * tick_period; |
113 | ||
114 | int division = (int)round(first_minor_division - | |
64b678ba | 115 | first_major_division * MinorTickSubdivision) - 1; |
f260e3bf | 116 | |
e456e8e1 | 117 | const int text_height = calculate_text_height(); |
249229ec | 118 | const int ruler_height = RulerHeight * text_height; |
e456e8e1 | 119 | const int major_tick_y1 = text_height + ValueMargin * 2; |
249229ec | 120 | const int minor_tick_y1 = (major_tick_y1 + ruler_height) / 2; |
f260e3bf | 121 | |
64b678ba | 122 | double x; |
ccdd3ef5 | 123 | |
64b678ba JH |
124 | do { |
125 | const double t = t0 + division * minor_tick_period; | |
8dbbc7f0 | 126 | x = (t - view_.offset()) / view_.scale(); |
ccdd3ef5 | 127 | |
333d5bbc | 128 | if (division % MinorTickSubdivision == 0) |
ccdd3ef5 JH |
129 | { |
130 | // Draw a major tick | |
e456e8e1 | 131 | p.drawText(x, ValueMargin, 0, text_height, |
f260e3bf | 132 | AlignCenter | AlignTop | TextDontClip, |
f0c9f81c | 133 | pv::util::format_time(t, prefix)); |
f260e3bf | 134 | p.drawLine(QPointF(x, major_tick_y1), |
249229ec | 135 | QPointF(x, ruler_height)); |
ccdd3ef5 JH |
136 | } |
137 | else | |
138 | { | |
139 | // Draw a minor tick | |
f260e3bf | 140 | p.drawLine(QPointF(x, minor_tick_y1), |
249229ec | 141 | QPointF(x, ruler_height)); |
ccdd3ef5 JH |
142 | } |
143 | ||
144 | division++; | |
64b678ba JH |
145 | |
146 | } while (x < width()); | |
ccdd3ef5 | 147 | |
b3a7c013 | 148 | // Draw the hover mark |
249229ec | 149 | draw_hover_mark(p, text_height); |
819e2e95 JH |
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. | |
09d5df11 | 154 | const QRect r = rect().adjusted(0, 0, 0, -ViewItem::HighlightRadius); |
819e2e95 JH |
155 | |
156 | // Draw the items | |
157 | const vector< shared_ptr<TimeItem> > items(view_.time_items()); | |
49028d6c JH |
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 | } | |
819e2e95 JH |
163 | } |
164 | ||
165 | void 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 | ||
23e75650 | 179 | const QPoint delta = e->pos() - mouse_down_point_; |
819e2e95 JH |
180 | const vector< shared_ptr<TimeItem> > items(view_.time_items()); |
181 | for (auto &i : items) | |
182 | if (i->dragging()) | |
23e75650 | 183 | i->drag_by(delta); |
819e2e95 JH |
184 | } |
185 | ||
186 | void Ruler::mousePressEvent(QMouseEvent *e) | |
187 | { | |
188 | if (e->buttons() & Qt::LeftButton) { | |
189 | mouse_down_point_ = e->pos(); | |
25a0d47c | 190 | mouse_down_item_ = get_mouse_over_item(e->pos()); |
819e2e95 JH |
191 | |
192 | clear_selection(); | |
193 | ||
819e2e95 JH |
194 | if (mouse_down_item_) { |
195 | mouse_down_item_->select(); | |
196 | mouse_down_item_->drag(); | |
197 | } | |
198 | ||
199 | selection_changed(); | |
200 | } | |
201 | } | |
202 | ||
203 | void Ruler::mouseReleaseEvent(QMouseEvent *) | |
204 | { | |
205 | using pv::widgets::Popup; | |
206 | ||
786b7678 JH |
207 | if (!dragging_ && mouse_down_item_) |
208 | show_popup(mouse_down_item_); | |
819e2e95 JH |
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 | ||
819e2e95 JH |
218 | void Ruler::mouseDoubleClickEvent(QMouseEvent *e) |
219 | { | |
220 | view_.add_flag(view_.offset() + ((double)e->x() + 0.5) * view_.scale()); | |
221 | } | |
222 | ||
223 | void 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 | } | |
ca4ec3ea JH |
234 | } |
235 | ||
249229ec | 236 | void Ruler::draw_hover_mark(QPainter &p, int text_height) |
b3a7c013 | 237 | { |
8dbbc7f0 | 238 | const int x = view_.hover_point().x(); |
333d5bbc | 239 | |
84a0d458 | 240 | if (x == -1) |
b3a7c013 JH |
241 | return; |
242 | ||
243 | p.setPen(QPen(Qt::NoPen)); | |
ad1d8e2b | 244 | p.setBrush(QBrush(palette().color(foregroundRole()))); |
b3a7c013 | 245 | |
249229ec | 246 | const int b = RulerHeight * text_height; |
415341a1 | 247 | const float hover_arrow_size = HoverArrowSize * text_height; |
b3a7c013 JH |
248 | const QPointF points[] = { |
249 | QPointF(x, b), | |
415341a1 JH |
250 | QPointF(x - hover_arrow_size, b - hover_arrow_size), |
251 | QPointF(x + hover_arrow_size, b - hover_arrow_size) | |
b3a7c013 JH |
252 | }; |
253 | p.drawPolygon(points, countof(points)); | |
254 | } | |
255 | ||
e456e8e1 | 256 | int Ruler::calculate_text_height() const |
819e2e95 | 257 | { |
c0096500 | 258 | return QFontMetrics(font()).ascent(); |
819e2e95 JH |
259 | } |
260 | ||
b3a7c013 JH |
261 | void Ruler::hover_point_changed() |
262 | { | |
263 | update(); | |
264 | } | |
265 | ||
ccdd3ef5 JH |
266 | } // namespace view |
267 | } // namespace pv |