Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qwizard_container.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
5
6#include <QtDesigner/qextensionmanager.h>
7#include <QtDesigner/abstractformeditor.h>
8
9#include <QtWidgets/qwizard.h>
10#include <QtCore/qdebug.h>
11#include <QtCore/qlist.h>
12
14
15using namespace Qt::StringLiterals;
16
17using WizardPageList = QList<QWizardPage *>;
18
19namespace qdesigner_internal {
20
21QWizardContainer::QWizardContainer(QWizard *widget, QObject *parent) :
22 QObject(parent),
23 m_wizard(widget)
24{
25}
26
28{
29 return m_wizard->pageIds().size();
30}
31
33{
34 QWidget *rc = nullptr;
35 if (index >= 0) {
36 const auto idList = m_wizard->pageIds();
37 if (index < idList.size())
38 rc = m_wizard->page(idList.at(index));
39 }
40 return rc;
41}
42
44{
45 return m_wizard->pageIds().indexOf(m_wizard->currentId());
46}
47
49{
50 if (index < 0 || m_wizard->pageIds().isEmpty())
51 return;
52
53 int currentIdx = currentIndex();
54
55 if (currentIdx == -1) {
56 m_wizard->restart();
57 currentIdx = currentIndex();
58 }
59
60 if (currentIdx == index)
61 return;
62
63 const int d = qAbs(index - currentIdx);
64 if (index > currentIdx) {
65 for (int i = 0; i < d; i++)
66 m_wizard->next();
67 } else {
68 for (int i = 0; i < d; i++)
69 m_wizard->back();
70 }
71}
72
73static const char msgWrongType[] = "** WARNING Attempt to add oject that is not of class WizardPage to a QWizard";
74
75void QWizardContainer::addWidget(QWidget *widget)
76{
77 QWizardPage *page = qobject_cast<QWizardPage *>(widget);
78 if (!page) {
79 qWarning("%s", msgWrongType);
80 return;
81 }
82 m_wizard->addPage(page);
83 // Might be -1 after adding the first page
84 setCurrentIndex(m_wizard->pageIds().size() - 1);
85}
86
87void QWizardContainer::insertWidget(int index, QWidget *widget)
88{
89 enum { delta = 5 };
90
91 QWizardPage *newPage = qobject_cast<QWizardPage *>(widget);
92 if (!newPage) {
93 qWarning("%s", msgWrongType);
94 return;
95 }
96
97 const auto idList = m_wizard->pageIds();
98 const auto pageCount = idList.size();
99 if (index >= pageCount) {
100 addWidget(widget);
101 return;
102 }
103
104 // Insert before, reshuffle ids if required
105 const int idBefore = idList.at(index);
106 const int newId = idBefore - 1;
107 const bool needsShuffle =
108 (index == 0 && newId < 0) // At start: QWizard refuses to insert id -1
109 || (index > 0 && idList.at(index - 1) == newId); // In-between
110 if (needsShuffle) {
111 // Create a gap by shuffling pages
112 WizardPageList pageList;
113 pageList.push_back(newPage);
114 for (qsizetype i = index; i < pageCount; ++i) {
115 pageList.push_back(m_wizard->page(idList.at(i)));
116 m_wizard->removePage(idList.at(i));
117 }
118 int newId = idBefore + delta;
119 for (QWizardPage *page : std::as_const(pageList)) {
120 m_wizard->setPage(newId, page);
121 newId += delta;
122 }
123 } else {
124 // Gap found, just insert
125 m_wizard->setPage(newId, newPage);
126 }
127 // Might be at -1 after adding the first page
129}
130
131void QWizardContainer::remove(int index)
132{
133 if (index < 0)
134 return;
135
136 const auto idList = m_wizard->pageIds();
137 if (index >= idList.size())
138 return;
139
140 m_wizard->removePage(idList.at(index));
141 // goto next page, preferably
142 const int newSize = idList.size() - 1;
143 if (index < newSize) {
145 } else {
146 if (newSize > 0)
147 setCurrentIndex(newSize - 1);
148 }
149}
150
151// ---------------- QWizardPagePropertySheet
152const char *QWizardPagePropertySheet::pageIdProperty = "pageId";
153
154QWizardPagePropertySheet::QWizardPagePropertySheet(QWizardPage *object, QObject *parent) :
155 QDesignerPropertySheet(object, parent),
156 m_pageIdIndex(createFakeProperty(QLatin1StringView(pageIdProperty), QString()))
157{
158 setAttribute(m_pageIdIndex, true);
159}
160
162{
163 if (index == m_pageIdIndex) {
164 setProperty(index, QString());
165 return true;
166 }
167 return QDesignerPropertySheet::reset(index);
168}
169
170// ---------------- QWizardPropertySheet
171QWizardPropertySheet::QWizardPropertySheet(QWizard *object, QObject *parent) :
172 QDesignerPropertySheet(object, parent),
173 m_startId(u"startId"_s)
174{
175}
176
177bool QWizardPropertySheet::isVisible(int index) const
178{
179 if (propertyName(index) == m_startId)
180 return false;
181 return QDesignerPropertySheet::isVisible(index);
182}
183}
184
185QT_END_NAMESPACE
QWidget * widget(int index) const override
bool isVisible(int index) const override
Combined button and popup list for selecting options.
Auxiliary methods to store/retrieve settings.
static const char msgWrongType[]