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 
48 enum { debugSettings = 0 };
49 
50 QT_BEGIN_NAMESPACE
51 
52 static const char *newFormShowKey = "newFormDialog/ShowOnStartup";
53 
54 // Change the version whenever the arrangement changes significantly.
55 static const char *mainWindowStateKey = "MainWindowState45";
56 static const char *toolBarsStateKey = "ToolBarsState45";
57 
58 static const char *backupOrgListKey = "backup/fileListOrg";
59 static const char *backupBakListKey = "backup/fileListBak";
60 static const char *recentFilesListKey = "recentFilesList";
61 
QDesignerSettings(QDesignerFormEditorInterface * core)62 QDesignerSettings::QDesignerSettings(QDesignerFormEditorInterface *core) :
63     qdesigner_internal::QDesignerSharedSettings(core)
64 {
65 }
66 
setValue(const QString & key,const QVariant & value)67 void QDesignerSettings::setValue(const QString &key, const QVariant &value)
68 {
69     settings()->setValue(key, value);
70 }
71 
value(const QString & key,const QVariant & defaultValue) const72 QVariant QDesignerSettings::value(const QString &key, const QVariant &defaultValue) const
73 {
74     return settings()->value(key, defaultValue);
75 }
76 
modeChar(UIMode mode)77 static inline QChar modeChar(UIMode mode)
78 {
79     return QLatin1Char(static_cast<char>(mode) + '0');
80 }
81 
saveGeometryFor(const QWidget * w)82 void 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(w->objectName());
90     s->setValue(QStringLiteral("visible"), visible);
91     s->setValue(QStringLiteral("geometry"), w->saveGeometry());
92     s->endGroup();
93 }
94 
restoreGeometry(QWidget * w,QRect fallBack) const95 void 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 + QStringLiteral("/geometry")).toByteArray());
100     const bool visible = settings()->value(key + QStringLiteral("/visible"), 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(ba);
116     }
117 
118     if (visible)
119         w->show();
120 }
121 
recentFilesList() const122 QStringList QDesignerSettings::recentFilesList() const
123 {
124     return settings()->value(QLatin1String(recentFilesListKey)).toStringList();
125 }
126 
setRecentFilesList(const QStringList & sl)127 void QDesignerSettings::setRecentFilesList(const QStringList &sl)
128 {
129     settings()->setValue(QLatin1String(recentFilesListKey), sl);
130 }
131 
setShowNewFormOnStartup(bool showIt)132 void QDesignerSettings::setShowNewFormOnStartup(bool showIt)
133 {
134     settings()->setValue(QLatin1String(newFormShowKey), showIt);
135 }
136 
showNewFormOnStartup() const137 bool QDesignerSettings::showNewFormOnStartup() const
138 {
139     return settings()->value(QLatin1String(newFormShowKey), true).toBool();
140 }
141 
mainWindowState(UIMode mode) const142 QByteArray QDesignerSettings::mainWindowState(UIMode mode) const
143 {
144     return settings()->value(QLatin1String(mainWindowStateKey) + modeChar(mode)).toByteArray();
145 }
146 
setMainWindowState(UIMode mode,const QByteArray & mainWindowState)147 void QDesignerSettings::setMainWindowState(UIMode mode, const QByteArray &mainWindowState)
148 {
149     settings()->setValue(QLatin1String(mainWindowStateKey) + modeChar(mode), mainWindowState);
150 }
151 
toolBarsState(UIMode mode) const152 QByteArray QDesignerSettings::toolBarsState(UIMode mode) const
153 {
154     QString key = QLatin1String(toolBarsStateKey);
155     key += modeChar(mode);
156     return settings()->value(key).toByteArray();
157 }
158 
setToolBarsState(UIMode mode,const QByteArray & toolBarsState)159 void QDesignerSettings::setToolBarsState(UIMode mode, const QByteArray &toolBarsState)
160 {
161     QString key = QLatin1String(toolBarsStateKey);
162     key += modeChar(mode);
163     settings()->setValue(key, toolBarsState);
164 }
165 
clearBackup()166 void QDesignerSettings::clearBackup()
167 {
168     QDesignerSettingsInterface *s = settings();
169     s->remove(QLatin1String(backupOrgListKey));
170     s->remove(QLatin1String(backupBakListKey));
171 }
172 
setBackup(const QMap<QString,QString> & map)173 void 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(QLatin1String(backupOrgListKey), org);
180     s->setValue(QLatin1String(backupBakListKey), bak);
181 }
182 
backup() const183 QMap<QString, QString> QDesignerSettings::backup() const
184 {
185     const QStringList org = settings()->value(QLatin1String(backupOrgListKey), QStringList()).toStringList();
186     const QStringList bak = settings()->value(QLatin1String(backupBakListKey), 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(org.at(i), bak.at(i));
192 
193     return map;
194 }
195 
setUiMode(UIMode mode)196 void QDesignerSettings::setUiMode(UIMode mode)
197 {
198     QDesignerSettingsInterface *s = settings();
199     s->beginGroup(QStringLiteral("UI"));
200     s->setValue(QStringLiteral("currentMode"), mode);
201     s->endGroup();
202 }
203 
uiMode() const204 UIMode 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"), defaultMode).toInt());
212     return uiMode;
213 }
214 
setToolWindowFont(const ToolWindowFontSettings & fontSettings)215 void QDesignerSettings::setToolWindowFont(const ToolWindowFontSettings &fontSettings)
216 {
217     QDesignerSettingsInterface *s = settings();
218     s->beginGroup(QStringLiteral("UI"));
219     s->setValue(QStringLiteral("font"), fontSettings.m_font);
220     s->setValue(QStringLiteral("useFont"), fontSettings.m_useFont);
221     s->setValue(QStringLiteral("writingSystem"), fontSettings.m_writingSystem);
222     s->endGroup();
223 }
224 
toolWindowFont() const225 ToolWindowFontSettings QDesignerSettings::toolWindowFont() const
226 {
227     ToolWindowFontSettings fontSettings;
228     fontSettings.m_writingSystem =
229             static_cast<QFontDatabase::WritingSystem>(value(QStringLiteral("UI/writingSystem"),
230                                                             QFontDatabase::Any).toInt());
231     fontSettings.m_font = qvariant_cast<QFont>(value(QStringLiteral("UI/font")));
232     fontSettings.m_useFont =
233             settings()->value(QStringLiteral("UI/useFont"), QVariant(false)).toBool();
234     return fontSettings;
235 }
236 
237 QT_END_NAMESPACE
238