-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtreeWidget.cpp
244 lines (230 loc) · 8.64 KB
/
treeWidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
* Copyright (C) Pedram Pourang (aka Tsu Jan) 2018-2021 <[email protected]>
*
* Arqiver is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Arqiver is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @license GPL-3.0+ <https://spdx.org/licenses/GPL-3.0+.html>
*/
#include "treeWidget.h"
#include <QUrl>
#include <QScrollBar>
#include <QMimeData>
#include <QIcon>
#include <QApplication>
#define SCROLL_FRAMES_PER_SEC 50
#define SCROLL_DURATION 300 // in ms
static const int scrollAnimFrames = SCROLL_FRAMES_PER_SEC * SCROLL_DURATION / 1000;
namespace Arqiver {
TreeWidget::TreeWidget(QWidget *parent) : QTreeWidget(parent) {
dragStarted_ = false; // not needed
enterPressedHere_ = false;
setDragDropMode(QAbstractItemView::DragOnly);
setContextMenuPolicy(Qt::CustomContextMenu);
setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
smoothScrollTimer_ = nullptr;
/* set the scrolling step to the row height (Qt should have done it, IMO) */
if (model()) { // we don't change the model
connect(model(), &QAbstractItemModel::rowsInserted, this, [this] (const QModelIndex &parent, int first, int last) {
if (!parent.isValid() && first == 0 && last == 0)
verticalScrollBar()->setSingleStep(rowHeight(model()->index(0,0)));
});
}
}
/*************************/
TreeWidget::~TreeWidget() {
if (smoothScrollTimer_) {
disconnect(smoothScrollTimer_, &QTimer::timeout, this, &TreeWidget::scrollSmoothly);
smoothScrollTimer_->stop();
delete smoothScrollTimer_;
}
}
/*************************/
// The default implementation doesn't scroll to a deep nonexpanded item
// because it doesn't have time to.
void TreeWidget::scrollTo(const QModelIndex &index, QAbstractItemView::ScrollHint hint) {
if (!index.isValid()) return;
if (itemsExpandable()) {
QModelIndex parent = index.parent();
while (parent != rootIndex() && parent.isValid() && state() == QAbstractItemView::NoState) {
if (!isExpanded(parent))
expand(parent);
parent = model()->parent(parent);
}
}
QTreeWidget::scrollTo(index, hint);
}
/*************************/
void TreeWidget::mousePressEvent(QMouseEvent *event) {
QTreeWidget::mousePressEvent(event);
if (event->button() == Qt::LeftButton && indexAt(event->position().toPoint()).isValid())
dragStartPosition_ = event->position().toPoint();
else
dragStartPosition_ = QPoint();
dragStarted_ = false;
}
/*************************/
void TreeWidget::mouseMoveEvent(QMouseEvent *event) {
if (dragStartPosition_.isNull()) {
event->ignore();
return;
}
if (!dragStarted_
&& (event->buttons() & Qt::LeftButton)
&& (event->position().toPoint() - dragStartPosition_).manhattanLength() >= qMax(22, QApplication::startDragDistance())) {
dragStarted_ = true;
if (!selectedItems().isEmpty())
emit dragStarted();
event->accept();
}
}
/*************************/
void TreeWidget::keyReleaseEvent(QKeyEvent *event) {
if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) {
/* NOTE: If Enter is kept pressed, it will be released and then will be
pressed again, and so on. So, auto-repeating is ignored here. */
if (enterPressedHere_ && !event->isAutoRepeat() && currentItem() && !currentItem()->isHidden()) {
emit enterPressed(currentItem());
event->accept();
enterPressedHere_ = false;
return;
}
enterPressedHere_ = false;
}
QTreeWidget::keyReleaseEvent(event);
}
/*************************/
void TreeWidget::keyPressEvent(QKeyEvent *event) {
if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter)
enterPressedHere_ = true; // to know if it's pressed here and, e.g., not inside a modal dialog
else if (event->key() != Qt::Key_Up && event->key() != Qt::Key_Down
&& event->key() != Qt::Key_Home && event->key() != Qt::Key_End
&& event->key() != Qt::Key_PageUp && event->key() != Qt::Key_PageDown
&& !(event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_A)) {
/* ignore typing */
event->accept();
return;
}
QTreeWidget::keyPressEvent(event);
}
/*************************/
void TreeWidget::wheelEvent(QWheelEvent *event) {
/* smooth scrolling */
if (event->spontaneous()
&& event->source() == Qt::MouseEventNotSynthesized) {
QPoint deltaPoint = event->angleDelta();
bool horizontal(qAbs(deltaPoint.x()) > qAbs(deltaPoint.y()));
QScrollBar *sbar = horizontal ? horizontalScrollBar()
: verticalScrollBar();
if (sbar && sbar->isVisible()) {
/* keep track of the wheel event for smooth scrolling */
int delta = horizontal ? deltaPoint.x() : deltaPoint.y();
if ((delta > 0 && sbar->value() == sbar->minimum())
|| (delta < 0 && sbar->value() == sbar->maximum())) {
return; // the scrollbar can't move
}
if (QApplication::wheelScrollLines() > 1) {
if (horizontal
|| (event->modifiers() & Qt::ShiftModifier)
|| qAbs(delta) < 120) { // touchpad
if (qAbs(delta) >= scrollAnimFrames * QApplication::wheelScrollLines())
delta /= QApplication::wheelScrollLines(); // scrolling with minimum speed
}
else if (QApplication::wheelScrollLines() > 2
&& iconSize().height() >= 48
&& qAbs(delta * 2) >= scrollAnimFrames * QApplication::wheelScrollLines()) {
/* 2 rows per wheel turn with large icons */
delta = delta * 2 / QApplication::wheelScrollLines();
}
}
/* wait until the angle delta reaches an acceptable value */
static int _delta = 0;
_delta += delta;
if (abs(_delta) < scrollAnimFrames)
return;
if (smoothScrollTimer_ == nullptr) {
smoothScrollTimer_ = new QTimer();
connect(smoothScrollTimer_, &QTimer::timeout, this, &TreeWidget::scrollSmoothly);
}
/* set the data for smooth scrolling */
scrollData data;
data.delta = _delta;
data.leftFrames = scrollAnimFrames;
data.vertical = !horizontal;
queuedScrollSteps_.append(data);
if (!smoothScrollTimer_->isActive())
smoothScrollTimer_->start(1000 / SCROLL_FRAMES_PER_SEC);
_delta = 0;
return;
}
}
QTreeWidget::wheelEvent (event);
}
/*************************/
void TreeWidget::scrollSmoothly() {
int totalDeltaH = 0, totalDeltaV = 0;
QList<scrollData>::iterator it = queuedScrollSteps_.begin();
while (it != queuedScrollSteps_.end()) {
int delta = qRound(static_cast<qreal>(it->delta) / static_cast<qreal>(scrollAnimFrames));
int remainingDelta = it->delta - (scrollAnimFrames - it->leftFrames) * delta;
if ((delta >= 0 && remainingDelta < 0) || (delta < 0 && remainingDelta >= 0))
remainingDelta = 0;
if (qAbs(delta) >= qAbs(remainingDelta)) {
/* this is the last frame or, due to rounding, there can be no more frame */
if (it->vertical)
totalDeltaV += remainingDelta;
else
totalDeltaH += remainingDelta;
it = queuedScrollSteps_.erase(it);
}
else {
if (it->vertical)
totalDeltaV += delta;
else
totalDeltaH += delta;
-- it->leftFrames;
++it;
}
}
if (totalDeltaH != 0) {
QScrollBar *hbar = horizontalScrollBar();
if (hbar && hbar->isVisible()) {
QWheelEvent eventH(QPointF(),
QPointF(),
QPoint(),
QPoint(0, totalDeltaH),
Qt::NoButton,
Qt::NoModifier,
Qt::NoScrollPhase,
false);
QApplication::sendEvent(hbar, &eventH);
}
}
if (totalDeltaV != 0) {
QScrollBar *vbar = verticalScrollBar();
if (vbar && vbar->isVisible()) {
QWheelEvent eventV(QPointF(),
QPointF(),
QPoint(),
QPoint(0, totalDeltaV),
Qt::NoButton,
Qt::NoModifier,
Qt::NoScrollPhase,
false);
QApplication::sendEvent(vbar, &eventV);
}
}
if (queuedScrollSteps_.empty())
smoothScrollTimer_->stop();
}
}