1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt Designer of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef DEFAULT_CONTAINER_H
43 #define DEFAULT_CONTAINER_H
44 
45 #include <QtDesigner/QDesignerContainerExtension>
46 #include <QtDesigner/extension.h>
47 #include <extensionfactory_p.h>
48 
49 #include <QtGui/QStackedWidget>
50 #include <QtGui/QTabWidget>
51 #include <QtGui/QToolBox>
52 #include <QtGui/QScrollArea>
53 #include <QtGui/QDockWidget>
54 
55 QT_BEGIN_NAMESPACE
56 
57 namespace qdesigner_internal {
58 
59 // ------------ QStackedWidgetContainer
60 class QStackedWidgetContainer: public QObject, public QDesignerContainerExtension
61 {
62     Q_OBJECT
63     Q_INTERFACES(QDesignerContainerExtension)
64 public:
65     explicit QStackedWidgetContainer(QStackedWidget *widget, QObject *parent = 0);
66 
count()67     virtual int count() const { return m_widget->count(); }
widget(int index)68     virtual QWidget *widget(int index) const { return m_widget->widget(index); }
69 
currentIndex()70     virtual int currentIndex() const { return m_widget->currentIndex(); }
71     virtual void setCurrentIndex(int index);
72 
73     virtual void addWidget(QWidget *widget);
74     virtual void insertWidget(int index, QWidget *widget);
75     virtual void remove(int index);
76 
77 private:
78     QStackedWidget *m_widget;
79 };
80 
81 // ------------ QTabWidgetContainer
82 class QTabWidgetContainer: public QObject, public QDesignerContainerExtension
83 {
84     Q_OBJECT
85     Q_INTERFACES(QDesignerContainerExtension)
86 public:
87     explicit QTabWidgetContainer(QTabWidget *widget, QObject *parent = 0);
88 
count()89     virtual int count() const { return m_widget->count(); }
widget(int index)90     virtual QWidget *widget(int index) const { return m_widget->widget(index); }
91 
currentIndex()92     virtual int currentIndex() const { return m_widget->currentIndex(); }
93     virtual void setCurrentIndex(int index);
94 
95     virtual void addWidget(QWidget *widget);
96     virtual void insertWidget(int index, QWidget *widget);
97     virtual void remove(int index);
98 
99 private:
100     QTabWidget *m_widget;
101 };
102 
103 // ------------  QToolBoxContainer
104 class QToolBoxContainer: public QObject, public QDesignerContainerExtension
105 {
106     Q_OBJECT
107     Q_INTERFACES(QDesignerContainerExtension)
108 public:
109     explicit QToolBoxContainer(QToolBox *widget, QObject *parent = 0);
110 
count()111     virtual int count() const { return m_widget->count(); }
widget(int index)112     virtual QWidget *widget(int index) const { return m_widget->widget(index); }
113 
currentIndex()114     virtual int currentIndex() const { return m_widget->currentIndex(); }
115     virtual void setCurrentIndex(int index);
116 
117     virtual void addWidget(QWidget *widget);
118     virtual void insertWidget(int index, QWidget *widget);
119     virtual void remove(int index);
120 
121 private:
122     QToolBox *m_widget;
123 };
124 
125 // ------------ SingleChildContainer:
126 //  Template for containers that have a single child widget using widget()/setWidget().
127 
128 template <class Container>
129 class SingleChildContainer: public QDesignerContainerExtension
130 {
131 protected:
132     explicit SingleChildContainer(Container *widget, bool active = true);
133 public:
134     virtual int count() const;
135     virtual QWidget *widget(int index) const;
136     virtual int currentIndex() const;
setCurrentIndex(int)137     virtual void setCurrentIndex(int /*index*/) {}
138     virtual void addWidget(QWidget *widget);
139     virtual void insertWidget(int index, QWidget *widget);
remove(int)140     virtual void remove(int /*index*/) {}
141 
142 private:
143     const bool m_active;
144     Container *m_container;
145 };
146 
147 template <class Container>
SingleChildContainer(Container * widget,bool active)148 SingleChildContainer<Container>::SingleChildContainer(Container *widget, bool active) :
149     m_active(active),
150     m_container(widget)
151 {
152 }
153 
154 template <class Container>
count()155 int SingleChildContainer<Container>::count() const
156 {
157     return  m_active && m_container->widget() ? 1 : 0;
158 }
159 
160 template <class Container>
widget(int)161 QWidget *SingleChildContainer<Container>::widget(int /* index */) const
162 {
163     return m_container->widget();
164 }
165 
166 template <class Container>
currentIndex()167 int SingleChildContainer<Container>::currentIndex() const
168 {
169     return m_active && m_container->widget() ? 0 : -1;
170 }
171 
172 template <class Container>
addWidget(QWidget * widget)173 void SingleChildContainer<Container>::addWidget(QWidget *widget)
174 {
175     Q_ASSERT(m_container->widget() == 0);
176     widget->setParent(m_container);
177     m_container->setWidget(widget);
178 }
179 
180 template <class Container>
insertWidget(int,QWidget * widget)181 void SingleChildContainer<Container>::insertWidget(int /* index */, QWidget *widget)
182 {
183     addWidget(widget);
184 }
185 
186 // ------------  QScrollAreaContainer
187 class QScrollAreaContainer: public QObject, public SingleChildContainer<QScrollArea>
188 {
189     Q_OBJECT
190     Q_INTERFACES(QDesignerContainerExtension)
191 public:
192     explicit QScrollAreaContainer(QScrollArea *widget, QObject *parent = 0);
193 };
194 
195 // --------------- QDockWidgetContainer
196 class QDockWidgetContainer: public QObject, public SingleChildContainer<QDockWidget>
197 {
198     Q_OBJECT
199     Q_INTERFACES(QDesignerContainerExtension)
200 public:
201     explicit QDockWidgetContainer(QDockWidget *widget, QObject *parent = 0);
202 };
203 
204 typedef ExtensionFactory<QDesignerContainerExtension, QStackedWidget, QStackedWidgetContainer> QDesignerStackedWidgetContainerFactory;
205 typedef ExtensionFactory<QDesignerContainerExtension, QTabWidget, QTabWidgetContainer> QDesignerTabWidgetContainerFactory;
206 typedef ExtensionFactory<QDesignerContainerExtension, QToolBox, QToolBoxContainer> QDesignerToolBoxContainerFactory;
207 typedef ExtensionFactory<QDesignerContainerExtension, QScrollArea, QScrollAreaContainer> QScrollAreaContainerFactory;
208 typedef ExtensionFactory<QDesignerContainerExtension,  QDockWidget, QDockWidgetContainer> QDockWidgetContainerFactory;
209 }  // namespace qdesigner_internal
210 
211 QT_END_NAMESPACE
212 
213 #endif // DEFAULT_CONTAINER_H
214