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
qdesigner_settings.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
5#include "qdesigner.h"
9
10#include <QtDesigner/abstractsettings.h>
11
12#include <abstractformeditor.h>
13#include <qdesigner_utils_p.h>
14#include <previewmanager_p.h>
15
16#include <QtCore/qvariant.h>
17#include <QtCore/qdir.h>
18
19#include <QtWidgets/qstyle.h>
20#include <QtWidgets/qlistview.h>
21
22#include <QtCore/qdebug.h>
23
24enum { debugSettings = 0 };
25
26QT_BEGIN_NAMESPACE
27
28using namespace Qt::StringLiterals;
29
30static constexpr auto newFormShowKey = "newFormDialog/ShowOnStartup"_L1;
31
32// Change the version whenever the arrangement changes significantly.
33static constexpr auto mainWindowStateKey = "MainWindowState45"_L1;
34static constexpr auto toolBarsStateKey = "ToolBarsState45"_L1;
35
36static constexpr auto backupOrgListKey = "backup/fileListOrg"_L1;
37static constexpr auto backupBakListKey = "backup/fileListBak"_L1;
38static constexpr auto recentFilesListKey = "recentFilesList"_L1;
39
40QDesignerSettings::QDesignerSettings(QDesignerFormEditorInterface *core) :
42{
43}
44
45void QDesignerSettings::setValue(const QString &key, const QVariant &value)
46{
47 settings()->setValue(key, value);
48}
49
50QVariant QDesignerSettings::value(const QString &key, const QVariant &defaultValue) const
51{
52 return settings()->value(key, defaultValue);
53}
54
55static inline QChar modeChar(UIMode mode)
56{
57 return QLatin1Char(static_cast<char>(mode) + '0');
58}
59
61{
62 Q_ASSERT(w && !w->objectName().isEmpty());
63 QDesignerSettingsInterface *s = settings();
64 const bool visible = w->isVisible();
65 if (debugSettings)
66 qDebug() << Q_FUNC_INFO << w << "visible=" << visible;
67 s->beginGroup(w->objectName());
68 s->setValue(u"visible"_s, visible);
69 s->setValue(u"geometry"_s, w->saveGeometry());
70 s->endGroup();
71}
72
73void QDesignerSettings::restoreGeometry(QWidget *w, QRect fallBack) const
74{
75 Q_ASSERT(w && !w->objectName().isEmpty());
76 const QString key = w->objectName();
77 const QByteArray ba(settings()->value(key + "/geometry"_L1).toByteArray());
78 const bool visible = settings()->value(key + "/visible"_L1, true).toBool();
79
80 if (debugSettings)
81 qDebug() << Q_FUNC_INFO << w << fallBack << "visible=" << visible;
82 if (ba.isEmpty()) {
83 /// Apply default geometry, check for null and maximal size
84 if (fallBack.isNull())
85 fallBack = QRect(QPoint(0, 0), w->sizeHint());
86 if (fallBack.size() == QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX)) {
87 w->setWindowState(w->windowState() | Qt::WindowMaximized);
88 } else {
89 w->move(fallBack.topLeft());
90 w->resize(fallBack.size());
91 }
92 } else {
93 w->restoreGeometry(ba);
94 }
95
96 if (visible)
97 w->show();
98}
99
101{
102 return settings()->value(recentFilesListKey).toStringList();
103}
104
105void QDesignerSettings::setRecentFilesList(const QStringList &sl)
106{
107 settings()->setValue(recentFilesListKey, sl);
108}
109
111{
112 settings()->setValue(newFormShowKey, showIt);
113}
114
116{
117 return settings()->value(newFormShowKey, true).toBool();
118}
119
121{
122 return settings()->value(mainWindowStateKey + modeChar(mode)).toByteArray();
123}
124
125void QDesignerSettings::setMainWindowState(UIMode mode, const QByteArray &mainWindowState)
126{
127 settings()->setValue(mainWindowStateKey + modeChar(mode), mainWindowState);
128}
129
131{
132 QString key = toolBarsStateKey;
133 key += modeChar(mode);
134 return settings()->value(key).toByteArray();
135}
136
137void QDesignerSettings::setToolBarsState(UIMode mode, const QByteArray &toolBarsState)
138{
139 QString key = toolBarsStateKey;
140 key += modeChar(mode);
141 settings()->setValue(key, toolBarsState);
142}
143
145{
146 QDesignerSettingsInterface *s = settings();
147 s->remove(backupOrgListKey);
148 s->remove(backupBakListKey);
149}
150
151void QDesignerSettings::setBackup(const QMap<QString, QString> &map)
152{
153 const QStringList org = map.keys();
154 const QStringList bak = map.values();
155
156 QDesignerSettingsInterface *s = settings();
157 s->setValue(backupOrgListKey, org);
158 s->setValue(backupBakListKey, bak);
159}
160
162{
163 const QStringList org = settings()->value(backupOrgListKey, QStringList()).toStringList();
164 const QStringList bak = settings()->value(backupBakListKey, QStringList()).toStringList();
165
166 QMap<QString, QString> map;
167 const qsizetype orgCount = org.size();
168 for (qsizetype i = 0; i < orgCount; ++i)
169 map.insert(org.at(i), bak.at(i));
170
171 return map;
172}
173
174void QDesignerSettings::setUiMode(UIMode mode)
175{
176 QDesignerSettingsInterface *s = settings();
177 s->beginGroup(u"UI"_s);
178 s->setValue(u"currentMode"_s, mode);
179 s->endGroup();
180}
181
183{
184 constexpr UIMode defaultMode = DockedMode;
185 UIMode uiMode = static_cast<UIMode>(value(u"UI/currentMode"_s, defaultMode).toInt());
186 return uiMode;
187}
188
190{
191 QDesignerSettingsInterface *s = settings();
192 s->beginGroup(u"UI"_s);
193 s->setValue(u"font"_s, fontSettings.m_font);
194 s->setValue(u"useFont"_s, fontSettings.m_useFont);
195 s->setValue(u"writingSystem"_s, fontSettings.m_writingSystem);
196 s->endGroup();
197}
198
200{
201 ToolWindowFontSettings fontSettings;
202 fontSettings.m_writingSystem =
203 static_cast<QFontDatabase::WritingSystem>(value(u"UI/writingSystem"_s,
204 QFontDatabase::Any).toInt());
205 fontSettings.m_font = qvariant_cast<QFont>(value(u"UI/font"_s));
206 fontSettings.m_useFont =
207 settings()->value(u"UI/useFont"_s, QVariant(false)).toBool();
208 return fontSettings;
209}
210
211QT_END_NAMESPACE
QStringList recentFilesList() const
void setToolWindowFont(const ToolWindowFontSettings &fontSettings)
void setShowNewFormOnStartup(bool showIt)
void setToolBarsState(UIMode mode, const QByteArray &mainWindowState)
void setMainWindowState(UIMode mode, const QByteArray &mainWindowState)
void setUiMode(UIMode mode)
void setBackup(const QMap< QString, QString > &map)
void restoreGeometry(QWidget *w, QRect fallBack=QRect()) const
QByteArray mainWindowState(UIMode mode) const
QDesignerSettings(QDesignerFormEditorInterface *core)
QMap< QString, QString > backup() const
QByteArray toolBarsState(UIMode mode) const
void setRecentFilesList(const QStringList &list)
ToolWindowFontSettings toolWindowFont() const
void saveGeometryFor(const QWidget *w)
bool showNewFormOnStartup() const
friend class QWidget
Definition qpainter.h:432
static constexpr auto newFormShowKey
static constexpr auto mainWindowStateKey
static constexpr auto toolBarsStateKey
static constexpr auto backupOrgListKey
static QChar modeChar(UIMode mode)
static constexpr auto backupBakListKey
static constexpr auto recentFilesListKey