~ubuntu-branches/debian/jessie/universalindentgui/jessie

« back to all changes in this revision

Viewing changes to src/UiGuiSettings.cpp

  • Committer: Package Import Robot
  • Author(s): Fathi Boudra
  • Date: 2012-05-22 08:49:27 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20120522084927-oijnyby6gedsinbh
Tags: 1.2.0-1
* New upstream release.
* Drop 02_fix_gcc_4.5_build.patch - merged upstream.
* Update debian/contol:
  - bump debhelper to 9.
  - bump Standards-Version to 3.9.3 (no changes needed).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
*   Copyright (C) 2006-2009 by Thomas Schweitzer                          *
3
 
*   thomas-schweitzer(at)arcor.de                                         *
4
 
*                                                                         *
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.                            *
8
 
*                                                                         *
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.                          *
13
 
*                                                                         *
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
 
***************************************************************************/
19
 
 
20
 
#include "UiGuiSettings.h"
21
 
 
22
 
#include "SettingsPaths.h"
23
 
 
24
 
//! \defgroup grp_Settings All concerning the settings.
25
 
 
26
 
/*!
27
 
    \class UiGuiSettings
28
 
    \ingroup grp_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().
31
 
*/
32
 
 
33
 
// Inits the single class instance pointer.
34
 
UiGuiSettings* UiGuiSettings::instance = NULL;
35
 
 
36
 
/*!
37
 
    \brief The constructor for the settings.
38
 
*/
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);
42
 
 
43
 
    indenterDirctoryStr = SettingsPaths::getGlobalFilesPath() + "/indenters";
44
 
    readAvailableTranslations();
45
 
    loadSettings();
46
 
}
47
 
 
48
 
 
49
 
/*!
50
 
    \brief Returns the instance of the settings class. If no instance exists, ONE will be created.
51
 
 */
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();
56
 
    }
57
 
 
58
 
    return instance;
59
 
}
60
 
 
61
 
/*!
62
 
    \brief Deletes the existing instance of UiGuiSettings and removes the created temp dir.
63
 
 */
64
 
void UiGuiSettings::deleteInstance() {
65
 
    SettingsPaths::cleanAndRemoveTempDir();
66
 
    if ( instance != NULL ) {
67
 
        delete instance;
68
 
        instance = NULL;
69
 
    }
70
 
}
71
 
 
72
 
 
73
 
/*!
74
 
    \brief The destructor saves the settings to a file.
75
 
 */
76
 
UiGuiSettings::~UiGuiSettings() {
77
 
    saveSettings();
78
 
}
79
 
 
80
 
 
81
 
/*!
82
 
    \brief Scans the translations directory for available translation files and
83
 
    stores them in the QList \a availableTranslations.
84
 
 */
85
 
void UiGuiSettings::readAvailableTranslations() {
86
 
    QString languageShort;
87
 
    QStringList languageFileList;
88
 
 
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";
91
 
 
92
 
    // Find all translation files in the "translations" directory.
93
 
    QDir translationDirectory = QDir( SettingsPaths::getGlobalFilesPath() + "/translations" );
94
 
    languageFileList << translationDirectory.entryList( QStringList("universalindent_*.qm") );
95
 
 
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);
102
 
 
103
 
        availableTranslations.append(languageShort);
104
 
    }
105
 
}
106
 
 
107
 
 
108
 
/*!
109
 
    \brief Returns a list of the mnemonics of the available translations.
110
 
 */
111
 
QStringList UiGuiSettings::getAvailableTranslations() {
112
 
    return availableTranslations;
113
 
}
114
 
 
115
 
 
116
 
/*!
117
 
    \brief Extern widgets can connect to this slot to change settings.
118
 
 
119
 
    According to the objects property "connectedSettingName" the corresponding
120
 
    setting is known and set.
121
 
 */
122
 
void UiGuiSettings::handleValueChangeFromExtern(int value) {
123
 
    if ( sender() ) {
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);
131
 
        }
