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 "qdesigner_dockwidget_p.h"
30 #include "layoutinfo_p.h"
31 
32 #include <QtDesigner/abstractformwindow.h>
33 #include <QtDesigner/abstractformeditor.h>
34 #include <QtDesigner/container.h>
35 #include <QtDesigner/qextensionmanager.h>
36 #include <QtDesigner/abstractformwindowcursor.h>
37 
38 #include <qdesigner_propertysheet_p.h>
39 
40 #include <QtWidgets/qmainwindow.h>
41 #include <QtWidgets/qlayout.h>
42 
43 QT_BEGIN_NAMESPACE
44 
isEnabled(int index) const45 bool QDockWidgetPropertySheet::isEnabled(int index) const
46 {
47     const QString &name = propertyName(index);
48     if (name == QLatin1String("dockWidgetArea"))
49         return static_cast<const QDesignerDockWidget *>(object())->docked();
50     if (name == QLatin1String("docked"))
51         return static_cast<const QDesignerDockWidget *>(object())->inMainWindow();
52     return QDesignerPropertySheet::isEnabled(index);
53 }
54 
QDesignerDockWidget(QWidget * parent)55 QDesignerDockWidget::QDesignerDockWidget(QWidget *parent)
56     : QDockWidget(parent)
57 {
58 }
59 
60 QDesignerDockWidget::~QDesignerDockWidget() = default;
61 
docked() const62 bool QDesignerDockWidget::docked() const
63 {
64     return qobject_cast<QMainWindow*>(parentWidget()) != 0;
65 }
66 
setDocked(bool b)67 void QDesignerDockWidget::setDocked(bool b)
68 {
69     if (QMainWindow *mainWindow = findMainWindow()) {
70         QDesignerFormEditorInterface *core = formWindow()->core();
71         QDesignerContainerExtension *c;
72         c = qt_extension<QDesignerContainerExtension*>(core->extensionManager(), mainWindow);
73         if (b && !docked()) {
74             // Dock it
75             // ### undo/redo stack
76             setParent(nullptr);
77             c->addWidget(this);
78             formWindow()->selectWidget(this, formWindow()->cursor()->isWidgetSelected(this));
79         } else if (!b && docked()) {
80             // Undock it
81             for (int i = 0; i < c->count(); ++i) {
82                 if (c->widget(i) == this) {
83                     c->remove(i);
84                     break;
85                 }
86             }
87             // #### restore the position
88             setParent(mainWindow->centralWidget());
89             show();
90             formWindow()->selectWidget(this, formWindow()->cursor()->isWidgetSelected(this));
91         }
92     }
93 }
94 
dockWidgetArea() const95 Qt::DockWidgetArea QDesignerDockWidget::dockWidgetArea() const
96 {
97     if (QMainWindow *mainWindow = qobject_cast<QMainWindow*>(parentWidget()))
98         return mainWindow->dockWidgetArea(const_cast<QDesignerDockWidget*>(this));
99 
100     return Qt::LeftDockWidgetArea;
101 }
102 
setDockWidgetArea(Qt::DockWidgetArea dockWidgetArea)103 void QDesignerDockWidget::setDockWidgetArea(Qt::DockWidgetArea dockWidgetArea)
104 {
105     if (QMainWindow *mainWindow = qobject_cast<QMainWindow*>(parentWidget())) {
106         if ((dockWidgetArea != Qt::NoDockWidgetArea)
107             && isAreaAllowed(dockWidgetArea)) {
108             mainWindow->addDockWidget(dockWidgetArea, this);
109         }
110     }
111 }
112 
inMainWindow() const113 bool QDesignerDockWidget::inMainWindow() const
114 {
115     QMainWindow *mw = findMainWindow();
116     if (mw && !mw->centralWidget()->layout()) {
117         if (mw == parentWidget())
118             return true;
119         if (mw->centralWidget() == parentWidget())
120             return true;
121     }
122     return false;
123 }
124 
formWindow() const125 QDesignerFormWindowInterface *QDesignerDockWidget::formWindow() const
126 {
127     return QDesignerFormWindowInterface::findFormWindow(const_cast<QDesignerDockWidget*>(this));
128 }
129 
findMainWindow() const130 QMainWindow *QDesignerDockWidget::findMainWindow() const
131 {
132     if (QDesignerFormWindowInterface *fw = formWindow())
133         return qobject_cast<QMainWindow*>(fw->mainContainer());
134     return nullptr;
135 }
136 
137 QT_END_NAMESPACE
138