1
/***************************************************************************
2
* Copyright (C) 2006-2009 by Thomas Schweitzer *
3
* thomas-schweitzer(at)arcor.de *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License version 2.0 as *
7
* published by the Free Software Foundation. *
9
* This program is distributed in the hope that it will be useful, *
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12
* GNU General Public License for more details. *
14
* You should have received a copy of the GNU General Public License *
15
* along with this program in the file LICENSE.GPL; if not, write to the *
16
* Free Software Foundation, Inc., *
17
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18
***************************************************************************/
20
#include "UiGuiSettings.h"
22
#include "SettingsPaths.h"
24
//! \defgroup grp_Settings All concerning the settings.
29
\brief Handles the settings of the program. Reads them on startup and saves them on exit.
30
Is a singleton class and can only be accessed via getInstance().
33
// Inits the single class instance pointer.
34
UiGuiSettings* UiGuiSettings::instance = NULL;
37
\brief The constructor for the settings.
39
UiGuiSettings::UiGuiSettings() : QObject() {
40
// Create the main application settings object from the UniversalIndentGUI.ini file.
41
qsettings = new QSettings(SettingsPaths::getSettingsPath() + "/UniversalIndentGUI.ini", QSettings::IniFormat, this);
43
indenterDirctoryStr = SettingsPaths::getGlobalFilesPath() + "/indenters";
44
readAvailableTranslations();
50
\brief Returns the instance of the settings class. If no instance exists, ONE will be created.
52
UiGuiSettings* UiGuiSettings::getInstance() {
53
if ( instance == NULL ) {
54
// Create the settings object, which loads all UiGui settings from a file.
55
instance = new UiGuiSettings();
62
\brief Deletes the existing instance of UiGuiSettings and removes the created temp dir.
64
void UiGuiSettings::deleteInstance() {
65
SettingsPaths::cleanAndRemoveTempDir();
66
if ( instance != NULL ) {
74
\brief The destructor saves the settings to a file.
76
UiGuiSettings::~UiGuiSettings() {
82
\brief Scans the translations directory for available translation files and
83
stores them in the QList \a availableTranslations.
85
void UiGuiSettings::readAvailableTranslations() {
86
QString languageShort;
87
QStringList languageFileList;
89
// English is the default language. A translation file does not exist but to have a menu entry, added here.
90
languageFileList << "universalindent_en.qm";
92
// Find all translation files in the "translations" directory.
93
QDir translationDirectory = QDir( SettingsPaths::getGlobalFilesPath() + "/translations" );
94
languageFileList << translationDirectory.entryList( QStringList("universalindent_*.qm") );
96
// Loop for each found translation file
97
foreach ( languageShort, languageFileList ) {
98
// Remove the leading string "universalindent_" from the filename.
99
languageShort.remove(0,16);
100
// Remove trailing file extension ".qm".
101
languageShort.chop(3);
103
availableTranslations.append(languageShort);
109
\brief Returns a list of the mnemonics of the available translations.
111
QStringList UiGuiSettings::getAvailableTranslations() {
112
return availableTranslations;
117
\brief Extern widgets can connect to this slot to change settings.
119
According to the objects property "connectedSettingName" the corresponding
120
setting is known and set.
122
void UiGuiSettings::handleValueChangeFromExtern(int value) {
124
// Get the corresponding setting name from the sender objects property and remove "DONOTTRANSLATE:" from its beginning..
125
QString settingName = sender()->property("connectedSettingName").toString().remove(0, 15);
126
// If the property is not set, try using the objects name for convenience.
127
if ( settingName.isEmpty() ) {
128
// Get the objects name and remove "uiGui" from its beginning and use that as setting name.
129
settingName = sender()->objectName();
130
settingName.remove(0,5);
133
// Set the value of the setting to the objects value.
134
setValueByName( settingName, value );
140
\brief Extern widgets can connect to this slot to change settings.
142
According to the objects property "connectedSettingName" the corresponding
143
setting is known and set.
145
void UiGuiSettings::handleValueChangeFromExtern(bool value) {
147
// Get the corresponding setting name from the sender objects property and remove "DONOTTRANSLATE:" from its beginning.
148
QString settingName = sender()->property("connectedSettingName").toString().remove(0, 15);
149
// If the property is not set, try using the objects name for convenience.
150
if ( settingName.isEmpty() ) {
151
// Get the objects name and remove "uiGui" from its beginning and use that as setting name.
152
settingName = sender()->objectName();
153
settingName.remove(0,5);
156
// Set the value of the setting to the objects value.
157
setValueByName( settingName, value );
163
\brief Extern widgets can connect to this slot to change settings.
165
According to the objects property "connectedSettingName" the corresponding
166
setting is known and set.
168
void UiGuiSettings::handleValueChangeFromExtern(QDate value) {
170
// Get the corresponding setting name from the sender objects property and remove "DONOTTRANSLATE:" from its beginning.
171
QString settingName = sender()->property("connectedSettingName").toString().remove(0, 15);
172
// If the property is not set, try using the objects name for convenience.
173
if ( settingName.isEmpty() ) {
174
// Get the objects name and remove "uiGui" from its beginning and use that as setting name.
175
settingName = sender()->objectName();
176
settingName.remove(0,5);
179
// Set the value of the setting to the objects value.
180
setValueByName( settingName, value );
186
\brief Extern widgets can connect to this slot to change settings.
188
According to the objects property "connectedSettingName" the corresponding
189
setting is known and set.
191
void UiGuiSettings::handleValueChangeFromExtern(QByteArray value) {
193
// Get the corresponding setting name from the sender objects property.
194
QString settingName = sender()->property("connectedSettingName").toString();
195
// If the property is not set, try using the objects name for convenience.
196
if ( settingName.isEmpty() ) {
197
// Get the objects name and remove "uiGui" from its beginning and use that as setting name.
198
settingName = sender()->objectName();
199
settingName.remove(0,5);
202
// Set the value of the setting to the objects value.
203
setValueByName( settingName, value );
209
\brief Sets the value of the by \a settingsName defined setting to the value \a value.
211
The to \a settingsName corresponding signal is emitted, if the value has changed.
213
bool UiGuiSettings::setValueByName(QString settingName, QVariant value) {
214
// Test if the named setting really exists.
215
if ( settings.contains(settingName) ) {
216
// Test if the new value is different to the one before.
217
if ( settings[settingName] != value ) {
218
// Set the new value.
219
settings[settingName] = value;
220
// Emit the signal for the changed setting.
221
emitSignalForSetting(settingName);
230
\brief Emits the correct signal for the given \a settingName.
232
If \a settingName equals "all", all signals are emitted. This can be used to update all
233
dependent widgets. \a value is the new value that is emitted along with the signal.
235
void UiGuiSettings::emitSignalForSetting(QString settingName) {
236
// Emit the signal for the changed value.
237
if ( settingName == "VersionInSettingsFile" ) emit versionInSettingsFile( settings[settingName].toString() );
238
else if ( settingName == "WindowIsMaximized" ) emit windowIsMaximized( settings[settingName].toBool() );
239
else if ( settingName == "WindowPosition" ) emit windowPosition( settings[settingName].toPoint() );
240
else if ( settingName == "WindowSize" ) emit windowSize( settings[settingName].toSize() );
241
else if ( settingName == "FileEncoding" ) emit fileEncoding( settings[settingName].toString() );
242
else if ( settingName == "RecentlyOpenedListSize" ) emit recentlyOpenedListSize( settings[settingName].toInt() );
243
else if ( settingName == "LoadLastOpenedFileOnStartup" ) emit loadLastOpenedFileOnStartup( settings[settingName].toBool() );
244
else if ( settingName == "LastOpenedFiles" ) emit lastOpenedFiles( settings[settingName].toString() );
245
else if ( settingName == "SelectedIndenter" ) emit selectedIndenter( settings[settingName].toInt() );
246
else if ( settingName == "SyntaxHighlightningEnabled" ) emit syntaxHighlightningEnabled( settings[settingName].toBool() );
247
else if ( settingName == "WhiteSpaceIsVisible" ) emit whiteSpaceIsVisible( settings[settingName].toBool() );
248
else if ( settingName == "IndenterParameterTooltipsEnabled" ) emit indenterParameterTooltipsEnabled( settings[settingName].toBool() );
249
else if ( settingName == "TabWidth" ) emit tabWidth( settings[settingName].toInt() );
250
else if ( settingName == "Language" ) emit language( settings[settingName].toInt() );
251
else if ( settingName == "CheckForUpdate" ) emit checkForUpdate( settings[settingName].toBool() );
252
else if ( settingName == "LastUpdateCheck" ) emit lastUpdateCheck( settings[settingName].toDate() );
253
else if ( settingName == "MainWindowState" ) emit mainWindowState( settings[settingName].toByteArray() );
254
else if ( settingName == "all" ) {
255
emit versionInSettingsFile( settings["VersionInSettingsFile"].toString() );
256
emit windowIsMaximized( settings["WindowIsMaximized"].toBool() );
257
emit windowPosition( settings["WindowPosition"].toPoint() );
258
emit windowSize( settings["WindowSize"].toSize() );
259
emit fileEncoding( settings["FileEncoding"].toString() );
260
emit recentlyOpenedListSize( settings["RecentlyOpenedListSize"].toInt() );
261
emit loadLastOpenedFileOnStartup( settings["LoadLastOpenedFileOnStartup"].toBool() );
262
emit lastOpenedFiles( settings["LastOpenedFiles"].toString() );
263
emit selectedIndenter( settings["SelectedIndenter"].toInt() );
264
emit syntaxHighlightningEnabled( settings["SyntaxHighlightningEnabled"].toBool() );
265
emit whiteSpaceIsVisible( settings["WhiteSpaceIsVisible"].toBool() );
266
emit indenterParameterTooltipsEnabled( settings["IndenterParameterTooltipsEnabled"].toBool() );
267
emit tabWidth( settings["TabWidth"].toInt() );
268
emit language( settings["Language"].toInt() );
269
emit checkForUpdate( settings["CheckForUpdate"].toBool() );
270
emit lastUpdateCheck( settings["LastUpdateCheck"].toDate() );
271
emit mainWindowState( settings["MainWindowState"].toByteArray() );
277
\brief Calls \sa emitSignalForSetting with settingName "all" to update all widgets or whatever
278
is connected to each setting.
280
void UiGuiSettings::updateAllDependend() {
281
emitSignalForSetting("all");
286
\brief Returns the value of the by \a settingsName defined setting as QVariant.
288
If the named setting does not exist, 0 is being returned.
290
QVariant UiGuiSettings::getValueByName(QString settingName) {
291
// Test if the named setting really exists.
292
if ( settings.contains(settingName) ) {
293
return settings[settingName];
300
\brief Loads the settings for the main application.
302
Settings are for example last selected indenter, last loaded source code file and so on.
304
bool UiGuiSettings::loadSettings() {
305
// Read the version string saved in the settings file.
306
settings["VersionInSettingsFile"] = qsettings->value("UniversalIndentGUI/version", "").toString();
308
// Read windows last size and position from the settings file.
309
settings["WindowIsMaximized"] = qsettings->value("UniversalIndentGUI/maximized", false).toBool();
310
settings["WindowPosition"] = qsettings->value("UniversalIndentGUI/position", QPoint(50, 50)).toPoint();
311
settings["WindowSize"] = qsettings->value("UniversalIndentGUI/size", QSize(800, 600)).toSize();
313
// Read last selected encoding for the opened source code file.
314
settings["FileEncoding"] = qsettings->value("UniversalIndentGUI/encoding", "UTF-8").toString();
316
// Read maximum length of list for recently opened files.
317
settings["RecentlyOpenedListSize"] = qsettings->value("UniversalIndentGUI/recentlyOpenedListSize", 5).toInt();
319
// Read if last opened source code file should be loaded on startup.
320
settings["LoadLastOpenedFileOnStartup"] = qsettings->value("UniversalIndentGUI/loadLastSourceCodeFileOnStartup", true).toBool();
322
// Read last opened source code file from the settings file.
323
settings["LastOpenedFiles"] = qsettings->value("UniversalIndentGUI/lastSourceCodeFile", indenterDirctoryStr+"/example.cpp").toString();
325
// Read last selected indenter from the settings file.
326
int SelectedIndenter = qsettings->value("UniversalIndentGUI/selectedIndenter", 0).toInt();
327
if ( SelectedIndenter < 0 ) {
328
SelectedIndenter = 0;
330
settings["SelectedIndenter"] = SelectedIndenter;
332
// Read if syntax highlighting is enabled.
333
settings["SyntaxHighlightningEnabled"] = qsettings->value("UniversalIndentGUI/SyntaxHighlightningEnabled", true).toBool();
335
// Read if white space characters should be displayed.
336
settings["WhiteSpaceIsVisible"] = qsettings->value("UniversalIndentGUI/whiteSpaceIsVisible", false).toBool();
338
// Read if indenter parameter tool tips are enabled.
339
settings["IndenterParameterTooltipsEnabled"] = qsettings->value("UniversalIndentGUI/indenterParameterTooltipsEnabled", true).toBool();
341
// Read the tab width from the settings file.
342
settings["TabWidth"] = qsettings->value("UniversalIndentGUI/tabWidth", 4).toInt();
344
// Read the last selected language and stores the index it has in the list of available translations.
345
settings["Language"] = availableTranslations.indexOf( qsettings->value("UniversalIndentGUI/language", "").toString() );
347
// Read the update check settings from the settings file.
348
settings["CheckForUpdate"] = qsettings->value("UniversalIndentGUI/CheckForUpdate", false).toBool();
349
settings["LastUpdateCheck"] = qsettings->value("UniversalIndentGUI/LastUpdateCheck", QDate(1900,1,1)).toDate();
351
// Read the main window state.
352
settings["MainWindowState"] = qsettings->value("UniversalIndentGUI/MainWindowState", QByteArray()).toByteArray();
359
\brief Saves the settings for the main application.
361
Settings are for example last selected indenter, last loaded source code file and so on.
363
bool UiGuiSettings::saveSettings() {
364
qsettings->setValue( "UniversalIndentGUI/recentlyOpenedListSize", settings["RecentlyOpenedListSize"] );
365
qsettings->setValue( "UniversalIndentGUI/lastSourceCodeFile", settings["LastOpenedFiles"] );
366
qsettings->setValue( "UniversalIndentGUI/loadLastSourceCodeFileOnStartup", settings["LoadLastOpenedFileOnStartup"] );
367
qsettings->setValue( "UniversalIndentGUI/selectedIndenter", settings["SelectedIndenter"] );
368
qsettings->setValue( "UniversalIndentGUI/indenterParameterTooltipsEnabled", settings["IndenterParameterTooltipsEnabled"] );
369
qsettings->setValue( "UniversalIndentGUI/language", availableTranslations[ settings["Language"].toInt() ] );
370
qsettings->setValue( "UniversalIndentGUI/encoding", settings["FileEncoding"] );
371
qsettings->setValue( "UniversalIndentGUI/version", settings["VersionInSettingsFile"] );
372
qsettings->setValue( "UniversalIndentGUI/maximized", settings["WindowIsMaximized"] );
373
if ( !settings["WindowIsMaximized"].toBool() ) {
374
qsettings->setValue( "UniversalIndentGUI/position", settings["WindowPosition"] );
375
qsettings->setValue( "UniversalIndentGUI/size", settings["WindowSize"] );
377
qsettings->setValue( "UniversalIndentGUI/SyntaxHighlightningEnabled", settings["SyntaxHighlightningEnabled"] );
378
qsettings->setValue( "UniversalIndentGUI/whiteSpaceIsVisible", settings["WhiteSpaceIsVisible"] );
379
qsettings->setValue( "UniversalIndentGUI/tabWidth", settings["TabWidth"] );
380
// Write the update check settings to the settings file.
381
qsettings->setValue("UniversalIndentGUI/CheckForUpdate", settings["CheckForUpdate"].toBool() );
382
qsettings->setValue("UniversalIndentGUI/LastUpdateCheck", settings["LastUpdateCheck"].toDate() );
383
// Write the main window state.
384
qsettings->setValue("UniversalIndentGUI/MainWindowState", settings["MainWindowState"].toByteArray() );
1
/***************************************************************************
2
* Copyright (C) 2006-2012 by Thomas Schweitzer *
3
* thomas-schweitzer(at)arcor.de *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License version 2.0 as *
7
* published by the Free Software Foundation. *
9
* This program is distributed in the hope that it will be useful, *
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12
* GNU General Public License for more details. *
14
* You should have received a copy of the GNU General Public License *
15
* along with this program in the file LICENSE.GPL; if not, write to the *
16
* Free Software Foundation, Inc., *
17
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18
***************************************************************************/
20
#include "UiGuiSettings.h"
22
#include "SettingsPaths.h"
29
#include <QStringList>
30
#include <QCoreApplication>
31
#include <QMetaMethod>
32
#include <QMetaProperty>
35
//! \defgroup grp_Settings All concerning the settings.
40
\brief Handles the settings of the program. Reads them on startup and saves them on exit.
41
Is a singleton class and can only be accessed via getInstance().
44
// Inits the single class instance pointer.
45
QWeakPointer<UiGuiSettings> UiGuiSettings::_instance;
49
\brief The constructor for the settings.
51
UiGuiSettings::UiGuiSettings() : QObject() {
52
// Create the main application settings object from the UniversalIndentGUI.ini file.
53
_qsettings = new QSettings(SettingsPaths::getSettingsPath() + "/UniversalIndentGUI.ini", QSettings::IniFormat, this);
55
_indenterDirctoryStr = SettingsPaths::getGlobalFilesPath() + "/indenters";
56
readAvailableTranslations();
62
\brief Returns the instance of the settings class. If no instance exists, ONE will be created.
64
QSharedPointer<UiGuiSettings> UiGuiSettings::getInstance() {
65
QSharedPointer<UiGuiSettings> sharedInstance = _instance.toStrongRef();
66
if ( sharedInstance.isNull() ) {
67
// Create the settings object, which loads all UiGui settings from a file.
68
sharedInstance = QSharedPointer<UiGuiSettings>(new UiGuiSettings());
69
_instance = sharedInstance.toWeakRef();
72
return sharedInstance;
77
\brief The destructor saves the settings to a file.
79
UiGuiSettings::~UiGuiSettings() {
80
// Convert the language setting from an integer index to a string.
81
int index = _qsettings->value("UniversalIndentGUI/language", 0).toInt();
82
if ( index < 0 || index >= _availableTranslations.size() )
85
_qsettings->setValue( "UniversalIndentGUI/language", _availableTranslations.at(index) );
90
\brief Scans the translations directory for available translation files and
91
stores them in the QList \a _availableTranslations.
93
void UiGuiSettings::readAvailableTranslations() {
94
QString languageShort;
95
QStringList languageFileList;
97
// English is the default language. A translation file does not exist but to have a menu entry, added here.
98
languageFileList << "universalindent_en.qm";
100
// Find all translation files in the "translations" directory.
101
QDir translationDirectory = QDir( SettingsPaths::getGlobalFilesPath() + "/translations" );
102
languageFileList << translationDirectory.entryList( QStringList("universalindent_*.qm") );
104
// Loop for each found translation file
105
foreach ( languageShort, languageFileList ) {
106
// Remove the leading string "universalindent_" from the filename.
107
languageShort.remove(0,16);
108
// Remove trailing file extension ".qm".
109
languageShort.chop(3);
111
_availableTranslations.append(languageShort);
117
\brief Returns a list of the mnemonics of the available translations.
119
QStringList UiGuiSettings::getAvailableTranslations() {
120
return _availableTranslations;
125
\brief Returns the value of the by \a settingsName defined setting as QVariant.
127
If the named setting does not exist, 0 is being returned.
129
QVariant UiGuiSettings::getValueByName(QString settingName) {
130
return _qsettings->value("UniversalIndentGUI/" + settingName);
135
\brief Loads the settings for the main application.
137
Settings are for example last selected indenter, last loaded source code file and so on.
139
bool UiGuiSettings::initSettings()
141
// Read the version string saved in the settings file.
142
_qsettings->setValue( "UniversalIndentGUI/version", _qsettings->value("UniversalIndentGUI/version", "") );
144
// Read windows last size and position from the settings file.
145
_qsettings->setValue( "UniversalIndentGUI/maximized", _qsettings->value("UniversalIndentGUI/maximized", false) );
146
_qsettings->setValue( "UniversalIndentGUI/position", _qsettings->value("UniversalIndentGUI/position", QPoint(50, 50)) );
147
_qsettings->setValue( "UniversalIndentGUI/size", _qsettings->value("UniversalIndentGUI/size", QSize(800, 600)) );
149
// Read last selected encoding for the opened source code file.
150
_qsettings->setValue( "UniversalIndentGUI/encoding", _qsettings->value("UniversalIndentGUI/encoding", "UTF-8") );
152
// Read maximum length of list for recently opened files.
153
_qsettings->setValue( "UniversalIndentGUI/recentlyOpenedListSize", _qsettings->value("UniversalIndentGUI/recentlyOpenedListSize", 5) );
155
// Read if last opened source code file should be loaded on startup.
156
_qsettings->setValue( "UniversalIndentGUI/loadLastSourceCodeFileOnStartup", _qsettings->value("UniversalIndentGUI/loadLastSourceCodeFileOnStartup", true) );
158
// Read last opened source code file from the settings file.
159
_qsettings->setValue( "UniversalIndentGUI/lastSourceCodeFile", _qsettings->value("UniversalIndentGUI/lastSourceCodeFile", _indenterDirctoryStr+"/example.cpp") );
161
// Read last selected indenter from the settings file.
162
int selectedIndenter = _qsettings->value("UniversalIndentGUI/selectedIndenter", 0).toInt();
163
if ( selectedIndenter < 0 ) {
164
selectedIndenter = 0;
166
_qsettings->setValue( "UniversalIndentGUI/selectedIndenter", selectedIndenter );
168
// Read if syntax highlighting is enabled.
169
_qsettings->setValue( "UniversalIndentGUI/SyntaxHighlightingEnabled", _qsettings->value("UniversalIndentGUI/SyntaxHighlightingEnabled", true) );
171
// Read if white space characters should be displayed.
172
_qsettings->setValue( "UniversalIndentGUI/whiteSpaceIsVisible", _qsettings->value("UniversalIndentGUI/whiteSpaceIsVisible", false) );
174
// Read if indenter parameter tool tips are enabled.
175
_qsettings->setValue( "UniversalIndentGUI/indenterParameterTooltipsEnabled", _qsettings->value("UniversalIndentGUI/indenterParameterTooltipsEnabled", true) );
177
// Read the tab width from the settings file.
178
_qsettings->setValue( "UniversalIndentGUI/tabWidth", _qsettings->value("UniversalIndentGUI/tabWidth", 4) );
180
// Read the last selected language and stores the index it has in the list of available translations.
181
_qsettings->setValue( "UniversalIndentGUI/language", _availableTranslations.indexOf( _qsettings->value("UniversalIndentGUI/language", "").toString() ) );
183
// Read the update check settings from the settings file.
184
_qsettings->setValue( "UniversalIndentGUI/CheckForUpdate", _qsettings->value("UniversalIndentGUI/CheckForUpdate", false) );
185
_qsettings->setValue( "UniversalIndentGUI/LastUpdateCheck", _qsettings->value("UniversalIndentGUI/LastUpdateCheck", QDate(1900,1,1)) );
187
// Read the main window state.
188
_qsettings->setValue( "UniversalIndentGUI/MainWindowState", _qsettings->value("UniversalIndentGUI/MainWindowState", QByteArray()) );
195
\brief Register the by \a propertyName defined property of \a obj to be connected to the setting defined by \a settingName.
197
The \a propertyName must be one of those that are listed in the Qt "Properties" documentation section of a Qt Object.
198
All further needed info is retrieved via the \a obj's MetaObject, like the to the property bound signal.
200
bool UiGuiSettings::registerObjectProperty( QObject *obj, const QString &propertyName, const QString &settingName )
202
const QMetaObject *metaObject = obj->metaObject();
203
bool connectSuccess = false;
205
// Connect to the objects destroyed signal, so that it will be correctly unregistered.
206
connectSuccess = connect(obj, SIGNAL(destroyed(QObject*)), this, SLOT(unregisterObjectProperty(QObject*)));
208
int indexOfProp = metaObject->indexOfProperty( qPrintable(propertyName) );
209
if ( connectSuccess && indexOfProp > -1 ) {
210
QMetaProperty mProp = metaObject->property(indexOfProp);
212
// Connect to the property's value changed signal.
213
if ( mProp.hasNotifySignal() ) {
214
QMetaMethod signal = mProp.notifySignal();
215
//QString teststr = qPrintable(SIGNAL() + QString(signal.signature()));
216
// The command "SIGNAL() + QString(signal.signature())" assembles the signal methods signature to a valid Qt SIGNAL.
217
connectSuccess = connect(obj, qPrintable(SIGNAL() + QString(signal.signature())), this, SLOT(handleObjectPropertyChange()));
220
if ( connectSuccess ) {
221
_registeredObjectProperties[obj] = QStringList() << propertyName << settingName;
224
//TODO: Write a debug warning to the log.
225
disconnect(obj, SIGNAL(destroyed(QObject*)), this, SLOT(unregisterObjectProperty(QObject*)));
229
// If setting already exists, set the objects property to the setting value.
230
if ( _qsettings->contains("UniversalIndentGUI/" + settingName) ) {
231
mProp.write(obj, _qsettings->value("UniversalIndentGUI/" + settingName));
233
// Otherwise add the setting and set it to the value of the objects property.
235
_qsettings->setValue("UniversalIndentGUI/" + settingName, mProp.read(obj));
239
//TODO: Write a debug warning to the log.
240
disconnect(obj, SIGNAL(destroyed(QObject*)), this, SLOT(unregisterObjectProperty(QObject*)));
249
\brief Searches the child QObjects of \a obj for a property name and setting name definition within
250
their custom properties and registers this property name to that setting name if both were found.
252
The custom properties, for which are searched, are "connectedPropertyName" and "connectedSettingName",
253
where "connectedPropertyName" is the name of a QObject property as it is documented in the QtDocs, and
254
"connectedSettingName" is the name of a setting here within UiGuiSettings. If the mentioned setting
255
name doesn't exist, it will be created.
257
Returns true, if all found property and setting definitions could be successfully registered.
258
Returns false, if any of those registrations fails.
260
bool UiGuiSettings::registerObjectPropertyRecursive(QObject *obj) {
261
return checkCustomPropertiesAndCallFunction(obj, &UiGuiSettings::registerObjectProperty);
266
\brief Assigns the by \a settingName defined setting value to the by \a propertyName defined property of \a obj.
268
Returns true, if the value could be assigned, otherwise returns false, which is the case if settingName doesn't exist
269
within the settings or if the mentioned propertyName wasn't found for the \a obj.
271
bool UiGuiSettings::setObjectPropertyToSettingValue( QObject *obj, const QString &propertyName, const QString &settingName )
273
const QMetaObject *metaObject = obj->metaObject();
275
int indexOfProp = metaObject->indexOfProperty( qPrintable(propertyName) );
276
if ( indexOfProp > -1 ) {
277
QMetaProperty mProp = metaObject->property(indexOfProp);
279
// If setting already exists, set the objects property to the setting value.
280
if ( _qsettings->contains("UniversalIndentGUI/" + settingName) ) {
281
mProp.write(obj, _qsettings->value("UniversalIndentGUI/" + settingName));
283
// The setting didn't exist so return that setting the objects property failed.
285
//TODO: Write a debug warning to the log.
290
//TODO: Write a debug warning to the log.
299
\brief Searches the child QObjects of \a obj for a property name and setting name definition within
300
their custom properties and sets each property to settings value.
302
The custom properties, for which are searched, are "connectedPropertyName" and "connectedSettingName",
303
where "connectedPropertyName" is the name of a QObject property as it is documented in the QtDocs, and
304
"connectedSettingName" is the name of a setting here within UiGuiSettings.
306
Returns true, if all found property and setting definitions could be successfully registered.
307
Returns false, if any of those registrations fails.
309
bool UiGuiSettings::setObjectPropertyToSettingValueRecursive(QObject *obj) {
310
return checkCustomPropertiesAndCallFunction(obj, &UiGuiSettings::setObjectPropertyToSettingValue);
315
\brief Assigns the by \a propertyName defined property's value of \a obj to the by \a settingName defined setting.
317
If the \a settingName didn't exist yet, it will be created.
319
Returns true, if the value could be assigned, otherwise returns false, which is the case if the mentioned
320
propertyName wasn't found for the \a obj.
322
bool UiGuiSettings::setSettingToObjectPropertyValue( QObject *obj, const QString &propertyName, const QString &settingName )
324
const QMetaObject *metaObject = obj->metaObject();
326
int indexOfProp = metaObject->indexOfProperty( qPrintable(propertyName) );
327
if ( indexOfProp > -1 ) {
328
QMetaProperty mProp = metaObject->property(indexOfProp);
330
setValueByName(settingName, mProp.read(obj));
333
//TODO: Write a debug warning to the log.
342
\brief Searches the child QObjects of \a obj for a property name and setting name definition within
343
their custom properties and sets each setting to the property value.
345
The custom properties, for which are searched, are "connectedPropertyName" and "connectedSettingName",
346
where "connectedPropertyName" is the name of a QObject property as it is documented in the QtDocs, and
347
"connectedSettingName" is the name of a setting here within UiGuiSettings. If the settingName
348
didn't exist yet, it will be created.
350
Returns true, if all found property and setting definitions could be successfully registered.
351
Returns false, if any of those registrations fails.
353
bool UiGuiSettings::setSettingToObjectPropertyValueRecursive(QObject *obj) {
354
return checkCustomPropertiesAndCallFunction(obj, &UiGuiSettings::setSettingToObjectPropertyValue);
359
\brief Iterates over all \a objs child QObjects and checks whether they have the custom properties
360
"connectedPropertyName" and "connectedSettingName" set. If both are set, it invokes the \a callBackFunc
363
bool UiGuiSettings::checkCustomPropertiesAndCallFunction(QObject *obj, bool (UiGuiSettings::*callBackFunc)(QObject *obj, const QString &propertyName, const QString &settingName)) {
366
// Find all widgets that have PropertyName and SettingName defined in their style sheet.
367
QList<QObject *> allObjects = obj->findChildren<QObject *>();
368
foreach (QObject *object, allObjects) {
369
QString propertyName = object->property("connectedPropertyName").toString();
370
QString settingName = object->property("connectedSettingName").toString();
372
// If property and setting name were found, register that widget with the settings.
373
if ( !propertyName.isEmpty() && !settingName.isEmpty() ) {
374
success &= (this->*callBackFunc)( object, propertyName, settingName );
383
\brief The with a certain property registered \a obj gets unregistered.
385
void UiGuiSettings::unregisterObjectProperty(QObject *obj) {
386
if ( _registeredObjectProperties.contains(obj) ) {
387
const QMetaObject *metaObject = obj->metaObject();
388
QString propertyName = _registeredObjectProperties[obj].first();
389
QString settingName = _registeredObjectProperties[obj].last();
391
bool connectSuccess = false;
392
int indexOfProp = metaObject->indexOfProperty( qPrintable(propertyName) );
393
if ( indexOfProp > -1 ) {
394
QMetaProperty mProp = metaObject->property(indexOfProp);
396
// Disconnect to the property's value changed signal.
397
if ( mProp.hasNotifySignal() ) {
398
QMetaMethod signal = mProp.notifySignal();
399
// The command "SIGNAL() + QString(signal.signature())" assembles the signal methods signature to a valid Qt SIGNAL.
400
connectSuccess = disconnect(obj, qPrintable(SIGNAL() + QString(signal.signature())), this, SLOT(handleObjectPropertyChange()));
403
_registeredObjectProperties.remove(obj);
409
\brief Registers a slot form the \a obj by its \a slotName to be invoked, if the by \a settingName defined
412
The registered slot may have no parameters or exactly one. If it accepts one parameter, whenever the setting
413
\a settingName changes the slot gets tried to be invoked with the settings value as parameter. This only works,
414
if the slot parameter is of the same type as the setting.
416
bool UiGuiSettings::registerObjectSlot(QObject *obj, const QString &slotName, const QString &settingName) {
418
const QMetaObject *metaObject = obj->metaObject();
420
bool connectSuccess = false;
421
// Connect to the objects destroyed signal, so that it will be correctly unregistered.
422
connectSuccess = connect(obj, SIGNAL(destroyed(QObject*)), this, SLOT(unregisterObjectSlot(QObject*)));
424
QString normalizedSlotName = QMetaObject::normalizedSignature( qPrintable(slotName) );
425
int indexOfMethod = metaObject->indexOfMethod( qPrintable(normalizedSlotName) );
426
if ( connectSuccess && indexOfMethod > -1 ) {
427
QMetaMethod mMethod = metaObject->method(indexOfMethod);
428
//QMetaMethod::Access access = mMethod.access();
429
//QMetaMethod::MethodType methType = mMethod.methodType();
431
// Since the method can at maximum be invoked with the setting value as argument,
432
// only methods taking max one argument are allowed.
433
if ( mMethod.parameterTypes().size() <= 1 ) {
434
_registeredObjectSlots.insert(obj, QStringList() << normalizedSlotName << settingName);
437
//TODO: Write a debug warning to the log.
438
disconnect(obj, SIGNAL(destroyed(QObject*)), this, SLOT(unregisterObjectSlot(QObject*)));
443
//TODO: Write a debug warning to the log.
444
disconnect(obj, SIGNAL(destroyed(QObject*)), this, SLOT(unregisterObjectSlot(QObject*)));
453
\brief If \a obj, \a slotName and \a settingName are given, that certain connection is unregistered.
454
If only \a obj is given, all to this object registered slot-setting connections are unregistered.
456
void UiGuiSettings::unregisterObjectSlot(QObject *obj, const QString &slotName, const QString &settingName) {
457
//const QMetaObject *metaObject = obj->metaObject();
458
QString normalizedSlotName = QMetaObject::normalizedSignature( qPrintable(slotName) );
459
QMutableMapIterator<QObject*, QStringList> it(_registeredObjectSlots);
460
while (it.hasNext()) {
462
if (it.key() == obj && slotName.isEmpty() && settingName.isEmpty())
464
else if (it.key() == obj && it.value().first() == slotName && it.value().last() == settingName)
471
\brief This private slot gets invoked whenever a registered objects property changes
472
and distributes the new value to all other to the same settingName registered objects.
474
void UiGuiSettings::handleObjectPropertyChange() {
475
QObject *obj = QObject::sender();
476
QString className = obj->metaObject()->className();
477
const QMetaObject *metaObject = obj->metaObject();
478
QString propertyName = _registeredObjectProperties[obj].first();
479
QString settingName = _registeredObjectProperties[obj].last();
481
int indexOfProp = metaObject->indexOfProperty( qPrintable(propertyName) );
482
if ( indexOfProp > -1 ) {
483
QMetaProperty mProp = metaObject->property(indexOfProp);
484
setValueByName(settingName, mProp.read(obj));
490
\brief Sets the setting defined by \a settingName to \a value.
492
When setting a changed value, all to this settingName registered objects get
493
the changed value set too.
494
If the \a settingName didn't exist yet, it will be created.
496
void UiGuiSettings::setValueByName(const QString &settingName, const QVariant &value) {
497
// Do the updating only, if the setting was really changed.
498
if ( _qsettings->value("UniversalIndentGUI/" + settingName) != value ) {
499
_qsettings->setValue("UniversalIndentGUI/" + settingName, value);
501
// Set the new value for all registered object properties for settingName.
502
for ( QMap<QObject*, QStringList>::ConstIterator it = _registeredObjectProperties.begin(); it != _registeredObjectProperties.end(); ++it ) {
503
if ( it.value().last() == settingName ) {
504
QObject *obj = it.key();
505
const QMetaObject *metaObject = obj->metaObject();
506
QString propertyName = it.value().first();
508
int indexOfProp = metaObject->indexOfProperty( qPrintable(propertyName) );
509
if ( indexOfProp > -1 ) {
510
QMetaProperty mProp = metaObject->property(indexOfProp);
511
mProp.write(obj, value);
516
// Invoke all registered object methods for settingName.
517
for ( QMap<QObject*, QStringList>::ConstIterator it = _registeredObjectSlots.begin(); it != _registeredObjectSlots.end(); ++it ) {
518
if ( it.value().last() == settingName ) {
519
QObject *obj = it.key();
520
const QMetaObject *metaObject = obj->metaObject();
521
QString slotName = it.value().first();
523
int indexOfMethod = metaObject->indexOfMethod( qPrintable(slotName) );
524
if ( indexOfMethod > -1 ) {
525
QMetaMethod mMethod = metaObject->method(indexOfMethod);
526
//QMetaMethod::Access access = mMethod.access();
527
//QMetaMethod::MethodType methType = mMethod.methodType();
529
bool success = false;
531
// Handle registered slots taking one parameter.
532
if ( mMethod.parameterTypes().size() == 1 ) {
533
if ( mMethod.parameterTypes().first() == value.typeName() ) {
534
success = invokeMethodWithValue(obj, mMethod, value);
537
// Handle registered slots taking zero parameters.
539
success = mMethod.invoke( obj, Qt::DirectConnection );
542
if ( success == false ) {
543
// TODO: Write a warning to the log if no success.
559
#include <QKeySequence>
563
#include <QSizePolicy>
564
#include <QTextFormat>
565
#include <QTextLength>
567
#if QT_VERSION >= 0x040600
568
#include <QMatrix4x4>
572
bool UiGuiSettings::invokeMethodWithValue( QObject *obj, QMetaMethod mMethod, QVariant value )
574
switch (value.type()) {
575
case QVariant::BitArray :
576
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QBitArray, value.toBitArray()) );
577
case QVariant::Bitmap :
578
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QBitmap, value.value<QBitmap>()) );
579
case QVariant::Bool :
580
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(bool, value.toBool()) );
581
case QVariant::Brush :
582
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QBrush, value.value<QBrush>()) );
583
case QVariant::ByteArray :
584
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QByteArray, value.toByteArray()) );
585
case QVariant::Char :
586
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QChar, value.toChar()) );
587
case QVariant::Color :
588
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QColor, value.value<QColor>()) );
589
case QVariant::Cursor :
590
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QCursor, value.value<QCursor>()) );
591
case QVariant::Date :
592
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QDate, value.toDate()) );
593
case QVariant::DateTime :
594
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QDateTime, value.toDateTime()) );
595
case QVariant::Double :
596
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(double, value.toDouble()) );
597
case QVariant::Font :
598
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QFont, value.value<QFont>()) );
599
case QVariant::Hash :
600
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QVariantHash, value.toHash()) );
601
case QVariant::Icon :
602
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QIcon, value.value<QIcon>()) );
603
case QVariant::Image :
604
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QImage, value.value<QImage>()) );
606
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(int, value.toInt()) );
607
case QVariant::KeySequence :
608
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QKeySequence, value.value<QKeySequence>()) );
609
case QVariant::Line :
610
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QLine, value.toLine()) );
611
case QVariant::LineF :
612
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QLineF, value.toLineF()) );
613
case QVariant::List :
614
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QVariantList, value.toList()) );
615
case QVariant::Locale :
616
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QLocale, value.toLocale()) );
617
case QVariant::LongLong :
618
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(qlonglong, value.toLongLong()) );
620
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QVariantMap, value.toMap()) );
621
case QVariant::Matrix :
622
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QMatrix, value.value<QMatrix>()) );
623
case QVariant::Transform :
624
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QTransform, value.value<QTransform>()) );
625
#if QT_VERSION >= 0x040600
626
case QVariant::Matrix4x4 :
627
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QMatrix4x4, value.value<QMatrix4x4>()) );
629
case QVariant::Palette :
630
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QPalette, value.value<QPalette>()) );
632
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QPen, value.value<QPen>()) );
633
case QVariant::Pixmap :
634
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QPixmap, value.value<QPixmap>()) );
635
case QVariant::Point :
636
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QPoint, value.toPoint()) );
637
// case QVariant::PointArray :
638
// return Q_ARG(QPointArray, value.value<QPointArray>()) );
639
case QVariant::PointF :
640
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QPointF, value.toPointF()) );
641
case QVariant::Polygon :
642
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QPolygon, value.value<QPolygon>()) );
643
#if QT_VERSION >= 0x040600
644
case QVariant::Quaternion :
645
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QQuaternion, value.value<QQuaternion>()) );
647
case QVariant::Rect :
648
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QRect, value.toRect()) );
649
case QVariant::RectF :
650
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QRectF, value.toRectF()) );
651
case QVariant::RegExp :
652
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QRegExp, value.toRegExp()) );
653
case QVariant::Region :
654
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QRegion, value.value<QRegion>()) );
655
case QVariant::Size :
656
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QSize, value.toSize()) );
657
case QVariant::SizeF :
658
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QSizeF, value.toSizeF()) );
659
case QVariant::SizePolicy :
660
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QSizePolicy, value.value<QSizePolicy>()) );
661
case QVariant::String :
662
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QString, value.toString()) );
663
case QVariant::StringList :
664
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QStringList, value.toStringList()) );
665
case QVariant::TextFormat :
666
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QTextFormat, value.value<QTextFormat>()) );
667
case QVariant::TextLength :
668
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QTextLength, value.value<QTextLength>()) );
669
case QVariant::Time :
670
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QTime, value.toTime()) );
671
case QVariant::UInt :
672
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(uint, value.toUInt()) );
673
case QVariant::ULongLong :
674
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(qulonglong, value.toULongLong()) );
676
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QUrl, value.toUrl()) );
677
#if QT_VERSION >= 0x040600
678
case QVariant::Vector2D :
679
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QVector2D, value.value<QVector2D>()) );
680
case QVariant::Vector3D :
681
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QVector3D, value.value<QVector3D>()) );
682
case QVariant::Vector4D :
683
return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QVector4D, value.value<QVector4D>()) );