KDDockWidgets API Documentation  1.5
MainWindow.cpp
Go to the documentation of this file.
1 /*
2  This file is part of KDDockWidgets.
3 
4  SPDX-FileCopyrightText: 2019-2022 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
5  Author: SĂ©rgio Martins <sergio.martins@kdab.com>
6 
7  SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
8 
9  Contact KDAB at <info@kdab.com> for commercial licensing options.
10 */
11 
19 #include "MainWindow.h"
20 #include "Config.h"
21 #include "FrameworkWidgetFactory.h"
22 
23 #include "private/DockRegistry_p.h"
24 #include "private/DropAreaWithCentralFrame_p.h"
25 #include "private/DropArea_p.h"
26 #include "private/Frame_p.h"
27 #include "private/Logging_p.h"
28 #include "private/SideBar_p.h"
29 
30 #include <QPainter>
31 #include <QScreen>
32 #include <QVBoxLayout>
33 #include <QWindow>
34 
35 // clazy:excludeall=ctor-missing-parent-argument,missing-qobject-macro
36 
37 using namespace KDDockWidgets;
38 
39 namespace KDDockWidgets {
40 class MyCentralWidget : public QWidget
41 {
42 public:
43  explicit MyCentralWidget(QWidget *parent = nullptr)
44  : QWidget(parent)
45  {
46  setObjectName(QStringLiteral("MyCentralWidget"));
47  }
48 
49  ~MyCentralWidget() override;
50 };
51 }
52 
53 class MainWindow::Private
54 {
55 public:
56  explicit Private(MainWindowOptions, MainWindow *mainWindow)
57  : q(mainWindow)
58  , m_supportsAutoHide(Config::self().flags() & Config::Flag_AutoHideSupport)
59  , m_centralWidget(new MyCentralWidget(mainWindow))
60  , m_layout(new QHBoxLayout(m_centralWidget)) // 1 level of indirection so we can add some margins
61  {
62  if (m_supportsAutoHide) {
63  for (auto location : { SideBarLocation::North, SideBarLocation::East,
65  m_sideBars.insert(location, Config::self().frameworkWidgetFactory()->createSideBar(location, mainWindow));
66  }
67  }
68 
69  m_layout->setSpacing(0);
70  updateMargins();
71  }
72 
73  void updateMargins()
74  {
75  m_layout->setContentsMargins(q->centerWidgetMargins());
76  }
77 
78  MainWindow *const q;
79  const bool m_supportsAutoHide;
81  MyCentralWidget *const m_centralWidget;
82  QHBoxLayout *const m_layout;
83 };
84 
85 MyCentralWidget::~MyCentralWidget()
86 {
87 }
88 
89 
90 MainWindow::MainWindow(const QString &name, MainWindowOptions options,
91  QWidget *parent, Qt::WindowFlags flags)
92  : MainWindowBase(name, options, parent, flags)
93  , d(new Private(options, this))
94 {
95  if (d->m_supportsAutoHide) {
96  d->m_layout->addWidget(sideBar(SideBarLocation::West));
97  auto innerVLayout = new QVBoxLayout();
98  innerVLayout->setSpacing(0);
99  innerVLayout->setContentsMargins(0, 0, 0, 0);
100  innerVLayout->addWidget(sideBar(SideBarLocation::North));
101  innerVLayout->addWidget(layoutWidget());
102  innerVLayout->addWidget(sideBar(SideBarLocation::South));
103  d->m_layout->addLayout(innerVLayout);
104  d->m_layout->addWidget(sideBar(SideBarLocation::East));
105  } else {
106  d->m_layout->addWidget(layoutWidget());
107  }
108 
109  setCentralWidget(d->m_centralWidget);
110 
111  create();
112  connect(windowHandle(), &QWindow::screenChanged, DockRegistry::self(),
113  [this] {
114  d->updateMargins(); // logical dpi might have changed
115  Q_EMIT DockRegistry::self()->windowChangedScreen(windowHandle());
116  });
117 }
118 
120 {
121  delete d;
122 }
123 
124 void MainWindow::setCentralWidget(QWidget *w)
125 {
127 }
128 
129 SideBar *MainWindow::sideBar(SideBarLocation location) const
130 {
131  return d->m_sideBars.value(location);
132 }
133 
135 {
137  onResized(ev); // Also call our own handler, since QtQuick doesn't have resizeEvent()
138 }
139 
141 {
142  const QMargins margins = { 1, 5, 1, 1 };
143  return margins * logicalDpiFactor(this);
144 }
145 
147 {
148  return centralWidget()->geometry();
149 }
QVBoxLayout
QRect
QObject::Q_EMIT
Q_EMITQ_EMIT
KDDockWidgets::MainWindow::centerWidgetMargins
QMargins centerWidgetMargins() const override
Definition: MainWindow.cpp:140
KDDockWidgets::MainWindowBase::layoutWidget
LayoutWidget * layoutWidget() const
Definition: MainWindowBase.cpp:200
QWidget::resizeEvent
virtual void resizeEvent(QResizeEvent *event)
QMainWindow::setCentralWidget
void setCentralWidget(QWidget *widget)
QWidget
KDDockWidgets::SideBarLocation::West
@ West
QObject::connect
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Qt::WindowFlags
typedef WindowFlags
QString
KDDockWidgets::Config
Singleton to allow to choose certain behaviours of the framework.
Definition: Config.h:55
KDDockWidgets::MainWindow::centralAreaGeometry
QRect centralAreaGeometry() const override
Definition: MainWindow.cpp:146
KDDockWidgets::MainWindow::resizeEvent
void resizeEvent(QResizeEvent *) override
Definition: MainWindow.cpp:134
QMargins
KDDockWidgets::SideBarLocation
SideBarLocation
Each main window supports 4 sidebars.
Definition: KDDockWidgets.h:211
KDDockWidgets::SideBarLocation::East
@ East
QMainWindow::centralWidget
QWidget * centralWidget() const const
KDDockWidgets::SideBarLocation::South
@ South
Config.h
Application-wide config to tune certain behaviours of the framework.
QWindow::screenChanged
void screenChanged(QScreen *screen)
QWidget::windowHandle
QWindow * windowHandle() const const
MainWindow.h
QMainWindow sub-class to enable KDDockWidgets support.
KDDockWidgets::MainWindow
The QMainwindow sub-class that the application should use to be able to dock KDDockWidget::DockWidget...
Definition: MainWindow.h:33
KDDockWidgets::SideBarLocation::North
@ North
QResizeEvent
QObject::setObjectName
void setObjectName(const QString &name)
QHBoxLayout
KDDockWidgets
Definition: Config.cpp:36
QWidget::geometry
geometry
KDDockWidgets::MainWindowBase
The MainWindow base-class. MainWindow and MainWindowBase are only split in two so we can share some c...
Definition: MainWindowBase.h:56
KDDockWidgets::MainWindow::sideBar
SideBar * sideBar(SideBarLocation) const override
returns the sidebar for the specified location
Definition: MainWindow.cpp:129
QHash< SideBarLocation, SideBar * >
KDDockWidgets::MainWindowBase::onResized
void onResized(QResizeEvent *)
Definition: MainWindowBase.cpp:685
KDDockWidgets::MainWindow::~MainWindow
~MainWindow() override
Destructor.
Definition: MainWindow.cpp:119
QWidget::create
void create(WId window, bool initializeWindow, bool destroyOldWindow)
QObject::parent
QObject * parent() const const
KDDockWidgets::MainWindow::MainWindow
MainWindow(const QString &uniqueName, MainWindowOptions options=MainWindowOption_None, QWidget *parent=nullptr, Qt::WindowFlags flags=Qt::WindowFlags())
Constructor. Use it as you would use QMainWindow.
Definition: MainWindow.cpp:90
KDDockWidgets::Config::self
static Config & self()
returns the singleton Config instance
Definition: Config.cpp:82
FrameworkWidgetFactory.h
A factory class for allowing the user to customize some internal widgets.

© 2019-2022 Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/
KDDockWidgets
Advanced Dock Widget Framework for Qt
https://www.kdab.com/development-resources/qt-tools/kddockwidgets/
Generated on Mon Mar 7 2022 02:01:20 for KDDockWidgets API Documentation by doxygen 1.8.20