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 "qdesigner.h"
30#include "qdesigner_settings.h"
31#include "qdesigner_toolwindow.h"
32#include "qdesigner_workbench.h"
33
34#include <QtDesigner/abstractsettings.h>
35
36#include <abstractformeditor.h>
37#include <qdesigner_utils_p.h>
38#include <previewmanager_p.h>
39
40#include <QtCore/qvariant.h>
41#include <QtCore/qdir.h>
42
43#include <QtWidgets/qstyle.h>
44#include <QtWidgets/qlistview.h>
45
46#include <QtCore/qdebug.h>
47
48enum { debugSettings = 0 };
49
50QT_BEGIN_NAMESPACE
51
52static const char *newFormShowKey = "newFormDialog/ShowOnStartup";
53
54// Change the version whenever the arrangement changes significantly.
55static const char *mainWindowStateKey = "MainWindowState45";
56static const char *toolBarsStateKey = "ToolBarsState45";
57
58static const char *backupOrgListKey = "backup/fileListOrg";
59static const char *backupBakListKey = "backup/fileListBak";
60static const char *recentFilesListKey = "recentFilesList";
61
62QDesignerSettings::QDesignerSettings(QDesignerFormEditorInterface *core) :
63 qdesigner_internal::QDesignerSharedSettings(core)
64{
65}
66
67void QDesignerSettings::setValue(const QString &key, const QVariant &value)
68{
69 settings()->setValue(key, value);
70}
71
72QVariant QDesignerSettings::value(const QString &key, const QVariant &defaultValue) const
73{
74 return settings()->value(key, defaultValue);
75}
76
77static inline QChar modeChar(UIMode mode)
78{
79 return QLatin1Char(static_cast<char>(mode) + '0');
80}
81
82void QDesignerSettings::saveGeometryFor(const QWidget *w)
83{
84 Q_ASSERT(w && !w->objectName().isEmpty());
85 QDesignerSettingsInterface *s = settings();
86 const bool visible = w->isVisible();
87 if (debugSettings)
88 qDebug() << Q_FUNC_INFO << w << "visible=" << visible;
89 s->beginGroup(prefix: w->objectName());
90 s->setValue(QStringLiteral("visible"), value: visible);
91 s->setValue(QStringLiteral("geometry"), value: w->saveGeometry());
92 s->endGroup();
93}
94
95void QDesignerSettings::restoreGeometry(QWidget *w, QRect fallBack) const
96{
97 Q_ASSERT(w && !w->objectName().isEmpty());
98 const QString key = w->objectName();
99 const QByteArray ba(settings()->value(key: key + QStringLiteral("/geometry")).toByteArray());
100 const bool visible = settings()->value(key: key + QStringLiteral("/visible"), defaultValue: true).toBool();
101
102 if (debugSettings)
103 qDebug() << Q_FUNC_INFO << w << fallBack << "visible=" << visible;
104 if (ba.isEmpty()) {
105 /// Apply default geometry, check for null and maximal size
106 if (fallBack.isNull())
107 fallBack = QRect(QPoint(0, 0), w->sizeHint());
108 if (fallBack.size() == QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX)) {
109 w->setWindowState(w->windowState() | Qt::WindowMaximized);
110 } else {
111 w->move(fallBack.topLeft());
112 w->resize(fallBack.size());
113 }
114 } else {
115 w->restoreGeometry(geometry: ba);
116 }
117
118 if (visible)
119 w->show();
120}
121
122QStringList QDesignerSettings::recentFilesList() const
123{
124 return settings()->value(key: QLatin1String(recentFilesListKey)).toStringList();
125}
126
127void QDesignerSettings::setRecentFilesList(const QStringList &sl)
128{
129 settings()->setValue(key: QLatin1String(recentFilesListKey), value: sl);
130}
131
132void QDesignerSettings::setShowNewFormOnStartup(bool showIt)
133{
134 settings()->setValue(key: QLatin1String(newFormShowKey), value: showIt);
135}
136
137bool QDesignerSettings::showNewFormOnStartup() const
138{
139 return settings()->value(key: QLatin1String(newFormShowKey), defaultValue: true).toBool();
140}
141
142QByteArray QDesignerSettings::mainWindowState(UIMode mode) const
143{
144 return settings()->value(key: QLatin1String(mainWindowStateKey) + modeChar(mode)).toByteArray();
145}
146
147void QDesignerSettings::setMainWindowState(UIMode mode, const QByteArray &mainWindowState)
148{
149 settings()->setValue(key: QLatin1String(mainWindowStateKey) + modeChar(mode), value: mainWindowState);
150}
151
152QByteArray QDesignerSettings::toolBarsState(UIMode mode) const
153{
154 QString key = QLatin1String(toolBarsStateKey);
155 key += modeChar(mode);
156 return settings()->value(key).toByteArray();
157}
158
159void QDesignerSettings::setToolBarsState(UIMode mode, const QByteArray &toolBarsState)
160{
161 QString key = QLatin1String(toolBarsStateKey);
162 key += modeChar(mode);
163 settings()->setValue(key, value: toolBarsState);
164}
165
166void QDesignerSettings::clearBackup()
167{
168 QDesignerSettingsInterface *s = settings();
169 s->remove(key: QLatin1String(backupOrgListKey));
170 s->remove(key: QLatin1String(backupBakListKey));
171}
172
173void QDesignerSettings::setBackup(const QMap<QString, QString> &map)
174{
175 const QStringList org = map.keys();
176 const QStringList bak = map.values();
177
178 QDesignerSettingsInterface *s = settings();
179 s->setValue(key: QLatin1String(backupOrgListKey), value: org);
180 s->setValue(key: QLatin1String(backupBakListKey), value: bak);
181}
182
183QMap<QString, QString> QDesignerSettings::backup() const
184{
185 const QStringList org = settings()->value(key: QLatin1String(backupOrgListKey), defaultValue: QStringList()).toStringList();
186 const QStringList bak = settings()->value(key: QLatin1String(backupBakListKey), defaultValue: QStringList()).toStringList();
187
188 QMap<QString, QString> map;
189 const int orgCount = org.count();
190 for (int i = 0; i < orgCount; ++i)
191 map.insert(akey: org.at(i), avalue: bak.at(i));
192
193 return map;
194}
195
196void QDesignerSettings::setUiMode(UIMode mode)
197{
198 QDesignerSettingsInterface *s = settings();
199 s->beginGroup(QStringLiteral("UI"));
200 s->setValue(QStringLiteral("currentMode"), value: mode);
201 s->endGroup();
202}
203
204UIMode QDesignerSettings::uiMode() const
205{
206#ifdef Q_OS_MACOS
207 const UIMode defaultMode = TopLevelMode;
208#else
209 const UIMode defaultMode = DockedMode;
210#endif
211 UIMode uiMode = static_cast<UIMode>(value(QStringLiteral("UI/currentMode"), defaultValue: defaultMode).toInt());
212 return uiMode;
213}
214
215void QDesignerSettings::setToolWindowFont(const ToolWindowFontSettings &fontSettings)
216{
217 QDesignerSettingsInterface *s = settings();
218 s->beginGroup(QStringLiteral("UI"));
219 s->setValue(QStringLiteral("font"), value: fontSettings.m_font);
220 s->setValue(QStringLiteral("useFont"), value: fontSettings.m_useFont);
221 s->setValue(QStringLiteral("writingSystem"), value: fontSettings.m_writingSystem);
222 s->endGroup();
223}
224
225ToolWindowFontSettings QDesignerSettings::toolWindowFont() const
226{
227 ToolWindowFontSettings fontSettings;
228 fontSettings.m_writingSystem =
229 static_cast<QFontDatabase::WritingSystem>(value(QStringLiteral("UI/writingSystem"),
230 defaultValue: QFontDatabase::Any).toInt());
231 fontSettings.m_font = qvariant_cast<QFont>(v: value(QStringLiteral("UI/font")));
232 fontSettings.m_useFont =
233 settings()->value(QStringLiteral("UI/useFont"), defaultValue: QVariant(false)).toBool();
234 return fontSettings;
235}
236
237QT_END_NAMESPACE
238

source code of qttools/src/designer/src/designer/qdesigner_settings.cpp