Add dockdepth1 example

This commit is contained in:
Jean Porcherot 2021-04-07 16:51:46 +02:00
parent 6179832a2b
commit be3180df6f
8 changed files with 362 additions and 1 deletions

View File

@ -0,0 +1,26 @@
cmake_minimum_required(VERSION 3.5)
project(ads_example_simple VERSION ${VERSION_SHORT})
find_package(Qt5 5.5 COMPONENTS Core Gui Widgets REQUIRED)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
add_executable(DockDepth1 WIN32
innertabs.cpp
main.cpp
MainWindow.cpp
MainWindow.ui
)
target_include_directories(SimpleExample PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../../src")
target_link_libraries(SimpleExample PRIVATE qtadvanceddocking)
target_link_libraries(SimpleExample PUBLIC Qt5::Core Qt5::Gui Qt5::Widgets)
set_target_properties(SimpleExample PROPERTIES
AUTOMOC ON
AUTORCC ON
AUTOUIC ON
CXX_STANDARD 14
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF
VERSION ${VERSION_SHORT}
EXPORT_NAME "Qt Advanced Docking System Simple Example"
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${ads_PlatformDir}/lib"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${ads_PlatformDir}/lib"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${ads_PlatformDir}/bin"
)

View File

@ -0,0 +1,63 @@
#include "../../examples/dockdepth1/MainWindow.h"
#include "../../examples/dockdepth1/innertabs.h"
#include <QLabel>
#include <QMenuBar>
#include "DockAreaWidget.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
resize( 400, 400 );
DockManagerWithInnerTabs* dockManager = new DockManagerWithInnerTabs(this);
dockManager->attachViewMenu( menuBar()->addMenu( "View" ) );
ads::CDockAreaWidget* previousDockWidget = NULL;
for ( int i = 0; i != 3; ++i )
{
// Create example content label - this can be any application specific
// widget
QLabel* l = new QLabel();
l->setWordWrap(true);
l->setAlignment(Qt::AlignTop | Qt::AlignLeft);
l->setText("Lorem ipsum dolor sit amet, consectetuer adipiscing elit. ");
// Create a dock widget with the title Label 1 and set the created label
// as the dock widget content
ads::CDockWidget* DockWidget = new ads::CDockWidget("Label " + QString::number(i));
DockWidget->setWidget(l);
// Add the dock widget to the top dock widget area
previousDockWidget = dockManager->addDockWidget(ads::CenterDockWidgetArea, DockWidget, previousDockWidget);
}
ads::CDockContainerWidget* groupManager = dockManager->createGroup( "Group", previousDockWidget ).second;
previousDockWidget = NULL;
for ( int i = 0; i != 3; ++i )
{
// Create example content label - this can be any application specific
// widget
QLabel* l = new QLabel();
l->setWordWrap(true);
l->setAlignment(Qt::AlignTop | Qt::AlignLeft);
l->setText("Lorem ipsum dolor sit amet, consectetuer adipiscing elit. ");
// Create a dock widget with the title Label 1 and set the created label
// as the dock widget content
ads::CDockWidget* DockWidget = new ads::CDockWidget("Inner " + QString::number(i));
DockWidget->setWidget(l);
// Add the dock widget to the top dock widget area
previousDockWidget = groupManager->addDockWidget(ads::CenterDockWidgetArea, DockWidget, previousDockWidget);
}
}
MainWindow::~MainWindow()
{
}

View File

@ -0,0 +1,15 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
};
#endif // MAINWINDOW_H

View File

@ -0,0 +1,29 @@
ADS_OUT_ROOT = $${OUT_PWD}/../..
QT += core gui widgets
TARGET = DockDepth1
DESTDIR = $${ADS_OUT_ROOT}/lib
TEMPLATE = app
CONFIG += c++14
CONFIG += debug_and_release
adsBuildStatic {
DEFINES += ADS_STATIC
}
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
innertabs.cpp \
main.cpp \
MainWindow.cpp
HEADERS += \
innertabs.h \
MainWindow.h
LIBS += -L$${ADS_OUT_ROOT}/lib
include(../../ads.pri)
INCLUDEPATH += ../../src
DEPENDPATH += ../../src

View File

