1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Designer of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include "qmainwindow_container.h"
30#include "qdesigner_toolbar_p.h"
31#include "formwindow.h"
32
33#include <QtCore/qdebug.h>
34
35#include <QtWidgets/qlayout.h>
36#include <QtWidgets/qmenubar.h>
37#include <QtWidgets/qtoolbar.h>
38#include <QtWidgets/qstatusbar.h>
39#include <QtWidgets/qdockwidget.h>
40
41QT_BEGIN_NAMESPACE
42
43using namespace qdesigner_internal;
44
45QMainWindowContainer::QMainWindowContainer(QMainWindow *widget, QObject *parent)
46 : QObject(parent),
47 m_mainWindow(widget)
48{
49}
50
51int QMainWindowContainer::count() const
52{
53 return m_widgets.count();
54}
55
56QWidget *QMainWindowContainer::widget(int index) const
57{
58 if (index == -1)
59 return nullptr;
60
61 return m_widgets.at(i: index);
62}
63
64int QMainWindowContainer::currentIndex() const
65{
66 return m_mainWindow->centralWidget() ? 0 : -1;
67}
68
69void QMainWindowContainer::setCurrentIndex(int index)
70{
71 Q_UNUSED(index);
72}
73
74
75namespace {
76 // Pair of <area,break_before>
77 using ToolBarData = QPair<Qt::ToolBarArea,bool> ;
78
79 ToolBarData toolBarData(QToolBar *me) {
80 const QMainWindow *mw = qobject_cast<const QMainWindow*>(object: me->parentWidget());
81 if (!mw || !mw->layout() || mw->layout()->indexOf(me) == -1) {
82 const QVariant desiredAreaV = me->property(name: "_q_desiredArea");
83 const Qt::ToolBarArea desiredArea = desiredAreaV.canConvert<Qt::ToolBarArea>()
84 ? desiredAreaV.value<Qt::ToolBarArea>() : Qt::TopToolBarArea;
85 return {desiredArea, false};
86 }
87 return ToolBarData(mw->toolBarArea(toolbar: me), mw->toolBarBreak(toolbar: me));
88 }
89
90Qt::DockWidgetArea dockWidgetArea(QDockWidget *me)
91{
92 if (const QMainWindow *mw = qobject_cast<const QMainWindow*>(object: me->parentWidget())) {
93 // Make sure that me is actually managed by mw, otherwise
94 // QMainWindow::dockWidgetArea() will be VERY upset
95 QList<QLayout*> candidates;
96 if (mw->layout()) {
97 candidates.append(t: mw->layout());
98 candidates += mw->layout()->findChildren<QLayout*>();
99 }
100 for (QLayout *l : qAsConst(t&: candidates)) {
101 if (l->indexOf(me) != -1)
102 return mw->dockWidgetArea(dockwidget: me);
103 }
104 }
105 return Qt::LeftDockWidgetArea;
106}
107}
108
109void QMainWindowContainer::addWidget(QWidget *widget)
110{
111 // remove all the occurrences of widget
112 m_widgets.removeAll(t: widget);
113
114 // the
115 if (QToolBar *toolBar = qobject_cast<QToolBar*>(object: widget)) {
116 m_widgets.append(t: widget);
117 const ToolBarData data = toolBarData(me: toolBar);
118 m_mainWindow->addToolBar(area: data.first, toolbar: toolBar);
119 if (data.second) m_mainWindow->insertToolBarBreak(before: toolBar);
120 toolBar->show();
121 }
122
123 else if (QMenuBar *menuBar = qobject_cast<QMenuBar*>(object: widget)) {
124 if (menuBar != m_mainWindow->menuBar())
125 m_mainWindow->setMenuBar(menuBar);
126
127 m_widgets.append(t: widget);
128 menuBar->show();
129 }
130
131 else if (QStatusBar *statusBar = qobject_cast<QStatusBar*>(object: widget)) {
132 if (statusBar != m_mainWindow->statusBar())
133 m_mainWindow->setStatusBar(statusBar);
134
135 m_widgets.append(t: widget);
136 statusBar->show();
137 }
138
139 else if (QDockWidget *dockWidget = qobject_cast<QDockWidget*>(object: widget)) {
140 m_widgets.append(t: widget);
141 m_mainWindow->addDockWidget(area: dockWidgetArea(me: dockWidget), dockwidget: dockWidget);
142 dockWidget->show();
143
144 if (FormWindow *fw = FormWindow::findFormWindow(w: m_mainWindow)) {
145 fw->manageWidget(w: widget);
146 }
147 }
148
149 else if (widget) {
150 m_widgets.prepend(t: widget);
151
152 if (widget != m_mainWindow->centralWidget()) {
153 // note that qmainwindow will delete the current central widget if you
154 // call setCentralWidget(), we end up with dangeling pointers in m_widgets list
155 m_widgets.removeAll(t: m_mainWindow->centralWidget());
156
157 widget->setParent(m_mainWindow);
158 m_mainWindow->setCentralWidget(widget);
159 }
160 }
161}
162
163void QMainWindowContainer::insertWidget(int index, QWidget *widget)
164{
165 Q_UNUSED(index);
166
167 addWidget(widget);
168}
169
170void QMainWindowContainer::remove(int index)
171{
172 QWidget *widget = m_widgets.at(i: index);
173 if (QToolBar *toolBar = qobject_cast<QToolBar*>(object: widget)) {
174 m_mainWindow->removeToolBar(toolbar: toolBar);
175 } else if (QMenuBar *menuBar = qobject_cast<QMenuBar*>(object: widget)) {
176 menuBar->hide();
177 menuBar->setParent(nullptr);
178 m_mainWindow->setMenuBar(nullptr);
179 } else if (QStatusBar *statusBar = qobject_cast<QStatusBar*>(object: widget)) {
180 statusBar->hide();
181 statusBar->setParent(nullptr);
182 m_mainWindow->setStatusBar(nullptr);
183 } else if (QDockWidget *dockWidget = qobject_cast<QDockWidget*>(object: widget)) {
184 m_mainWindow->removeDockWidget(dockwidget: dockWidget);
185 }
186 m_widgets.removeAt(i: index);
187}
188
189QT_END_NAMESPACE
190

source code of qttools/src/designer/src/components/formeditor/qmainwindow_container.cpp