source: tspsg-svn/trunk/src/settingsdialog.cpp @ 139

Last change on this file since 139 was 139, checked in by laleppa, 14 years ago

+ Automatically check for updates at the given interval functionality (Windows only at this moment).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id URL
File size: 18.0 KB
Line 
1/*
2 *  TSPSG: TSP Solver and Generator
3 *  Copyright (C) 2007-2010 Lёppa <contacts[at]oleksii[dot]name>
4 *
5 *  $Id: settingsdialog.cpp 139 2010-09-29 21:21:46Z laleppa $
6 *  $URL: https://tspsg.svn.sourceforge.net/svnroot/tspsg/trunk/src/settingsdialog.cpp $
7 *
8 *  This file is part of TSPSG.
9 *
10 *  TSPSG is free software: you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License as published by
12 *  the Free Software Foundation, either version 3 of the License, or
13 *  (at your option) any later version.
14 *
15 *  TSPSG is distributed in the hope that it will be useful,
16 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 *  GNU General Public License for more details.
19 *
20 *  You should have received a copy of the GNU General Public License
21 *  along with TSPSG.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24#include "settingsdialog.h"
25
26/*!
27 * \brief Class constructor.
28 * \param parent Settings Dialog parent widget.
29 *
30 * Initializes Settings Dialog and creates its layout based on target OS.
31 */
32SettingsDialog::SettingsDialog(QWidget *parent)
33        : QDialog(parent), _newFont(false), _newColor(false), _translucency(0)
34{
35        setupUi(this);
36
37        setWindowIcon(GET_ICON("preferences-system"));
38
39        buttonBox->button(QDialogButtonBox::Ok)->setIcon(GET_ICON("dialog-ok"));
40        buttonBox->button(QDialogButtonBox::Ok)->setStatusTip(tr("Save new preferences"));
41        buttonBox->button(QDialogButtonBox::Ok)->setCursor(QCursor(Qt::PointingHandCursor));
42        buttonBox->button(QDialogButtonBox::Cancel)->setIcon(GET_ICON("dialog-cancel"));
43        buttonBox->button(QDialogButtonBox::Cancel)->setStatusTip(tr("Close without saving preferences"));
44        buttonBox->button(QDialogButtonBox::Cancel)->setCursor(QCursor(Qt::PointingHandCursor));
45
46        buttonColor->setIcon(GET_ICON("format-text-color"));
47        buttonFont->setIcon(GET_ICON("preferences-desktop-font"));
48        buttonHelp->setIcon(GET_ICON("help-hint"));
49
50QPalette p = bgWhite->palette();
51        p.setColor(QPalette::Window, p.color(QPalette::Base));
52        bgWhite->setPalette(p);
53
54        p = lineHorizontal->palette();
55        p.setColor(QPalette::Window, p.color(QPalette::Text));
56        lineHorizontal->setPalette(p);
57
58#ifndef QT_NO_PRINTER
59        cbHQGraph = new QCheckBox(bgWhite);
60        cbHQGraph->setObjectName("cbHQGraph");
61#ifndef QT_NO_STATUSTIP
62        cbHQGraph->setStatusTip(tr("Higher quality graph looks much better when printing but uglier on the screen"));
63#endif // QT_NO_STATUSTIP
64        cbHQGraph->setText(tr("Draw solution graph in higher quality"));
65        cbHQGraph->setCursor(QCursor(Qt::PointingHandCursor));
66
67QBoxLayout *box = static_cast<QBoxLayout *>(tabOutput->layout());
68QHBoxLayout *hbox1 = new QHBoxLayout();
69        hbox1->addSpacing(10);
70        hbox1->addWidget(cbHQGraph);
71        box->insertLayout(box->indexOf(cbShowGraph) + 1, hbox1);
72        connect(cbShowGraph, SIGNAL(toggled(bool)), cbHQGraph, SLOT(setEnabled(bool)));
73#endif
74
75        if (hasUpdater()) {
76                cbCheck4Updates = new QCheckBox(bgWhite);
77                cbCheck4Updates->setObjectName("cbCheck4Updates");
78#ifndef QT_NO_STATUSTIP
79                cbCheck4Updates->setStatusTip(tr("Automatically check for updates at the given interval"));
80#endif // QT_NO_STATUSTIP
81                cbCheck4Updates->setText(tr("Check for updates every"));
82                cbCheck4Updates->setCursor(QCursor(Qt::PointingHandCursor));
83
84                spinUpdateCheckInterval = new QSpinBox(bgWhite);
85                spinUpdateCheckInterval->setObjectName("spinUpdateCheckInterval");
86#ifndef QT_NO_STATUSTIP
87                spinUpdateCheckInterval->setStatusTip(tr("Minimal interval at which to check for updates"));
88#endif // QT_NO_STATUSTIP
89                spinUpdateCheckInterval->setSuffix(tr(" days", "Don't forget a space at the beginning!"));
90                spinUpdateCheckInterval->setRange(1, 365);
91                spinUpdateCheckInterval->setCursor(QCursor(Qt::PointingHandCursor));
92
93                connect(cbCheck4Updates, SIGNAL(toggled(bool)), spinUpdateCheckInterval, SLOT(setEnabled(bool)));
94
95                box = static_cast<QBoxLayout *>(tabGeneral->layout());
96                hbox1 = new QHBoxLayout();
97                hbox1->setSpacing(0);
98                hbox1->addWidget(cbCheck4Updates);
99                hbox1->addWidget(spinUpdateCheckInterval);
100                hbox1->addStretch();
101                box->insertLayout(box->indexOf(cbUseNativeDialogs) + 1, hbox1);
102        } else
103                cbCheck4Updates = NULL;
104
105#ifdef HANDHELD
106QVBoxLayout *vbox1; // Layout helper
107
108#ifdef Q_OS_WINCE_WM
109        // On screens with small height when SIP is shown and the window is resized
110        // there is not enought space for all elements.
111        // So we show the scrollbars to be able to access them.
112QScrollArea *scrollArea = new QScrollArea(this);
113        scrollArea->setFrameShape(QFrame::NoFrame);
114        scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
115        scrollArea->setWidgetResizable(true);
116        scrollArea->setWidget(bgWhite);
117#else
118        buttons->insertStretch(buttons->indexOf(buttonHelp) + 1);
119#endif // Q_OS_WINCE_WM
120
121        bgWhite->layout()->setMargin(0);
122
123        // Central layout
124        vbox1 = new QVBoxLayout(this);
125        vbox1->setMargin(0);
126        vbox1->setSpacing(0);
127#ifdef Q_OS_WINCE_WM
128        vbox1->addWidget(scrollArea);
129#else
130        vbox1->addWidget(bgWhite);
131#endif // Q_OS_WINCE_WM
132        vbox1->addWidget(bgGrey);
133        setLayout(vbox1);
134#else // HANDHELD
135        // Layout helper elements
136QVBoxLayout *vbox;
137QHBoxLayout *hbox;
138
139#ifdef Q_OS_WIN32
140        if (QtWin::isCompositionEnabled()) {
141                cbUseTranslucency = new QCheckBox(bgWhite);
142                cbUseTranslucency->setObjectName("cbUseTranslucency");
143#ifndef QT_NO_STATUSTIP
144                cbUseTranslucency->setStatusTip(tr("Use translucent effect on the Main Window under Windows Vista and 7"));
145#endif // QT_NO_STATUSTIP
146                cbUseTranslucency->setText(tr("Use translucency effects"));
147                cbUseTranslucency->setCursor(QCursor(Qt::PointingHandCursor));
148        }
149#endif // Q_OS_WIN32
150
151        cbSaveState = new QCheckBox(bgWhite);
152        cbSaveState->setObjectName("cbSaveState");
153#ifndef QT_NO_STATUSTIP
154        cbSaveState->setStatusTip(tr("Restore main window state and position on application restart"));
155#endif // QT_NO_STATUSTIP
156        cbSaveState->setText(tr("Save main window state and position"));
157        cbSaveState->setCursor(QCursor(Qt::PointingHandCursor));
158
159        imgIcon = new QLabel(this);
160        imgIcon->setObjectName("imgIcon");
161        imgIcon->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Expanding);
162        imgIcon->setFrameShape(QFrame::NoFrame);
163        imgIcon->setPixmap(GET_ICON("preferences-system").pixmap(128, 128));
164        imgIcon->setStyleSheet("background-color: #0080C0; padding-top: 11px;");
165        imgIcon->setAlignment(Qt::AlignTop | Qt::AlignHCenter);
166        imgIcon->setMinimumWidth(150);
167
168        labelHint = new QLabel(bgGrey);
169        labelHint->setObjectName("labelHint");
170        labelHint->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Preferred);
171//      labelHint->setMinimumSize(QSize(190,28));
172        labelHint->setMinimumSize(QSize(0,28));
173        labelHint->setMaximumSize(QSize(QWIDGETSIZE_MAX,28));
174        labelHint->setTextFormat(Qt::PlainText);
175//      labelHint->setAlignment(Qt::AlignLeft | Qt::AlignTop);
176        labelHint->setWordWrap(true);
177#ifndef QT_NO_STATUSTIP
178        labelHint->setStatusTip(tr("Hover mouse pointer over dialog elements to get additional help"));
179#endif // QT_NO_STATUSTIP
180
181        lineVertical = new QFrame(this);
182        lineVertical->setObjectName("lineVertical");
183        lineVertical->setFrameShadow(QFrame::Plain);
184        lineVertical->setFrameShape(QFrame::VLine);
185        lineVertical->setLineWidth(2);
186
187        // Top line
188        hbox = new QHBoxLayout();
189        hbox->addWidget(imgIcon);
190        hbox->addWidget(lineVertical);
191        hbox->addWidget(bgWhite);
192
193#ifdef QT_NO_PRINTER
194QBoxLayout *box;
195#endif
196        box = static_cast<QBoxLayout *>(tabGeneral->layout());
197        box->insertWidget(box->indexOf(cbUseNativeDialogs) + 1, cbSaveState);
198#ifdef Q_OS_WIN32
199        if (QtWin::isCompositionEnabled())
200                box->insertWidget(box->indexOf(cbUseNativeDialogs) + 1, cbUseTranslucency);
201#endif // Q_OS_WIN32
202
203        // Inserting label for hints to the bottom part (with grey bg)
204        buttons->insertWidget(buttons->indexOf(buttonHelp) + 1, labelHint, 1);
205
206        // Central layout
207        vbox = new QVBoxLayout(this);
208        vbox->setMargin(0);
209        vbox->setSpacing(0);
210        vbox->addLayout(hbox);
211        vbox->addWidget(bgGrey);
212        setLayout(vbox);
213#endif // HANDHELD
214
215#ifdef Q_OS_WINCE_WM
216        // We need to react to SIP show/hide and resize the window appropriately
217        connect(QApplication::desktop(), SIGNAL(workAreaResized(int)), SLOT(desktopResized(int)));
218#endif // Q_OS_WINCE_WM
219        connect(spinRandMin, SIGNAL(valueChanged(int)), SLOT(spinRandMinValueChanged(int)));
220        connect(buttonFont, SIGNAL(clicked()), SLOT(buttonFontClicked()));
221        connect(buttonColor, SIGNAL(clicked()), SLOT(buttonColorClicked()));
222        setWindowFlags(windowFlags() ^ Qt::WindowContextHelpButtonHint);
223#ifndef HANDHELD
224        // Setting initial text of dialog hint label to own status tip text.
225        labelHint->setText(labelHint->statusTip());
226#endif // HANDHELD
227
228        settings = new QSettings(QSettings::IniFormat, QSettings::UserScope, "TSPSG", "tspsg", this);
229        settings->remove("SettingsReset");
230
231        cbAutosize->setChecked(settings->value("Autosize", DEF_AUTOSIZE).toBool());
232        cbSaveLastUsed->setChecked(settings->value("SaveLastUsed", DEF_SAVE_LAST_USED).toBool());
233        cbUseNativeDialogs->setChecked(settings->value("UseNativeDialogs", DEF_USE_NATIVE_DIALOGS).toBool());
234#ifdef Q_OS_WIN32
235        if (QtWin::isCompositionEnabled())
236                cbUseTranslucency->setChecked(settings->value("UseTranslucency", DEF_USE_TRANSLUCENCY).toBool());
237#endif // Q_OS_WIN32
238#ifndef HANDHELD
239        cbSaveState->setChecked(settings->value("SavePos", DEF_SAVEPOS).toBool());
240#endif // HANDHELD
241        if (cbCheck4Updates != NULL) {
242                settings->beginGroup("Check4Updates");
243                cbCheck4Updates->setChecked(settings->value("Enabled", DEF_CHECK_FOR_UPDATES).toBool());
244                spinUpdateCheckInterval->setValue(settings->value("Interval", DEF_UPDATE_CHECK_INTERVAL).toInt());
245                settings->endGroup();
246                spinUpdateCheckInterval->setEnabled(cbCheck4Updates->isChecked());
247        }
248
249        settings->beginGroup("Task");
250        cbSymmetricMode->setChecked(settings->value("SymmetricMode", DEF_SYMMETRIC_MODE).toBool());
251        spinFractionalAccuracy->setValue(settings->value("FractionalAccuracy", DEF_FRACTIONAL_ACCURACY).toInt());
252        spinRandMin->setMaximum(MAX_RAND_VALUE);
253        spinRandMin->setValue(settings->value("RandMin",DEF_RAND_MIN).toInt());
254        spinRandMax->setMaximum(MAX_RAND_VALUE);
255        spinRandMax->setValue(settings->value("RandMax",DEF_RAND_MAX).toInt());
256        cbFractionalRandom->setChecked(settings->value("FractionalRandom", DEF_FRACTIONAL_RANDOM).toBool());
257        settings->endGroup();
258
259        settings->beginGroup("Output");
260        cbShowGraph->setChecked(settings->value("ShowGraph", DEF_SHOW_GRAPH).toBool());
261
262#ifndef QT_NO_PRINTER
263        cbHQGraph->setEnabled(cbShowGraph->isChecked());
264        cbHQGraph->setChecked(settings->value("HQGraph", DEF_HQ_GRAPH && cbShowGraph->isChecked()).toBool());
265#endif
266
267#if !defined(NOSVG) && (QT_VERSION >= 0x040500)
268        comboGraphImageFormat->addItem("svg");
269#endif // NOSVG && QT_VERSION >= 0x040500
270        // We create whitelist of formats, supported by the most popular web browsers according to
271        //  http://en.wikipedia.org/wiki/Comparison_of_web_browsers#Image_format_support
272        //  + TIFF format (there are plugins to support it).
273QStringList whitelist;
274        whitelist << "bmp" << "jpeg" << "png" << "tiff" << "xbm";
275        foreach (QByteArray format, QImageWriter::supportedImageFormats()) {
276                if (whitelist.contains(format))
277                        comboGraphImageFormat->addItem(format);
278        }
279        comboGraphImageFormat->model()->sort(0);
280        comboGraphImageFormat->setCurrentIndex(comboGraphImageFormat->findText(settings->value("GraphImageFormat", DEF_GRAPH_IMAGE_FORMAT).toString(), Qt::MatchFixedString));
281        if (comboGraphImageFormat->currentIndex() < 0)
282                comboGraphImageFormat->setCurrentIndex(comboGraphImageFormat->findText(DEF_GRAPH_IMAGE_FORMAT, Qt::MatchFixedString));
283        labelGraphImageFormat->setEnabled(cbShowGraph->isChecked());
284        comboGraphImageFormat->setEnabled(cbShowGraph->isChecked());
285
286        cbShowMatrix->setChecked(settings->value("ShowMatrix", DEF_SHOW_MATRIX).toBool());
287        cbCitiesLimit->setEnabled(cbShowMatrix->isChecked());
288        cbCitiesLimit->setChecked(settings->value("UseShowMatrixLimit", DEF_USE_SHOW_MATRIX_LIMIT && cbShowMatrix->isChecked()).toBool());
289        spinCitiesLimit->setEnabled(cbShowMatrix->isChecked() && cbCitiesLimit->isChecked());
290        spinCitiesLimit->setValue(settings->value("ShowMatrixLimit", DEF_SHOW_MATRIX_LIMIT).toInt());
291        spinCitiesLimit->setMaximum(MAX_NUM_CITIES);
292        cbScrollToEnd->setChecked(settings->value("ScrollToEnd", DEF_SCROLL_TO_END).toBool());
293
294        font = qvariant_cast<QFont>(settings->value("Font", QFont(DEF_FONT_FACE, DEF_FONT_SIZE)));
295        color = qvariant_cast<QColor>(settings->value("Colors/Text", DEF_TEXT_COLOR));
296        settings->endGroup();
297
298#ifdef HANDHELD
299        setWindowState(Qt::WindowMaximized);
300#else
301        adjustSize();
302#endif // Q_OS_WINCE_WM
303}
304
305/*!
306 * \brief Indicates whether or not the font color has been changed.
307 * \return \c true if font color changed, otherwise \c false.
308 */
309bool SettingsDialog::colorChanged() const
310{
311        return _newColor;
312}
313
314/*!
315 * \brief Indicates whether or not the font properties have been changed.
316 * \return \c true if font properties changed, otherwise \c false.
317 */
318bool SettingsDialog::fontChanged() const
319{
320        return _newFont;
321}
322
323/*!
324 * \brief Indicates whether and how the translucency setting was changed
325 * \retval -1 the translucency was \em disabled.
326 * \retval  0 the translucency was <em>not changed</em>.
327 * \retval  1 the translucency was \em enabled.
328 */
329qint8 SettingsDialog::translucencyChanged() const
330{
331        return _translucency;
332}
333
334/* Privates **********************************************************/
335
336void SettingsDialog::accept()
337{
338        if (QApplication::keyboardModifiers() & Qt::ShiftModifier) {
339                if (QMessageBox::question(this, tr("Settings Reset"), tr("Do you really want to <b>reset all application settings to their defaults</b>?"), QMessageBox::RestoreDefaults | QMessageBox::Cancel) == QMessageBox::RestoreDefaults) {
340                        _newFont = (font != QFont(DEF_FONT_FACE, DEF_FONT_SIZE));
341                        _newColor = (color != DEF_TEXT_COLOR);
342                        settings->remove("");
343                        settings->setValue("SettingsReset", true);
344                        QDialog::accept();
345                        QMessageBox::information(this->parentWidget(), tr("Settings Reset"), tr("All settings where successfully reset to their defaults.\nIt is recommended to restart the application now."));
346                        return;
347                } else
348                        return;
349        }
350        settings->setValue("Autosize", cbAutosize->isChecked());
351        settings->setValue("SaveLastUsed", cbSaveLastUsed->isChecked());
352        settings->setValue("UseNativeDialogs", cbUseNativeDialogs->isChecked());
353#ifdef Q_OS_WIN32
354        if (QtWin::isCompositionEnabled()) {
355bool old = settings->value("UseTranslucency", DEF_USE_TRANSLUCENCY).toBool();
356                if ((!old && cbUseTranslucency->isChecked()) || (old && !cbUseTranslucency->isChecked())) {
357                        _translucency = old ? -1 : 1;
358                } else
359                        _translucency = 0;
360                settings->setValue("UseTranslucency", cbUseTranslucency->isChecked());
361        }
362#endif // Q_OS_WIN32
363#ifndef HANDHELD
364        settings->setValue("SavePos", cbSaveState->isChecked());
365#endif // HANDHELD
366        if (cbCheck4Updates != NULL) {
367                settings->beginGroup("Check4Updates");
368                settings->setValue("Enabled", cbCheck4Updates->isChecked());
369                if (cbCheck4Updates->isChecked())
370                        settings->setValue("Interval", spinUpdateCheckInterval->value());
371                settings->endGroup();
372        }
373
374        settings->beginGroup("Task");
375        settings->setValue("SymmetricMode", cbSymmetricMode->isChecked());
376        settings->setValue("FractionalAccuracy", spinFractionalAccuracy->value());
377        settings->setValue("RandMin", spinRandMin->value());
378        settings->setValue("RandMax", spinRandMax->value());
379        settings->setValue("FractionalRandom", cbFractionalRandom->isChecked());
380        settings->endGroup();
381
382        settings->beginGroup("Output");
383        settings->setValue("ShowGraph", cbShowGraph->isChecked());
384#ifndef QT_NO_PRINTER
385        settings->setValue("HQGraph", cbShowGraph->isChecked() && cbHQGraph->isChecked());
386#endif
387        if (cbShowGraph->isChecked()) {
388                if (comboGraphImageFormat->currentIndex() >= 0)
389                        settings->setValue("GraphImageFormat", comboGraphImageFormat->currentText());
390                else
391                        settings->setValue("GraphImageFormat", DEF_GRAPH_IMAGE_FORMAT);
392        }
393        settings->setValue("ShowMatrix", cbShowMatrix->isChecked());
394        settings->setValue("UseShowMatrixLimit", cbShowMatrix->isChecked() && cbCitiesLimit->isChecked());
395        if (cbCitiesLimit->isChecked())
396                settings->setValue("ShowMatrixLimit", spinCitiesLimit->value());
397        settings->setValue("ScrollToEnd", cbScrollToEnd->isChecked());
398        if (_newFont)
399                settings->setValue("Font", font);
400        if (_newColor)
401                settings->setValue("Colors/Text", color);
402        settings->endGroup();
403        QDialog::accept();
404}
405
406void SettingsDialog::buttonColorClicked()
407{
408QColor color = QColorDialog::getColor(this->color,this);
409        if (color.isValid() && (this->color != color)) {
410                this->color = color;
411                _newColor = true;
412        }
413}
414
415void SettingsDialog::buttonFontClicked()
416{
417bool ok;
418QFont font = QFontDialog::getFont(&ok,this->font,this);
419        if (ok && (this->font != font)) {
420                this->font = font;
421                _newFont = true;
422        }
423}
424
425#ifdef Q_OS_WINCE_WM
426void SettingsDialog::desktopResized(int screen)
427{
428        if (screen != 0)
429                return;
430
431QRect availableGeometry = QApplication::desktop()->availableGeometry(0);
432        if (currentGeometry != availableGeometry) {
433                QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
434                /*!
435                 * \hack HACK: This hack checks whether \link QDesktopWidget::availableGeometry() availableGeometry()\endlink's \c top + \c hegiht = \link QDesktopWidget::screenGeometry() screenGeometry()\endlink's \c height.
436                 *  If \c true, the window gets maximized. If we used \c setGeometry() in this case, the bottom of the
437                 *  window would end up being behind the soft buttons. Is this a bug in Qt or Windows Mobile?
438                 */
439                if ((availableGeometry.top() + availableGeometry.height()) == QApplication::desktop()->screenGeometry().height()) {
440                        setWindowState(windowState() | Qt::WindowMaximized);
441                } else {
442                        if (windowState() & Qt::WindowMaximized)
443                                setWindowState(windowState() ^ Qt::WindowMaximized);
444                        setGeometry(availableGeometry);
445                }
446                currentGeometry = availableGeometry;
447                QApplication::restoreOverrideCursor();
448        }
449}
450
451void SettingsDialog::showEvent(QShowEvent *ev)
452{
453        desktopResized(0);
454
455        QWidget::showEvent(ev);
456}
457#endif // Q_OS_WINCE_WM
458
459void SettingsDialog::spinRandMinValueChanged(int val) {
460        spinRandMax->setMinimum(val);
461}
462
463#ifndef HANDHELD
464bool SettingsDialog::event(QEvent *ev)
465{
466        // Checking for StatusTip event and if tip text is not empty string
467        // setting it as text of the dialog hint label. Otherwise, setting
468        // dialog hint label text to own status tip text.
469        if (ev->type() == QEvent::StatusTip) {
470QString tip = static_cast<QStatusTipEvent *>(ev)->tip();
471                if (tip.length() != 0)
472                        labelHint->setText(tip);
473                else
474                        labelHint->setText(labelHint->statusTip());
475                return true;
476        } else
477                return QDialog::event(ev);
478}
479#endif // HANDHELD
Note: See TracBrowser for help on using the repository browser.