-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathcustomizabletoolbar.cpp
More file actions
338 lines (282 loc) · 9.65 KB
/
Copy pathcustomizabletoolbar.cpp
File metadata and controls
338 lines (282 loc) · 9.65 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2026 FTA7700
*
* This program 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 2
* of the License, or (at your option) any later version.
*
* This program 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#include "customizabletoolbar.h"
#include <QAction>
#include <QActionEvent>
#include <QApplication>
#include <QCursor>
#include <QGraphicsOpacityEffect>
#include <QLabel>
#include <QMouseEvent>
#include <QPixmap>
#include <QTimer>
#include "base/global.h"
CustomizableToolBar::CustomizableToolBar(QWidget *parent)
: QToolBar(parent)
{
setMouseTracking(true);
}
CustomizableToolBar::CustomizableToolBar(const QString &title, QWidget *parent)
: QToolBar(title, parent)
{
setMouseTracking(true);
}
void CustomizableToolBar::lockAction(QAction *action)
{
if (action && !m_lockedActions.contains(action))
m_lockedActions.append(action);
}
void CustomizableToolBar::setLocked(const bool locked)
{
m_locked = locked;
}
void CustomizableToolBar::actionEvent(QActionEvent *event)
{
QToolBar::actionEvent(event);
if (event->type() == QEvent::ActionAdded)
{
if (QWidget *widget = widgetForAction(event->action()))
widget->installEventFilter(this);
}
}
bool CustomizableToolBar::eventFilter(QObject *watched, QEvent *event)
{
if (m_locked)
return QToolBar::eventFilter(watched, event);
auto *widget = qobject_cast<QWidget *>(watched);
if (!widget)
return QToolBar::eventFilter(watched, event);
auto *mouseEvent = static_cast<QMouseEvent *>(event);
switch (event->type())
{
case QEvent::MouseButtonPress:
if (handleMousePress(widget, mouseEvent))
return true;
break;
case QEvent::MouseMove:
if (handleMouseMove(mouseEvent))
return true;
break;
case QEvent::MouseButtonRelease:
if (handleMouseRelease(mouseEvent))
return true;
break;
default:
break;
}
return QToolBar::eventFilter(watched, event);
}
bool CustomizableToolBar::handleMousePress(QWidget *widget, QMouseEvent *mouseEvent)
{
m_dragJustFinished = false;
if (mouseEvent->button() != Qt::LeftButton)
return false;
QAction *action = nullptr;
for (QAction *a : asConst(actions()))
{
if (widgetForAction(a) == widget)
{
action = a;
break;
}
}
if (!action || m_lockedActions.contains(action) || action->isSeparator())
return false;
m_dragAction = action;
m_dragWidget = widget;
m_dragStartPos = mouseEvent->globalPosition().toPoint();
m_dragOffsetX = mouseEvent->position().toPoint().x();
return false;
}
bool CustomizableToolBar::handleMouseMove(QMouseEvent *mouseEvent)
{
if (!m_dragAction)
return false;
const QPoint globalPos = mouseEvent->globalPosition().toPoint();
if (!m_dragging)
{
if ((globalPos - m_dragStartPos).manhattanLength() < QApplication::startDragDistance())
return false;
startDrag(m_dragWidget, globalPos);
}
return m_dragging;
}
bool CustomizableToolBar::handleMouseRelease(QMouseEvent *mouseEvent)
{
if (m_dragJustFinished)
{
m_dragJustFinished = false;
return true;
}
if (m_dragging)
{
endDrag(mouseEvent->globalPosition().toPoint());
return true;
}
if (m_dragAction)
m_dragAction = nullptr;
return false;
}
void CustomizableToolBar::onDragTimer()
{
if (!m_dragging)
return;
updateDrag(QCursor::pos());
if (!(QApplication::mouseButtons() & Qt::LeftButton))
endDrag(QCursor::pos());
}
void CustomizableToolBar::startDrag(QWidget *widget, const QPoint &globalPos)
{
m_dragging = true;
// Snapshot BEFORE making transparent
const QPixmap snap = widget->grab();
// Make drag widget invisible but keep it in layout, avoiding reflow
auto *effect = new QGraphicsOpacityEffect(widget);
effect->setOpacity(0.0);
widget->setGraphicsEffect(effect);
// Float label follows cursor globally
m_floatLabel = new QLabel(nullptr, Qt::ToolTip | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint);
m_floatLabel->setAttribute(Qt::WA_TransparentForMouseEvents);
m_floatLabel->setAttribute(Qt::WA_ShowWithoutActivating);
m_floatLabel->setPixmap(snap);
m_floatLabel->setFixedSize(snap.size());
m_floatLabel->show();
m_dragTimer = new QTimer(this);
m_dragTimer->setInterval(16);
connect(m_dragTimer, &QTimer::timeout, this, &CustomizableToolBar::onDragTimer);
m_dragTimer->start();
updateDrag(globalPos);
}
void CustomizableToolBar::updateDrag(const QPoint &globalPos)
{
if (!m_floatLabel)
return;
const int fw = m_floatLabel->width();
const int fh = m_floatLabel->height();
const QPoint toolbarTopLeft = mapToGlobal(QPoint(0, 0));
const int boundaryGX = getBoundaryGlobalX();
const int clampedX = qBound(toolbarTopLeft.x(), globalPos.x() - m_dragOffsetX, boundaryGX - fw + 20);
const int clampedY = toolbarTopLeft.y() + (height() - fh) / 2;
m_floatLabel->move(clampedX, clampedY);
const int dragFloatLeft = mapFromGlobal(QPoint(clampedX, 0)).x();
const int dragFloatRight = mapFromGlobal(QPoint(clampedX + fw, 0)).x();
const int dragFloatCentre = mapFromGlobal(QPoint(clampedX + fw / 2, 0)).x();
const bool atLeftWall = (clampedX <= toolbarTopLeft.x());
const bool movingLeft = atLeftWall || (dragFloatCentre < m_lastFloatCentreX)
|| ((m_lastFloatCentreX == 0) && (globalPos.x() < m_dragStartPos.x()));
m_lastFloatCentreX = dragFloatCentre;
const int bi = findBoundaryIndex();
const QList<QAction *> acts = actions();
if (!acts.isEmpty() && (acts.first() == m_dragAction) && !movingLeft
&& (m_lastSwapX >= 0) && (qAbs(dragFloatLeft - m_lastSwapX) < 20))
{
return;
}
QAction *newInsertBefore = (bi >= 0) ? acts[bi] : nullptr;
for (int i = 0; i < bi; ++i)
{
QAction *a = acts[i];
if (a == m_dragAction)
continue;
QWidget *w = widgetForAction(a);
if (!w && !a->isSeparator())
continue;
const int threshold = w ? (a->isSeparator() ? (movingLeft ? w->x() + w->width() + fw / 2 : w->x() - fw / 2) : w->x() + w->width() / 2) : 0;
const int compareX = a->isSeparator() ? dragFloatCentre : (movingLeft ? dragFloatLeft : dragFloatRight);
if (compareX < threshold)
{
newInsertBefore = a;
break;
}
}
const bool targetIsSeparator = newInsertBefore && newInsertBefore->isSeparator();
if (!targetIsSeparator && (m_lastSwapX >= 0) && (qAbs(dragFloatLeft - m_lastSwapX) < 14))
return;
// Only swap when target slot changes, one atomic reorder per crossing
if (newInsertBefore == m_gapTarget)
return;
m_gapTarget = newInsertBefore;
m_lastSwapX = dragFloatLeft;
// Briefly remove opacity effect during reorder, reapply to new widget
if (m_dragWidget)
m_dragWidget->setGraphicsEffect(nullptr);
removeAction(m_dragAction);
insertAction(m_gapTarget, m_dragAction);
QWidget *newWidget = widgetForAction(m_dragAction);
if (newWidget)
{
m_dragWidget = newWidget;
auto *effect = new QGraphicsOpacityEffect(newWidget);
effect->setOpacity(0.0);
newWidget->setGraphicsEffect(effect);
}
}
void CustomizableToolBar::endDrag(const QPoint &)
{
if (m_dragTimer)
{
m_dragTimer->stop();
delete m_dragTimer;
m_dragTimer = nullptr;
}
delete m_floatLabel;
m_floatLabel = nullptr;
// Remove opacity effect, restore widget appearance
if (m_dragWidget)
m_dragWidget->setGraphicsEffect(nullptr);
// Reorder already committed live in updateDrag, just save state
emit actionOrderChanged();
m_gapTarget = nullptr;
m_lastSwapX = -1;
m_lastFloatCentreX = 0;
m_dragAction = nullptr;
m_dragWidget = nullptr;
m_dragJustFinished = true;
m_dragging = false;
}
int CustomizableToolBar::findBoundaryIndex() const
{
const QList<QAction *> acts = actions();
for (int i = 0; i < acts.size(); ++i)
{
if (m_lockedActions.contains(acts[i]))
return i;
}
return -1;
}
int CustomizableToolBar::getBoundaryGlobalX() const
{
const int bi = findBoundaryIndex();
if (bi >= 0)
{
QWidget *bw = widgetForAction(actions()[bi]);
if (bw)
return mapToGlobal(QPoint(bw->x(), 0)).x();
}
return mapToGlobal(QPoint(width(), 0)).x();
}