]> sigrok.org Git - pulseview.git/blobdiff - pv/view/ruler.cpp
Change namespace for the trace view and implement ViewBase
[pulseview.git] / pv / view / ruler.cpp
index 934b0a67edc0e5f6a974cd6ad81e64d141d1d910..a8d25fc9df6a2ee4dfa5c3cfcfe9b9035e5c9b3a 100644 (file)
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
-#include "ruler.h"
-
-#include "cursor.h"
-#include "view.h"
-#include "viewport.h"
-#include "pv/util.h"
-
 #include <extdef.h>
 
 #include <QApplication>
+#include <QFontMetrics>
 #include <QMouseEvent>
-#include <QPainter>
-#include <QTextStream>
 
-#include <pv/widgets/popup.h>
+#include "ruler.hpp"
+#include "view.hpp"
 
 using namespace Qt;
+
 using std::shared_ptr;
+using std::vector;
 
 namespace pv {
-namespace view {
+namespace views {
+namespace TraceView {
 
-const int Ruler::RulerHeight = 30;
+const float Ruler::RulerHeight = 2.5f; // x Text Height
 const int Ruler::MinorTickSubdivision = 4;
-const int Ruler::ScaleUnits[3] = {1, 2, 5};
 
-const int Ruler::HoverArrowSize = 5;
+const float Ruler::HoverArrowSize = 0.5f; // x Text Height
 
 Ruler::Ruler(View &parent) :
-       MarginWidget(parent),
-       _dragging(false)
+       MarginWidget(parent)
 {
        setMouseTracking(true);
 
-       connect(&_view, SIGNAL(hover_point_changed()),
+       connect(&view_, SIGNAL(hover_point_changed()),
                this, SLOT(hover_point_changed()));
+       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()));
 }
 
-void Ruler::clear_selection()
+QSize Ruler::sizeHint() const
 {
-       CursorPair &cursors = _view.cursors();
-       cursors.first()->select(false);
-       cursors.second()->select(false);
-       update();
+       const int text_height = calculate_text_height();
+       return QSize(0, RulerHeight * text_height);
 }
 
-
-QSize Ruler::sizeHint() const
+QSize Ruler::extended_size_hint() const
 {
-       return QSize(0, RulerHeight);
+       QRectF max_rect;
+       std::vector< std::shared_ptr<TimeItem> > items(view_.time_items());
+       for (auto &i : items)
+               max_rect = max_rect.united(i->label_rect(QRect()));
+       return QSize(0, sizeHint().height() - max_rect.top() / 2 +
+               ViewItem::HighlightRadius);
 }
 
-void Ruler::paintEvent(QPaintEvent*)
+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);
+}
 
-       const double SpacingIncrement = 32.0f;
-       const double MinValueSpacing = 32.0f;
-       const int ValueMargin = 3;
-
-       QPainter p(this);
-       p.setRenderHint(QPainter::Antialiasing);
-
-       double min_width = SpacingIncrement, typical_width;
-       double tick_period;
-       unsigned int prefix;
-
-       // Find tick spacing, and number formatting that does not cause
-       // value to collide.
-       do
-       {
-               const double min_period = _view.scale() * min_width;
-
-               const int order = (int)floorf(log10f(min_period));
-               const double order_decimal = pow(10.0, order);
-
-               unsigned int unit = 0;
-
-               do
-               {
-                       tick_period = order_decimal * ScaleUnits[unit++];
-               } while (tick_period < min_period && unit < countof(ScaleUnits));
+vector< shared_ptr<ViewItem> > Ruler::items()
+{
+       const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
+       return vector< shared_ptr<ViewItem> >(
+               time_items.begin(), time_items.end());
+}
 
-               prefix = (order - pv::util::FirstSIPrefixPower) / 3;
+shared_ptr<ViewItem> Ruler::get_mouse_over_item(const QPoint &pt)
+{
+       const vector< shared_ptr<TimeItem> > items(view_.time_items());
+       for (auto i = items.rbegin(); i != items.rend(); i++)
+               if ((*i)->enabled() && (*i)->label_rect(rect()).contains(pt))
+                       return *i;
+       return nullptr;
+}
 
-               typical_width = p.boundingRect(0, 0, INT_MAX, INT_MAX,
-                       AlignLeft | AlignTop, pv::util::format_time(_view.offset(),
-                       prefix)).width() + MinValueSpacing;
+void Ruler::paintEvent(QPaintEvent*)
+{
+       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);
+       }
 
-               min_width += SpacingIncrement;
+       const int ValueMargin = 3;
 
-       } while(typical_width > tick_period / _view.scale());
+       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;
 
-       const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
-               AlignLeft | AlignTop, "8").height();
+       QPainter p(this);
 
        // 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;
