1 // Copyright 2001-2018 Crytek GmbH / Crytek Group. All rights reserved.
5 #include "QToolWindowDragHandlerDropTargets.h"
8 #define DROPTARGET_PADDING 4
10 QToolWindowDropTarget::QToolWindowDropTarget(QString imagePath
, QToolWindowAreaReference areaReference
)
12 , m_pixmap(new QPixmap())
13 , m_areaReference(areaReference
)
15 setWindowFlags(Qt::Tool
| Qt::FramelessWindowHint
| Qt::WindowStaysOnTopHint
);
16 setAttribute(Qt::WA_TranslucentBackground
);
17 setAttribute(Qt::WA_ShowWithoutActivating
);
19 m_pixmap
->load(imagePath
);
20 setFixedSize(m_pixmap
->size());
23 QToolWindowDropTarget::~QToolWindowDropTarget()
29 void QToolWindowDropTarget::paintEvent(QPaintEvent
*paintEvent
)
31 QPainter
painter(this);
32 painter
.drawPixmap(0, 0, *m_pixmap
);
35 QToolWindowDragHandlerDropTargets::QToolWindowDragHandlerDropTargets(QToolWindowManager
* manager
)
37 m_targets
.append(new QToolWindowDropTarget(manager
->config().value(QTWM_DROPTARGET_COMBINE
, ":/QtDockLibrary/gfx/base_window.png").toString(), QToolWindowAreaReference::Combine
));
38 m_targets
.append(new QToolWindowDropTarget(manager
->config().value(QTWM_DROPTARGET_TOP
, ":/QtDockLibrary/gfx/dock_top.png").toString(), QToolWindowAreaReference::Top
));
39 m_targets
.append(new QToolWindowDropTarget(manager
->config().value(QTWM_DROPTARGET_BOTTOM
, ":/QtDockLibrary/gfx/dock_bottom.png").toString(), QToolWindowAreaReference::Bottom
));
40 m_targets
.append(new QToolWindowDropTarget(manager
->config().value(QTWM_DROPTARGET_LEFT
, ":/QtDockLibrary/gfx/dock_left.png").toString(), QToolWindowAreaReference::Left
));
41 m_targets
.append(new QToolWindowDropTarget(manager
->config().value(QTWM_DROPTARGET_RIGHT
, ":/QtDockLibrary/gfx/dock_right.png").toString(), QToolWindowAreaReference::Right
));
42 m_targets
.append(new QToolWindowDropTarget(manager
->config().value(QTWM_DROPTARGET_SPLIT_LEFT
, ":/QtDockLibrary/gfx/vsplit_left.png").toString(), QToolWindowAreaReference::VSplitLeft
));
43 m_targets
.append(new QToolWindowDropTarget(manager
->config().value(QTWM_DROPTARGET_SPLIT_RIGHT
, ":/QtDockLibrary/gfx/vsplit_right.png").toString(), QToolWindowAreaReference::VSplitRight
));
44 m_targets
.append(new QToolWindowDropTarget(manager
->config().value(QTWM_DROPTARGET_SPLIT_TOP
, ":/QtDockLibrary/gfx/hsplit_top.png").toString(), QToolWindowAreaReference::HSplitTop
));
45 m_targets
.append(new QToolWindowDropTarget(manager
->config().value(QTWM_DROPTARGET_SPLIT_BOTTOM
, ":/QtDockLibrary/gfx/hsplit_bottom.png").toString(), QToolWindowAreaReference::HSplitBottom
));
50 QToolWindowDragHandlerDropTargets::~QToolWindowDragHandlerDropTargets()
52 for (QToolWindowDropTarget
* target
: m_targets
)
58 void QToolWindowDragHandlerDropTargets::startDrag()
62 void QToolWindowDragHandlerDropTargets::switchedArea(IToolWindowArea
* lastArea
, IToolWindowArea
* newArea
)
74 QToolWindowAreaTarget
QToolWindowDragHandlerDropTargets::getTargetFromPosition(IToolWindowArea
* area
) const
76 QToolWindowAreaTarget
result(area
, QToolWindowAreaReference::Floating
, -1);
79 QWidget
* widgetUnderMouse
= qApp
->widgetAt(QCursor::pos());
80 if (isHandlerWidget(widgetUnderMouse
))
82 result
.reference
= static_cast<QToolWindowDropTarget
*>(widgetUnderMouse
)->target();
85 if (area
&& result
.reference
== QToolWindowAreaReference::Floating
)
87 QPoint tabPos
= area
->mapCombineDropAreaFromGlobal(QCursor::pos());
88 if (area
->combineAreaRect().contains(tabPos
))
90 result
.reference
= QToolWindowAreaReference::Combine
;
91 result
.index
= area
->subWidgetAt(tabPos
);
99 bool QToolWindowDragHandlerDropTargets::isHandlerWidget(QWidget
* widget
) const
101 return m_targets
.contains(static_cast<QToolWindowDropTarget
*>(widget
));
104 QToolWindowAreaTarget
QToolWindowDragHandlerDropTargets::finishDrag(QList
<QWidget
*> toolWindows
, IToolWindowArea
* source
, IToolWindowArea
* destination
)
106 QToolWindowAreaTarget
result(destination
, QToolWindowAreaReference::Floating
, -1);
109 QWidget
* widgetUnderMouse
= qApp
->widgetAt(QCursor::pos());
110 if (isHandlerWidget(widgetUnderMouse
))
112 result
.reference
= static_cast<QToolWindowDropTarget
*>(widgetUnderMouse
)->target();
115 if (result
.reference
== QToolWindowAreaReference::Floating
)
117 QPoint tabPos
= destination
->mapCombineDropAreaFromGlobal(QCursor::pos());
118 if (destination
->combineAreaRect().contains(tabPos
))
120 result
.reference
= QToolWindowAreaReference::Combine
;
121 result
.index
= destination
->subWidgetAt(tabPos
);
122 if (destination
== source
&& result
.index
> destination
->indexOf(toolWindows
[0]))
133 IToolWindowDragHandler::SplitSizes
QToolWindowDragHandlerDropTargets::getSplitSizes(int originalSize
) const
135 IToolWindowDragHandler::SplitSizes result
;
136 result
.oldSize
= originalSize
/ 2;
137 result
.newSize
= originalSize
- result
.oldSize
;
141 void QToolWindowDragHandlerDropTargets::hideTargets()
143 for (QToolWindowDropTarget
* target
: m_targets
)
149 void QToolWindowDragHandlerDropTargets::showTargets(IToolWindowArea
* area
)
151 QPoint areaCenter
= area
->mapToGlobal(QPoint(area
->geometry().width() / 2, area
->geometry().height() / 2));
153 QWidget
* wrapper
= findClosestParent
<IToolWindowWrapper
*>(area
->getWidget())->getContents();
154 QPoint wrapperCenter
= wrapper
->mapToGlobal(QPoint(wrapper
->geometry().width() / 2, wrapper
->geometry().height() / 2));
156 for (QToolWindowDropTarget
* target
: m_targets
)
160 // Position drop targets
161 switch (target
->target())
163 case QToolWindowAreaReference::Combine
:
166 case QToolWindowAreaReference::Top
:
167 newPos
= wrapperCenter
- QPoint(0, (wrapper
->geometry().height() / 2) - target
->height() - DROPTARGET_PADDING
);
169 case QToolWindowAreaReference::Bottom
:
170 newPos
= wrapperCenter
+ QPoint(0, (wrapper
->geometry().height() / 2) - target
->height() - DROPTARGET_PADDING
);
172 case QToolWindowAreaReference::Left
:
173 newPos
= wrapperCenter
- QPoint((wrapper
->geometry().width() / 2) - target
->width() - DROPTARGET_PADDING
, 0);
175 case QToolWindowAreaReference::Right
:
176 newPos
= wrapperCenter
+ QPoint((wrapper
->geometry().width() / 2) - target
->width() - DROPTARGET_PADDING
, 0);
178 case QToolWindowAreaReference::HSplitTop
:
179 newPos
= areaCenter
- QPoint(0, target
->height() + DROPTARGET_PADDING
);
181 case QToolWindowAreaReference::HSplitBottom
:
182 newPos
= areaCenter
+ QPoint(0, target
->height() + DROPTARGET_PADDING
);
184 case QToolWindowAreaReference::VSplitLeft
:
185 newPos
= areaCenter
- QPoint(target
->width() + DROPTARGET_PADDING
, 0);
187 case QToolWindowAreaReference::VSplitRight
:
188 newPos
= areaCenter
+ QPoint(target
->width() + DROPTARGET_PADDING
, 0);
192 newPos
= target
->pos();
196 newPos
-= QPoint(target
->geometry().width() / 2, target
->geometry().height() / 2);
197 target
->move(newPos
);