dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
generalsettings.cpp
Go to the documentation of this file.
1 
15 /*
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful, but
22  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24  * for more details.
25  *
26  * You should have received a copy of the GNU General Public License along
27  * with this program; if not, see <http://www.gnu.org/licenses/>
28  */
29 
30 #include "generalsettings.h"
31 
32 #include <coreplugin/icore.h>
33 #include <QMainWindow>
34 #include <QMessageBox>
35 #include <QtCore/QDir>
36 #include <QtCore/QLibraryInfo>
37 #include <QtCore/QSettings>
38 #include <QDialog>
39 #include "ui_generalsettings.h"
40 
41 using namespace Core::Internal;
42 
44  : m_saveSettingsOnExit(true)
45  , m_autoConnect(true)
46  , m_autoSelect(true)
47  , m_useExpertMode(false)
48  , m_dialog(nullptr)
49  , m_proxyType(QNetworkProxy::NoProxy)
50  , m_proxyPort(0)
51 {
52 }
53 
54 QString GeneralSettings::id() const
55 {
56  return QLatin1String("General");
57 }
58 
59 QString GeneralSettings::trName() const
60 {
61  return tr("General");
62 }
63 
65 {
66  return QLatin1String("Environment");
67 }
68 
70 {
71  return tr("Environment");
72 }
73 
74 static bool hasQmFilesForLocale(const QString &locale, const QString &creatorTrPath)
75 {
76  static const QString qtTrPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
77 
78  const QString trFile = QLatin1String("qt_") + locale + QLatin1String(".qm");
79  return QFile::exists(qtTrPath + '/' + trFile) || QFile::exists(creatorTrPath + '/' + trFile);
80 }
81 
82 void GeneralSettings::fillLanguageBox() const
83 {
84  const QString currentLocale = language();
85 
86  m_page->languageBox->addItem(tr("<System Language>"), QString());
87  // need to add this explicitly, since there is no qm file for English
88  m_page->languageBox->addItem(QLatin1String("English"), QLatin1String("C"));
89  if (currentLocale == QLatin1String("C"))
90  m_page->languageBox->setCurrentIndex(m_page->languageBox->count() - 1);
91 
92  const QString creatorTrPath =
93  Core::ICore::instance()->resourcePath() + QLatin1String("/translations");
94  const QStringList languageFiles =
95  QDir(creatorTrPath).entryList(QStringList(QLatin1String("dronin*.qm")));
96 
97  Q_FOREACH (const QString &languageFile, languageFiles) {
98  int start = languageFile.indexOf(QLatin1Char('_')) + 1;
99  int end = languageFile.lastIndexOf(QLatin1Char('.'));
100  const QString locale = languageFile.mid(start, end - start);
101  // no need to show a language that creator will not load anyway
102  if (hasQmFilesForLocale(locale, creatorTrPath)) {
103  m_page->languageBox->addItem(QLocale::languageToString(QLocale(locale).language()),
104  locale);
105  if (locale == currentLocale)
106  m_page->languageBox->setCurrentIndex(m_page->languageBox->count() - 1);
107  }
108  }
109 }
110 
111 void GeneralSettings::fillProxyTypesBox() const
112 {
113  m_page->proxyTypeCB->addItem("No Proxy", 2);
114  m_page->proxyTypeCB->addItem("Socks5Proxy", 1);
115  m_page->proxyTypeCB->addItem("HttpProxy", 3);
116  m_page->proxyTypeCB->addItem("HttpCachingProxy", 4);
117  m_page->proxyTypeCB->addItem("FtpCachingProxy", 5);
118 }
119 
120 QWidget *GeneralSettings::createPage(QWidget *parent)
121 {
122  m_page = new Ui::GeneralSettings();
123  QWidget *w = new QWidget(parent);
124  m_page->setupUi(w);
125  fillLanguageBox();
126  fillProxyTypesBox();
127  connect(m_page->checkAutoConnect, SIGNAL(stateChanged(int)), this, SLOT(slotAutoConnect(int)));
128  m_page->checkBoxSaveOnExit->setChecked(m_saveSettingsOnExit);
129  m_page->checkAutoConnect->setChecked(m_autoConnect);
130  m_page->checkAutoSelect->setChecked(m_autoSelect);
131  m_page->cbExpertMode->setChecked(m_useExpertMode);
132  m_page->proxyTypeCB->setCurrentIndex(m_page->proxyTypeCB->findData(m_proxyType));
133  m_page->portLE->setText(QString::number(m_proxyPort));
134  m_page->hostNameLE->setText(m_proxyHostname);
135  m_page->userLE->setText(m_proxyUser);
136  m_page->passwordLE->setText(m_proxyPassword);
137 
138  return w;
139 }
140 
142 {
143  int currentIndex = m_page->languageBox->currentIndex();
144  setLanguage(m_page->languageBox->itemData(currentIndex, Qt::UserRole).toString());
145  // Apply the new base color if accepted
146 
147  m_saveSettingsOnExit = m_page->checkBoxSaveOnExit->isChecked();
148  m_useExpertMode = m_page->cbExpertMode->isChecked();
149  m_autoConnect = m_page->checkAutoConnect->isChecked();
150  m_autoSelect = m_page->checkAutoSelect->isChecked();
151  m_proxyType = m_page->proxyTypeCB->itemData(m_page->proxyTypeCB->currentIndex()).toInt();
152  m_proxyPort = m_page->portLE->text().toInt();
153  m_proxyHostname = m_page->hostNameLE->text();
154  m_proxyUser = m_page->userLE->text();
155  m_proxyPassword = m_page->passwordLE->text();
156  QNetworkProxy::setApplicationProxy(getNetworkProxy());
157  emit generalSettingsChanged();
158 }
159 
161 {
162  delete m_page;
163 }
164 
165 void GeneralSettings::readSettings(QSettings *qs)
166 {
167  qs->beginGroup(QLatin1String("General"));
168  m_language = qs->value(QLatin1String("OverrideLanguage"), QLocale::system().name()).toString();
169  m_saveSettingsOnExit =
170  qs->value(QLatin1String("SaveSettingsOnExit"), m_saveSettingsOnExit).toBool();
171  m_autoConnect = qs->value(QLatin1String("AutoConnect"), m_autoConnect).toBool();
172  m_autoSelect = qs->value(QLatin1String("AutoSelect"), m_autoSelect).toBool();
173  m_useExpertMode = qs->value(QLatin1String("ExpertMode"), m_useExpertMode).toBool();
174  m_proxyType = qs->value(QLatin1String("proxytype"), m_proxyType).toInt();
175  m_proxyPort = qs->value(QLatin1String("proxyport"), m_proxyPort).toInt();
176  m_proxyHostname = qs->value(QLatin1String("proxyhostname"), m_proxyHostname).toString();
177  m_proxyUser = qs->value(QLatin1String("proxyuser"), m_proxyUser).toString();
178  m_proxyPassword = qs->value(QLatin1String("proxypassword"), m_proxyPassword).toString();
179  m_observations = qs->value(QLatin1String("observations"), "").toString();
180  m_vehicle = qs->value(QLatin1String("vehicle"), "").toString();
181  m_board = qs->value(QLatin1String("board"), "").toString();
182  m_weight = qs->value(QLatin1String("weight"), 0).toInt();
183  m_size = qs->value(QLatin1String("size"), 0).toInt();
184  m_cells = qs->value(QLatin1String("cells"), 0).toInt();
185  m_motors = qs->value(QLatin1String("motors"), "").toString();
186  m_escs = qs->value(QLatin1String("escs"), "").toString();
187  m_props = qs->value(QLatin1String("props"), "").toString();
188  qs->endGroup();
189  emit generalSettingsChanged();
190 }
191 
192 void GeneralSettings::saveSettings(QSettings *qs)
193 {
194  qs->beginGroup(QLatin1String("General"));
195 
196  if (m_language.isEmpty())
197  qs->remove(QLatin1String("OverrideLanguage"));
198  else
199  qs->setValue(QLatin1String("OverrideLanguage"), m_language);
200 
201  qs->setValue(QLatin1String("SaveSettingsOnExit"), m_saveSettingsOnExit);
202  qs->setValue(QLatin1String("AutoConnect"), m_autoConnect);
203  qs->setValue(QLatin1String("AutoSelect"), m_autoSelect);
204  qs->setValue(QLatin1String("ExpertMode"), m_useExpertMode);
205 
206  qs->setValue(QLatin1String("proxytype"), m_proxyType);
207  qs->setValue(QLatin1String("proxyport"), m_proxyPort);
208  qs->setValue(QLatin1String("proxyhostname"), m_proxyHostname);
209  qs->setValue(QLatin1String("proxyuser"), m_proxyUser);
210  qs->setValue(QLatin1String("proxypassword"), m_proxyPassword);
211  qs->setValue(QLatin1String("observations"), m_observations);
212  qs->setValue(QLatin1String("vehicle"), m_vehicle);
213  qs->setValue(QLatin1String("board"), m_board);
214  qs->setValue(QLatin1String("weight"), m_weight);
215  qs->setValue(QLatin1String("size"), m_size);
216  qs->setValue(QLatin1String("cells"), m_cells);
217  qs->setValue(QLatin1String("motors"), m_motors);
218  qs->setValue(QLatin1String("escs"), m_escs);
219  qs->setValue(QLatin1String("props"), m_props);
220  qs->endGroup();
221 }
222 
223 void GeneralSettings::showHelpForExternalEditor()
224 {
225  if (m_dialog) {
226  m_dialog->show();
227  m_dialog->raise();
228  m_dialog->activateWindow();
229  return;
230  }
231 }
232 
233 void GeneralSettings::resetLanguage()
234 {
235  // system language is default
236  m_page->languageBox->setCurrentIndex(0);
237 }
238 
239 QString GeneralSettings::language() const
240 {
241  return m_language;
242 }
243 
244 void GeneralSettings::setLanguage(const QString &locale)
245 {
246  if (m_language != locale) {
247  if (!locale.isEmpty()) {
248  QMessageBox::information(
249  dynamic_cast<QWidget *>(Core::ICore::instance()->mainWindow()),
250  tr("Restart required"),
251  tr("The language change will take effect after a restart of the GCS."));
252  }
253  m_language = locale;
254  }
255 }
256 
258 {
259  return m_saveSettingsOnExit;
260 }
261 
263 {
264  return m_autoConnect;
265 }
266 
268 {
269  return m_autoSelect;
270 }
271 
273 {
274  return m_useExpertMode;
275 }
276 
278 {
279  return QNetworkProxy((QNetworkProxy::ProxyType)m_proxyType, m_proxyHostname, m_proxyPort,
280  m_proxyUser, m_proxyPassword);
281 }
282 
284 {
285  m_observations = value;
286 }
287 
289 {
290  m_vehicle = value;
291 }
292 
294 {
295  return m_observations;
296 }
297 
299 {
300  return m_vehicle;
301 }
302 
304 {
305  m_board = type;
306 }
307 
309 {
310  return m_board;
311 }
312 
314 {
315  m_weight = weight;
316 }
317 
319 {
320  return m_weight;
321 }
322 
324 {
325  m_size = size;
326 }
327 
329 {
330  return m_size;
331 }
332 
334 {
335  m_cells = cells;
336 }
337 
339 {
340  return m_cells;
341 }
342 
343 void GeneralSettings::setMotors(QString motors)
344 {
345  m_motors = motors;
346 }
347 
349 {
350  return m_motors;
351 }
352 
353 void GeneralSettings::setESCs(QString escs)
354 {
355  m_escs = escs;
356 }
357 
359 {
360  return m_escs;
361 }
362 
363 void GeneralSettings::setProps(QString props)
364 {
365  m_props = props;
366 }
367 
369 {
370  return m_props;
371 }
372 
373 void GeneralSettings::slotAutoConnect(int value)
374 {
375  if (value == Qt::Checked)
376  m_page->checkAutoSelect->setEnabled(false);
377  else
378  m_page->checkAutoSelect->setEnabled(true);
379 }
virtual QString resourcePath() const =0
Returns the absolute path that is used for resources like project templates and the debugger macros...
end(INSTANTIATIONCODE) fid
static ICore * instance()
Definition: coreimpl.cpp:46
QWidget * createPage(QWidget *parent)