132
 
 
133
 
        // Set the value of the setting to the objects value.
134
 
        setValueByName( settingName, value );
135
 
    }
136
 
}
137
 
 
138
 
 
139
 
/*!
140
 
    \brief Extern widgets can connect to this slot to change settings.
141
 
 
142
 
    According to the objects property "connectedSettingName" the corresponding
143
 
    setting is known and set.
144
 
 */
145
 
void UiGuiSettings::handleValueChangeFromExtern(bool value) {
146
 
    if ( sender() ) {
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);
154
 
        }
155
 
 
156
 
        // Set the value of the setting to the objects value.
157
 
        setValueByName( settingName, value );
158
 
    }
159
 
}
160
 
 
161
 
 
162
 
/*!
163
 
    \brief Extern widgets can connect to this slot to change settings.
164
 
 
165
 
    According to the objects property "connectedSettingName" the corresponding
166
 
    setting is known and set.
167
 
 */
168
 
void UiGuiSettings::handleValueChangeFromExtern(QDate value) {
169
 
    if ( sender() ) {
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);
177
 
        }
178
 
 
179
 
        // Set the value of the setting to the objects value.
180
 
        setValueByName( settingName, value );
181
 
    }
182
 
}
183
 
 
184
 
 
185
 
/*!
186
 
    \brief Extern widgets can connect to this slot to change settings.
187
 
 
188
 
    According to the objects property "connectedSettingName" the corresponding
189
 
    setting is known and set.
190
 
 */
191
 
void UiGuiSettings::handleValueChangeFromExtern(QByteArray value) {
192
 
    if ( sender() ) {
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);
200
 
        }
201
 
 
202
 
        // Set the value of the setting to the objects value.
203
 
        setValueByName( settingName, value );
204
 
    }
205
 
}
206
 
 
207
 
 
208
 
/*!
209
 
    \brief Sets the value of the by \a settingsName defined setting to the value \a value.
210
 
 
211
 
    The to \a settingsName corresponding signal is emitted, if the value has changed.
212
 
 */
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);
222
 
        }
223
 
        return true;
224
 
    }
225
 
    return false;
226
 
}
227
 
 
228
 
 
229
 
/*!
230
 
    \brief Emits the correct signal for the given \a settingName.
231
 
 
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.
234
 
 */
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() );
272
 
    }
273
 
}
274
 
 
275
 
 
276
 
/*!
277
 
    \brief Calls \sa emitSignalForSetting with settingName "all" to update all widgets or whatever
278
 
    is connected to each setting.
279
 
 */
280
 
void UiGuiSettings::updateAllDependend() {
281
 
    emitSignalForSetting("all");
282
 
}
283
 
 
284
 
 
285
 
/*!
286
 
    \brief Returns the value of the by \a settingsName defined setting as QVariant.
287
 
 
288
 
    If the named setting does not exist, 0 is being returned.
289
 
*/
290
 
QVariant UiGuiSettings::getValueByName(QString settingName) {
291
 
    // Test if the named setting really exists.
292
 
    if ( settings.contains(settingName) ) {
293
 
        return settings[settingName];
294
 
    }
295
 
    return QVariant(0);
296
 
}
297
 
 
298
 
 
299
 
/*!
300
 
    \brief Loads the settings for the main application.
301
 
 
302
 
    Settings are for example last selected indenter, last loaded source code file and so on.
303
 
*/
304
 
