QtPass 2.0.0
Developer documentation
Loading...
Searching...
No Matches
deselectabletreeview.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2016 Anne Jan Brouwer
2// SPDX-License-Identifier: GPL-3.0-or-later
8#ifndef SRC_DESELECTABLETREEVIEW_H_
9#define SRC_DESELECTABLETREEVIEW_H_
10
11#include <QGuiApplication>
12#include <QMouseEvent>
13#include <QPersistentModelIndex>
14#include <QStyleHints>
15#include <QTimer>
16#include <QTreeView>
17
30class DeselectableTreeView : public QTreeView {
31 Q_OBJECT
32
33public:
38 explicit DeselectableTreeView(QWidget *parent) : QTreeView(parent) {
39 deselectTimer.setSingleShot(true);
40 connect(&deselectTimer, &QTimer::timeout, this,
42 }
43
46 ~DeselectableTreeView() override = default;
47
48signals:
55
56private:
61 bool doubleClickHappened = false;
66 bool clickSelected = false;
72
78 void mousePressEvent(QMouseEvent *event) override {
79 // A new press belongs to a new click: whatever the previous release left
80 // pending must not clear the selection this press is about to make.
81 deselectTimer.stop();
83 selectionModel()->isSelected(indexAt(event->position().toPoint()));
84 QTreeView::mousePressEvent(event);
85 }
86
93 void mouseReleaseEvent(QMouseEvent *event) override {
94 // Persistent: QTreeView::mouseReleaseEvent() emits clicked(), and what
95 // that runs can change the model (a message box for a link, the file
96 // system model reloading meanwhile); a plain index then names another
97 // row, or memory that is gone.
98 const QPersistentModelIndex item(indexAt(event->position().toPoint()));
99 const bool pressWasOnSelection = clickSelected;
100 clickSelected = false;
101
102 // Handle the release immediately: waiting here would delay selection —
103 // and therefore every decrypt — by the double-click interval.
104 QTreeView::mouseReleaseEvent(event);
105
106 const bool emptyOrSelected =
107 !item.isValid() || selectionModel()->isSelected(item);
108 if (!pressWasOnSelection || !emptyOrSelected)
109 return;
110
111 // Deselecting is the one decision that must wait: a double-click starts
112 // with a release, and opening the editor must win over clearing the
113 // selection. Ask the platform how long a double-click may take instead
114 // of assuming 200 ms.
115 doubleClickHappened = false;
116 deselectTimer.start(
117 QGuiApplication::styleHints()->mouseDoubleClickInterval());
118 }
119
124 void deselect() {
126 return;
127 clearSelection();
128 selectionModel()->setCurrentIndex(QModelIndex(),
129 QItemSelectionModel::Select);
130 emit emptyClicked();
131 }
132
138 void mouseDoubleClickEvent(QMouseEvent *event) override {
139 doubleClickHappened = true;
140 deselectTimer.stop();
141 QTreeView::mouseDoubleClickEvent(event);
142 }
143};
144
145#endif // SRC_DESELECTABLETREEVIEW_H_
void mouseReleaseEvent(QMouseEvent *event) override
Let QTreeView handle the release at once, then, if the press was on a selected row and the release is...
QTimer deselectTimer
Single-shot timer, started by a qualifying release for one double-click interval; its timeout runs de...
bool doubleClickHappened
Set by mouseDoubleClickEvent() so a deselect() that fires after a double-click's first release does n...
void mouseDoubleClickEvent(QMouseEvent *event) override
Cancel the pending deselect so the double-click opens the entry instead of clearing the selection.
bool clickSelected
Whether the row under the last press was already selected; read and reset by the matching release.
DeselectableTreeView(QWidget *parent)
Construct the view and arm the single-shot deselect timer.
void mousePressEvent(QMouseEvent *event) override
Remember whether the pressed row was already selected and cancel any pending deselect from the previo...
void emptyClicked()
Emitted after deselect() has cleared the selection because of a single click on empty space or on a s...
~DeselectableTreeView() override=default
Default destructor.
void deselect()
Timer slot: clear the selection and current index and emit emptyClicked(), unless a double-click happ...