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 "qwizard_container.h"
30
31#include <QtDesigner/qextensionmanager.h>
32#include <QtDesigner/abstractformeditor.h>
33
34#include <QtWidgets/qwizard.h>
35#include <QtCore/qdebug.h>
36#include <QtCore/qvector.h>
37
38QT_BEGIN_NAMESPACE
39
40using WizardPageList = QVector<QWizardPage *>;
41
42namespace qdesigner_internal {
43
44QWizardContainer::QWizardContainer(QWizard *widget, QObject *parent) :
45 QObject(parent),
46 m_wizard(widget)
47{
48}
49
50int QWizardContainer::count() const
51{
52 return m_wizard->pageIds().size();
53}
54
55QWidget *QWizardContainer::widget(int index) const
56{
57 QWidget *rc = nullptr;
58 if (index >= 0) {
59 const auto idList = m_wizard->pageIds();
60 if (index < idList.size())
61 rc = m_wizard->page(id: idList.at(i: index));
62 }
63 return rc;
64}
65
66int QWizardContainer::currentIndex() const
67{
68 return m_wizard->pageIds().indexOf(t: m_wizard->currentId());
69}
70
71void QWizardContainer::setCurrentIndex(int index)
72{
73 if (index < 0 || m_wizard->pageIds().isEmpty())
74 return;
75
76 int currentIdx = currentIndex();
77
78 if (currentIdx == -1) {
79 m_wizard->restart();
80 currentIdx = currentIndex();
81 }
82
83 if (currentIdx == index)
84 return;
85
86 const int d = qAbs(t: index - currentIdx);
87 if (index > currentIdx) {
88 for (int i = 0; i < d; i++)
89 m_wizard->next();
90 } else {
91 for (int i = 0; i < d; i++)
92 m_wizard->back();
93 }
94}
95
96static const char *msgWrongType = "** WARNING Attempt to add oject that is not of class WizardPage to a QWizard";
97
98void QWizardContainer::addWidget(QWidget *widget)
99{
100 QWizardPage *page = qobject_cast<QWizardPage *>(object: widget);
101 if (!page) {
102 qWarning(msg: "%s", msgWrongType);
103 return;
104 }
105 m_wizard->addPage(page);
106 // Might be -1 after adding the first page
107 setCurrentIndex(m_wizard->pageIds().size() - 1);
108}
109
110void QWizardContainer::insertWidget(int index, QWidget *widget)
111{
112 enum { delta = 5 };
113
114 QWizardPage *newPage = qobject_cast<QWizardPage *>(object: widget);
115 if (!newPage) {
116 qWarning(msg: "%s", msgWrongType);
117 return;
118 }
119
120 const auto idList = m_wizard->pageIds();
121 const int pageCount = idList.size();
122 if (index >= pageCount) {
123 addWidget(widget);
124 return;
125 }
126
127 // Insert before, reshuffle ids if required
128 const int idBefore = idList.at(i: index);
129 const int newId = idBefore - 1;
130 const bool needsShuffle =
131 (index == 0 && newId < 0) // At start: QWizard refuses to insert id -1
132 || (index > 0 && idList.at(i: index - 1) == newId); // In-between
133 if (needsShuffle) {
134 // Create a gap by shuffling pages
135 WizardPageList pageList;
136 pageList.push_back(t: newPage);
137 for (int i = index; i < pageCount; i++) {
138 pageList.push_back(t: m_wizard->page(id: idList.at(i)));
139 m_wizard->removePage(id: idList.at(i));
140 }
141 int newId = idBefore + delta;
142 for (QWizardPage *page : qAsConst(t&: pageList)) {
143 m_wizard->setPage(id: newId, page);
144 newId += delta;
145 }
146 } else {
147 // Gap found, just insert
148 m_wizard->setPage(id: newId, page: newPage);
149 }
150 // Might be at -1 after adding the first page
151 setCurrentIndex(index);
152}
153
154void QWizardContainer::remove(int index)
155{
156 if (index < 0)
157 return;
158
159 const auto idList = m_wizard->pageIds();
160 if (index >= idList.size())
161 return;
162
163 m_wizard->removePage(id: idList.at(i: index));
164 // goto next page, preferably
165 const int newSize = idList.size() - 1;
166 if (index < newSize) {
167 setCurrentIndex(index);
168 } else {
169 if (newSize > 0)
170 setCurrentIndex(newSize - 1);
171 }
172}
173
174// ---------------- QWizardPagePropertySheet
175const char *QWizardPagePropertySheet::pageIdProperty = "pageId";
176
177QWizardPagePropertySheet::QWizardPagePropertySheet(QWizardPage *object, QObject *parent) :
178 QDesignerPropertySheet(object, parent),
179 m_pageIdIndex(createFakeProperty(propertyName: QLatin1String(pageIdProperty), value: QString()))
180{
181 setAttribute(index: m_pageIdIndex, b: true);
182}
183
184bool QWizardPagePropertySheet::reset(int index)
185{
186 if (index == m_pageIdIndex) {
187 setProperty(index, value: QString());
188 return true;
189 }
190 return QDesignerPropertySheet::reset(index);
191}
192
193// ---------------- QWizardPropertySheet
194QWizardPropertySheet::QWizardPropertySheet(QWizard *object, QObject *parent) :
195 QDesignerPropertySheet(object, parent),
196 m_startId(QStringLiteral("startId"))
197{
198}
199
200bool QWizardPropertySheet::isVisible(int index) const
201{
202 if (propertyName(index) == m_startId)
203 return false;
204 return QDesignerPropertySheet::isVisible(index);
205}
206}
207
208QT_END_NAMESPACE
209

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