bool UiGuiSettings::loadSettings() {
305
 
    // Read the version string saved in the settings file.
306
 
    settings["VersionInSettingsFile"] = qsettings->value("UniversalIndentGUI/version", "").toString();
307
 
 
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();
312
 
 
313
 
    // Read last selected encoding for the opened source code file.
314
 
    settings["FileEncoding"] = qsettings->value("UniversalIndentGUI/encoding", "UTF-8").toString();
315
 
 
316
 
    // Read maximum length of list for recently opened files.
317
 
    settings["RecentlyOpenedListSize"] = qsettings->value("UniversalIndentGUI/recentlyOpenedListSize", 5).toInt();
318
 
 
319
 
    // Read if last opened source code file should be loaded on startup.
320
 
    settings["LoadLastOpenedFileOnStartup"] = qsettings->value("UniversalIndentGUI/loadLastSourceCodeFileOnStartup", true).toBool();
321
 
 
322
 
    // Read last opened source code file from the settings file.
323
 
    settings["LastOpenedFiles"] = qsettings->value("UniversalIndentGUI/lastSourceCodeFile", indenterDirctoryStr+"/example.cpp").toString();
324
 
 
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;
329
 
    }
330
 
    settings["SelectedIndenter"] = SelectedIndenter;
331
 
 
332
 
    // Read if syntax highlighting is enabled.
333
 
    settings["SyntaxHighlightningEnabled"] = qsettings->value("UniversalIndentGUI/SyntaxHighlightningEnabled", true).toBool();
334
 
 
335
 
    // Read if white space characters should be displayed.
336
 
    settings["WhiteSpaceIsVisible"] = qsettings->value("UniversalIndentGUI/whiteSpaceIsVisible", false).toBool();
337
 
 
338
 
    // Read if indenter parameter tool tips are enabled.
339
 
    settings["IndenterParameterTooltipsEnabled"] = qsettings->value("UniversalIndentGUI/indenterParameterTooltipsEnabled", true).toBool();
340
 
 
341
 
    // Read the tab width from the settings file.
342
 
    settings["TabWidth"] = qsettings->value("UniversalIndentGUI/tabWidth", 4).toInt();
343
 
 
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() );
346
 
 
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();
350
 
 
351
 
    // Read the main window state.
352
 
    settings["MainWindowState"] = qsettings->value("UniversalIndentGUI/MainWindowState", QByteArray()).toByteArray();
353
 
 
354
 
    return true;
355
 
}
356
 
 
357
 
 
358
 
/*!
359
 
    \brief Saves the settings for the main application.
360
 
 
361
 
    Settings are for example last selected indenter, last loaded source code file and so on.
362
 
*/
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"] );
376
 
    }
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() );
385
 
 
386
 
    return true;
387
 
}
 
1
/***************************************************************************
 
2
*   Copyright (C) 2006-2012 by Thomas Schweitzer                          *
 
3
*   thomas-schweitzer(at)arcor.de                                         *
 
4
*                                                                         *
 
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.                            *
 
8
*                                                                         *
 
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.                          *
 
13
*                                                                         *
 
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
***************************************************************************/
 
19
 
 
20
#include "UiGuiSettings.h"
 
21
 
 
22
#include "SettingsPaths.h"
 
23
 
 
24
#include <QSettings>
 
25
#include <QPoint>
 
26
#include <QSize>
 
27
#include <QDir>
 
28
#include <QDate>
 
29
#include <QStringList>
 
30
#include <QCoreApplication>
 
31
#include <QMetaMethod>
 
32
#include <QMetaProperty>
 
33
#include <QWidget>
 
34
 
 
35
//! \defgroup grp_Settings All concerning the settings.
 
36
 
 
37
/*!
 
38
    \class UiGuiSettings
 
39
    \ingroup grp_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().
 
42
*/
 
43
 
 
44
// Inits the single class instance pointer.
 
45
QWeakPointer<UiGuiSettings> UiGuiSettings::_instance;
 
46
 
 
47
 
 
48
/*!
 
49
    \brief The constructor for the settings.
 
50
*/
 
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);
 
54
 
 
55
    _indenterDirctoryStr = SettingsPaths::getGlobalFilesPath() + "/indenters";
 
56
    readAvailableTranslations();
 
57
    initSettings();
 
58
}
 
59
 
 
60
 
 
61
/*!
 
62
    \brief Returns the instance of the settings class. If no instance exists, ONE will be created.
 
63
 */
 
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();
 
70
    }
 
71
 
 
72
    return sharedInstance;
 
73
}
 