@ -0,0 +1,171 @@
#include "innertabs.h"
#include "DockAreaWidget.h"
#include <QInputDialog>
#include <QMenu>
/////////////////////////////////////
// DockManagerWithInnerTabs
/////////////////////////////////////
void deleteAllChildren( ads::CDockContainerWidget* areaWidget )
{
// fix crash on close by manually deleting children
// maybe due to this issue: https://github.com/githubuser0xFFFF/Qt-Advanced-Docking-System/issues/307
std::vector<ads::CDockAreaWidget*> areas;
for ( int i = 0; i != areaWidget->dockAreaCount(); ++i )
{
areas.push_back( areaWidget->dockArea(i) );
}
for ( auto area : areas )
{
for ( auto widget : area->dockWidgets() )
{
ads::CDockContainerWidget* subArea = dynamic_cast<ads::CDockContainerWidget*>( widget->widget() );
if ( subArea )
deleteAllChildren( subArea );
delete widget;
}
delete area;
}
}
DockManagerWithInnerTabs::~DockManagerWithInnerTabs()
{
deleteAllChildren( this );
}
std::pair<ads::CDockWidget*,ads::CDockContainerWidget*> DockManagerWithInnerTabs::createGroup( const QString& groupName, ads::CDockAreaWidget*& insertPos )
{
ads::CDockWidget* groupedDockWidget = new ads::CDockWidget(groupName);
ads::CDockContainerWidget* groupManager = new ads::CDockContainerWidget(this);
groupedDockWidget->setWidget(groupManager);
insertPos = this->addDockWidget(ads::CenterDockWidgetArea, groupedDockWidget, insertPos);
return { groupedDockWidget, groupManager };
}
void DockManagerWithInnerTabs::attachViewMenu( QMenu* menu )
{
connect( menu, SIGNAL(aboutToShow()), this, SLOT(autoFillAttachedViewMenu()) );
}
void DockManagerWithInnerTabs::autoFillAttachedViewMenu()
{
QMenu* menu = dynamic_cast<QMenu*>( QObject::sender() );
if ( menu )
{
menu->clear();
setupViewMenu( menu );
}
else
{
assert( false );
}
}
bool SortFunc( ads::CDockWidget* left, ads::CDockWidget* right )
{
if ( left->windowTitle() == right->windowTitle() )
{
assert( false );
return left < right;
}
else
{
return left->windowTitle() < right->windowTitle();
}
}
void DockManagerWithInnerTabs::setupMenu( QMenu* menu, ads::CDockContainerWidget* areaWidget )
{
std::vector<ads::CDockWidget*> widgets;
ads::CDockManager* dockManager = dynamic_cast<ads::CDockManager*>( areaWidget );
if ( dockManager )
{
for ( ads::CFloatingDockContainer* floating : dockManager->floatingWidgets() )
{
for ( auto floated : floating->dockWidgets() )
widgets.push_back( floated );
}
}
for ( int i = 0; i != areaWidget->dockAreaCount(); ++i )
{
for ( auto docked : areaWidget->dockArea(i)->dockWidgets() )
widgets.push_back( docked );
}
std::sort( widgets.begin(), widgets.end(), SortFunc );
for ( auto widget : widgets )
{
auto action = widget->toggleViewAction();
ads::CDockContainerWidget* subArea = dynamic_cast<ads::CDockContainerWidget*>( widget->widget() );
if ( subArea )
{
auto subMenu = menu->addMenu( widget->windowTitle() );
subMenu->addAction( action );
subMenu->addSeparator();
setupMenu( subMenu, subArea );
}
else
{
menu->addAction(action);
}
}
if ( dockManager )
{
menu->addSeparator();
int count = areaWidget->dockAreaCount();
if ( count == 0 )
{
menu->addAction( new CreateGroupAction( this, NULL, menu ) );
}
else
{
for ( int i = 0; i != count; ++i )
{
menu->addAction( new CreateGroupAction( this, areaWidget->dockArea(i), menu ) );
}
}
}
// else, don't permit to add groups in groups
// 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
}
void DockManagerWithInnerTabs::setupViewMenu( QMenu* menu )
{
setupMenu( menu, this );
}
/////////////////////////////////////
// CreateGroupAction
/////////////////////////////////////
CreateGroupAction::CreateGroupAction( DockManagerWithInnerTabs* manager, ads::CDockAreaWidget* insertPos, QMenu* menu ) :
QAction("New group...", menu),
m_manager( manager),
m_insertPos( insertPos )
{
connect( this, SIGNAL(triggered()), this, SLOT(createGroup()) );
}
void CreateGroupAction::createGroup()
{
QString name = QInputDialog::getText( NULL, text(), "Enter group name" );
if ( !name.isEmpty() )
{
m_manager->createGroup( name, m_insertPos );
}
}

View File

@ -0,0 +1,45 @@
#pragma once
#include "DockManager.h"
#include <QAction>
class DockManagerWithInnerTabs : public ads::CDockManager
{
Q_OBJECT
public:
using ads::CDockManager::CDockManager;
~DockManagerWithInnerTabs() override;
std::pair<ads::CDockWidget*,ads::CDockContainerWidget*> createGroup( const QString& groupName, ads::CDockAreaWidget*& insertPos );
/** Manually fill a given view menu */
void setupViewMenu( QMenu* menu );
/** Attach a view menu that will be automatically fill */
void attachViewMenu( QMenu* menu );
private slots:
void autoFillAttachedViewMenu();
private:
void setupMenu( QMenu* menu, ads::CDockContainerWidget* areaWidget );
};
class CreateGroupAction : public QAction
{
Q_OBJECT
public:
CreateGroupAction( DockManagerWithInnerTabs* manager, ads::CDockAreaWidget* insertIn, QMenu* menu );
public slots:
void createGroup();
private:
DockManagerWithInnerTabs* m_manager;
ads::CDockAreaWidget* m_insertPos;
};

View File

@ -0,0 +1,11 @@
#include <QApplication>
#include "../../examples/simple/MainWindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

View File

@ -5,4 +5,5 @@ SUBDIRS = \
simple \
sidebar \
deleteonclose \
emptydockarea
emptydockarea \
dockdepth1