Use a type with a greater resolution to represent time values
[pulseview.git] / pv / view / timemarker.cpp
index 47ba34559a1dd15fa45fc2ff4d979d6a23f17dab..f1424627d9aa27dae374175a5d11b4bf000e5f23 100644 (file)
@@ -41,28 +41,29 @@ namespace view {
 
 const int TimeMarker::ArrowSize = 4;
 
-TimeMarker::TimeMarker(View &view, const QColor &colour, double time) :
+TimeMarker::TimeMarker(
+       View &view, const QColor &colour, const pv::util::Timestamp& time) :
        TimeItem(view),
        colour_(colour),
        time_(time),
-       value_action_(NULL),
-       value_widget_(NULL),
+       value_action_(nullptr),
+       value_widget_(nullptr),
        updating_value_widget_(false)
 {
 }
 
-double TimeMarker::time() const
+const pv::util::Timestamp& TimeMarker::time() const
 {
        return time_;
 }
 
-void TimeMarker::set_time(double time)
+void TimeMarker::set_time(const pv::util::Timestamp& time)
 {
        time_ = time;
 
        if (value_widget_) {
                updating_value_widget_ = true;
-               value_widget_->setValue(time);
+               value_widget_->setValue(time.convert_to<double>());
                updating_value_widget_ = false;
        }
 
@@ -71,32 +72,33 @@ void TimeMarker::set_time(double time)
 
 float TimeMarker::get_x() const
 {
-       return (time_ - view_.offset()) / view_.scale();
+       return ((time_ - view_.offset()) / view_.scale()).convert_to<float>();
 }
 
-QPoint TimeMarker::point() const
+QPoint TimeMarker::point(const QRect &rect) const
 {
-       return QPoint(get_x(), 0);
+       return QPoint(get_x(), rect.bottom());
 }
 
 QRectF TimeMarker::label_rect(const QRectF &rect) const
 {
-       const float x = (time_ - view_.offset()) / view_.scale();
-
        QFontMetrics m(QApplication::font());
-       const float text_width =
-               max(m.boundingRect(get_text()).size().width(), ArrowSize);
-       const float text_height = m.height();
-
-       const QSizeF label_size(
-               text_width + LabelPadding.width() * 2,
-               text_height + LabelPadding.height() * 2);
+       const QSizeF text_size(
+               max(m.boundingRect(get_text()).size().width(), ArrowSize),
+               m.height());
+       const QSizeF label_size(text_size + LabelPadding * 2);
        const float top = rect.height() - label_size.height() -
                TimeMarker::ArrowSize - 0.5f;
-       const float height = label_size.height();
+       const float x = get_x();
+
+       return QRectF(QPointF(x - label_size.width() / 2, top), label_size);
+}
 
-       return QRectF(x - label_size.width() / 2, top,
-               label_size.width(), height);
+QRectF TimeMarker::hit_box_rect(const QRectF &rect) const
+{
+       const float x = get_x();
+       const float h = QFontMetrics(QApplication::font()).height();
+       return QRectF(x - h / 2.0f, rect.top(), h, rect.height());
 }
 
 void TimeMarker::paint_label(QPainter &p, const QRect &rect, bool hover)
@@ -104,7 +106,7 @@ void TimeMarker::paint_label(QPainter &p, const QRect &rect, bool hover)
        if (!enabled())
                return;
 
-       const qreal x = (time_ - view_.offset()) / view_.scale();
+       const qreal x = ((time_ - view_.offset()) / view_.scale()).convert_to<qreal>();
        const QRectF r(label_rect(rect));
 
        const QPointF points[] = {
@@ -165,6 +167,9 @@ pv::widgets::Popup* TimeMarker::create_popup(QWidget *parent)
        using pv::widgets::Popup;
 
        Popup *const popup = new Popup(parent);
+       popup->set_position(parent->mapToGlobal(
+               point(parent->rect())), Popup::Bottom);
+
        QFormLayout *const form = new QFormLayout(popup);
        popup->setLayout(form);
 
@@ -173,7 +178,7 @@ pv::widgets::Popup* TimeMarker::create_popup(QWidget *parent)
        value_widget_->setSuffix("s");
        value_widget_->setSingleStep(1e-6);
        value_widget_->setRange(-1.0e9, 1.0e9);
-       value_widget_->setValue(time_);
+       value_widget_->setValue(time_.convert_to<double>());
 
        connect(value_widget_, SIGNAL(valueChanged(double)),
                this, SLOT(on_value_changed(double)));