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
formwindowsettings.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#include "ui_formwindowsettings.h"
7
8#include <formwindowbase_p.h>
9#include <grid_p.h>
10
11#include <QtWidgets/qstyle.h>
12
13#include <QtCore/qcompare.h>
14#include <QtCore/qregularexpression.h>
15#include <QtCore/qdebug.h>
16
17#include <algorithm>
18
19QT_BEGIN_NAMESPACE
20
21using namespace Qt::StringLiterals;
22
23namespace qdesigner_internal {
24
25// Data structure containing form dialog data providing comparison
54
55QDebug operator<<(QDebug str, const FormWindowData &d)
56{
57 str.nospace() << "LayoutDefault=" << d.layoutDefaultEnabled << ',' << d.defaultMargin
58 << ',' << d.defaultSpacing << " LayoutFunctions=" << d.layoutFunctionsEnabled << ','
59 << d.marginFunction << ',' << d.spacingFunction << " PixFunction="
60 << d.pixFunction << " Author=" << d.author << " Hints=" << d.includeHints
61 << " Grid=" << d.hasFormGrid << d.grid.deltaX() << d.grid.deltaY()
62 << " ID-based translations" << d.idBasedTranslations
63 << " ID-based translation label" << d.idBasedTranslationLabel
64 << " Connect slots by name" << d.connectSlotsByName
65 << '\n';
66 return str;
67}
68
69bool comparesEqual(const FormWindowData &lhs, const FormWindowData &rhs) noexcept
70{
75 lhs.marginFunction == rhs.marginFunction &&
76 lhs.spacingFunction == rhs.spacingFunction &&
77 lhs.pixFunction == rhs.pixFunction &&
78 lhs.author == rhs.author &&
79 lhs.includeHints == rhs.includeHints &&
80 lhs.hasFormGrid == rhs.hasFormGrid &&
81 lhs.grid == rhs.grid &&
83 lhs.idBasedTranslationLabel== rhs.idBasedTranslationLabel &&
85}
86
87void FormWindowData::fromFormWindow(FormWindowBase* fw)
88{
89 defaultMargin = defaultSpacing = INT_MIN;
90 fw->layoutDefault(&defaultMargin, &defaultSpacing);
91
92 auto container = fw->formContainer();
93 QStyle *style = container->style();
94 layoutDefaultEnabled = defaultMargin != INT_MIN || defaultSpacing != INT_MIN;
95 if (defaultMargin == INT_MIN)
96 defaultMargin = style->pixelMetric(QStyle::PM_LayoutLeftMargin, nullptr, container);
97 if (defaultSpacing == INT_MIN)
98 defaultSpacing = style->pixelMetric(QStyle::PM_LayoutHorizontalSpacing, nullptr);
99
100
101 marginFunction.clear();
102 spacingFunction.clear();
103 fw->layoutFunction(&marginFunction, &spacingFunction);
104 layoutFunctionsEnabled = !marginFunction.isEmpty() || !spacingFunction.isEmpty();
105
106 pixFunction = fw->pixmapFunction();
107
108 author = fw->author();
109
110 includeHints = fw->includeHints();
111 includeHints.removeAll(QString());
112
113 hasFormGrid = fw->hasFormGrid();
114 grid = hasFormGrid ? fw->designerGrid() : FormWindowBase::defaultDesignerGrid();
115 idBasedTranslations = fw->useIdBasedTranslations();
116 idBasedTranslationLabel = fw->idBasedTranslationLabel();
117 connectSlotsByName = fw->connectSlotsByName();
118}
119
120void FormWindowData::applyToFormWindow(FormWindowBase* fw) const
121{
122 fw->setAuthor(author);
123 fw->setPixmapFunction(pixFunction);
124
126 fw->setLayoutDefault(defaultMargin, defaultSpacing);
127 } else {
128 fw->setLayoutDefault(INT_MIN, INT_MIN);
129 }
130
132 fw->setLayoutFunction(marginFunction, spacingFunction);
133 } else {
134 fw->setLayoutFunction(QString(), QString());
135 }
136
137 fw->setIncludeHints(includeHints);
138
139 const bool hadFormGrid = fw->hasFormGrid();
140 fw->setHasFormGrid(hasFormGrid);
141 if (hasFormGrid || hadFormGrid != hasFormGrid)
142 fw->setDesignerGrid(hasFormGrid ? grid : FormWindowBase::defaultDesignerGrid());
143 fw->setUseIdBasedTranslations(idBasedTranslations);
144 fw->setIdBasedTranslationLabel(idBasedTranslationLabel);
145 fw->setConnectSlotsByName(connectSlotsByName);
146}
147
148// -------------------------- FormWindowSettings
149
150FormWindowSettings::FormWindowSettings(QDesignerFormWindowInterface *parent) :
151 QDialog(parent),
152 m_ui(new QT_PREPEND_NAMESPACE(Ui)::FormWindowSettings),
153 m_formWindow(qobject_cast<FormWindowBase*>(parent)),
154 m_oldData(new FormWindowData)
155{
156 Q_ASSERT(m_formWindow);
157
158 m_ui->setupUi(this);
159 m_ui->gridPanel->setCheckable(true);
160 m_ui->gridPanel->setResetButtonVisible(false);
161
162 QString deviceProfileName = m_formWindow->deviceProfileName();
163 if (deviceProfileName.isEmpty())
164 deviceProfileName = tr("None");
165 m_ui->deviceProfileLabel->setText(tr("Device Profile: %1").arg(deviceProfileName));
166
167 m_oldData->fromFormWindow(m_formWindow);
168 setData(*m_oldData);
169}
170
172{
173 delete m_oldData;
174 delete m_ui;
175}
176
178{
180 rc.author = m_ui->authorLineEdit->text();
181
182 if (m_ui->pixmapFunctionGroupBox->isChecked()) {
183 rc.pixFunction = m_ui->pixmapFunctionLineEdit->text();
184 } else {
185 rc.pixFunction.clear();
186 }
187
188 rc.layoutDefaultEnabled = m_ui->layoutDefaultGroupBox->isChecked();
189 rc.defaultMargin = m_ui->defaultMarginSpinBox->value();
190 rc.defaultSpacing = m_ui->defaultSpacingSpinBox->value();
191
192 rc.layoutFunctionsEnabled = m_ui->layoutFunctionGroupBox->isChecked();
193 rc.marginFunction = m_ui->marginFunctionLineEdit->text();
194 rc.spacingFunction = m_ui->spacingFunctionLineEdit->text();
195
196 const QString hints = m_ui->includeHintsTextEdit->toPlainText();
197 if (!hints.isEmpty()) {
198 rc.includeHints = hints.split(u'\n');
199 // Purge out any lines consisting of blanks only
200 const QRegularExpression blankLine(u"^\\s*$"_s);
201 Q_ASSERT(blankLine.isValid());
202 rc.includeHints.erase(std::remove_if(rc.includeHints.begin(), rc.includeHints.end(),
203 [blankLine](const QString &hint){ return blankLine.match(hint).hasMatch(); }),
204 rc.includeHints.end());
205 }
206
207 rc.hasFormGrid = m_ui->gridPanel->isChecked();
208 rc.grid = m_ui->gridPanel->grid();
209 rc.idBasedTranslations = m_ui->idBasedTranslationsCheckBox->isChecked();
210 rc.idBasedTranslationLabel = m_ui->idBasedTranslationLabelLineEdit->text();
211 rc.connectSlotsByName = m_ui->connectSlotsByNameCheckBox->isChecked();
212 return rc;
213}
214
215void FormWindowSettings::setData(const FormWindowData &data)
216{
217 m_ui->layoutDefaultGroupBox->setChecked(data.layoutDefaultEnabled);
218 m_ui->defaultMarginSpinBox->setValue(data.defaultMargin);
219 m_ui->defaultSpacingSpinBox->setValue(data.defaultSpacing);
220
221 m_ui->layoutFunctionGroupBox->setChecked(data.layoutFunctionsEnabled);
222 m_ui->marginFunctionLineEdit->setText(data.marginFunction);
223 m_ui->spacingFunctionLineEdit->setText(data.spacingFunction);
224
225 m_ui->pixmapFunctionLineEdit->setText(data.pixFunction);
226 m_ui->pixmapFunctionGroupBox->setChecked(!data.pixFunction.isEmpty());
227
228 m_ui->authorLineEdit->setText(data.author);
229
230 if (data.includeHints.isEmpty()) {
231 m_ui->includeHintsTextEdit->clear();
232 } else {
233 m_ui->includeHintsTextEdit->setText(data.includeHints.join(u'\n'));
234 }
235
236 m_ui->gridPanel->setChecked(data.hasFormGrid);
237 m_ui->gridPanel->setGrid(data.grid);
238 m_ui->idBasedTranslationsCheckBox->setChecked(data.idBasedTranslations);
239 m_ui->idBasedTranslationLabelLineEdit->setText(data.idBasedTranslationLabel);
240 m_ui->connectSlotsByNameCheckBox->setChecked(data.connectSlotsByName);
241}
242
244{
245 // Anything changed? -> Apply and set dirty
246 const FormWindowData newData = data();
247 if (newData != *m_oldData) {
248 newData.applyToFormWindow(m_formWindow);
249 m_formWindow->setDirty(true);
250 }
251
252 QDialog::accept();
253}
254}
255QT_END_NAMESPACE
void accept() override
Hides the modal dialog and sets the result code to Accepted.
Auxiliary methods to store/retrieve settings.
QDebug operator<<(QDebug str, const FormWindowData &d)
void applyToFormWindow(FormWindowBase *fw) const
void fromFormWindow(FormWindowBase *fw)
friend bool comparesEqual(const FormWindowData &lhs, const FormWindowData &rhs) noexcept