-
-       int division = (int)round(first_minor_division -
-               first_major_division * MinorTickSubdivision) - 1;
-
-       const int major_tick_y1 = text_height + ValueMargin * 2;
-       const int tick_y2 = height();
-       const int minor_tick_y1 = (major_tick_y1 + tick_y2) / 2;
-
-       double x;
-
-       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, tick_y2));
-               }
-               else
-               {
-                       // Draw a minor tick
-                       p.drawLine(QPointF(x, minor_tick_y1),
-                               QPointF(x, tick_y2));
-               }
-
-               division++;
-
-       } while (x < width());
+       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));
+       }
 
-       // Draw the cursors
-       if (_view.cursors_shown())
-               _view.cursors().draw_markers(p, rect(), prefix);
+       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);
+       draw_hover_mark(p, text_height);
+
+       p.setRenderHint(QPainter::Antialiasing);
 
-       p.end();
+       // 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.
+       const QRect r = rect().adjusted(0, 0, 0, -ViewItem::HighlightRadius);
+
+       // Draw the items
+       const vector< shared_ptr<TimeItem> > items(view_.time_items());
+       for (auto &i : items) {
+               const bool highlight = !item_dragging_ &&
+                       i->label_rect(r).contains(mouse_point_);
+               i->paint_label(p, r, highlight);
+       }
 }
 
-void Ruler::mouseMoveEvent(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<QString(const pv::util::Timestamp&)> format_function)
 {
-       if (!(e->buttons() & Qt::LeftButton))
-               return;
-       
-       if ((e->pos() - _mouse_down_point).manhattanLength() <
-               QApplication::startDragDistance())
-               return;
+       TickPositions tp;
 
-       _dragging = true;
+       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 (shared_ptr<TimeMarker> m = _grabbed_marker.lock())
-               m->set_time(_view.offset() +
-                       ((double)e->x() + 0.5) * _view.scale());
-}
+       int division = (round(first_minor_division -
+               first_major_division * MinorTickSubdivision)).convert_to<int>() - 1;
 
-void Ruler::mousePressEvent(QMouseEvent *e)
-{
-       if (e->buttons() & Qt::LeftButton)
-       {
-               _mouse_down_point = e->pos();
-
-               _grabbed_marker.reset();
-
-               clear_selection();
-
-               if (_view.cursors_shown()) {
-                       CursorPair &cursors = _view.cursors();
-                       if (cursors.first()->get_label_rect(
-                               rect()).contains(e->pos()))
-                               _grabbed_marker = cursors.first();
-                       else if (cursors.second()->get_label_rect(
-                               rect()).contains(e->pos()))
-                               _grabbed_marker = cursors.second();
+       double x;
+
+       do {
+               pv::util::Timestamp t = t0 + division * minor_period;
+               x = ((t - offset) / scale).convert_to<double>();
+
+               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);
                }
 
-               if (shared_ptr<TimeMarker> m = _grabbed_marker.lock())
-                       m->select();
+               division++;
+       } while (x < width);
 
-               selection_changed();
-       }
+       return tp;
 }
 
-void Ruler::mouseReleaseEvent(QMouseEvent *)
+void Ruler::mouseDoubleClickEvent(QMouseEvent *event)
 {
-       using pv::widgets::Popup;
-
-       if (!_dragging)
-               if (shared_ptr<TimeMarker> m = _grabbed_marker.lock()) {
-                       Popup *const p = m->create_popup(&_view);
-                       p->set_position(mapToGlobal(QPoint(m->get_x(),
-                               height())), Popup::Bottom);
-                       p->show();
-               }
-
-       _dragging = false;
-       _grabbed_marker.reset();
+       view_.add_flag(view_.offset() + ((double)event->x() + 0.5) * view_.scale());
 }
 
-void Ruler::draw_hover_mark(QPainter &p)
+void Ruler::draw_hover_mark(QPainter &p, int text_height)
 {
-       const int x = _view.hover_point().x();
+       const int x = view_.hover_point().x();
 
-       if (x == -1 || _dragging)
+       if (x == -1)
                return;
 
        p.setPen(QPen(Qt::NoPen));
        p.setBrush(QBrush(palette().color(foregroundRole())));
 
-       const int b = height() - 1;
+       const int b = RulerHeight * text_height;
+       const float hover_arrow_size = HoverArrowSize * text_height;
        const QPointF points[] = {
                QPointF(x, b),
-               QPointF(x - HoverArrowSize, b - HoverArrowSize),
-               QPointF(x + HoverArrowSize, b - HoverArrowSize)
+               QPointF(x - hover_arrow_size, b - hover_arrow_size),
+               QPointF(x + hover_arrow_size, b - hover_arrow_size)
        };
        p.drawPolygon(points, countof(points));
 }
 
+int Ruler::calculate_text_height() const
+{
+       return QFontMetrics(font()).ascent();
+}
+
 void Ruler::hover_point_changed()
 {
        update();
 }
 
-} // namespace view
+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 TraceView
+} // namespace views
 } // namespace pv