statistics: make selection keyboard modifiers more general

Up to now, we passed a "shiftPressed" flag to the individual
selection functions. To be more general replace by a struct
with "shift" and "ctrl" flags.

While doing this:
1) Move the struct into a new statsselection file for better
   encapsulation.
2) Change shift to control in the scatter series, since individual
   selection of items is usually done with control, not shift.
   Shift usually means "select range".

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2021-02-08 17:07:37 +01:00 committed by Dirk Hohndel
parent 21b8cded56
commit 64b82b16a2
16 changed files with 47 additions and 22 deletions

View file

@ -144,6 +144,7 @@ SOURCES += subsurface-mobile-main.cpp \
stats/statscolors.cpp \
stats/statsgrid.cpp \
stats/statshelper.cpp \
stats/statsselection.cpp \
stats/statsseries.cpp \
stats/statsstate.cpp \
mobile-widgets/qmlinterface.cpp \
@ -297,6 +298,7 @@ HEADERS += \
stats/statscolors.h \
stats/statsgrid.h \
stats/statshelper.h \
stats/statsselection.h \
stats/statsseries.h \
stats/statsstate.h \
stats/statstranslations.h \

View file

@ -36,6 +36,8 @@ set(SUBSURFACE_STATS_SRCS
statsgrid.cpp
statshelper.h
statshelper.cpp
statsselection.h
statsselection.cpp
statsseries.h
statsseries.cpp
statsstate.h

View file

@ -411,7 +411,7 @@ void BarSeries::unhighlight()
highlighted = Index();
}
bool BarSeries::selectItemsUnderMouse(const QPointF &pos, bool)
bool BarSeries::selectItemsUnderMouse(const QPointF &pos, SelectionModifier)
{
Index index = getItemUnderMouse(pos);
if (index.bar < 0) {

View file

@ -69,7 +69,7 @@ public:
void updatePositions() override;
bool hover(QPointF pos) override;
void unhighlight() override;
bool selectItemsUnderMouse(const QPointF &point, bool shiftPressed) override;
bool selectItemsUnderMouse(const QPointF &point, SelectionModifier modifier) override;
private:
BarSeries(StatsView &view, StatsAxis *xAxis, StatsAxis *yAxis,

View file

@ -146,7 +146,7 @@ void BoxSeries::unhighlight()
highlighted = -1;
}
bool BoxSeries::selectItemsUnderMouse(const QPointF &pos, bool)
bool BoxSeries::selectItemsUnderMouse(const QPointF &pos, SelectionModifier)
{
int index = getItemUnderMouse(pos);
if (index < 0) {

View file

@ -23,7 +23,7 @@ public:
void updatePositions() override;
bool hover(QPointF pos) override;
void unhighlight() override;
bool selectItemsUnderMouse(const QPointF &point, bool shiftPressed) override;
bool selectItemsUnderMouse(const QPointF &point, SelectionModifier modifier) override;
// Note: this expects that all items are added with increasing pos
// and that no bar is inside another bar, i.e. lowerBound and upperBound

View file

@ -266,7 +266,7 @@ void PieSeries::unhighlight()
highlighted = -1;
}
bool PieSeries::selectItemsUnderMouse(const QPointF &pos, bool)
bool PieSeries::selectItemsUnderMouse(const QPointF &pos, SelectionModifier)
{
int index = getItemUnderMouse(pos);
if (index < 0) {

View file

@ -28,7 +28,7 @@ public:
void updatePositions() override;
bool hover(QPointF pos) override;
void unhighlight() override;
bool selectItemsUnderMouse(const QPointF &point, bool shiftPressed) override;
bool selectItemsUnderMouse(const QPointF &point, SelectionModifier modifier) override;
std::vector<QString> binNames();

View file

@ -96,12 +96,12 @@ std::vector<int> ScatterSeries::getItemsInRect(const QRectF &rect) const
return res;
}
bool ScatterSeries::selectItemsUnderMouse(const QPointF &point, bool shiftPressed)
bool ScatterSeries::selectItemsUnderMouse(const QPointF &point, SelectionModifier modifier)
{
std::vector<struct dive *> selected;
std::vector<int> indices = getItemsUnderMouse(point);
if (shiftPressed) {
if (modifier.ctrl) {
// When shift is pressed, add the items under the mouse to the selection
// or, if all items under the mouse are selected, remove them.
selected = getDiveSelection();
@ -141,13 +141,13 @@ bool ScatterSeries::supportsLassoSelection() const
return true;
}
void ScatterSeries::selectItemsInRect(const QRectF &rect, bool shiftPressed, const std::vector<dive *> &oldSelection)
void ScatterSeries::selectItemsInRect(const QRectF &rect, SelectionModifier modifier, const std::vector<dive *> &oldSelection)
{
std::vector<struct dive *> selected;
std::vector<int> indices = getItemsInRect(rect);
selected.reserve(oldSelection.size() + indices.size());
if (shiftPressed) {
if (modifier.ctrl) {
selected = oldSelection;
// Ouch - this primitive merging of the selections grows with O(n^2). Fix this.
for (int idx: indices) {

View file

@ -27,9 +27,9 @@ public:
// Note: this expects that all items are added with increasing pos!
void append(dive *d, double pos, double value);
bool selectItemsUnderMouse(const QPointF &point, bool shiftPressed) override;
bool selectItemsUnderMouse(const QPointF &point, SelectionModifier modifier) override;
bool supportsLassoSelection() const override;
void selectItemsInRect(const QRectF &rect, bool shiftPressed, const std::vector<dive *> &oldSelection) override;
void selectItemsInRect(const QRectF &rect, SelectionModifier modifier, const std::vector<dive *> &oldSelection) override;
private:
// Get items under mouse.

2
stats/statsselection.cpp Normal file
View file

@ -0,0 +1,2 @@
// SPDX-License-Identifier: GPL-2.0
#include "statsselection.h"

15
stats/statsselection.h Normal file
View file

@ -0,0 +1,15 @@
// SPDX-License-Identifier: GPL-2.0
// Functions and data structures handling dive selections.
#ifndef STATS_SELECTION_H
#define STATS_SELECTION_H
struct SelectionModifier {
unsigned int ctrl : 1;
unsigned int shift : 1;
// Note: default member initializers for bit-fields becomes available with C++20.
// Therefore, for now an inline constructor.
SelectionModifier() : ctrl(0), shift(0) {}
};
#endif

View file

@ -22,6 +22,6 @@ bool StatsSeries::supportsLassoSelection() const
return false;
}
void StatsSeries::selectItemsInRect(const QRectF &, bool, const std::vector<dive *> &)
void StatsSeries::selectItemsInRect(const QRectF &, SelectionModifier, const std::vector<dive *> &)
{
}

View file

@ -4,6 +4,8 @@
#ifndef STATS_SERIES_H
#define STATS_SERIES_H
#include "statsselection.h"
#include <vector>
#include <QPointF>
@ -20,10 +22,10 @@ public:
virtual bool hover(QPointF pos) = 0; // Called on mouse movement. Return true if an item of this series is highlighted.
virtual void unhighlight() = 0; // Unhighlight any highlighted item.
// Returns true if an item was under the mouse.
virtual bool selectItemsUnderMouse(const QPointF &pos, bool shiftPressed) = 0;
virtual bool selectItemsUnderMouse(const QPointF &pos, SelectionModifier modifier) = 0;
virtual bool supportsLassoSelection() const;
// Needs only be defined if supportsLassoSelection() returns true.
virtual void selectItemsInRect(const QRectF &rect, bool shiftPressed, const std::vector<dive *> &oldSelection);
virtual void selectItemsInRect(const QRectF &rect, SelectionModifier modifier, const std::vector<dive *> &oldSelection);
virtual void divesSelected(const QVector<dive *> &dives) = 0;
protected:

View file

@ -39,7 +39,6 @@ StatsView::StatsView(QQuickItem *parent) : QQuickItem(parent),
xAxis(nullptr),
yAxis(nullptr),
draggedItem(nullptr),
shiftSelection(false),
rootNode(nullptr)
{
setFlag(ItemHasContents, true);
@ -84,10 +83,12 @@ void StatsView::mousePressEvent(QMouseEvent *event)
}
}
bool shiftPressed = event->modifiers() & Qt::ShiftModifier;
SelectionModifier modifier;
modifier.shift = (event->modifiers() & Qt::ShiftModifier) != 0;
modifier.ctrl = (event->modifiers() & Qt::ControlModifier) != 0;
bool itemSelected = false;
for (auto &series: series)
itemSelected |= series->selectItemsUnderMouse(pos, shiftPressed);
itemSelected |= series->selectItemsUnderMouse(pos, modifier);
// The user clicked in "empty" space. If there is a series supporting lasso-select,
// got into lasso mode. For now, we only support a rectangular lasso.
@ -98,8 +99,8 @@ void StatsView::mousePressEvent(QMouseEvent *event)
deleteChartItem(selectionRect); // Ooops. Already a selection in place.
dragStartMouse = pos;
selectionRect = createChartItem<ChartRectLineItem>(ChartZValue::Selection, selectionLassoColor, selectionLassoWidth);
shiftSelection = shiftPressed;
oldSelection = shiftPressed ? getDiveSelection() : std::vector<dive *>();
selectionModifier = modifier;
oldSelection = modifier.ctrl ? getDiveSelection() : std::vector<dive *>();
grabMouse();
setKeepMouseGrab(true); // don't allow Qt to steal the grab
update();
@ -400,7 +401,7 @@ void StatsView::mouseMoveEvent(QMouseEvent *event)
fabs(p2.x() - p1.x()), fabs(p2.y() - p1.y()));
for (auto &series: series) {
if (series->supportsLassoSelection())
series->selectItemsInRect(rect, shiftSelection, oldSelection);
series->selectItemsInRect(rect, selectionModifier, oldSelection);
}
update();
}

View file

@ -4,6 +4,7 @@
#include "statsstate.h"
#include "statshelper.h"
#include "statsselection.h"
#include <memory>
#include <QFont>
#include <QImage>
@ -145,7 +146,7 @@ private:
ChartItemPtr<RegressionItem> regressionItem;
ChartItemPtr<ChartRectLineItem> selectionRect;
QPointF dragStartMouse, dragStartItem;
bool shiftSelection;
SelectionModifier selectionModifier;
std::vector<dive *> oldSelection;
void hoverEnterEvent(QHoverEvent *event) override;