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