Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions pj_proto_app/src/chart_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,12 @@ void ChartPanel::updateData(PJ::Timestamp t_min, PJ::Timestamp t_max) {
s.line->replace(points);
}

// Auto-scale axes
double x_min_s = static_cast<double>(t_min - first_timestamp_) / 1e9;
double x_max_s = static_cast<double>(t_max - first_timestamp_) / 1e9;
x_axis_->setRange(x_min_s, x_max_s);
// Auto-scale x-axis only when the user has not zoomed manually
if (!user_zoom_) {
double x_min_s = static_cast<double>(t_min - first_timestamp_) / 1e9;
double x_max_s = static_cast<double>(t_max - first_timestamp_) / 1e9;
x_axis_->setRange(x_min_s, x_max_s);
}

if (y_min < y_max) {
double margin = (y_max - y_min) * 0.05;
Expand Down Expand Up @@ -150,6 +152,23 @@ void ChartPanel::dropEvent(QDropEvent* event) {
emit seriesDropped();
}

void ChartPanel::wheelEvent(QWheelEvent* event) {
double factor = (event->angleDelta().y() > 0) ? 0.8 : 1.25;
QPointF scene_pos = mapToScene(event->position().toPoint());
double mouse_x_s = chart()->mapToValue(scene_pos).x();
double x_min = x_axis_->min();
double x_max = x_axis_->max();
x_axis_->setRange(mouse_x_s - (mouse_x_s - x_min) * factor, mouse_x_s + (x_max - mouse_x_s) * factor);
user_zoom_ = true;
event->accept();
}

void ChartPanel::mouseDoubleClickEvent(QMouseEvent* event) {
user_zoom_ = false;
emit seriesDropped(); // triggers MainWindow to redraw with the full data range
QChartView::mouseDoubleClickEvent(event);
}

void ChartPanel::contextMenuEvent(QContextMenuEvent* event) {
if (series_.empty()) {
QChartView::contextMenuEvent(event);
Expand Down
5 changes: 5 additions & 0 deletions pj_proto_app/src/chart_panel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMenu>
#include <QMouseEvent>
#include <QWheelEvent>
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
#include <QtCharts/QValueAxis>
Expand Down Expand Up @@ -40,13 +42,16 @@ class ChartPanel : public QChartView {
void dragMoveEvent(QDragMoveEvent* event) override;
void dropEvent(QDropEvent* event) override;
void contextMenuEvent(QContextMenuEvent* event) override;
void wheelEvent(QWheelEvent* event) override;
void mouseDoubleClickEvent(QMouseEvent* event) override;

private:
const PJ::DataEngine& engine_;
QValueAxis* x_axis_;
QValueAxis* y_axis_;
std::vector<PlottedSeries> series_;
PJ::Timestamp first_timestamp_ = 0;
bool user_zoom_ = false;
};

} // namespace proto
Loading