Skip to content

Commit a13ed7e

Browse files
committed
Add AutoHideDragNDrop example
1 parent 8cfa5c8 commit a13ed7e

File tree

12 files changed

+425
-3
lines changed

12 files changed

+425
-3
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
cmake_minimum_required(VERSION 3.5)
2-
2+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
33
if (POLICY CMP0091)
44
cmake_policy(SET CMP0091 NEW)
55
endif (POLICY CMP0091)

doc/user-guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ If this flag is set (disabled by default), then holding a dragging cursor hover
712712
713713
![AutoHideOpenOnDragHover](cfg_flag_AutoHideOpenOnDragHover.gif)
714714
715-
Said dock must be set to accept drops to hide when cursor leaves its scope.
715+
Said dock must be set to accept drops to hide when cursor leaves its scope. See `AutoHideDragNDropExample` for more details.
716716
717717
## DockWidget Feature Flags
718718

examples/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ add_subdirectory(sidebar)
66
add_subdirectory(deleteonclose)
77
add_subdirectory(centralwidget)
88
add_subdirectory(autohide)
9+
add_subdirectory(autohidedragndrop)
910
add_subdirectory(emptydockarea)
10-
add_subdirectory(dockindock)
11+
add_subdirectory(dockindock)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
project(ads_example_autohide_dragndrop VERSION ${VERSION_SHORT})
3+
find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED)
4+
find_package(Qt${QT_VERSION_MAJOR} 5.5 COMPONENTS Core Gui Widgets REQUIRED)
5+
set(CMAKE_INCLUDE_CURRENT_DIR ON)
6+
add_executable(AutoHideDragNDropExample WIN32
7+
main.cpp
8+
mainwindow.cpp
9+
mainwindow.ui
10+
droppableitem.cpp
11+
)
12+
target_include_directories(AutoHideDragNDropExample PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../../src")
13+
target_link_libraries(AutoHideDragNDropExample PRIVATE qt${QT_VERSION_MAJOR}advanceddocking)
14+
target_link_libraries(AutoHideDragNDropExample PUBLIC Qt${QT_VERSION_MAJOR}::Core
15+
Qt${QT_VERSION_MAJOR}::Gui
16+
Qt${QT_VERSION_MAJOR}::Widgets)
17+
set_target_properties(AutoHideDragNDropExample PROPERTIES
18+
AUTOMOC ON
19+
AUTORCC ON
20+
AUTOUIC ON
21+
CXX_STANDARD 14
22+
CXX_STANDARD_REQUIRED ON
23+
CXX_EXTENSIONS OFF
24+
VERSION ${VERSION_SHORT}
25+
EXPORT_NAME "Qt Advanced Docking System Auto Hide With Drag N Drop Example"
26+
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${ads_PlatformDir}/lib"
27+
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${ads_PlatformDir}/lib"
28+
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${ads_PlatformDir}/bin"
29+
)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
ADS_OUT_ROOT = $${OUT_PWD}/../..
2+
3+
QT += core gui widgets
4+
5+
TARGET = AutoHideDragNDropExample
6+
DESTDIR = $${ADS_OUT_ROOT}/lib
7+
TEMPLATE = app
8+
CONFIG += c++14
9+
CONFIG += debug_and_release
10+
adsBuildStatic {
11+
DEFINES += ADS_STATIC
12+
}
13+
14+
# The following define makes your compiler emit warnings if you use
15+
# any Qt feature that has been marked deprecated (the exact warnings
16+
# depend on your compiler). Please consult the documentation of the
17+
# deprecated API in order to know how to port your code away from it.
18+
DEFINES += QT_DEPRECATED_WARNINGS
19+
20+
SOURCES += \
21+
main.cpp \
22+
mainwindow.cpp \
23+
droppableitem.cpp
24+
25+
HEADERS += \
26+
mainwindow.h
27+
droppableitem.h
28+
29+
FORMS += \
30+
mainwindow.ui
31+
32+
LIBS += -L$${ADS_OUT_ROOT}/lib
33+
include(../../ads.pri)
34+
INCLUDEPATH += ../../src
35+
DEPENDPATH += ../../src
36+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "droppableitem.h"
2+
3+
#include <QDragEnterEvent>
4+
#include <QDragLeaveEvent>
5+
#include <QDropEvent>
6+
#include <QMimeData>
7+
#include <qsizepolicy.h>
8+
9+
DroppableItem::DroppableItem(const QString& text, QWidget* parent)
10+
: QPushButton(text, parent)
11+
{
12+
setAcceptDrops(true);
13+
setSizePolicy(QSizePolicy::Policy::Expanding, QSizePolicy::Policy::Expanding);
14+
}
15+
16+
void DroppableItem::dragEnterEvent(QDragEnterEvent* event)
17+
{
18+
if (event->mimeData()->hasText())
19+
{
20+
event->acceptProposedAction();
21+
setCursor(Qt::DragMoveCursor);
22+
}
23+
}
24+
25+
void DroppableItem::dragLeaveEvent(QDragLeaveEvent* event)
26+
{
27+
unsetCursor();
28+
}
29+
30+
void DroppableItem::dropEvent(QDropEvent* event)
31+
{
32+
if (event->mimeData()->hasText())
33+
{
34+
event->acceptProposedAction();
35+
setText(event->mimeData()->text());
36+
}
37+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <QObject>
2+
#include <QPushButton>
3+
4+
class QDragEnterEvent;
5+
class QDragLeaveEvent;
6+
class QDropEvent;
7+
8+
class DroppableItem : public QPushButton
9+
{
10+
Q_OBJECT;
11+
12+
public:
13+
DroppableItem(const QString& text = QString(), QWidget* parent = nullptr);
14+
15+
protected:
16+
void dragEnterEvent(QDragEnterEvent* event) override;
17+
void dragLeaveEvent(QDragLeaveEvent* event) override;
18+
void dropEvent(QDropEvent* event) override;
19+
};

examples/autohidedragndrop/main.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <mainwindow.h>
2+
#include <QApplication>
3+
4+
int main(int argc, char *argv[])
5+
{
6+
QApplication a(argc, argv);
7+
CMainWindow w;
8+
w.show();
9+
return a.exec();
10+
}

examples/autohidedragndrop/main.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import os
2+
import sys
3+
4+
from PyQt5 import uic
5+
from PyQt5.QtCore import Qt, QTimer, QDir, QSignalBlocker
6+
from PyQt5.QtGui import QCloseEvent, QIcon
7+
from PyQt5.QtWidgets import (QApplication, QLabel, QCalendarWidget, QFrame, QTreeView,
8+
QTableWidget, QFileSystemModel, QPlainTextEdit, QToolBar,
9+
QWidgetAction, QComboBox, QAction, QSizePolicy, QInputDialog)
10+
11+
import PyQtAds as QtAds
12+
13+
UI_FILE = os.path.join(os.path.dirname(__file__), 'mainwindow.ui')
14+
MainWindowUI, MainWindowBase = uic.loadUiType(UI_FILE)
15+
16+
class MainWindow(MainWindowUI, MainWindowBase):
17+
18+
def __init__(self, parent=None):
19+
super().__init__(parent)
20+
21+
self.setupUi(self)
22+
23+
QtAds.CDockManager.setConfigFlag(QtAds.CDockManager.OpaqueSplitterResize, True)
24+
QtAds.CDockManager.setConfigFlag(QtAds.CDockManager.XmlCompressionEnabled, False)
25+
QtAds.CDockManager.setConfigFlag(QtAds.CDockManager.FocusHighlighting, True)
26+
QtAds.CDockManager.setAutoHideConfigFlag(QtAds.CDockManager.AutoHideOpenOnDragHover, True);
27+
self.dock_manager = QtAds.CDockManager(self)
28+
29+
# Set central widget
30+
text_edit = QPlainTextEdit()
31+
text_edit.setPlaceholderText("This is the central editor. Enter your text here.")
32+
central_dock_widget = QtAds.CDockWidget("CentralWidget")
33+
central_dock_widget.setWidget(text_edit)
34+
central_dock_area = self.dock_manager.setCentralWidget(central_dock_widget)
35+
central_dock_area.setAllowedAreas(QtAds.DockWidgetArea.OuterDockAreas)
36+
37+
38+
droppable_item = DroppableItem("Drop text here.")
39+
drop_dock_widget = QtAds.CDockWidget("Tab")
40+
drop_dock_widget.setWidget(droppable_item)
41+
drop_dock_widget.setMinimumSizeHintMode(QtAds.CDockWidget.MinimumSizeHintFromDockWidget)
42+
drop_dock_widget.setMinimumSize(200, 150)
43+
drop_dock_widget.setAcceptDrops(True)
44+
drop_area = self.dock_manager.addDockWidget(QtAds.DockWidgetArea.LeftDockWidgetArea, drop_dock_widget)
45+
drop_area.setAcceptDrops(True)
46+
self.menuView.addAction(drop_dock_widget.toggleViewAction())
47+
48+
self.create_perspective_ui()
49+
50+
def create_perspective_ui(self):
51+
save_perspective_action = QAction("Create Perspective", self)
52+
save_perspective_action.triggered.connect(self.save_perspective)
53+
perspective_list_action = QWidgetAction(self)
54+
self.perspective_combobox = QComboBox(self)
55+
self.perspective_combobox.setSizeAdjustPolicy(QComboBox.AdjustToContents)
56+
self.perspective_combobox.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
57+
self.perspective_combobox.activated[str].connect(self.dock_manager.openPerspective)
58+
perspective_list_action.setDefaultWidget(self.perspective_combobox)
59+
self.toolBar.addSeparator()
60+
self.toolBar.addAction(perspective_list_action)
61+
self.toolBar.addAction(save_perspective_action)
62+
63+
def save_perspective(self):
64+
perspective_name, ok = QInputDialog.getText(self, "Save Perspective", "Enter Unique name:")
65+
if not ok or not perspective_name:
66+
return
67+
68+
self.dock_manager.addPerspective(perspective_name)
69+
blocker = QSignalBlocker(self.perspective_combobox)
70+
self.perspective_combobox.clear()
71+
self.perspective_combobox.addItems(self.dock_manager.perspectiveNames())
72+
self.perspective_combobox.setCurrentText(perspective_name)
73+
74+
def closeEvent(self, event: QCloseEvent):
75+
self.dock_manager.deleteLater()
76+
super().closeEvent(event)
77+
78+
79+
if __name__ == '__main__':
80+
app = QApplication(sys.argv)
81+
82+
w = MainWindow()
83+
w.show()
84+
app.exec_()
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#include "mainwindow.h"
2+
#include "droppableitem.h"
3+
4+
#include "ui_mainwindow.h"
5+
6+
#include <QWidgetAction>
7+
#include <QFileSystemModel>
8+
#include <QTableWidget>
9+
#include <QHBoxLayout>
10+
#include <QInputDialog>
11+
#include <QFileDialog>
12+
#include <QSettings>
13+
#include <QPlainTextEdit>
14+
#include <QToolBar>
15+
16+
#include "AutoHideDockContainer.h"
17+
#include "DockAreaWidget.h"
18+
#include "DockAreaTitleBar.h"
19+
20+
using namespace ads;
21+
22+
CMainWindow::CMainWindow(QWidget *parent)
23+
: QMainWindow(parent)
24+
, ui(new Ui::CMainWindow)
25+
{
26+
ui->setupUi(this);
27+
CDockManager::setConfigFlag(CDockManager::OpaqueSplitterResize, true);
28+
CDockManager::setConfigFlag(CDockManager::XmlCompressionEnabled, false);
29+
CDockManager::setConfigFlag(CDockManager::FocusHighlighting, true);
30+
CDockManager::setAutoHideConfigFlags(CDockManager::DefaultAutoHideConfig);
31+
CDockManager::setAutoHideConfigFlag(CDockManager::AutoHideOpenOnDragHover, true);
32+
DockManager = new CDockManager(this);
33+
34+
// Set central widget
35+
QPlainTextEdit* w = new QPlainTextEdit();
36+
w->setPlaceholderText("This is the central editor. Enter your text here.");
37+
CDockWidget* CentralDockWidget = new CDockWidget("CentralWidget");
38+
CentralDockWidget->setWidget(w);
39+
auto* CentralDockArea = DockManager->setCentralWidget(CentralDockWidget);
40+
CentralDockArea->setAllowedAreas(DockWidgetArea::OuterDockAreas);
41+
42+
DroppableItem* droppableItem = new DroppableItem("Drop text here.");
43+
CDockWidget* dropDockWidget = new CDockWidget("Tab");
44+
dropDockWidget->setWidget(droppableItem);
45+
dropDockWidget->setMinimumSizeHintMode(CDockWidget::MinimumSizeHintFromDockWidget);
46+
dropDockWidget->setMinimumSize(200,150);
47+
dropDockWidget->setAcceptDrops(true);
48+
const auto autoHideContainer = DockManager->addAutoHideDockWidget(SideBarLocation::SideBarLeft, dropDockWidget);
49+
autoHideContainer->setSize(480);
50+
autoHideContainer->setAcceptDrops(true);
51+
ui->menuView->addAction(dropDockWidget->toggleViewAction());
52+
53+
QTableWidget* propertiesTable = new QTableWidget();
54+
propertiesTable->setColumnCount(3);
55+
propertiesTable->setRowCount(10);
56+
CDockWidget* PropertiesDockWidget = new CDockWidget("Properties");
57+
PropertiesDockWidget->setWidget(propertiesTable);
58+
PropertiesDockWidget->setMinimumSizeHintMode(CDockWidget::MinimumSizeHintFromDockWidget);
59+
PropertiesDockWidget->resize(250, 150);
60+
PropertiesDockWidget->setMinimumSize(200,150);
61+
DockManager->addDockWidget(DockWidgetArea::RightDockWidgetArea, PropertiesDockWidget, CentralDockArea);
62+
ui->menuView->addAction(PropertiesDockWidget->toggleViewAction());
63+
64+
createPerspectiveUi();
65+
}
66+
67+
CMainWindow::~CMainWindow()
68+
{
69+
delete ui;
70+
}
71+
72+
73+
void CMainWindow::createPerspectiveUi()
74+
{
75+
SavePerspectiveAction = new QAction("Create Perspective", this);
76+
connect(SavePerspectiveAction, SIGNAL(triggered()), SLOT(savePerspective()));
77+
PerspectiveListAction = new QWidgetAction(this);
78+
PerspectiveComboBox = new QComboBox(this);
79+
PerspectiveComboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
80+
PerspectiveComboBox->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
81+
connect(PerspectiveComboBox, SIGNAL(currentTextChanged(const QString&)),
82+
DockManager, SLOT(openPerspective(const QString&)));
83+
PerspectiveListAction->setDefaultWidget(PerspectiveComboBox);
84+
ui->toolBar->addSeparator();
85+
ui->toolBar->addAction(PerspectiveListAction);
86+
ui->toolBar->addAction(SavePerspectiveAction);
87+
}
88+
89+
90+
void CMainWindow::savePerspective()
91+
{
92+
QString PerspectiveName = QInputDialog::getText(this, "Save Perspective", "Enter unique name:");
93+
if (PerspectiveName.isEmpty())
94+
{
95+
return;
96+
}
97+
98+
DockManager->addPerspective(PerspectiveName);
99+
QSignalBlocker Blocker(PerspectiveComboBox);
100+
PerspectiveComboBox->clear();
101+
PerspectiveComboBox->addItems(DockManager->perspectiveNames());
102+
PerspectiveComboBox->setCurrentText(PerspectiveName);
103+
}
104+
105+
106+
//============================================================================
107+
void CMainWindow::closeEvent(QCloseEvent* event)
108+
{
109+
// Delete dock manager here to delete all floating widgets. This ensures
110+
// that all top level windows of the dock manager are properly closed
111+
DockManager->deleteLater();
112+
QMainWindow::closeEvent(event);
113+
}
114+
115+
116+

0 commit comments

Comments
 (0)