LP-56 - Better txpid option namings, fix tabs-spaces, tooltips. headers, variable...
[librepilot.git] / ground / openpilotgcs / src / plugins / coreplugin / generalsettings.cpp
blobf76de6a16beeb50b609c69f6a778c220afb9914e
1 /**
2 ******************************************************************************
4 * @file generalsettings.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009.
7 * @addtogroup GCSPlugins GCS Plugins
8 * @{
9 * @addtogroup CorePlugin Core Plugin
10 * @{
11 * @brief The Core GCS plugin
12 *****************************************************************************/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "generalsettings.h"
31 #include <utils/stylehelper.h>
32 #include <utils/qtcolorbutton.h>
33 #include <utils/consoleprocess.h>
34 #include <coreplugin/icore.h>
35 #include <QMessageBox>
36 #include <QtCore/QDir>
38 #include <QtCore/QLibraryInfo>
39 #include <QtCore/QSettings>
41 #include "ui_generalsettings.h"
43 using namespace Utils;
44 using namespace Core::Internal;
46 GeneralSettings::GeneralSettings() :
47 m_saveSettingsOnExit(true),
48 m_autoConnect(true),
49 m_autoSelect(true),
50 m_useUDPMirror(false),
51 m_useExpertMode(false),
52 m_collectUsageData(true),
53 m_showUsageDataDisclaimer(true),
54 m_dialog(0)
57 QString GeneralSettings::id() const
59 return QLatin1String("General");
62 QString GeneralSettings::trName() const
64 return tr("General");
67 QString GeneralSettings::category() const
69 return QLatin1String("Environment");
72 QString GeneralSettings::trCategory() const
74 return tr("Environment");
77 static bool hasQmFilesForLocale(const QString &locale, const QString &creatorTrPath)
79 static const QString qtTrPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
81 const QString trFile = QLatin1String("qt_") + locale + QLatin1String(".qm");
83 return QFile::exists(qtTrPath + '/' + trFile) || QFile::exists(creatorTrPath + '/' + trFile);
86 void GeneralSettings::fillLanguageBox() const
88 const QString currentLocale = language();
90 m_page->languageBox->addItem(tr("<System Language>"), QString());
91 // need to add this explicitly, since there is no qm file for English
92 m_page->languageBox->addItem(QLatin1String("English"), QLatin1String("C"));
93 if (currentLocale == QLatin1String("C")) {
94 m_page->languageBox->setCurrentIndex(m_page->languageBox->count() - 1);
97 const QString creatorTrPath = Core::ICore::instance()->resourcePath() + QLatin1String("/translations");
98 const QStringList languageFiles = QDir(creatorTrPath).entryList(QStringList(QLatin1String("openpilotgcs*.qm")));
100 foreach(QString languageFile, languageFiles) {
101 int start = languageFile.indexOf(QLatin1Char('_')) + 1;
102 int end = languageFile.lastIndexOf(QLatin1Char('.'));
103 const QString locale = languageFile.mid(start, end - start);
105 // no need to show a language that creator will not load anyway
106 if (hasQmFilesForLocale(locale, creatorTrPath)) {
107 m_page->languageBox->addItem(QLocale::languageToString(QLocale(locale).language()), locale);
108 if (locale == currentLocale) {
109 m_page->languageBox->setCurrentIndex(m_page->languageBox->count() - 1);
115 QWidget *GeneralSettings::createPage(QWidget *parent)
117 m_page = new Ui::GeneralSettings();
118 QWidget *w = new QWidget(parent);
119 m_page->setupUi(w);
121 fillLanguageBox();
123 connect(m_page->checkAutoConnect, SIGNAL(stateChanged(int)), this, SLOT(slotAutoConnect(int)));
125 m_page->checkBoxSaveOnExit->setChecked(m_saveSettingsOnExit);
126 m_page->checkAutoConnect->setChecked(m_autoConnect);
127 m_page->checkAutoSelect->setChecked(m_autoSelect);
128 m_page->cbUseUDPMirror->setChecked(m_useUDPMirror);
129 m_page->cbExpertMode->setChecked(m_useExpertMode);
130 m_page->cbUsageData->setChecked(m_collectUsageData);
131 m_page->colorButton->setColor(StyleHelper::baseColor());
133 connect(m_page->resetButton, SIGNAL(clicked()), this, SLOT(resetInterfaceColor()));
135 return w;
138 void GeneralSettings::apply()
140 int currentIndex = m_page->languageBox->currentIndex();
142 setLanguage(m_page->languageBox->itemData(currentIndex, Qt::UserRole).toString());
143 // Apply the new base color if accepted
144 StyleHelper::setBaseColor(m_page->colorButton->color());
146 m_saveSettingsOnExit = m_page->checkBoxSaveOnExit->isChecked();
147 m_useUDPMirror = m_page->cbUseUDPMirror->isChecked();
148 m_useExpertMode = m_page->cbExpertMode->isChecked();
149 m_autoConnect = m_page->checkAutoConnect->isChecked();
150 m_autoSelect = m_page->checkAutoSelect->isChecked();
151 setCollectUsageData(m_page->cbUsageData->isChecked());
154 void GeneralSettings::finish()
156 delete m_page;
159 void GeneralSettings::readSettings(QSettings *qs)
161 qs->beginGroup(QLatin1String("General"));
162 m_language = qs->value(QLatin1String("OverrideLanguage"), QLocale::system().name()).toString();
163 m_saveSettingsOnExit = qs->value(QLatin1String("SaveSettingsOnExit"), m_saveSettingsOnExit).toBool();
164 m_autoConnect = qs->value(QLatin1String("AutoConnect"), m_autoConnect).toBool();
165 m_autoSelect = qs->value(QLatin1String("AutoSelect"), m_autoSelect).toBool();
166 m_useUDPMirror = qs->value(QLatin1String("UDPMirror"), m_useUDPMirror).toBool();
167 m_useExpertMode = qs->value(QLatin1String("ExpertMode"), m_useExpertMode).toBool();
168 m_collectUsageData = qs->value(QLatin1String("CollectUsageData"), m_collectUsageData).toBool();
169 m_showUsageDataDisclaimer = qs->value(QLatin1String("ShowUsageDataDisclaimer"), m_showUsageDataDisclaimer).toBool();
170 m_lastUsageHash = qs->value(QLatin1String("LastUsageHash"), m_lastUsageHash).toString();
171 qs->endGroup();
174 void GeneralSettings::saveSettings(QSettings *qs)
176 qs->beginGroup(QLatin1String("General"));
178 if (m_language.isEmpty()) {
179 qs->remove(QLatin1String("OverrideLanguage"));
180 } else {
181 qs->setValue(QLatin1String("OverrideLanguage"), m_language);
184 qs->setValue(QLatin1String("SaveSettingsOnExit"), m_saveSettingsOnExit);
185 qs->setValue(QLatin1String("AutoConnect"), m_autoConnect);
186 qs->setValue(QLatin1String("AutoSelect"), m_autoSelect);
187 qs->setValue(QLatin1String("UDPMirror"), m_useUDPMirror);
188 qs->setValue(QLatin1String("ExpertMode"), m_useExpertMode);
189 qs->setValue(QLatin1String("CollectUsageData"), m_collectUsageData);
190 qs->setValue(QLatin1String("ShowUsageDataDisclaimer"), m_showUsageDataDisclaimer);
191 qs->setValue(QLatin1String("LastUsageHash"), m_lastUsageHash);
192 qs->endGroup();
195 void GeneralSettings::resetInterfaceColor()
197 m_page->colorButton->setColor(0x666666);
200 void GeneralSettings::showHelpForExternalEditor()
202 if (m_dialog) {
203 m_dialog->show();
204 m_dialog->raise();
205 m_dialog->activateWindow();
206 return;
208 #if 0
209 QMessageBox *mb = new QMessageBox(QMessageBox::Information,
210 tr("Variables"),
211 EditorManager::instance()->externalEditorHelpText(),
212 QMessageBox::Cancel,
213 m_page->helpExternalEditorButton);
214 mb->setWindowModality(Qt::NonModal);
215 m_dialog = mb;
216 mb->show();
217 #endif
220 void GeneralSettings::resetLanguage()
222 // system language is default
223 m_page->languageBox->setCurrentIndex(0);
226 QString GeneralSettings::language() const
228 return m_language;
231 void GeneralSettings::setLanguage(const QString &locale)
233 if (m_language != locale) {
234 if (!locale.isEmpty()) {
235 QMessageBox::information((QWidget *)Core::ICore::instance()->mainWindow(), tr("Restart required"),
236 tr("The language change will take effect after a restart of %1.").arg(GCS_BIG_NAME));
238 m_language = locale;
242 bool GeneralSettings::saveSettingsOnExit() const
244 return m_saveSettingsOnExit;
247 bool GeneralSettings::autoConnect() const
249 return m_autoConnect;
252 bool GeneralSettings::autoSelect() const
254 return m_autoSelect;
257 bool GeneralSettings::useUDPMirror() const
259 return m_useUDPMirror;
262 bool GeneralSettings::collectUsageData() const
264 return m_collectUsageData;
267 bool GeneralSettings::showUsageDataDisclaimer() const
269 return m_showUsageDataDisclaimer;
272 QString GeneralSettings::lastUsageHash() const
274 return m_lastUsageHash;
277 bool GeneralSettings::useExpertMode() const
279 return m_useExpertMode;
282 void GeneralSettings::setCollectUsageData(bool collect)
284 if (collect && collect != m_collectUsageData) {
285 setShowUsageDataDisclaimer(true);
287 m_collectUsageData = collect;
290 void GeneralSettings::setShowUsageDataDisclaimer(bool show)
292 m_showUsageDataDisclaimer = show;
295 void GeneralSettings::setLastUsageHash(QString hash)
297 m_lastUsageHash = hash;
300 void GeneralSettings::slotAutoConnect(int value)
302 if (value == Qt::Checked) {
303 m_page->checkAutoSelect->setEnabled(false);
304 } else {
305 m_page->checkAutoSelect->setEnabled(true);