QtPass
2.0.0
Developer documentation
Toggle main menu visibility
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
30
class
DeselectableTreeView
:
public
QTreeView {
31
Q_OBJECT
32
33
public
:
38
explicit
DeselectableTreeView
(QWidget *parent) : QTreeView(parent) {
39
deselectTimer
.setSingleShot(
true
);
40
connect(&
deselectTimer
, &QTimer::timeout,
this
,
41
&
DeselectableTreeView::deselect
);
42
}
43
46
~DeselectableTreeView
()
override
=
default
;
47
48
signals:
54
void
emptyClicked
();
55
56
private
:
61
bool
doubleClickHappened
=
false
;
66
bool
clickSelected
=
false
;
71
QTimer
deselectTimer
;
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();
82
clickSelected
=
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
() {
125
if
(
doubleClickHappened
)
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_
DeselectableTreeView::mouseReleaseEvent
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...
Definition
deselectabletreeview.h:93
DeselectableTreeView::deselectTimer
QTimer deselectTimer
Single-shot timer, started by a qualifying release for one double-click interval; its timeout runs de...
Definition
deselectabletreeview.h:71
DeselectableTreeView::doubleClickHappened
bool doubleClickHappened
Set by mouseDoubleClickEvent() so a deselect() that fires after a double-click's first release does n...
Definition
deselectabletreeview.h:61
DeselectableTreeView::mouseDoubleClickEvent
void mouseDoubleClickEvent(QMouseEvent *event) override
Cancel the pending deselect so the double-click opens the entry instead of clearing the selection.
Definition
deselectabletreeview.h:138
DeselectableTreeView::clickSelected
bool clickSelected
Whether the row under the last press was already selected; read and reset by the matching release.
Definition
deselectabletreeview.h:66
DeselectableTreeView::DeselectableTreeView
DeselectableTreeView(QWidget *parent)
Construct the view and arm the single-shot deselect timer.
Definition
deselectabletreeview.h:38
DeselectableTreeView::mousePressEvent
void mousePressEvent(QMouseEvent *event) override
Remember whether the pressed row was already selected and cancel any pending deselect from the previo...
Definition
deselectabletreeview.h:78
DeselectableTreeView::emptyClicked
void emptyClicked()
Emitted after deselect() has cleared the selection because of a single click on empty space or on a s...
DeselectableTreeView::~DeselectableTreeView
~DeselectableTreeView() override=default
Default destructor.
DeselectableTreeView::deselect
void deselect()
Timer slot: clear the selection and current index and emit emptyClicked(), unless a double-click happ...
Definition
deselectabletreeview.h:124
src
deselectabletreeview.h
Generated by
1.18.0