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 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
|
//============================================================================
/// \file FloatingDragPreview.cpp
/// \author Uwe Kindler
/// \date 26.11.2019
/// \brief Implementation of CFloatingDragPreview
//============================================================================
//============================================================================
// INCLUDES
//============================================================================
#include <AutoHideDockContainer.h>
#include "FloatingDragPreview.h"
#include <iostream>
#include <QEvent>
#include <QApplication>
#include <QPainter>
#include <QKeyEvent>
#include "DockWidget.h"
#include "DockAreaWidget.h"
#include "DockManager.h"
#include "DockContainerWidget.h"
#include "DockOverlay.h"
#include "AutoHideDockContainer.h"
#include "ads_globals.h"
namespace ads
{
/**
* Private data class (pimpl)
*/
struct FloatingDragPreviewPrivate
{
CFloatingDragPreview *_this;
QWidget* Content;
CDockWidget::DockWidgetFeatures ContentFeatures;
CDockAreaWidget* ContentSourceArea = nullptr;
QPoint DragStartMousePosition;
CDockManager* DockManager;
CDockContainerWidget *DropContainer = nullptr;
qreal WindowOpacity;
bool Hidden = false;
QPixmap ContentPreviewPixmap;
bool Canceled = false;
/**
* Private data constructor
*/
FloatingDragPreviewPrivate(CFloatingDragPreview *_public);
void updateDropOverlays(const QPoint &GlobalPos);
void setHidden(bool Value)
{
Hidden = Value;
_this->update();
}
/**
* Cancel dragging and emit the draggingCanceled event
*/
void cancelDragging()
{
Canceled = true;
Q_EMIT _this->draggingCanceled();
DockManager->containerOverlay()->hideOverlay();
DockManager->dockAreaOverlay()->hideOverlay();
_this->close();
}
/**
* Creates the real floating widget in case the mouse is released outside
* outside of any drop area
*/
void createFloatingWidget();
/**
* Returns true, if the content is floatable
*/
bool isContentFloatable() const
{
return this->ContentFeatures.testFlag(CDockWidget::DockWidgetFloatable);
}
/**
* Returns true, if the content is pinnable
*/
bool isContentPinnable() const
{
return this->ContentFeatures.testFlag(CDockWidget::DockWidgetPinnable);
}
/**
* Returns the content features
*/
CDockWidget::DockWidgetFeatures contentFeatures() const
{
CDockWidget* DockWidget = qobject_cast<CDockWidget*>(Content);
if (DockWidget)
{
return DockWidget->features();
}
CDockAreaWidget* DockArea = qobject_cast<CDockAreaWidget*>(Content);
if (DockArea)
{
return DockArea->features();
}
return CDockWidget::DockWidgetFeatures();
}
};
// struct LedArrayPanelPrivate
//============================================================================
void FloatingDragPreviewPrivate::updateDropOverlays(const QPoint &GlobalPos)
{
if (!_this->isVisible() || !DockManager)
{
return;
}
auto Containers = DockManager->dockContainers();
CDockContainerWidget *TopContainer = nullptr;
for (auto ContainerWidget : Containers)
{
if (!ContainerWidget->isVisible())
{
continue;
}
QPoint MappedPos = ContainerWidget->mapFromGlobal(GlobalPos);
if (ContainerWidget->rect().contains(MappedPos))
{
if (!TopContainer || ContainerWidget->isInFrontOf(TopContainer))
{
TopContainer = ContainerWidget;
}
}
}
DropContainer = TopContainer;
auto ContainerOverlay = DockManager->containerOverlay();
auto DockAreaOverlay = DockManager->dockAreaOverlay();
if (!TopContainer)
{
ContainerOverlay->hideOverlay();
DockAreaOverlay->hideOverlay();
if (CDockManager::testConfigFlag(CDockManager::DragPreviewIsDynamic))
{
setHidden(false);
}
return;
}
auto DockDropArea = DockAreaOverlay->dropAreaUnderCursor();
auto ContainerDropArea = ContainerOverlay->dropAreaUnderCursor();
int VisibleDockAreas = TopContainer->visibleDockAreaCount();
// Include the overlay widget we're dragging as a visible widget
auto dockAreaWidget = qobject_cast<CDockAreaWidget*>(Content);
if (dockAreaWidget && dockAreaWidget->isAutoHide())
{
VisibleDockAreas++;
}
DockWidgetAreas AllowedContainerAreas = (VisibleDockAreas > 1) ? OuterDockAreas : AllDockAreas;
//ContainerOverlay->enableDropPreview(ContainerDropArea != InvalidDockWidgetArea);
auto DockArea = TopContainer->dockAreaAt(GlobalPos);
// If the dock container contains only one single DockArea, then we need
// to respect the allowed areas - only the center area is relevant here because
// all other allowed areas are from the container
if (VisibleDockAreas == 1 && DockArea)
{
AllowedContainerAreas.setFlag(CenterDockWidgetArea, DockArea->allowedAreas().testFlag(CenterDockWidgetArea));
}
if (isContentPinnable())
{
AllowedContainerAreas |= AutoHideDockAreas;
}
ContainerOverlay->setAllowedAreas(AllowedContainerAreas);
ContainerOverlay->enableDropPreview(ContainerDropArea != InvalidDockWidgetArea);
if (DockArea && DockArea->isVisible() && VisibleDockAreas >= 0 && DockArea != ContentSourceArea)
{
DockAreaOverlay->enableDropPreview(true);
DockAreaOverlay->setAllowedAreas( (VisibleDockAreas == 1) ? NoDockWidgetArea : DockArea->allowedAreas());
DockWidgetArea Area = DockAreaOverlay->showOverlay(DockArea);
// A CenterDockWidgetArea for the dockAreaOverlay() indicates that
// the mouse is in the title bar. If the ContainerArea is valid
// then we ignore the dock area of the dockAreaOverlay() and disable
// the drop preview
if ((Area == CenterDockWidgetArea) && (ContainerDropArea != InvalidDockWidgetArea))
{
DockAreaOverlay->enableDropPreview(false);
ContainerOverlay->enableDropPreview(true);
}
else
{
ContainerOverlay->enableDropPreview(InvalidDockWidgetArea == Area);
}
ContainerOverlay->showOverlay(TopContainer);
}
else
{
DockAreaOverlay->hideOverlay();
// If there is only one single visible dock area in a container, then
// it does not make sense to show a dock overlay because the dock area
// would be removed and inserted at the same position. Only auto hide
// area is allowed
if (VisibleDockAreas == 1)
{
ContainerOverlay->setAllowedAreas(AutoHideDockAreas);
}
ContainerOverlay->showOverlay(TopContainer);
if (DockArea == ContentSourceArea && InvalidDockWidgetArea == ContainerDropArea)
{
DropContainer = nullptr;
}
}
if (CDockManager::testConfigFlag(CDockManager::DragPreviewIsDynamic))
{
setHidden(DockDropArea != InvalidDockWidgetArea || ContainerDropArea != InvalidDockWidgetArea);
}
}
//============================================================================
FloatingDragPreviewPrivate::FloatingDragPreviewPrivate(CFloatingDragPreview *_public) :
_this(_public)
{
}
//============================================================================
void FloatingDragPreviewPrivate::createFloatingWidget()
{
CDockWidget* DockWidget = qobject_cast<CDockWidget*>(Content);
CDockAreaWidget* DockArea = qobject_cast<CDockAreaWidget*>(Content);
CFloatingDockContainer* FloatingWidget = nullptr;
if (DockWidget && DockWidget->features().testFlag(CDockWidget::DockWidgetFloatable))
{
FloatingWidget = new CFloatingDockContainer(DockWidget);
}
else if (DockArea && DockArea->features().testFlag(CDockWidget::DockWidgetFloatable))
{
FloatingWidget = new CFloatingDockContainer(DockArea);
}
if (FloatingWidget)
{
FloatingWidget->setGeometry(_this->geometry());
FloatingWidget->show();
if (!CDockManager::testConfigFlag(CDockManager::DragPreviewHasWindowFrame))
{
QApplication::processEvents();
int FrameHeight = FloatingWidget->frameGeometry().height() - FloatingWidget->geometry().height();
QRect FixedGeometry = _this->geometry();
FixedGeometry.adjust(0, FrameHeight, 0, 0);
FloatingWidget->setGeometry(FixedGeometry);
}
}
}
//============================================================================
CFloatingDragPreview::CFloatingDragPreview(QWidget* Content, QWidget* parent) :
QWidget(parent),
d(new FloatingDragPreviewPrivate(this))
{
d->Content = Content;
d->ContentFeatures = d->contentFeatures();
setAttribute(Qt::WA_DeleteOnClose);
if (CDockManager::testConfigFlag(CDockManager::DragPreviewHasWindowFrame))
{
setWindowFlags(
Qt::Window | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint);
}
else
{
setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);
setAttribute(Qt::WA_NoSystemBackground);
setAttribute(Qt::WA_TranslucentBackground);
}
#if defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)
auto Flags = windowFlags();
Flags |= Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint;
setWindowFlags(Flags);
#endif
// Create a static image of the widget that should get undocked
// This is like some kind preview image like it is uses in drag and drop
// operations
if (CDockManager::testConfigFlag(CDockManager::DragPreviewShowsContentPixmap))
{
d->ContentPreviewPixmap = QPixmap(Content->size());
Content->render(&d->ContentPreviewPixmap);
}
connect(qApp, SIGNAL(applicationStateChanged(Qt::ApplicationState)),
SLOT(onApplicationStateChanged(Qt::ApplicationState)));
// The only safe way to receive escape key presses is to install an event
// filter for the application object
qApp->installEventFilter(this);
}
//============================================================================
CFloatingDragPreview::CFloatingDragPreview(CDockWidget* Content)
: CFloatingDragPreview((QWidget*)Content, Content->dockManager())
{
d->DockManager = Content->dockManager();
if (Content->dockAreaWidget()->openDockWidgetsCount() == 1)
{
d->ContentSourceArea = Content->dockAreaWidget();
}
setWindowTitle(Content->windowTitle());
}
//============================================================================
CFloatingDragPreview::CFloatingDragPreview(CDockAreaWidget* Content)
: CFloatingDragPreview((QWidget*)Content, Content->dockManager())
{
d->DockManager = Content->dockManager();
d->ContentSourceArea = Content;
setWindowTitle(Content->currentDockWidget()->windowTitle());
}
//============================================================================
CFloatingDragPreview::~CFloatingDragPreview()
{
delete d;
}
//============================================================================
void CFloatingDragPreview::moveFloating()
{
int BorderSize = (frameSize().width() - size().width()) / 2;
const QPoint moveToPos = QCursor::pos() - d->DragStartMousePosition
- QPoint(BorderSize, 0);
move(moveToPos);
d->updateDropOverlays(QCursor::pos());
}
//============================================================================
void CFloatingDragPreview::startFloating(const QPoint &DragStartMousePos,
const QSize &Size, eDragState DragState, QWidget *MouseEventHandler)
{
Q_UNUSED(MouseEventHandler)
Q_UNUSED(DragState)
resize(Size);
d->DragStartMousePosition = DragStartMousePos;
moveFloating();
show();
}
//============================================================================
void CFloatingDragPreview::finishDragging()
{
ADS_PRINT("CFloatingDragPreview::finishDragging");
auto DockDropArea = d->DockManager->dockAreaOverlay()->visibleDropAreaUnderCursor();
auto ContainerDropArea = d->DockManager->containerOverlay()->visibleDropAreaUnderCursor();
bool ValidDropArea = (DockDropArea != InvalidDockWidgetArea) || (ContainerDropArea != InvalidDockWidgetArea);
// Non floatable auto hide widgets should stay in its current auto hide
// state if they are dragged into a floating window
if (ValidDropArea || d->isContentFloatable())
{
cleanupAutoHideContainerWidget(ContainerDropArea);
}
if (!d->DropContainer)
{
d->createFloatingWidget();
}
else if (DockDropArea != InvalidDockWidgetArea)
{
d->DropContainer->dropWidget(d->Content, DockDropArea, d->DropContainer->dockAreaAt(QCursor::pos()),
d->DockManager->dockAreaOverlay()->tabIndexUnderCursor());
}
else if (ContainerDropArea != InvalidDockWidgetArea)
{
CDockAreaWidget* DockArea = nullptr;
// If there is only one single dock area, and we drop into the center
// then we tabify the dropped widget into the only visible dock area
if (d->DropContainer->visibleDockAreaCount() <= 1 && CenterDockWidgetArea == ContainerDropArea)
{
DockArea = d->DropContainer->dockAreaAt(QCursor::pos());
}
d->DropContainer->dropWidget(d->Content, ContainerDropArea, DockArea,
d->DockManager->containerOverlay()->tabIndexUnderCursor());
}
else
{
d->createFloatingWidget();
}
this->close();
d->DockManager->containerOverlay()->hideOverlay();
d->DockManager->dockAreaOverlay()->hideOverlay();
}
//============================================================================
void CFloatingDragPreview::cleanupAutoHideContainerWidget(DockWidgetArea ContainerDropArea)
{
auto DroppedDockWidget = qobject_cast<CDockWidget*>(d->Content);
auto DroppedArea = qobject_cast<CDockAreaWidget*>(d->Content);
auto AutoHideContainer = DroppedDockWidget
? DroppedDockWidget->autoHideDockContainer()
: DroppedArea->autoHideDockContainer();
if (!AutoHideContainer)
{
return;
}
// If the dropped widget is already an auto hide widget and if it is moved
// to a new side bar location in the same container, then we do not need
// to cleanup
if (ads::internal::isSideBarArea(ContainerDropArea)
&& (d->DropContainer == AutoHideContainer->dockContainer()))
{
return;
}
AutoHideContainer->cleanupAndDelete();
}
//============================================================================
void CFloatingDragPreview::paintEvent(QPaintEvent* event)
{
Q_UNUSED(event);
if (d->Hidden)
{
return;
}
QPainter painter(this);
painter.setOpacity(0.6);
if (CDockManager::testConfigFlag(CDockManager::DragPreviewShowsContentPixmap))
{
painter.drawPixmap(QPoint(0, 0), d->ContentPreviewPixmap);
}
// If we do not have a window frame then we paint a QRubberBand like
// frameless window
if (!CDockManager::testConfigFlag(CDockManager::DragPreviewHasWindowFrame))
{
QColor Color = palette().color(QPalette::Active, QPalette::Highlight);
QPen Pen = painter.pen();
Pen.setColor(Color.darker(120));
Pen.setStyle(Qt::SolidLine);
Pen.setWidth(1);
Pen.setCosmetic(true);
painter.setPen(Pen);
Color = Color.lighter(130);
Color.setAlpha(64);
painter.setBrush(Color);
painter.drawRect(rect().adjusted(0, 0, -1, -1));
}
}
//============================================================================
void CFloatingDragPreview::onApplicationStateChanged(Qt::ApplicationState state)
{
if (state != Qt::ApplicationActive)
{
disconnect(qApp, SIGNAL(applicationStateChanged(Qt::ApplicationState)),
this, SLOT(onApplicationStateChanged(Qt::ApplicationState)));
d->cancelDragging();
}
}
//============================================================================
bool CFloatingDragPreview::eventFilter(QObject *watched, QEvent *event)
{
Q_UNUSED(watched);
if (!d->Canceled && event->type() == QEvent::KeyPress)
{
QKeyEvent* e = static_cast<QKeyEvent*>(event);
if (e->key() == Qt::Key_Escape)
{
watched->removeEventFilter(this);
d->cancelDragging();
}
}
return false;
}
} // namespace ads
//---------------------------------------------------------------------------
// EOF FloatingDragPreview.cpp
|