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 #ifndef DEFAULT_CONTAINER_H
30 #define DEFAULT_CONTAINER_H
31 
32 #include <QtDesigner/container.h>
33 #include <QtDesigner/extension.h>
34 #include <extensionfactory_p.h>
35 
36 #include <QtWidgets/qstackedwidget.h>
37 #include <QtWidgets/qtabwidget.h>
38 #include <QtWidgets/qtoolbox.h>
39 #include <QtWidgets/qscrollarea.h>
40 #include <QtWidgets/qdockwidget.h>
41 
42 QT_BEGIN_NAMESPACE
43 
44 namespace qdesigner_internal {
45 
46 // ------------ QStackedWidgetContainer
47 class QStackedWidgetContainer: public QObject, public QDesignerContainerExtension
48 {
49     Q_OBJECT
50     Q_INTERFACES(QDesignerContainerExtension)
51 public:
52     explicit QStackedWidgetContainer(QStackedWidget *widget, QObject *parent = nullptr);
53 
count()54     int count() const override { return m_widget->count(); }
widget(int index)55     QWidget *widget(int index) const override { return m_widget->widget(index); }
56 
currentIndex()57     int currentIndex() const override { return m_widget->currentIndex(); }
58     void setCurrentIndex(int index) override;
59 
60     void addWidget(QWidget *widget) override;
61     void insertWidget(int index, QWidget *widget) override;
62     void remove(int index) override;
63 
64 private:
65     QStackedWidget *m_widget;
66 };
67 
68 // ------------ QTabWidgetContainer
69 class QTabWidgetContainer: public QObject, public QDesignerContainerExtension
70 {
71     Q_OBJECT
72     Q_INTERFACES(QDesignerContainerExtension)
73 public:
74     explicit QTabWidgetContainer(QTabWidget *widget, QObject *parent = nullptr);
75 
count()76     int count() const override { return m_widget->count(); }
widget(int index)77     QWidget *widget(int index) const override { return m_widget->widget(index); }
78 
currentIndex()79     int currentIndex() const override { return m_widget->currentIndex(); }
80     void setCurrentIndex(int index) override;
81 
82     void addWidget(QWidget *widget) override;
83     void insertWidget(int index, QWidget *widget) override;
84     void remove(int index) override;
85 
86 private:
87     QTabWidget *m_widget;
88 };
89 
90 // ------------  QToolBoxContainer
91 class QToolBoxContainer: public QObject, public QDesignerContainerExtension
92 {
93     Q_OBJECT
94     Q_INTERFACES(QDesignerContainerExtension)
95 public:
96     explicit QToolBoxContainer(QToolBox *widget, QObject *parent = nullptr);
97 
count()98     int count() const override { return m_widget->count(); }
widget(int index)99     QWidget *widget(int index) const override { return m_widget->widget(index); }
100 
currentIndex()101     int currentIndex() const override { return m_widget->currentIndex(); }
102     void setCurrentIndex(int index) override;
103 
104     void addWidget(QWidget *widget) override;
105     void insertWidget(int index, QWidget *widget) override;
106     void remove(int index) override;
107 
108 private:
109     QToolBox *m_widget;
110 };
111 
112 // ------------ SingleChildContainer:
113 //  Template for containers that have a single child widget using widget()/setWidget().
114 
115 template <class Container>
116 class SingleChildContainer: public QDesignerContainerExtension
117 {
118 protected:
119     explicit SingleChildContainer(Container *widget, bool active = true);
120 public:
121     int count() const override;
122     QWidget *widget(int index) const override;
123     int currentIndex() const override;
setCurrentIndex(int)124     void setCurrentIndex(int /*index*/) override {}
125     void addWidget(QWidget *widget) override;
126     void insertWidget(int index, QWidget *widget) override;
remove(int)127     void remove(int /*index*/) override {}
128 
canAddWidget()129     bool canAddWidget() const override { return false; }
canRemove(int)130     bool canRemove(int) const override { return false; }
131 
132 private:
133     const bool m_active;
134     Container *m_container;
135 };
136 
137 template <class Container>
SingleChildContainer(Container * widget,bool active)138 SingleChildContainer<Container>::SingleChildContainer(Container *widget, bool active) :
139     m_active(active),
140     m_container(widget)
141 {
142 }
143 
144 template <class Container>
count()145 int SingleChildContainer<Container>::count() const
146 {
147     return  m_active && m_container->widget() ? 1 : 0;
148 }
149 
150 template <class Container>
widget(int)151 QWidget *SingleChildContainer<Container>::widget(int /* index */) const
152 {
153     return m_container->widget();
154 }
155 
156 template <class Container>
currentIndex()157 int SingleChildContainer<Container>::currentIndex() const
158 {
159     return m_active && m_container->widget() ? 0 : -1;
160 }
161 
162 template <class Container>
addWidget(QWidget * widget)163 void SingleChildContainer<Container>::addWidget(QWidget *widget)
164 {
165     Q_ASSERT(m_container->widget() == nullptr);
166     widget->setParent(m_container);
167     m_container->setWidget(widget);
168 }
169 
170 template <class Container>
insertWidget(int,QWidget * widget)171 void SingleChildContainer<Container>::insertWidget(int /* index */, QWidget *widget)
172 {
173     addWidget(widget);
174 }
175 
176 // ------------  QScrollAreaContainer
177 class QScrollAreaContainer: public QObject, public SingleChildContainer<QScrollArea>
178 {
179     Q_OBJECT
180     Q_INTERFACES(QDesignerContainerExtension)
181 public:
182     explicit QScrollAreaContainer(QScrollArea *widget, QObject *parent = nullptr);
183 };
184 
185 // --------------- QDockWidgetContainer
186 class QDockWidgetContainer: public QObject, public SingleChildContainer<QDockWidget>
187 {
188     Q_OBJECT
189     Q_INTERFACES(QDesignerContainerExtension)
190 public:
191     explicit QDockWidgetContainer(QDockWidget *widget, QObject *parent = nullptr);
192 };
193 
194 using QDesignerStackedWidgetContainerFactory = ExtensionFactory<QDesignerContainerExtension, QStackedWidget, QStackedWidgetContainer>;
195 using QDesignerTabWidgetContainerFactory = ExtensionFactory<QDesignerContainerExtension, QTabWidget, QTabWidgetContainer>;
196 using QDesignerToolBoxContainerFactory = ExtensionFactory<QDesignerContainerExtension, QToolBox, QToolBoxContainer>;
197 using QScrollAreaContainerFactory = ExtensionFactory<QDesignerContainerExtension, QScrollArea, QScrollAreaContainer>;
198 using QDockWidgetContainerFactory = ExtensionFactory<QDesignerContainerExtension,  QDockWidget, QDockWidgetContainer>;
199 }  // namespace qdesigner_internal
200 
201 QT_END_NAMESPACE
202 
203 #endif // DEFAULT_CONTAINER_H
204