forked from musescore/MuseScore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dropcontroller.cpp
422 lines (337 loc) · 12.4 KB
/
dropcontroller.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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
* SPDX-License-Identifier: GPL-3.0-only
* MuseScore-CLA-applies
*
* MuseScore
* Music Composition & Notation
*
* Copyright (C) 2021 MuseScore BVBA and others
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* 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, see <https://www.gnu.org/licenses/>.
*/
#include "dropcontroller.h"
#include "../idockwindow.h"
#include "../dockcentralview.h"
#include "../dockingholderview.h"
#include "../dockpageview.h"
#include "../dockpanelview.h"
#include "../docktoolbarview.h"
#include "globaltypes.h"
#include "log.h"
#include "thirdparty/KDDockWidgets/src/DockWidgetQuick.h"
#include "thirdparty/KDDockWidgets/src/MainWindowBase.h"
#include "thirdparty/KDDockWidgets/src/private/DockRegistry_p.h"
#include "thirdparty/KDDockWidgets/src/private/DragController_p.h"
#include "thirdparty/KDDockWidgets/src/private/DropAreaWithCentralFrame_p.h"
using KDDropLocation = KDDockWidgets::DropIndicatorOverlayInterface::DropLocation;
namespace mu::dock {
static constexpr double MAX_DISTANCE_TO_HOLDER = 50;
static KDDropLocation dropLocationToKDDockLocation(Location location)
{
switch (location) {
case Location::Undefined: return KDDropLocation::DropLocation_None;
case Location::Left: return KDDropLocation::DropLocation_Left;
case Location::Right: return KDDropLocation::DropLocation_Right;
case Location::Center: return KDDropLocation::DropLocation_Center;
case Location::Top: return KDDropLocation::DropLocation_Top;
case Location::Bottom: return KDDropLocation::DropLocation_Bottom;
}
return KDDropLocation::DropLocation_None;
}
static bool isPointAllowedForDrop(const QPoint& point, const DropDestination& dropDestination)
{
QRect dropRect = dropDestination.dock->frameGeometry();
if (!dropRect.contains(point)) {
return false;
}
if (dropDestination.dropDistance == 0) {
return true;
}
if (dropDestination.dropLocation == Location::Left) {
if (std::abs(dropRect.left() - point.x()) <= dropDestination.dropDistance) {
return true;
}
}
if (dropDestination.dropLocation == Location::Right) {
if (std::abs(dropRect.right() - point.x()) <= dropDestination.dropDistance) {
return true;
}
}
return false;
}
}
using namespace mu::dock;
DropController::DropController(KDDockWidgets::DropArea* dropArea)
: KDDockWidgets::DropIndicatorOverlayInterface(dropArea)
{
KDDockWidgets::DragController::instance()->setResolveDropAreaFunc([](const QPoint& globalPos) -> KDDockWidgets::DropArea* {
for (auto mainWindow : KDDockWidgets::DockRegistry::self()->mainwindows()) {
if (mainWindow->windowGeometry().contains(globalPos)) {
return mainWindow->dropArea();
}
}
return nullptr;
});
}
KDDropLocation DropController::hover_impl(QPoint globalPos)
{
DockBase* draggedDock = this->draggedDock();
if (!draggedDock) {
return DropLocation_None;
}
QPoint hoveredLocalPos = dockWindow()->asItem().mapFromGlobal(globalPos).toPoint();
DropDestination dropDestination = resolveDropDestination(draggedDock, hoveredLocalPos);
if (auto toolBar = dynamic_cast<DockToolBarView*>(draggedDock)) {
updateToolBarOrientation(toolBar, dropDestination);
}
setCurrentDropDestination(draggedDock, dropDestination);
setCurrentDropLocation(dropLocationToKDDockLocation(m_currentDropDestination.dropLocation));
if (m_currentDropDestination.isValid()) {
setHoveredFrame(m_currentDropDestination.dock->dockWidget()->frame());
}
return currentDropLocation();
}
void DropController::updateVisibility()
{
auto resetDropLocation = [this]() {
setCurrentDropLocation(DropLocation_None);
endHover();
};
auto draggedDock = this->draggedDock();
if (!draggedDock) {
resetDropLocation();
return;
}
if (!isHovered()) {
if (auto toolBar = dynamic_cast<DockToolBarView*>(draggedDock)) {
updateToolBarOrientation(toolBar);
}
resetDropLocation();
}
}
QPoint DropController::posForIndicator(KDDropLocation) const
{
return QPoint();
}
void DropController::endHover()
{
if (!m_currentDropDestination.isValid()) {
return;
}
m_currentDropDestination.dock->hideHighlighting();
if (m_currentDropDestination.dock->type() == DockType::DockingHolder) {
m_currentDropDestination.dock->close();
}
m_currentDropDestination.clear();
}
bool DropController::isMouseOverDock(const QPoint& mouseLocalPos, const DockBase* dock) const
{
QRect geometry = dock ? dock->frameGeometry() : QRect();
return geometry.contains(mouseLocalPos);
}
void DropController::updateToolBarOrientation(DockToolBarView* draggedToolBar, const DropDestination& dropDestination)
{
IF_ASSERT_FAILED(draggedToolBar) {
return;
}
framework::Orientation orientation = framework::Orientation::Horizontal;
if (!dropDestination.isValid()) {
draggedToolBar->setOrientation(static_cast<Qt::Orientation>(orientation));
return;
}
switch (dropDestination.dock->location()) {
case Location::Left:
case Location::Right:
orientation = framework::Orientation::Vertical;
break;
case Location::Top:
case Location::Bottom:
orientation = framework::Orientation::Horizontal;
break;
case Location::Center:
case Location::Undefined:
break;
}
draggedToolBar->setOrientation(static_cast<Qt::Orientation>(orientation));
}
void DropController::setCurrentDropDestination(const DockBase* draggedDock, const DropDestination& dropDestination)
{
if (m_currentDropDestination == dropDestination) {
return;
}
endHover();
m_currentDropDestination = dropDestination;
if (!m_currentDropDestination.isValid()) {
return;
}
auto showHighlighting = [this, draggedDock]() {
QRect highlightingRect = resolveHighlightingRect(draggedDock, m_currentDropDestination);
m_currentDropDestination.dock->showHighlighting(highlightingRect);
};
if (m_currentDropDestination.dock->type() != DockType::DockingHolder) {
showHighlighting();
return;
}
switch (m_currentDropDestination.dock->location()) {
case Location::Left:
case Location::Right:
m_currentDropDestination.dock->setMinimumWidth(draggedDock->minimumWidth());
break;
case Location::Top:
case Location::Bottom:
m_currentDropDestination.dock->setMinimumHeight(draggedDock->minimumHeight());
break;
case Location::Center:
case Location::Undefined:
break;
}
m_currentDropDestination.dock->open();
showHighlighting();
m_currentDropDestination.dock->init();
}
DropDestination DropController::resolveDropDestination(const DockBase* draggedDock, const QPoint& localPos) const
{
if (draggedDock->type() == DockType::Panel) {
DropDestination destination;
destination.dock = resolvePanelForDrop(dynamic_cast<const DockPanelView*>(draggedDock), localPos);
destination.dropLocation = resolveDropLocation(destination.dock, localPos);
if (destination.isValid()) {
return destination;
}
}
const DockingHolderView* holder = resolveDockingHolder(draggedDock->type(), localPos);
QList<DropDestination> destinations = draggedDock->dropDestinations();
for (const DropDestination& destination : destinations) {
if (holder == destination.dock) {
return destination;
}
if (isPointAllowedForDrop(localPos, destination)) {
return destination;
}
}
return DropDestination();
}
DockingHolderView* DropController::resolveDockingHolder(DockType draggedDockType, const QPoint& localPos) const
{
if (!dockWindow()->asItem().contains(localPos)) {
return nullptr;
}
QRect centralGeometry = currentPage()->centralDock()->frameGeometry();
// TODO: Need to take any panels docked at top into account
if (localPos.y() <= centralGeometry.top() + MAX_DISTANCE_TO_HOLDER) {
return currentPage()->holder(draggedDockType, Location::Top);
}
if (localPos.y() >= centralGeometry.bottom() - MAX_DISTANCE_TO_HOLDER) {
return currentPage()->holder(draggedDockType, Location::Bottom);
}
if (localPos.x() <= MAX_DISTANCE_TO_HOLDER) {
return currentPage()->holder(draggedDockType, Location::Left);
}
if (localPos.x() >= dockWindow()->asItem().boundingRect().right() - MAX_DISTANCE_TO_HOLDER) {
return currentPage()->holder(draggedDockType, Location::Right);
}
return nullptr;
}
DockPanelView* DropController::resolvePanelForDrop(const DockPanelView* panel, const QPoint& localPos) const
{
QList<DockPanelView*> panels = currentPage()->possiblePanelsForTab(panel);
for (DockPanelView* p : panels) {
if (isMouseOverDock(localPos, p)) {
return p;
}
}
return nullptr;
}
Location DropController::resolveDropLocation(const DockBase* hoveredDock, const QPoint& localPos) const
{
if (!hoveredDock) {
return Location::Undefined;
}
QRect geometry = hoveredDock->frameGeometry();
Location dockLocation = hoveredDock->location();
qreal frameEnd = geometry.bottom();
qreal mousePos = localPos.y();
Location beginDropLocation = Location::Top;
Location endDropLocation = Location::Bottom;
if (dockLocation == Location::Top || dockLocation == Location::Bottom) {
mousePos = localPos.x();
frameEnd = geometry.right();
beginDropLocation = Location::Left;
endDropLocation = Location::Right;
}
if (mousePos <= frameEnd / 3) {
return beginDropLocation;
}
if (mousePos <= frameEnd / 1.5) {
return Location::Center;
}
if (mousePos <= frameEnd) {
return endDropLocation;
}
return Location::Undefined;
}
QRect DropController::resolveHighlightingRect(const DockBase* draggedDock, const DropDestination& destination) const
{
if (!destination.isValid()) {
return QRect();
}
QRect frameGeometry = destination.dock->frameGeometry();
int frameWidth = frameGeometry.width();
int frameHeight = frameGeometry.height();
QRect fullFrameHighlightingRect = QRect(0, 0, frameWidth, frameHeight);
if (destination.dock->type() == DockType::DockingHolder) {
return fullFrameHighlightingRect;
}
if (destination.dock->type() == DockType::Central) {
int draggedDockWidth = draggedDock->frameGeometry().width();
if (destination.dropLocation == Location::Left) {
return QRect(0, 0, draggedDockWidth, frameHeight);
}
if (destination.dropLocation == Location::Right) {
return QRect(frameWidth - draggedDockWidth, 0, draggedDockWidth, frameHeight);
}
}
switch (destination.dropLocation) {
case Location::Top:
return QRect(0, 0, frameWidth, frameHeight / 2);
case Location::Bottom:
return QRect(0, frameHeight / 2, frameWidth, frameHeight / 2);
case Location::Left:
return QRect(0, 0, frameWidth / 2, frameHeight);
case Location::Right:
return QRect(frameWidth / 2, 0, frameWidth / 2, frameHeight);
case Location::Center:
return fullFrameHighlightingRect;
case Location::Undefined:
break;
}
return QRect();
}
IDockWindow* DropController::dockWindow() const
{
return dockWindowProvider()->window();
}
DockPageView* DropController::currentPage() const
{
return dockWindow() ? dockWindow()->currentPage() : nullptr;
}
DockBase* DropController::draggedDock() const
{
auto windowBeingDragged = KDDockWidgets::DragController::instance()->windowBeingDragged();
if (!windowBeingDragged || windowBeingDragged->dockWidgets().isEmpty()) {
return nullptr;
}
QString dockName = windowBeingDragged->dockWidgets().first()->uniqueName();
const DockPageView* page = currentPage();
return page ? page->dockByName(dockName) : nullptr;
}