X-Git-Url: https://sigrok.org/gitweb/?p=pulseview.git;a=blobdiff_plain;f=pv%2Fview%2Fruler.cpp;h=f6ec4ac78d7d142a486d2347287bd4693816ca5c;hp=bcf6a5281d494195b97bfc6c152dfc59227453f1;hb=d9ea96280ab1128427143660ae375c30b19aa0cb;hpb=115a522abb9dc4071f62a3df77b9501e1f9ff722 diff --git a/pv/view/ruler.cpp b/pv/view/ruler.cpp index bcf6a528..f6ec4ac7 100644 --- a/pv/view/ruler.cpp +++ b/pv/view/ruler.cpp @@ -22,15 +22,11 @@ #include #include -#include #include #include "ruler.hpp" #include "view.hpp" -#include -#include - using namespace Qt; using std::shared_ptr; @@ -39,10 +35,10 @@ using std::vector; namespace pv { namespace view { -const float Ruler::RulerHeight = 2.5f; // x Text Height +const float Ruler::RulerHeight = 2.5f; // x Text Height const int Ruler::MinorTickSubdivision = 4; -const float Ruler::HoverArrowSize = 0.5f; // x Text Height +const float Ruler::HoverArrowSize = 0.5f; // x Text Height Ruler::Ruler(View &parent) : MarginWidget(parent) @@ -51,14 +47,18 @@ Ruler::Ruler(View &parent) : connect(&view_, SIGNAL(hover_point_changed()), this, SLOT(hover_point_changed())); -} - -void Ruler::clear_selection() -{ - const vector< shared_ptr > items(view_.time_items()); - for (auto &i : items) - i->select(false); - update(); + connect(&view_, SIGNAL(offset_changed()), + this, SLOT(invalidate_tick_position_cache())); + connect(&view_, SIGNAL(scale_changed()), + this, SLOT(invalidate_tick_position_cache())); + connect(&view_, SIGNAL(tick_prefix_changed()), + this, SLOT(invalidate_tick_position_cache())); + connect(&view_, SIGNAL(tick_precision_changed()), + this, SLOT(invalidate_tick_position_cache())); + connect(&view_, SIGNAL(tick_period_changed()), + this, SLOT(invalidate_tick_position_cache())); + connect(&view_, SIGNAL(time_unit_changed()), + this, SLOT(invalidate_tick_position_cache())); } QSize Ruler::sizeHint() const @@ -77,7 +77,46 @@ QSize Ruler::extended_size_hint() const ViewItem::HighlightRadius); } -shared_ptr Ruler::get_mouse_over_item(const QPoint &pt) +QString Ruler::format_time_with_distance( + const pv::util::Timestamp& distance, + const pv::util::Timestamp& t, + pv::util::SIPrefix prefix, + pv::util::TimeUnit unit, + unsigned precision, + bool sign) +{ + const unsigned limit = 60; + + if (t.is_zero()) + return "0"; + + // If we have to use samples then we have no alternative formats + if (unit == pv::util::TimeUnit::Samples) + return pv::util::format_time_si_adjusted(t, prefix, precision, "sa", sign); + + // View zoomed way out -> low precision (0), big distance (>=60s) + // -> DD:HH:MM + if ((precision == 0) && (distance >= limit)) + return pv::util::format_time_minutes(t, 0, sign); + + // View in "normal" range -> medium precision, medium step size + // -> HH:MM:SS.mmm... or xxxx (si unit) if less than limit seconds + // View zoomed way in -> high precision (>3), low step size (<1s) + // -> HH:MM:SS.mmm... or xxxx (si unit) if less than limit seconds + if (abs(t) < limit) + return pv::util::format_time_si_adjusted(t, prefix, precision, "s", sign); + else + return pv::util::format_time_minutes(t, precision, sign); +} + +vector< shared_ptr > Ruler::items() +{ + const vector< shared_ptr > time_items(view_.time_items()); + return vector< shared_ptr >( + time_items.begin(), time_items.end()); +} + +shared_ptr Ruler::get_mouse_over_item(const QPoint &pt) { const vector< shared_ptr > items(view_.time_items()); for (auto i = items.rbegin(); i != items.rend(); i++) @@ -88,61 +127,53 @@ shared_ptr Ruler::get_mouse_over_item(const QPoint &pt) void Ruler::paintEvent(QPaintEvent*) { - const int ValueMargin = 3; - - QPainter p(this); - p.setRenderHint(QPainter::Antialiasing); - - const double tick_period = view_.tick_period(); - const unsigned int prefix = view_.tick_prefix(); - - // Draw the tick marks - p.setPen(palette().color(foregroundRole())); - - const double minor_tick_period = tick_period / MinorTickSubdivision; - const double first_major_division = - floor(view_.offset() / tick_period); - const double first_minor_division = - ceil(view_.offset() / minor_tick_period); - const double t0 = first_major_division * tick_period; + if (!tick_position_cache_) { + auto ffunc = [this](const pv::util::Timestamp& t) { + return format_time_with_distance( + this->view_.tick_period(), + t, + this->view_.tick_prefix(), + this->view_.time_unit(), + this->view_.tick_precision()); + }; + + tick_position_cache_ = calculate_tick_positions( + view_.tick_period(), + view_.offset(), + view_.scale(), + width(), + ffunc); + } - int division = (int)round(first_minor_division - - first_major_division * MinorTickSubdivision) - 1; + const int ValueMargin = 3; const int text_height = calculate_text_height(); const int ruler_height = RulerHeight * text_height; const int major_tick_y1 = text_height + ValueMargin * 2; const int minor_tick_y1 = (major_tick_y1 + ruler_height) / 2; - double x; + QPainter p(this); - do { - const double t = t0 + division * minor_tick_period; - x = (t - view_.offset()) / view_.scale(); - - if (division % MinorTickSubdivision == 0) - { - // Draw a major tick - p.drawText(x, ValueMargin, 0, text_height, - AlignCenter | AlignTop | TextDontClip, - pv::util::format_time(t, prefix)); - p.drawLine(QPointF(x, major_tick_y1), - QPointF(x, ruler_height)); - } - else - { - // Draw a minor tick - p.drawLine(QPointF(x, minor_tick_y1), - QPointF(x, ruler_height)); - } + // Draw the tick marks + p.setPen(palette().color(foregroundRole())); - division++; + for (const auto& tick: tick_position_cache_->major) { + p.drawText(tick.first, ValueMargin, 0, text_height, + AlignCenter | AlignTop | TextDontClip, tick.second); + p.drawLine(QPointF(tick.first, major_tick_y1), + QPointF(tick.first, ruler_height)); + } - } while (x < width()); + for (const auto& tick: tick_position_cache_->minor) { + p.drawLine(QPointF(tick, minor_tick_y1), + QPointF(tick, ruler_height)); + } // Draw the hover mark draw_hover_mark(p, text_height); + p.setRenderHint(QPainter::Antialiasing); + // The cursor labels are not drawn with the arrows exactly on the // bottom line of the widget, because then the selection shadow // would be clipped away. @@ -151,107 +182,52 @@ void Ruler::paintEvent(QPaintEvent*) // Draw the items const vector< shared_ptr > items(view_.time_items()); for (auto &i : items) { - const bool highlight = !dragging_ && + const bool highlight = !item_dragging_ && i->label_rect(r).contains(mouse_point_); i->paint_label(p, r, highlight); } } -void Ruler::mouseMoveEvent(QMouseEvent *e) -{ - mouse_point_ = e->pos(); - - if (!(e->buttons() & Qt::LeftButton)) - return; - - if ((e->pos() - mouse_down_point_).manhattanLength() < - QApplication::startDragDistance()) - return; - - // Do the drag - dragging_ = true; - - const int delta = e->pos().x() - mouse_down_point_.x(); - const vector< shared_ptr > items(view_.time_items()); - for (auto &i : items) - if (i->dragging()) - i->set_time(view_.offset() + - (i->drag_point().x() + delta - 0.5) * - view_.scale()); -} - -void Ruler::mousePressEvent(QMouseEvent *e) +Ruler::TickPositions Ruler::calculate_tick_positions( + const pv::util::Timestamp& major_period, + const pv::util::Timestamp& offset, + const double scale, + const int width, + std::function format_function) { - if (e->buttons() & Qt::LeftButton) { - mouse_down_point_ = e->pos(); - mouse_down_item_ = get_mouse_over_item(e->pos()); + TickPositions tp; - clear_selection(); + const pv::util::Timestamp minor_period = major_period / MinorTickSubdivision; + const pv::util::Timestamp first_major_division = floor(offset / major_period); + const pv::util::Timestamp first_minor_division = ceil(offset / minor_period); + const pv::util::Timestamp t0 = first_major_division * major_period; - if (mouse_down_item_) { - mouse_down_item_->select(); - mouse_down_item_->drag(); - } + int division = (round(first_minor_division - + first_major_division * MinorTickSubdivision)).convert_to() - 1; - selection_changed(); - } -} + double x; -void Ruler::mouseReleaseEvent(QMouseEvent *) -{ - using pv::widgets::Popup; - - if (!dragging_ && mouse_down_item_) { - Popup *const p = mouse_down_item_->create_popup(&view_); - if (p) { - const QPoint arrpos(mouse_down_item_->get_x(), - height() - ViewItem::HighlightRadius); - p->set_position(mapToGlobal(arrpos), Popup::Bottom); - p->show(); + do { + pv::util::Timestamp t = t0 + division * minor_period; + x = ((t - offset) / scale).convert_to(); + + if (division % MinorTickSubdivision == 0) { + // Recalculate 't' without using 'minor_period' which is a fraction + t = t0 + division / MinorTickSubdivision * major_period; + tp.major.emplace_back(x, format_function(t)); + } else { + tp.minor.emplace_back(x); } - } - - dragging_ = false; - mouse_down_item_.reset(); - - const vector< shared_ptr > items(view_.time_items()); - for (auto &i : items) - i->drag_release(); -} - -void Ruler::leaveEvent(QEvent*) -{ - mouse_point_ = QPoint(-1, -1); - update(); -} -void Ruler::mouseDoubleClickEvent(QMouseEvent *e) -{ - view_.add_flag(view_.offset() + ((double)e->x() + 0.5) * view_.scale()); -} - -void Ruler::contextMenuEvent(QContextMenuEvent *event) -{ - const shared_ptr r = get_mouse_over_item(mouse_point_); - if (!r) - return; + division++; + } while (x < width); - QMenu *menu = r->create_context_menu(this); - if (menu) - menu->exec(event->globalPos()); + return tp; } -void Ruler::keyPressEvent(QKeyEvent *e) +void Ruler::mouseDoubleClickEvent(QMouseEvent *event) { - assert(e); - - if (e->key() == Qt::Key_Delete) - { - const vector< shared_ptr > items(view_.time_items()); - for (auto &i : items) - if (i->selected()) - i->delete_pressed(); - } + view_.add_flag(view_.offset() + ((double)event->x() + 0.5) * view_.scale()); } void Ruler::draw_hover_mark(QPainter &p, int text_height) @@ -284,5 +260,16 @@ void Ruler::hover_point_changed() update(); } +void Ruler::invalidate_tick_position_cache() +{ + tick_position_cache_ = boost::none; +} + +void Ruler::resizeEvent(QResizeEvent*) +{ + // the tick calculation depends on the width of this widget + invalidate_tick_position_cache(); +} + } // namespace view } // namespace pv