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