74
 
 
75
 
 
76
/*!
 
77
    \brief The destructor saves the settings to a file.
 
78
 */
 
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() )
 
83
        index = 0;
 
84
 
 
85
    _qsettings->setValue( "UniversalIndentGUI/language", _availableTranslations.at(index) );
 
86
}
 
87
 
 
88
 
 
89
/*!
 
90
    \brief Scans the translations directory for available translation files and
 
91
    stores them in the QList \a _availableTranslations.
 
92
 */
 
93
void UiGuiSettings::readAvailableTranslations() {
 
94
    QString languageShort;
 
95
    QStringList languageFileList;
 
96
 
 
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";
 
99
 
 
100
    // Find all translation files in the "translations" directory.
 
101
    QDir translationDirectory = QDir( SettingsPaths::getGlobalFilesPath() + "/translations" );
 
102
    languageFileList << translationDirectory.entryList( QStringList("universalindent_*.qm") );
 
103
 
 
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);
 
110
 
 
111
        _availableTranslations.append(languageShort);
 
112
    }
 
113
}
 
114
 
 
115
 
 
116
/*!
 
117
    \brief Returns a list of the mnemonics of the available translations.
 
118
 */
 
119
QStringList UiGuiSettings::getAvailableTranslations() {
 
120
    return _availableTranslations;
 
121
}
 
122
 
 
123
 
 
124
/*!
 
125
    \brief Returns the value of the by \a settingsName defined setting as QVariant.
 
126
 
 
127
    If the named setting does not exist, 0 is being returned.
 
128
*/
 
129
QVariant UiGuiSettings::getValueByName(QString settingName) {
 
130
    return _qsettings->value("UniversalIndentGUI/" + settingName);
 
131
}
 
132
 
 
133
 
 
134
/*!
 
135
    \brief Loads the settings for the main application.
 
136
 
 
137
    Settings are for example last selected indenter, last loaded source code file and so on.
 
138
*/
 
139
bool UiGuiSettings::initSettings()
 
140
{
 
141
    // Read the version string saved in the settings file.
 
142
    _qsettings->setValue( "UniversalIndentGUI/version", _qsettings->value("UniversalIndentGUI/version", "") );
 
143
 
 
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)) );
 
148
 
 
149
    // Read last selected encoding for the opened source code file.
 
150
    _qsettings->setValue( "UniversalIndentGUI/encoding", _qsettings->value("UniversalIndentGUI/encoding", "UTF-8") );
 
151
 
 
152
    // Read maximum length of list for recently opened files.
 
153
    _qsettings->setValue( "UniversalIndentGUI/recentlyOpenedListSize", _qsettings->value("UniversalIndentGUI/recentlyOpenedListSize", 5) );
 
154
 
 
155
    // Read if last opened source code file should be loaded on startup.
 
156
    _qsettings->setValue( "UniversalIndentGUI/loadLastSourceCodeFileOnStartup", _qsettings->value("UniversalIndentGUI/loadLastSourceCodeFileOnStartup", true) );
 
157
 
 
158
    // Read last opened source code file from the settings file.
 
159
    _qsettings->setValue( "UniversalIndentGUI/lastSourceCodeFile", _qsettings->value("UniversalIndentGUI/lastSourceCodeFile",  _indenterDirctoryStr+"/example.cpp") );
 
160
 
 
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;
 
165
    }
 
166
    _qsettings->setValue( "UniversalIndentGUI/selectedIndenter", selectedIndenter );
 
167
 
 
168
    // Read if syntax highlighting is enabled.
 
169
    _qsettings->setValue( "UniversalIndentGUI/SyntaxHighlightingEnabled", _qsettings->value("UniversalIndentGUI/SyntaxHighlightingEnabled", true) );
 
170
 
 
171
    // Read if white space characters should be displayed.
 
172
    _qsettings->setValue( "UniversalIndentGUI/whiteSpaceIsVisible", _qsettings->value("UniversalIndentGUI/whiteSpaceIsVisible", false) );
 
