Skip to content

Commit

Permalink
Allow zooming and dragging only in one orientation
Browse files Browse the repository at this point in the history
By selecting the axis or the labels associated to the axis,the user can
restrict the subsequent zoom or drag operation to that orientation.

The possible user interactions are documented in the Whats's This
information.

See issue #1258
  • Loading branch information
mgrojo committed Dec 8, 2017
1 parent 232130b commit 5807c0b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
40 changes: 39 additions & 1 deletion src/PlotDock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ PlotDock::PlotDock(QWidget* parent)
// Connect signals
connect(ui->treePlotColumns, &QTreeWidget::itemChanged, this, &PlotDock::on_treePlotColumns_itemChanged);
connect(ui->plotWidget, SIGNAL(selectionChangedByUser()), this, SLOT(selectionChanged()));

// connect slots that takes care that when an axis is selected, only that direction can be dragged and zoomed:
connect(ui->plotWidget, SIGNAL(mousePress(QMouseEvent*)), this, SLOT(mousePress()));
connect(ui->plotWidget, SIGNAL(mouseWheel(QWheelEvent*)), this, SLOT(mouseWheel()));
}

PlotDock::~PlotDock()
Expand Down Expand Up @@ -203,7 +207,8 @@ void PlotDock::updatePlot(SqliteTableModel* model, BrowseDataTableSettings* sett
graph->setSelectable (QCP::stDataRange);
// Enable: click on items to select them, Ctrl+Click for multi-selection, mouse-wheel for zooming and mouse drag for
// changing the visible range.
ui->plotWidget->setInteractions(QCP::iSelectPlottables | QCP::iMultiSelect | QCP::iRangeZoom | QCP::iRangeDrag);
// Select one axis for zoom and drag applying only to that orientation.
ui->plotWidget->setInteractions(QCP::iSelectPlottables | QCP::iMultiSelect | QCP::iRangeZoom | QCP::iRangeDrag | QCP::iSelectAxes);
ui->plotWidget->setSelectionRectMode(QCP::srmNone);

// prepare the data vectors for qcustomplot
Expand Down Expand Up @@ -572,3 +577,36 @@ void PlotDock::selectionChanged()
}

}
void PlotDock::mousePress()
{
// if an axis (or axis labels) is selected, only allow the direction of that axis to be dragged
// if no axis (or axis labels) is selected, both directions may be dragged

if (ui->plotWidget->xAxis->selectedParts().testFlag(QCPAxis::spAxis) ||
ui->plotWidget->xAxis->selectedParts().testFlag(QCPAxis::spTickLabels) ||
ui->plotWidget->xAxis->selectedParts().testFlag(QCPAxis::spAxisLabel))
ui->plotWidget->axisRect()->setRangeDrag(ui->plotWidget->xAxis->orientation());
else if (ui->plotWidget->yAxis->selectedParts().testFlag(QCPAxis::spAxis) ||
ui->plotWidget->yAxis->selectedParts().testFlag(QCPAxis::spTickLabels) ||
ui->plotWidget->yAxis->selectedParts().testFlag(QCPAxis::spAxisLabel))
ui->plotWidget->axisRect()->setRangeDrag(ui->plotWidget->yAxis->orientation());
else
ui->plotWidget->axisRect()->setRangeDrag(Qt::Horizontal | Qt::Vertical);
}

void PlotDock::mouseWheel()
{
// if an axis (or axis labels) is selected, only allow the direction of that axis to be zoomed
// if no axis (or axis labels) is selected, both directions may be zoomed

if (ui->plotWidget->xAxis->selectedParts().testFlag(QCPAxis::spAxis) ||
ui->plotWidget->xAxis->selectedParts().testFlag(QCPAxis::spTickLabels) ||
ui->plotWidget->xAxis->selectedParts().testFlag(QCPAxis::spAxisLabel))
ui->plotWidget->axisRect()->setRangeZoom(ui->plotWidget->xAxis->orientation());
else if (ui->plotWidget->yAxis->selectedParts().testFlag(QCPAxis::spAxis) ||
ui->plotWidget->yAxis->selectedParts().testFlag(QCPAxis::spTickLabels) ||
ui->plotWidget->yAxis->selectedParts().testFlag(QCPAxis::spAxisLabel))
ui->plotWidget->axisRect()->setRangeZoom(ui->plotWidget->yAxis->orientation());
else
ui->plotWidget->axisRect()->setRangeZoom(Qt::Horizontal | Qt::Vertical);
}
2 changes: 2 additions & 0 deletions src/PlotDock.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ private slots:
void on_comboLineType_currentIndexChanged(int index);
void on_comboPointShape_currentIndexChanged(int index);
void selectionChanged();
void mousePress();
void mouseWheel();
};

#endif
9 changes: 9 additions & 0 deletions src/PlotDock.ui
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@
<verstretch>8</verstretch>
</sizepolicy>
</property>
<property name="whatsThis">
<string>Here is a plot drawn when you select the x and y values above.

Click on points to select them in the plot and in the table. Ctrl+Click for selecting a range of points.

Use mouse-wheel for zooming and mouse drag for changing the axis range.

Select the axes or axes labels to drag and zoom only in that orientation.</string>
</property>
</widget>
</widget>
</item>
Expand Down

0 comments on commit 5807c0b

Please sign in to comment.