!I (1670414, 1670415, 1670416, 1670424, 1670431):
[CRYENGINE.git] / Code / Sandbox / Libs / CryQt / QToolWindowManager / QToolWindowDragHandlerDropTargets.cpp
blob7c332604f557eed49ab8690b36169bca33522954
1 // Copyright 2001-2018 Crytek GmbH / Crytek Group. All rights reserved.
3 #include "StdAfx.h"
5 #include "QToolWindowDragHandlerDropTargets.h"
6 #include <QPainter>
8 #define DROPTARGET_PADDING 4
10 QToolWindowDropTarget::QToolWindowDropTarget(QString imagePath, QToolWindowAreaReference areaReference)
11 : QWidget(nullptr)
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()
25 if (m_pixmap)
26 delete m_pixmap;
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));
47 hideTargets();
50 QToolWindowDragHandlerDropTargets::~QToolWindowDragHandlerDropTargets()
52 for (QToolWindowDropTarget* target : m_targets)
54 delete target;
58 void QToolWindowDragHandlerDropTargets::startDrag()
62 void QToolWindowDragHandlerDropTargets::switchedArea(IToolWindowArea* lastArea, IToolWindowArea* newArea)
64 if (newArea)
66 showTargets(newArea);
68 else
70 hideTargets();
74 QToolWindowAreaTarget QToolWindowDragHandlerDropTargets::getTargetFromPosition(IToolWindowArea* area) const
76 QToolWindowAreaTarget result(area, QToolWindowAreaReference::Floating, -1);
77 if (area)
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);
95 return result;
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);
107 if (destination)
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]))
124 result.index--;
129 hideTargets();
130 return result;
133 IToolWindowDragHandler::SplitSizes QToolWindowDragHandlerDropTargets::getSplitSizes(int originalSize) const
135 IToolWindowDragHandler::SplitSizes result;
136 result.oldSize = originalSize / 2;
137 result.newSize = originalSize - result.oldSize;
138 return result;
141 void QToolWindowDragHandlerDropTargets::hideTargets()
143 for (QToolWindowDropTarget* target : m_targets)
145 target->hide();
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)
158 QPoint newPos;
160 // Position drop targets
161 switch (target->target())
163 case QToolWindowAreaReference::Combine:
164 newPos = areaCenter;
165 break;
166 case QToolWindowAreaReference::Top:
167 newPos = wrapperCenter - QPoint(0, (wrapper->geometry().height() / 2) - target->height() - DROPTARGET_PADDING);
168 break;
169 case QToolWindowAreaReference::Bottom:
170 newPos = wrapperCenter + QPoint(0, (wrapper->geometry().height() / 2) - target->height() - DROPTARGET_PADDING);
171 break;
172 case QToolWindowAreaReference::Left:
173 newPos = wrapperCenter - QPoint((wrapper->geometry().width() / 2) - target->width() - DROPTARGET_PADDING, 0);
174 break;
175 case QToolWindowAreaReference::Right:
176 newPos = wrapperCenter + QPoint((wrapper->geometry().width() / 2) - target->width() - DROPTARGET_PADDING, 0);
177 break;
178 case QToolWindowAreaReference::HSplitTop:
179 newPos = areaCenter - QPoint(0, target->height() + DROPTARGET_PADDING);
180 break;
181 case QToolWindowAreaReference::HSplitBottom:
182 newPos = areaCenter + QPoint(0, target->height() + DROPTARGET_PADDING);
183 break;
184 case QToolWindowAreaReference::VSplitLeft:
185 newPos = areaCenter - QPoint(target->width() + DROPTARGET_PADDING, 0);
186 break;
187 case QToolWindowAreaReference::VSplitRight:
188 newPos = areaCenter + QPoint(target->width() + DROPTARGET_PADDING, 0);
189 break;
191 default:
192 newPos = target->pos();
193 break;
196 newPos -= QPoint(target->geometry().width() / 2, target->geometry().height() / 2);
197 target->move(newPos);
198 target->show();
199 target->raise();