173
 
 
174
    // Read if indenter parameter tool tips are enabled.
 
175
    _qsettings->setValue( "UniversalIndentGUI/indenterParameterTooltipsEnabled", _qsettings->value("UniversalIndentGUI/indenterParameterTooltipsEnabled", true) );
 
176
 
 
177
    // Read the tab width from the settings file.
 
178
    _qsettings->setValue( "UniversalIndentGUI/tabWidth", _qsettings->value("UniversalIndentGUI/tabWidth", 4) );
 
179
 
 
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() ) );
 
182
 
 
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)) );
 
186
 
 
187
    // Read the main window state.
 
188
    _qsettings->setValue( "UniversalIndentGUI/MainWindowState", _qsettings->value("UniversalIndentGUI/MainWindowState", QByteArray()) );
 
189
 
 
190
    return true;
 
191
}
 
192
 
 
193
 
 
194
/*!
 
195
    \brief Register the by \a propertyName defined property of \a obj to be connected to the setting defined by \a settingName.
 
196
 
 
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.
 
199
 */
 
200
bool UiGuiSettings::registerObjectProperty( QObject *obj, const QString &propertyName, const QString &settingName )
 
201
{
 
202
    const QMetaObject *metaObject = obj->metaObject();
 
203
    bool connectSuccess = false;
 
204
 
 
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*)));
 
207
 
 
208
    int indexOfProp = metaObject->indexOfProperty( qPrintable(propertyName) );
 
209
    if ( connectSuccess && indexOfProp > -1 ) {
 
210
        QMetaProperty mProp = metaObject->property(indexOfProp);
 
211
 
 
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()));
 
218
        }
 
219
 
 
220
        if ( connectSuccess ) {
 
221
            _registeredObjectProperties[obj] = QStringList() << propertyName << settingName;
 
222
        }
 
223
        else {
 
224
            //TODO: Write a debug warning to the log.
 
225
            disconnect(obj, SIGNAL(destroyed(QObject*)), this, SLOT(unregisterObjectProperty(QObject*)));
 
226
            return false;
 
227
        }
 
228
 
 
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));
 
232
        }
 
233
        // Otherwise add the setting and set it to the value of the objects property.
 
234
        else {
 
235
            _qsettings->setValue("UniversalIndentGUI/" + settingName, mProp.read(obj));
 
236
        }
 
237
    }
 
238
    else {
 
239
        //TODO: Write a debug warning to the log.
 
240
        disconnect(obj, SIGNAL(destroyed(QObject*)), this, SLOT(unregisterObjectProperty(QObject*)));
 
241
        return false;
 
242
    }
 
243
 
 
244
    return true;
 
245
}
 
246
 
 
247
 
 
248
/*!
 
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.
 
251
 
 
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.
 
256
 
 
257
    Returns true, if all found property and setting definitions could be successfully registered.
 
258
    Returns false, if any of those registrations fails.
 
259
 */
 
260
bool UiGuiSettings::registerObjectPropertyRecursive(QObject *obj) {
 
261
    return checkCustomPropertiesAndCallFunction(obj, &UiGuiSettings::registerObjectProperty);
 
262
}
 
263
 
 
264
 
 
265
/*!
 
266
    \brief Assigns the by \a settingName defined setting value to the by \a propertyName defined property of \a obj.
 
267
 
 
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.
 
270
 */
 
271
bool UiGuiSettings::setObjectPropertyToSettingValue( QObject *obj, const QString &propertyName, const QString &settingName )
 
272
{
 
273
    const QMetaObject *metaObject = obj->metaObject();
 
274
 
 
275
    int indexOfProp = metaObject->indexOfProperty( qPrintable(propertyName) );
 
276
    if ( indexOfProp > -1 ) {
 
277
        QMetaProperty mProp = metaObject->property(indexOfProp);
 
278
 
 
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));
 
282
        }
 
283
        // The setting didn't exist so return that setting the objects property failed.
 
