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// Qt-Security score:significant reason:default
4
6
7#include <QtDesigner/qextensionmanager.h>
8#include <QtDesigner/abstractformeditor.h>
9
10#include <QtWidgets/qwizard.h>
11#include <QtCore/qdebug.h>
12#include <QtCore/qlist.h>
13
15
16using namespace Qt::StringLiterals;
17
18using WizardPageList = QList<QWizardPage *>;
19
20namespace qdesigner_internal {
21
22QWizardContainer::QWizardContainer(QWizard *widget, QObject *parent) :
23 QObject(parent),
24 m_wizard(widget)
25{
26}
27
29{
30 return m_wizard->pageIds().size();
31}
32
34{
35 QWidget *rc = nullptr;
36 if (index >= 0) {
37 const auto idList = m_wizard->pageIds();
38 if (index < idList.size())
39 rc = m_wizard->page(idList.at(index));
40 }
41 return rc;
42}
43
45{
46 return m_wizard->pageIds().indexOf(m_wizard->currentId());
47}
48
50{
51 if (index < 0 || m_wizard->pageIds().isEmpty())
52 return;
53
54 int currentIdx = currentIndex();
55
56 if (currentIdx == -1) {
57 m_wizard->restart();
58 currentIdx = currentIndex();
59 }
60
61 if (currentIdx == index)
62 return;
63
64 const int d = qAbs(index - currentIdx);
65 if (index > currentIdx) {
66 for (int i = 0; i < d; i++)
67 m_wizard->next();
68 } else {
69 for (int i = 0; i < d; i++)
70 m_wizard->back();
71 }
72}
73
74static const char msgWrongType[] = "** WARNING Attempt to add oject that is not of class WizardPage to a QWizard";
75
76void QWizardContainer::addWidget(QWidget *widget)
77{
78 QWizardPage *page = qobject_cast<QWizardPage *>(widget);
79 if (!page) {
80 qWarning("%s", msgWrongType);
81 return;
82 }
83 m_wizard->addPage(page);
84 // Might be -1 after adding the first page
85 setCurrentIndex(m_wizard->pageIds().size() - 1);
86}
87
88void QWizardContainer::insertWidget(int index, QWidget *widget)
89{
90 enum { delta = 5 };
91
92 QWizardPage *newPage = qobject_cast<QWizardPage *>(widget);
93 if (!newPage) {
94 qWarning("%s", msgWrongType);
95 return;
96 }
97
98 const auto idList = m_wizard->pageIds();
99 const auto pageCount = idList.size();
100 if (index >= pageCount) {
101 addWidget(widget);
102 return;
103 }
104
105 // Insert before, reshuffle ids if required
106 const int idBefore = idList.at(index);
107 const int newId = idBefore - 1;
108 const bool needsShuffle =
109 (index == 0 && newId < 0) // At start: QWizard refuses to insert id -1
110 || (index > 0 && idList.at(index - 1) == newId); // In-between
111 if (needsShuffle) {
112 // Create a gap by shuffling pages
113 WizardPageList pageList;
114 pageList.push_back(newPage);
115 for (qsizetype i = index; i < pageCount; ++i) {
116 pageList.push_back(m_wizard->page(idList.at(i)));
117 m_wizard->removePage(idList.at(i));
118 }
119 int newId = idBefore + delta;
120 for (QWizardPage *page : std::as_const(pageList)) {
121 m_wizard->setPage(newId, page);
122 newId += delta;
123 }
124 } else {
125 // Gap found, just insert
126 m_wizard->setPage(newId, newPage);
127 }
128 // Might be at -1 after adding the first page
130}
131
132void QWizardContainer::remove(int index)
133{
134 if (index < 0)
135 return;
136
137 const auto idList = m_wizard->pageIds();
138 if (index >= idList.size())
139 return;
140
141 m_wizard->removePage(idList.at(index));
142 // goto next page, preferably
143 const int newSize = idList.size() - 1;
144 if (index < newSize) {
146 } else {
147 if (newSize > 0)
148 setCurrentIndex(newSize - 1);
149 }
150}
151
152// ---------------- QWizardPagePropertySheet
153const char *QWizardPagePropertySheet::pageIdProperty = "pageId";
154
155QWizardPagePropertySheet::QWizardPagePropertySheet(QWizardPage *object, QObject *parent) :
156 QDesignerPropertySheet(object, parent),
157 m_pageIdIndex(createFakeProperty(QLatin1StringView(pageIdProperty), QString()))
158{
159 setAttribute(m_pageIdIndex, true);
160}
161
163{
164 if (index == m_pageIdIndex) {
165 setProperty(index, QString());
166 return true;
167 }
168 return QDesignerPropertySheet::reset(index);
169}
170
171// ---------------- QWizardPropertySheet
172QWizardPropertySheet::QWizardPropertySheet(QWizard *object, QObject *parent) :
173 QDesignerPropertySheet(object, parent),
174 m_startId(u"startId"_s)
175{
176}
177
178bool QWizardPropertySheet::isVisible(int index) const
179{
180 if (propertyName(index) == m_startId)
181 return false;
182 return QDesignerPropertySheet::isVisible(index);
183}
184}
185
186QT_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[]