Skip to content

Commit be3180d

Browse files
committed
Add dockdepth1 example
1 parent 6179832 commit be3180d

File tree

8 files changed

+362
-1
lines changed

8 files changed

+362
-1
lines changed

examples/dockdepth1/CMakeLists.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
project(ads_example_simple VERSION ${VERSION_SHORT})
3+
find_package(Qt5 5.5 COMPONENTS Core Gui Widgets REQUIRED)
4+
set(CMAKE_INCLUDE_CURRENT_DIR ON)
5+
add_executable(DockDepth1 WIN32
6+
innertabs.cpp
7+
main.cpp
8+
MainWindow.cpp
9+
MainWindow.ui
10+
)
11+
target_include_directories(SimpleExample PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../../src")
12+
target_link_libraries(SimpleExample PRIVATE qtadvanceddocking)
13+
target_link_libraries(SimpleExample PUBLIC Qt5::Core Qt5::Gui Qt5::Widgets)
14+
set_target_properties(SimpleExample PROPERTIES
15+
AUTOMOC ON
16+
AUTORCC ON
17+
AUTOUIC ON
18+
CXX_STANDARD 14
19+
CXX_STANDARD_REQUIRED ON
20+
CXX_EXTENSIONS OFF
21+
VERSION ${VERSION_SHORT}
22+
EXPORT_NAME "Qt Advanced Docking System Simple Example"
23+
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${ads_PlatformDir}/lib"
24+
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${ads_PlatformDir}/lib"
25+
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${ads_PlatformDir}/bin"
26+
)

examples/dockdepth1/MainWindow.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include "../../examples/dockdepth1/MainWindow.h"
2+
#include "../../examples/dockdepth1/innertabs.h"
3+
4+
#include <QLabel>
5+
#include <QMenuBar>
6+
7+
#include "DockAreaWidget.h"
8+
9+
MainWindow::MainWindow(QWidget *parent) :
10+
QMainWindow(parent)
11+
{
12+
resize( 400, 400 );
13+
14+
DockManagerWithInnerTabs* dockManager = new DockManagerWithInnerTabs(this);
15+
16+
dockManager->attachViewMenu( menuBar()->addMenu( "View" ) );
17+
18+
ads::CDockAreaWidget* previousDockWidget = NULL;
19+
for ( int i = 0; i != 3; ++i )
20+
{
21+
// Create example content label - this can be any application specific
22+
// widget
23+
QLabel* l = new QLabel();
24+
l->setWordWrap(true);
25+
l->setAlignment(Qt::AlignTop | Qt::AlignLeft);
26+
l->setText("Lorem ipsum dolor sit amet, consectetuer adipiscing elit. ");
27+
28+
// Create a dock widget with the title Label 1 and set the created label
29+
// as the dock widget content
30+
ads::CDockWidget* DockWidget = new ads::CDockWidget("Label " + QString::number(i));
31+
DockWidget->setWidget(l);
32+
33+
// Add the dock widget to the top dock widget area
34+
previousDockWidget = dockManager->addDockWidget(ads::CenterDockWidgetArea, DockWidget, previousDockWidget);
35+
}
36+
37+
ads::CDockContainerWidget* groupManager = dockManager->createGroup( "Group", previousDockWidget ).second;
38+
39+
previousDockWidget = NULL;
40+
for ( int i = 0; i != 3; ++i )
41+
{
42+
// Create example content label - this can be any application specific
43+
// widget
44+
QLabel* l = new QLabel();
45+
l->setWordWrap(true);
46+
l->setAlignment(Qt::AlignTop | Qt::AlignLeft);
47+
l->setText("Lorem ipsum dolor sit amet, consectetuer adipiscing elit. ");
48+
49+
// Create a dock widget with the title Label 1 and set the created label
50+
// as the dock widget content
51+
ads::CDockWidget* DockWidget = new ads::CDockWidget("Inner " + QString::number(i));
52+
DockWidget->setWidget(l);
53+
54+
// Add the dock widget to the top dock widget area
55+
previousDockWidget = groupManager->addDockWidget(ads::CenterDockWidgetArea, DockWidget, previousDockWidget);
56+
}
57+
}
58+
59+
MainWindow::~MainWindow()
60+
{
61+
62+
}
63+

examples/dockdepth1/MainWindow.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef MAINWINDOW_H
2+
#define MAINWINDOW_H
3+
4+
#include <QMainWindow>
5+
6+
class MainWindow : public QMainWindow
7+
{
8+
Q_OBJECT
9+
10+
public:
11+
explicit MainWindow(QWidget *parent = 0);
12+
~MainWindow();
13+
};
14+
15+
#endif // MAINWINDOW_H

examples/dockdepth1/dockdepth1.pro

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
ADS_OUT_ROOT = $${OUT_PWD}/../..
2+
3+
QT += core gui widgets
4+
5+
TARGET = DockDepth1
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+
DEFINES += QT_DEPRECATED_WARNINGS
15+
16+
SOURCES += \
17+
innertabs.cpp \
18+
main.cpp \
19+
MainWindow.cpp
20+
21+
HEADERS += \
22+
innertabs.h \
23+
MainWindow.h
24+
25+
LIBS += -L$${ADS_OUT_ROOT}/lib
26+
include(../../ads.pri)
27+
INCLUDEPATH += ../../src
28+
DEPENDPATH += ../../src
29+

examples/dockdepth1/innertabs.cpp

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
#include "innertabs.h"
2+
3+
#include "DockAreaWidget.h"
4+
5+
#include <QInputDialog>
6+
#include <QMenu>
7+
8+
/////////////////////////////////////
9+
// DockManagerWithInnerTabs
10+
/////////////////////////////////////
11+
void deleteAllChildren( ads::CDockContainerWidget* areaWidget )
12+
{
13+
// fix crash on close by manually deleting children
14+
// maybe due to this issue: https://github.com/githubuser0xFFFF/Qt-Advanced-Docking-System/issues/307
15+
16+
std::vector<ads::CDockAreaWidget*> areas;
17+
for ( int i = 0; i != areaWidget->dockAreaCount(); ++i )
18+
{
19+
areas.push_back( areaWidget->dockArea(i) );
20+
}
21+
22+
for ( auto area : areas )
23+
{
24+
for ( auto widget : area->dockWidgets() )
25+
{
26+
ads::CDockContainerWidget* subArea = dynamic_cast<ads::CDockContainerWidget*>( widget->widget() );
27+
if ( subArea )
28+
deleteAllChildren( subArea );
29+
delete widget;
30+
}
31+
32+
delete area;
33+
}
34+
}
35+
36+
DockManagerWithInnerTabs::~DockManagerWithInnerTabs()
37+
{
38+
deleteAllChildren( this );
39+
}
40+
41+
std::pair<ads::CDockWidget*,ads::CDockContainerWidget*> DockManagerWithInnerTabs::createGroup( const QString& groupName, ads::CDockAreaWidget*& insertPos )
42+
{
43+
ads::CDockWidget* groupedDockWidget = new ads::CDockWidget(groupName);
44+
45+
ads::CDockContainerWidget* groupManager = new ads::CDockContainerWidget(this);
46+
groupedDockWidget->setWidget(groupManager);
47+
48+
insertPos = this->addDockWidget(ads::CenterDockWidgetArea, groupedDockWidget, insertPos);
49+
50+
return { groupedDockWidget, groupManager };
51+
}
52+
53+
void DockManagerWithInnerTabs::attachViewMenu( QMenu* menu )
54+
{
55+
connect( menu, SIGNAL(aboutToShow()), this, SLOT(autoFillAttachedViewMenu()) );
56+
}
57+
58+
void DockManagerWithInnerTabs::autoFillAttachedViewMenu()
59+
{
60+
QMenu* menu = dynamic_cast<QMenu*>( QObject::sender() );
61+
62+
if ( menu )
63+
{
64+
menu->clear();
65+
setupViewMenu( menu );
66+
}
67+
else
68+
{
69+
assert( false );
70+
}
71+
}
72+
73+
bool SortFunc( ads::CDockWidget* left, ads::CDockWidget* right )
74+
{
75+
if ( left->windowTitle() == right->windowTitle() )
76+
{
77+
assert( false );
78+
return left < right;
79+
}
80+
else
81+
{
82+
return left->windowTitle() < right->windowTitle();
83+
}
84+
}
85+
86+
void DockManagerWithInnerTabs::setupMenu( QMenu* menu, ads::CDockContainerWidget* areaWidget )
87+
{
88+
std::vector<ads::CDockWidget*> widgets;
89+
90+
ads::CDockManager* dockManager = dynamic_cast<ads::CDockManager*>( areaWidget );
91+
if ( dockManager )
92+
{
93+
for ( ads::CFloatingDockContainer* floating : dockManager->floatingWidgets() )
94+
{
95+
for ( auto floated : floating->dockWidgets() )
96+
widgets.push_back( floated );
97+
}
98+
}
99+
100+
for ( int i = 0; i != areaWidget->dockAreaCount(); ++i )
101+
{
102+
for ( auto docked : areaWidget->dockArea(i)->dockWidgets() )
103+
widgets.push_back( docked );
104+
}
105+
106+
std::sort( widgets.begin(), widgets.end(), SortFunc );
107+
108+
for ( auto widget : widgets )
109+
{
110+
auto action = widget->toggleViewAction();
111+
112+
ads::CDockContainerWidget* subArea = dynamic_cast<ads::CDockContainerWidget*>( widget->widget() );
113+
if ( subArea )
114+
{
115+
auto subMenu = menu->addMenu( widget->windowTitle() );
116+
117+
subMenu->addAction( action );
118+
subMenu->addSeparator();
119+
120+
setupMenu( subMenu, subArea );
121+
}
122+
else
123+
{
124+
menu->addAction(action);
125+
}
126+
}
127+
128+
if ( dockManager )
129+
{
130+
menu->addSeparator();
131+
int count = areaWidget->dockAreaCount();
132+
if ( count == 0 )
133+
{
134+
menu->addAction( new CreateGroupAction( this, NULL, menu ) );
135+
}
136+
else
137+
{
138+
for ( int i = 0; i != count; ++i )
139+
{
140+
menu->addAction( new CreateGroupAction( this, areaWidget->dockArea(i), menu ) );
141+
}
142+
}
143+
}
144+
// else, don't permit to add groups in groups
145+
// that would be nice, but it's not handled correctly upon drag/drop of a widget, it cannot be dropped in the inner docking area
146+
}
147+
148+
void DockManagerWithInnerTabs::setupViewMenu( QMenu* menu )
149+
{
150+
setupMenu( menu, this );
151+
}
152+
153+
/////////////////////////////////////
154+
// CreateGroupAction
155+
/////////////////////////////////////
156+
CreateGroupAction::CreateGroupAction( DockManagerWithInnerTabs* manager, ads::CDockAreaWidget* insertPos, QMenu* menu ) :
157+
QAction("New group...", menu),
158+
m_manager( manager),
159+
m_insertPos( insertPos )
160+
{
161+
connect( this, SIGNAL(triggered()), this, SLOT(createGroup()) );
162+
}
163+
164+
void CreateGroupAction::createGroup()
165+
{
166+
QString name = QInputDialog::getText( NULL, text(), "Enter group name" );
167+
if ( !name.isEmpty() )
168+
{
169+
m_manager->createGroup( name, m_insertPos );
170+
}
171+
}

examples/dockdepth1/innertabs.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#pragma once
2+
3+
#include "DockManager.h"
4+
5+
#include <QAction>
6+
7+
class DockManagerWithInnerTabs : public ads::CDockManager
8+
{
9+
Q_OBJECT
10+
11+
public:
12+
using ads::CDockManager::CDockManager;
13+
14+
~DockManagerWithInnerTabs() override;
15+
16+
std::pair<ads::CDockWidget*,ads::CDockContainerWidget*> createGroup( const QString& groupName, ads::CDockAreaWidget*& insertPos );
17+
18+
/** Manually fill a given view menu */
19+
void setupViewMenu( QMenu* menu );
20+
21+
/** Attach a view menu that will be automatically fill */
22+
void attachViewMenu( QMenu* menu );
23+
24+
private slots:
25+
void autoFillAttachedViewMenu();
26+
27+
private:
28+
void setupMenu( QMenu* menu, ads::CDockContainerWidget* areaWidget );
29+
};
30+
31+
class CreateGroupAction : public QAction
32+
{
33+
Q_OBJECT
34+
public:
35+
CreateGroupAction( DockManagerWithInnerTabs* manager, ads::CDockAreaWidget* insertIn, QMenu* menu );
36+
37+
public slots:
38+
void createGroup();
39+
40+
private:
41+
DockManagerWithInnerTabs* m_manager;
42+
ads::CDockAreaWidget* m_insertPos;
43+
};
44+
45+

examples/dockdepth1/main.cpp

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

examples/examples.pro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ SUBDIRS = \
55
simple \
66
sidebar \
77
deleteonclose \
8-
emptydockarea
8+
emptydockarea \
9+
dockdepth1

0 commit comments

Comments
 (0)