284
        else {
 
285
            //TODO: Write a debug warning to the log.
 
286
            return false;
 
287
        }
 
288
    }
 
289
    else {
 
290
        //TODO: Write a debug warning to the log.
 
291
        return false;
 
292
    }
 
293
 
 
294
    return true;
 
295
}
 
296
 
 
297
 
 
298
/*!
 
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.
 
301
 
 
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.
 
305
 
 
306
    Returns true, if all found property and setting definitions could be successfully registered.
 
307
    Returns false, if any of those registrations fails.
 
308
 */
 
309
bool UiGuiSettings::setObjectPropertyToSettingValueRecursive(QObject *obj) {
 
310
    return checkCustomPropertiesAndCallFunction(obj, &UiGuiSettings::setObjectPropertyToSettingValue);
 
311
}
 
312
 
 
313
 
 
314
/*!
 
315
    \brief Assigns the by \a propertyName defined property's value of \a obj to the by \a settingName defined setting.
 
316
 
 
317
    If the \a settingName didn't exist yet, it will be created.
 
318
 
 
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.
 
321
 */
 
322
bool UiGuiSettings::setSettingToObjectPropertyValue( QObject *obj, const QString &propertyName, const QString &settingName )
 
323
{
 
324
    const QMetaObject *metaObject = obj->metaObject();
 
325
 
 
326
    int indexOfProp = metaObject->indexOfProperty( qPrintable(propertyName) );
 
327
    if ( indexOfProp > -1 ) {
 
328
        QMetaProperty mProp = metaObject->property(indexOfProp);
 
329
 
 
330
        setValueByName(settingName, mProp.read(obj));
 
331
    }
 
332
    else {
 
333
        //TODO: Write a debug warning to the log.
 
334
        return false;
 
335
    }
 
336
 
 
337
    return true;
 
338
}
 
339
 
 
340
 
 
341
/*!
 
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.
 
344
 
 
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.
 
349
 
 
350
    Returns true, if all found property and setting definitions could be successfully registered.
 
351
    Returns false, if any of those registrations fails.
 
352
 */
 
353
bool UiGuiSettings::setSettingToObjectPropertyValueRecursive(QObject *obj) {
 
354
    return checkCustomPropertiesAndCallFunction(obj, &UiGuiSettings::setSettingToObjectPropertyValue);
 
355
}
 
356
 
 
357
 
 
358
/*!
 
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
 
361
    with both.
 
362
 */
 
363
bool UiGuiSettings::checkCustomPropertiesAndCallFunction(QObject *obj, bool (UiGuiSettings::*callBackFunc)(QObject *obj, const QString &propertyName, const QString &settingName)) {
 
364
    bool success = true;
 
365
 
 
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();
 
371
 
 
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 );
 
375
        }
 
376
    }
 
377
 
 
378
    return success;
 
379
}
 
380
 
 
381
 
 
382
/*!
 
383
    \brief The with a certain property registered \a obj gets unregistered.
 
384
 */
 
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();
 
390
 
 
391
        bool connectSuccess = false;
 
392
        int indexOfProp = metaObject->indexOfProperty( qPrintable(propertyName) );
 
393
        if ( indexOfProp > -1 ) {
 
394
            QMetaProperty mProp = metaObject->property(indexOfProp);
 
395
 
 
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()));
 
401
            }
 
402
        }
 
403
        _registeredObjectProperties.remove(obj);
 
404
    }
 
405
}
 
406
 
 
407
 
 
408
/*!
 
409
    \brief Registers a slot form the \a obj by its \a slotName to be invoked, if the by \a settingName defined
 
410
    setting changes.
 
411
 
 
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.
 
415
 */
 
416
bool UiGuiSettings::registerObjectSlot(QObject *obj, const QString &slotName, const QString &settingName) {
 
417
 
 
418
    const QMetaObject *metaObject = obj->metaObject();
 
419
 
 
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*)));
 
423
 
 
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();
 
430
 
 
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);
 
435
        }
 
