LP-419 cleanup QSettings usage throughout GCS
[librepilot.git] / ground / gcs / src / plugins / coreplugin / generalsettings.cpp
blob4117bcc207eff26d0ac7eb292ac581f743c94233
1 /**
2 ******************************************************************************
4 * @file generalsettings.cpp
5 * @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2015.
6 * The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
7 * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009.
8 * @addtogroup GCSPlugins GCS Plugins
9 * @{
10 * @addtogroup CorePlugin Core Plugin
11 * @{
12 * @brief The Core GCS plugin
13 *****************************************************************************/
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 3 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 * for more details.
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "generalsettings.h"
32 #include "ui_generalsettings.h"
34 #include <utils/stylehelper.h>
35 #include <utils/qtcolorbutton.h>
36 #include <utils/consoleprocess.h>
37 #include <coreplugin/icore.h>
39 #include <QMessageBox>
40 #include <QDir>
41 #include <QLibraryInfo>
42 #include <QSettings>
44 using namespace Utils;
45 using namespace Core::Internal;
47 GeneralSettings::GeneralSettings() :
48 m_saveSettingsOnExit(true),
49 m_autoConnect(true),
50 m_autoSelect(true),
51 m_useUDPMirror(false),
52 m_useExpertMode(false),
53 m_collectUsageData(true),
54 m_showUsageDataDisclaimer(true),
55 m_dialog(0)
58 QString GeneralSettings::id() const
60 return QLatin1String("General");
63 QString GeneralSettings::trName() const
65 return tr("General");
68 QString GeneralSettings::category() const
70 return QLatin1String("Environment");
73 QString GeneralSettings::trCategory() const
75 return tr("Environment");
78 static bool hasQmFilesForLocale(const QString &locale, const QString &creatorTrPath)
80 static const QString qtTrPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
82 const QString trFile = QLatin1String("qt_") + locale + QLatin1String(".qm");
84 return QFile::exists(qtTrPath + '/' + trFile) || QFile::exists(creatorTrPath + '/' + trFile);
87 void GeneralSettings::fillLanguageBox() const
89 const QString currentLocale = language();
91 m_page->languageBox->addItem(tr("<System Language>"), QString());
92 // need to add this explicitly, since there is no qm file for English
93 m_page->languageBox->addItem(QLatin1String("English"), QLatin1String("C"));
94 if (currentLocale == QLatin1String("C")) {
95 m_page->languageBox->setCurrentIndex(m_page->languageBox->count() - 1);
98 const QString creatorTrPath = Core::ICore::instance()->resourcePath() + QLatin1String("/translations");
99 const QStringList languageFiles = QDir(creatorTrPath).entryList(QStringList(QLatin1String("gcs*.qm")));
101 foreach(QString languageFile, languageFiles) {
102 int start = languageFile.indexOf(QLatin1Char('_')) + 1;
103 int end = languageFile.lastIndexOf(QLatin1Char('.'));
104 const QString locale = languageFile.mid(start, end - start);
106 // no need to show a language that creator will not load anyway
107 if (hasQmFilesForLocale(locale, creatorTrPath)) {
108 m_page->languageBox->addItem(QLocale::languageToString(QLocale(locale).language()), locale);
109 if (locale == currentLocale) {
110 m_page->languageBox->setCurrentIndex(m_page->languageBox->count() - 1);
116 QWidget *GeneralSettings::createPage(QWidget *parent)
118 m_page = new Ui::GeneralSettings();
119 QWidget *w = new QWidget(parent);
120 m_page->setupUi(w);
122 fillLanguageBox();
124 connect(m_page->checkAutoConnect, SIGNAL(stateChanged(int)), this, SLOT(slotAutoConnect(int)));
126 m_page->checkBoxSaveOnExit->setChecked(m_saveSettingsOnExit);
127 m_page->checkAutoConnect->setChecked(m_autoConnect);
128 m_page->checkAutoSelect->setChecked(m_autoSelect);
129 m_page->cbUseUDPMirror->setChecked(m_useUDPMirror);
130 m_page->cbExpertMode->setChecked(m_useExpertMode);
131 m_page->cbUsageData->setChecked(m_collectUsageData);
132 m_page->colorButton->setColor(StyleHelper::baseColor());
134 connect(m_page->resetButton, SIGNAL(clicked()), this, SLOT(resetInterfaceColor()));
136 return w;
139 void GeneralSettings::apply()
141 int currentIndex = m_page->languageBox->currentIndex();
143 setLanguage(m_page->languageBox->itemData(currentIndex, Qt::UserRole).toString());
144 // Apply the new base color if accepted
145 StyleHelper::setBaseColor(m_page->colorButton->color());
147 m_saveSettingsOnExit = m_page->checkBoxSaveOnExit->isChecked();
148 m_useUDPMirror = m_page->cbUseUDPMirror->isChecked();
149 m_useExpertMode = m_page->cbExpertMode->isChecked();
150 m_autoConnect = m_page->checkAutoConnect->isChecked();
151 m_autoSelect = m_page->checkAutoSelect->isChecked();
152 setCollectUsageData(m_page->cbUsageData->isChecked());
155 void GeneralSettings::finish()
157 delete m_page;
160 void GeneralSettings::readSettings(QSettings &settings)
162 settings.beginGroup(QLatin1String("General"));
163 m_language = settings.value(QLatin1String("OverrideLanguage"), QLocale::system().name()).toString();
164 m_saveSettingsOnExit = settings.value(QLatin1String("SaveSettingsOnExit"), m_saveSettingsOnExit).toBool();
165 m_autoConnect = settings.value(QLatin1String("AutoConnect"), m_autoConnect).toBool();
166 m_autoSelect = settings.value(QLatin1String("AutoSelect"), m_autoSelect).toBool();
167 m_useUDPMirror = settings.value(QLatin1String("UDPMirror"), m_useUDPMirror).toBool();
168 m_useExpertMode = settings.value(QLatin1String("ExpertMode"), m_useExpertMode).toBool();
169 m_collectUsageData = settings.value(QLatin1String("CollectUsageData"), m_collectUsageData).toBool();
170 m_showUsageDataDisclaimer = settings.value(QLatin1String("ShowUsageDataDisclaimer"), m_showUsageDataDisclaimer).toBool();
171 m_lastUsageHash = settings.value(QLatin1String("LastUsageHash"), m_lastUsageHash).toString();
172 settings.endGroup();
175 void GeneralSettings::saveSettings(QSettings &settings) const
177 settings.beginGroup(QLatin1String("General"));
179 if (m_language.isEmpty()) {
180 settings.remove(QLatin1String("OverrideLanguage"));
181 } else {
182 settings.setValue(QLatin1String("OverrideLanguage"), m_language);
185 settings.setValue(QLatin1String("SaveSettingsOnExit"), m_saveSettingsOnExit);
186 settings.setValue(QLatin1String("AutoConnect"), m_autoConnect);
187 settings.setValue(QLatin1String("AutoSelect"), m_autoSelect);
188 settings.setValue(QLatin1String("UDPMirror"), m_useUDPMirror);
189 settings.setValue(QLatin1String("ExpertMode"), m_useExpertMode);
190 settings.setValue(QLatin1String("CollectUsageData"), m_collectUsageData);
191 settings.setValue(QLatin1String("ShowUsageDataDisclaimer"), m_showUsageDataDisclaimer);
192 settings.setValue(QLatin1String("LastUsageHash"), m_lastUsageHash);
193 settings.endGroup();
196 void GeneralSettings::resetInterfaceColor()
198 m_page->colorButton->setColor(0x666666);
201 void GeneralSettings::showHelpForExternalEditor()
203 if (m_dialog) {
204 m_dialog->show();
205 m_dialog->raise();
206 m_dialog->activateWindow();
207 return;
209 #if 0
210 QMessageBox *mb = new QMessageBox(QMessageBox::Information,
211 tr("Variables"),
212 EditorManager::instance()->externalEditorHelpText(),
213 QMessageBox::Cancel,
214 m_page->helpExternalEditorButton);
215 mb->setWindowModality(Qt::NonModal);
216 m_dialog = mb;
217 mb->show();
218 #endif
221 void GeneralSettings::resetLanguage()
223 // system language is default
224 m_page->languageBox->setCurrentIndex(0);
227 QString GeneralSettings::language() const
229 return m_language;
232 void GeneralSettings::setLanguage(const QString &locale)
234 if (m_language != locale) {
235 if (!locale.isEmpty()) {
236 QMessageBox::information((QWidget *)Core::ICore::instance()->mainWindow(), tr("Restart required"),
237 tr("The language change will take effect after a restart of %1.").arg(GCS_BIG_NAME));
239 m_language = locale;
243 bool GeneralSettings::saveSettingsOnExit() const
245 return m_saveSettingsOnExit;
248 bool GeneralSettings::autoConnect() const
250 return m_autoConnect;
253 bool GeneralSettings::autoSelect() const
255 return m_autoSelect;
258 bool GeneralSettings::useUDPMirror() const
260 return m_useUDPMirror;
263 bool GeneralSettings::collectUsageData() const
265 return m_collectUsageData;
268 bool GeneralSettings::showUsageDataDisclaimer() const
270 return m_showUsageDataDisclaimer;
273 QString GeneralSettings::lastUsageHash() const
275 return m_lastUsageHash;
278 bool GeneralSettings::useExpertMode() const
280 return m_useExpertMode;
283 void GeneralSettings::setCollectUsageData(bool collect)
285 if (collect && collect != m_collectUsageData) {
286 setShowUsageDataDisclaimer(true);
288 m_collectUsageData = collect;
291 void GeneralSettings::setShowUsageDataDisclaimer(bool show)
293 m_showUsageDataDisclaimer = show;
296 void GeneralSettings::setLastUsageHash(QString hash)
298 m_lastUsageHash = hash;
301 void GeneralSettings::slotAutoConnect(int value)
303 if (value == Qt::Checked) {
304 m_page->checkAutoSelect->setEnabled(false);
305 } else {
306 m_page->checkAutoSelect->setEnabled(true);