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