436
        else {
 
437
            //TODO: Write a debug warning to the log.
 
438
            disconnect(obj, SIGNAL(destroyed(QObject*)), this, SLOT(unregisterObjectSlot(QObject*)));
 
439
            return false;
 
440
        }
 
441
    }
 
442
    else {
 
443
        //TODO: Write a debug warning to the log.
 
444
        disconnect(obj, SIGNAL(destroyed(QObject*)), this, SLOT(unregisterObjectSlot(QObject*)));
 
445
        return false;
 
446
    }
 
447
 
 
448
    return true;
 
449
}
 
450
 
 
451
 
 
452
/*!
 
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.
 
455
 */
 
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()) {
 
461
        it.next();
 
462
        if (it.key() == obj && slotName.isEmpty() && settingName.isEmpty())
 
463
            it.remove();
 
464
        else if (it.key() == obj && it.value().first() == slotName && it.value().last() == settingName)
 
465
            it.remove();
 
466
    }
 
467
}
 
468
 
 
469
 
 
470
/*!
 
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.
 
473
 */
 
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();
 
480
 
 
481
    int indexOfProp = metaObject->indexOfProperty( qPrintable(propertyName) );
 
482
    if ( indexOfProp > -1 ) {
 
483
        QMetaProperty mProp = metaObject->property(indexOfProp);
 
484
        setValueByName(settingName, mProp.read(obj));
 
485
    }
 
486
}
 
487
 
 
488
 
 
489
/*!
 
490
    \brief Sets the setting defined by \a settingName to \a value.
 
491
 
 
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.
 
495
 */
 
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);
 
500
 
 
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();
 
507
 
 
508
                int indexOfProp = metaObject->indexOfProperty( qPrintable(propertyName) );
 
509
                if ( indexOfProp > -1 ) {
 
510
                    QMetaProperty mProp = metaObject->property(indexOfProp);
 
511
                    mProp.write(obj, value);
 
512
                }
 
513
            }
 
514
        }
 
515
 
 
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();
 
522
 
 
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();
 
528
 
 
529
                    bool success = false;
 
530
 
 
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);
 
535
                        }
 
536
                    }
 
537
                    // Handle registered slots taking zero parameters.
 
538
                    else {
 
539
                        success = mMethod.invoke( obj, Qt::DirectConnection );
 
540
                    }
 
541
 
 
542
                    if ( success == false ) {
 
543
                        // TODO: Write a warning to the log if no success.
 
544
                    }
 
545
                }
 
546
            }
 
547
        }
 
548
    }
 
549
}
 
550
 
 
551
 
 
552
#include <QBitArray>
 
553
#include <QBitmap>
 
554
#include <QBrush>
 
555
#include <QCursor>
 
556
#include <QDateTime>
 
557
#include <QFont>
 
558
#include <QIcon>
 
559
#include <QKeySequence>
 
560
#include <QLocale>
 
561
#include <QPalette>
 
562
#include <QPen>
 
563
#include <QSizePolicy>
 
564
#include <QTextFormat>
 
565
#include <QTextLength>
 
566
#include <QUrl>
 
567
#if QT_VERSION >= 0x040600
 
568
#include <QMatrix4x4>
 
569
#include <QVector2D>
 
570
#endif
 
571
 
 
572
bool UiGuiSettings::invokeMethodWithValue( QObject *obj, QMetaMethod mMethod, QVariant value )
 
573
{
 
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>()) );
 
605
    case QVariant::Int :
 
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()) );
 
619
    case QVariant::Map :
 
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>()) );
 
628
#endif
 
629
    case QVariant::Palette :
 
630
        return mMethod.invoke( obj, Qt::DirectConnection, Q_ARG(QPalette, value.value<QPalette>()) );
 
631
    case QVariant::Pen :
 
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>()) );
 
646
#endif
 
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()) );
 
675
    case QVariant::Url :
 
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>()) );
 
684
#endif
 
685
    default:
 
686
        return false;
 
687
    }
 
688
}