#include <QApplication>
#include <QFontMetrics>
+#include <QMenu>
#include <QMouseEvent>
#include "ruler.hpp"
return pv::util::format_time_minutes(t, precision, sign);
}
+pv::util::Timestamp Ruler::get_time_from_x_pos(uint32_t x)
+{
+ return view_.ruler_offset() + ((double)x + 0.5) * view_.scale();
+}
+
+void Ruler::contextMenuEvent(QContextMenuEvent *event)
+{
+ context_menu_x_pos_ = event->pos().x();
+
+ QMenu *const menu = new QMenu(this);
+
+ QAction *const create_marker = new QAction(tr("Create marker here"), this);
+ connect(create_marker, SIGNAL(triggered()), this, SLOT(on_createMarker()));
+ menu->addAction(create_marker);
+
+ menu->popup(event->globalPos());
+}
+
+void Ruler::resizeEvent(QResizeEvent*)
+{
+ // the tick calculation depends on the width of this widget
+ invalidate_tick_position_cache();
+}
+
vector< shared_ptr<ViewItem> > Ruler::items()
{
const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
return nullptr;
}
+void Ruler::mouseDoubleClickEvent(QMouseEvent *event)
+{
+ view_.add_flag(get_time_from_x_pos(event->x()));
+}
+
void Ruler::paintEvent(QPaintEvent*)
{
if (!tick_position_cache_) {
}
}
-Ruler::TickPositions Ruler::calculate_tick_positions(
+void Ruler::draw_hover_mark(QPainter &p, int text_height)
+{
+ const int x = view_.hover_point().x();
+
+ if (x == -1)
+ return;
+
+ p.setPen(QPen(Qt::NoPen));
+ p.setBrush(QBrush(palette().color(foregroundRole())));
+
+ const int b = RulerHeight * text_height;
+ const float hover_arrow_size = HoverArrowSize * text_height;
+ const QPointF points[] = {
+ QPointF(x, b),
+ 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();
+}
+
+TickPositions Ruler::calculate_tick_positions(
const pv::util::Timestamp& major_period,
const pv::util::Timestamp& offset,
const double scale,
return tp;
}
-void Ruler::mouseDoubleClickEvent(QMouseEvent *event)
-{
- view_.add_flag(view_.ruler_offset() + ((double)event->x() + 0.5) * view_.scale());
-}
-
-void Ruler::draw_hover_mark(QPainter &p, int text_height)
-{
- const int x = view_.hover_point().x();
-
- if (x == -1)
- return;
-
- p.setPen(QPen(Qt::NoPen));
- p.setBrush(QBrush(palette().color(foregroundRole())));
-
- const int b = RulerHeight * text_height;
- const float hover_arrow_size = HoverArrowSize * text_height;
- const QPointF points[] = {
- QPointF(x, b),
- 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(const QPoint &hp)
{
(void)hp;
tick_position_cache_ = boost::none;
}
-void Ruler::resizeEvent(QResizeEvent*)
+void Ruler::on_createMarker()
{
- // the tick calculation depends on the width of this widget
- invalidate_tick_position_cache();
+ view_.add_flag(get_time_from_x_pos(context_menu_x_pos_));
}
} // namespace trace
class TimeItem;
class ViewItem;
+struct TickPositions
+{
+ vector<pair<double, QString>> major;
+ vector<double> minor;
+};
+
/**
* The Ruler class manages and displays the time scale above the trace canvas.
* It may also contain @ref TimeItem instances used to identify or highlight
public:
Ruler(View &parent);
-public:
QSize sizeHint() const override;
/**
unsigned precision = 0,
bool sign = true);
+ pv::util::Timestamp get_time_from_x_pos(uint32_t x);
+
+protected:
+ virtual void contextMenuEvent(QContextMenuEvent *event) override;
+ void resizeEvent(QResizeEvent*) override;
+
private:
/**
* Gets the time items.
*/
shared_ptr<ViewItem> get_mouse_over_item(const QPoint &pt) override;
- void paintEvent(QPaintEvent *event) override;
-
void mouseDoubleClickEvent(QMouseEvent *event) override;
+ void paintEvent(QPaintEvent *event) override;
+
/**
* Draw a hover arrow under the cursor position.
* @param p The painter to draw into.
int calculate_text_height() const;
- struct TickPositions
- {
- vector<pair<double, QString>> major;
- vector<double> minor;
- };
-
- /**
- * Holds the tick positions so that they don't have to be recalculated on
- * every redraw. Set by 'paintEvent()' when needed.
- */
- boost::optional<TickPositions> tick_position_cache_;
-
/**
* Calculates the major and minor tick positions.
*
const unsigned int minor_tick_count,
function<QString(const pv::util::Timestamp&)> format_function);
-protected:
- void resizeEvent(QResizeEvent*) override;
-
private Q_SLOTS:
void hover_point_changed(const QPoint &hp);
- // Resets the 'tick_position_cache_'.
void invalidate_tick_position_cache();
+
+ void on_createMarker();
+
+private:
+ /**
+ * Holds the tick positions so that they don't have to be recalculated on
+ * every redraw. Set by 'paintEvent()' when needed.
+ */
+ boost::optional<TickPositions> tick_position_cache_;
+
+ uint32_t context_menu_x_pos_;
};
} // namespace trace
const int width(500);
const unsigned int minor_tick_count(4);
- const Ruler::TickPositions ts = Ruler::calculate_tick_positions(
+ const TickPositions ts = Ruler::calculate_tick_positions(
major_period, offset, scale, width, minor_tick_count, format);
BOOST_REQUIRE_EQUAL(ts.major.size(), 6);
const int width(500);
const unsigned int minor_tick_count(4);
- const Ruler::TickPositions ts = Ruler::calculate_tick_positions(
+ const TickPositions ts = Ruler::calculate_tick_positions(
major_period, offset, scale, width, minor_tick_count, format);
BOOST_REQUIRE_EQUAL(ts.major.size(), 5);
const int width(580);
const unsigned int minor_tick_count(4);
- const Ruler::TickPositions ts = Ruler::calculate_tick_positions(
+ const TickPositions ts = Ruler::calculate_tick_positions(
major_period, offset, scale, width, minor_tick_count, format);
const double mp = 5;