source: tspsg/src/settingsdialog.cpp @ 278bc7818f

0.1.3.145-beta1-symbian0.1.4.170-beta2-bb10appveyorimgbotreadme
Last change on this file since 278bc7818f was 278bc7818f, checked in by Oleksii Serdiuk, 14 years ago

+ Added Symmetric mode: in this mode the cost of travel from city 1 to city 2 and vice versa is the same.
+ Added the ability to reset all settings to their defaults: hold Shift while clicking Save button in Settings Dialog.

  • Better SIP show/hide handling under wince: no need to resize the Main Window, when it isn't active.
  • Property mode set to 100644
File size: 13.4 KB
RevLine 
[5515c2c2a7]1/*
[430bd7f7e9]2 *  TSPSG: TSP Solver and Generator
[1757eb594b]3 *  Copyright (C) 2007-2010 Lёppa <contacts[at]oleksii[dot]name>
[5515c2c2a7]4 *
[bb994a7ff8]5 *  $Id$
6 *  $URL$
[f99964aa0b]7 *
[bb994a7ff8]8 *  This file is part of TSPSG.
[5515c2c2a7]9 *
[bb994a7ff8]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.
[5515c2c2a7]14 *
[bb994a7ff8]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.
[5515c2c2a7]19 *
[bb994a7ff8]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/>.
[5515c2c2a7]22 */
23
[7836f136eb]24#include "settingsdialog.h"
[5515c2c2a7]25
[caef58b531]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 */
[5515c2c2a7]32SettingsDialog::SettingsDialog(QWidget *parent)
[11086c2def]33        : QDialog(parent), _newFont(false), _newColor(false), _translucency(0)
[5515c2c2a7]34{
35        setupUi(this);
[1757eb594b]36
[1fbf016a09]37        buttonBox->button(QDialogButtonBox::Save)->setIcon(QIcon(":/images/icons/button_ok.png"));
[1757eb594b]38        buttonBox->button(QDialogButtonBox::Save)->setStatusTip(tr("Save new preferences"));
[1fbf016a09]39        buttonBox->button(QDialogButtonBox::Save)->setCursor(QCursor(Qt::PointingHandCursor));
40        buttonBox->button(QDialogButtonBox::Cancel)->setIcon(QIcon(":/images/icons/button_cancel.png"));
[1757eb594b]41        buttonBox->button(QDialogButtonBox::Cancel)->setStatusTip(tr("Close without saving preferences"));
[1fbf016a09]42        buttonBox->button(QDialogButtonBox::Cancel)->setCursor(QCursor(Qt::PointingHandCursor));
43
[6beb157497]44#if defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN)
[b2bf8e3b6b]45QVBoxLayout *vbox1; // Layout helper
46
47#ifdef Q_OS_WINCE
48        // On screens with small height when SIP is shown and the window is resized
49        // there is not enought space for all elements.
50        //  So we show scrollbars to be able to access them.
51QScrollArea *scrollArea = new QScrollArea(this);
52        scrollArea->setFrameShape(QFrame::NoFrame);
53        scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
54        scrollArea->setWidgetResizable(true);
55        scrollArea->setWidget(bgWhite);
[278bc7818f]56#else
57        buttons->insertStretch(buttons->indexOf(buttonHelp) + 1);
[b2bf8e3b6b]58#endif // Q_OS_WINCE
[281303f1f7]59
[a5be8eb2c8]60        bgWhite->layout()->setMargin(0);
[281303f1f7]61
62        // Central layout
63        vbox1 = new QVBoxLayout(this);
64        vbox1->setMargin(0);
65        vbox1->setSpacing(0);
[b2bf8e3b6b]66#ifdef Q_OS_WINCE
67        vbox1->addWidget(scrollArea);
68#else
[281303f1f7]69        vbox1->addWidget(bgWhite);
[b2bf8e3b6b]70#endif // Q_OS_WINCE
[281303f1f7]71        vbox1->addWidget(bgGrey);
[b2bf8e3b6b]72        setLayout(vbox1);
[281303f1f7]73#else
74        // Layout helper elements
[a5be8eb2c8]75QVBoxLayout *vbox1, *vbox2;
[b2bf8e3b6b]76QHBoxLayout *hbox1;
[281303f1f7]77
[11086c2def]78        if (QtWin::isCompositionEnabled()) {
79                cbUseTranslucency = new QCheckBox(bgWhite);
80                cbUseTranslucency->setObjectName("cbUseTranslucency");
81#ifndef QT_NO_STATUSTIP
82                cbUseTranslucency->setStatusTip(tr("Use translucent effect on the Main Window under Windows Vista and 7"));
83#endif // QT_NO_STATUSTIP
84                cbUseTranslucency->setText(tr("Use translucency effects"));
85                cbUseTranslucency->setCursor(QCursor(Qt::PointingHandCursor));
86        }
87
[281303f1f7]88        cbSaveState = new QCheckBox(bgWhite);
89        cbSaveState->setObjectName("cbSaveState");
90#ifndef QT_NO_STATUSTIP
[1757eb594b]91        cbSaveState->setStatusTip(tr("Restore main window state and position on application restart"));
[281303f1f7]92#endif // QT_NO_STATUSTIP
[1757eb594b]93        cbSaveState->setText(tr("Save main window state and position"));
[281303f1f7]94        cbSaveState->setCursor(QCursor(Qt::PointingHandCursor));
95
96        imgIcon = new QLabel(this);
97        imgIcon->setObjectName("imgIcon");
[5a81a64d74]98        imgIcon->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Expanding);
[b2bf8e3b6b]99        imgIcon->setFrameShape(QFrame::NoFrame);
[1fbf016a09]100        imgIcon->setPixmap(QPixmap(":/images/icons/preferences_system.png"));
[946f442ab0]101        imgIcon->setStyleSheet("background-color: #0080C0; padding-top: 11px;");
102        imgIcon->setAlignment(Qt::AlignTop | Qt::AlignHCenter);
[1fbf016a09]103        imgIcon->setMinimumWidth(150);
[281303f1f7]104
105        labelHint = new QLabel(bgGrey);
106        labelHint->setObjectName("labelHint");
107        labelHint->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Preferred);
[5a81a64d74]108//      labelHint->setMinimumSize(QSize(190,28));
109        labelHint->setMinimumSize(QSize(0,28));
110        labelHint->setMaximumSize(QSize(QWIDGETSIZE_MAX,28));
[281303f1f7]111        labelHint->setTextFormat(Qt::PlainText);
112//      labelHint->setAlignment(Qt::AlignLeft | Qt::AlignTop);
113        labelHint->setWordWrap(true);
114#ifndef QT_NO_STATUSTIP
[1757eb594b]115        labelHint->setStatusTip(tr("Hover mouse pointer over dialog elements to get additional help"));
[281303f1f7]116#endif // QT_NO_STATUSTIP
117
118        lineVertical = new QFrame(this);
119        lineVertical->setObjectName("lineVertical");
120        lineVertical->setFrameShadow(QFrame::Plain);
121        lineVertical->setFrameShape(QFrame::VLine);
122        lineVertical->setLineWidth(2);
123
124        // Top line
125        hbox1 = new QHBoxLayout();
126        hbox1->addWidget(imgIcon);
127        hbox1->addWidget(lineVertical);
128        hbox1->addWidget(bgWhite);
129
[a5be8eb2c8]130        vbox1 = static_cast<QVBoxLayout *>(tabGeneral->layout());
[1757eb594b]131        vbox1->insertWidget(vbox1->indexOf(cbUseNativeDialogs) + 1, cbSaveState);
[11086c2def]132#ifdef Q_OS_WIN32
133        if (QtWin::isCompositionEnabled())
134                vbox1->insertWidget(vbox1->indexOf(cbUseNativeDialogs) + 1, cbUseTranslucency);
135#endif // Q_OS_WIN32
[281303f1f7]136
[b2bf8e3b6b]137        // Inserting label for hints to the bottom part (with grey bg)
138        buttons->insertWidget(buttons->indexOf(buttonHelp) + 1, labelHint, 1);
[281303f1f7]139
140        // Central layout
[a5be8eb2c8]141        vbox2 = new QVBoxLayout(this);
142        vbox2->setMargin(0);
143        vbox2->setSpacing(0);
144        vbox2->addLayout(hbox1);
145        vbox2->addWidget(bgGrey);
[b2bf8e3b6b]146        setLayout(vbox2);
[281303f1f7]147#endif // Q_OS_WINCE
[a5be8eb2c8]148
[b2bf8e3b6b]149#ifdef Q_OS_WINCE
150        // We need to react to SIP show/hide and resize the window appropriately
151        connect(QApplication::desktop(), SIGNAL(workAreaResized(int)), SLOT(desktopResized(int)));
152#endif // Q_OS_WINCE
[5515c2c2a7]153        connect(spinRandMin,SIGNAL(valueChanged(int)),this,SLOT(spinRandMinValueChanged(int)));
[5354a01311]154        connect(buttonFont,SIGNAL(clicked()),this,SLOT(buttonFontClicked()));
[aecdf994f9]155        connect(buttonColor,SIGNAL(clicked()),this,SLOT(buttonColorClicked()));
[5515c2c2a7]156        setWindowFlags(windowFlags() ^ Qt::WindowContextHelpButtonHint);
[6beb157497]157#if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN)
[1fbf016a09]158        // Setting initial text of dialog hint label to own status tip text.
[7836f136eb]159        labelHint->setText(labelHint->statusTip());
[5354a01311]160#endif // Q_OS_WINCE
[4ccf855df8]161
162        settings = new QSettings(QSettings::IniFormat, QSettings::UserScope, "TSPSG", "tspsg", this);
[278bc7818f]163        settings->remove("SettingsReset");
[4ccf855df8]164
[946f442ab0]165        cbAutosize->setChecked(settings->value("Autosize", DEF_AUTOSIZE).toBool());
166        cbUseNativeDialogs->setChecked(settings->value("UseNativeDialogs", DEF_USE_NATIVE_DIALOGS).toBool());
[11086c2def]167#ifdef Q_OS_WIN32
168        if (QtWin::isCompositionEnabled())
169                cbUseTranslucency->setChecked(settings->value("UseTranslucency", DEF_USE_TRANSLUCENCY).toBool());
170#endif // Q_OS_WIN32
[6beb157497]171#if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN)
[946f442ab0]172        cbSaveState->setChecked(settings->value("SavePos", DEF_SAVEPOS).toBool());
[aecdf994f9]173#endif // Q_OS_WINCE
[1fbf016a09]174
[1757eb594b]175        settings->beginGroup("Task");
[278bc7818f]176        cbSymmetricMode->setChecked(settings->value("SymmetricMode", DEF_SYMMETRIC_MODE).toBool());
[1757eb594b]177        spinFractionalAccuracy->setValue(settings->value("FractionalAccuracy", DEF_FRACTIONAL_ACCURACY).toInt());
178        spinRandMin->setMaximum(MAX_RAND_VALUE);
179        spinRandMin->setValue(settings->value("RandMin",DEF_RAND_MIN).toInt());
180        spinRandMax->setMaximum(MAX_RAND_VALUE);
181        spinRandMax->setValue(settings->value("RandMax",DEF_RAND_MAX).toInt());
182        cbFractionalRandom->setChecked(settings->value("FractionalRandom", DEF_FRACTIONAL_RANDOM).toBool());
183        settings->endGroup();
184
[430bd7f7e9]185        settings->beginGroup("Output");
[1fbf016a09]186        cbShowMatrix->setChecked(settings->value("ShowMatrix", DEF_SHOW_MATRIX).toBool());
187        cbCitiesLimit->setEnabled(cbShowMatrix->isChecked());
188        cbCitiesLimit->setChecked(settings->value("UseShowMatrixLimit", DEF_USE_SHOW_MATRIX_LIMIT && cbShowMatrix->isChecked()).toBool());
189        spinCitiesLimit->setEnabled(cbShowMatrix->isChecked());
[946f442ab0]190        spinCitiesLimit->setValue(settings->value("ShowMatrixLimit", DEF_SHOW_MATRIX_LIMIT).toInt());
[1fbf016a09]191        spinCitiesLimit->setMaximum(MAX_NUM_CITIES);
[f1fb54b9f7]192        cbScrollToEnd->setChecked(settings->value("ScrollToEnd", DEF_SCROLL_TO_END).toBool());
[1fbf016a09]193
[aecdf994f9]194        font = settings->value("Font",QFont(DEF_FONT_FAMILY,DEF_FONT_SIZE)).value<QFont>();
195        color = settings->value("Color",DEF_FONT_COLOR).value<QColor>();
196        settings->endGroup();
[1fbf016a09]197
[278bc7818f]198#ifndef Q_OS_WINCE
[1fbf016a09]199        adjustSize();
[278bc7818f]200#endif // Q_OS_WINCE
[7836f136eb]201}
202
[e0fcac5f2c]203/*!
204 * \brief Indicates whether or not the font color has been changed.
205 * \return \c true if font color changed, otherwise \c false.
206 */
207bool SettingsDialog::colorChanged() const
208{
[11086c2def]209        return _newColor;
[e0fcac5f2c]210}
211
212/*!
213 * \brief Indicates whether or not the font properties have been changed.
214 * \return \c true if font properties changed, otherwise \c false.
215 */
216bool SettingsDialog::fontChanged() const
217{
[11086c2def]218        return _newFont;
219}
220
221/*!
[b2bf8e3b6b]222 * \brief Indicates whether and how the translucency setting was changed
223 * \retval -1 the translucency was \em disabled.
224 * \retval  0 the translucency was <em>not changed</em>.
225 * \retval  1 the translucency was \em enabled.
[11086c2def]226 */
227qint8 SettingsDialog::translucencyChanged() const
228{
229        return _translucency;
[e0fcac5f2c]230}
231
232/* Privates **********************************************************/
233
[430bd7f7e9]234void SettingsDialog::accept()
[7836f136eb]235{
[278bc7818f]236        if (QApplication::keyboardModifiers() & Qt::ShiftModifier) {
237                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) {
238                        _newFont = (font != QFont(DEF_FONT_FAMILY, DEF_FONT_SIZE));
239                        _newColor = (color != DEF_FONT_COLOR);
240                        settings->remove("");
241                        settings->setValue("SettingsReset", true);
242                        QDialog::accept();
243                        QMessageBox::information(this, tr("Settings Reset"), tr("All settings where successfully reset to their defaults.\nIt is recommended to restart the application now."));
244                        return;
245                } else
246                        return;
247        }
[6beb157497]248#if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN)
[1fbf016a09]249        settings->setValue("SavePos", cbSaveState->isChecked());
[5354a01311]250#endif // Q_OS_WINCE
[11086c2def]251#ifdef Q_OS_WIN32
252        if (QtWin::isCompositionEnabled()) {
253bool old = settings->value("UseTranslucency", DEF_USE_TRANSLUCENCY).toBool();
254                if ((!old && cbUseTranslucency->isChecked()) || (old && !cbUseTranslucency->isChecked())) {
255                        _translucency = old ? -1 : 1;
256                } else
257                        _translucency = 0;
258                settings->setValue("UseTranslucency", cbUseTranslucency->isChecked());
259        }
260#endif // Q_OS_WIN32
[946f442ab0]261        settings->setValue("UseNativeDialogs", cbUseNativeDialogs->isChecked());
[1757eb594b]262
263        settings->beginGroup("Task");
[278bc7818f]264        settings->setValue("SymmetricMode", cbSymmetricMode->isChecked());
[1757eb594b]265        settings->setValue("FractionalAccuracy", spinFractionalAccuracy->value());
266        settings->setValue("RandMin", spinRandMin->value());
267        settings->setValue("RandMax", spinRandMax->value());
[f1fb54b9f7]268        settings->setValue("FractionalRandom", cbFractionalRandom->isChecked());
[1757eb594b]269        settings->endGroup();
[1fbf016a09]270
[430bd7f7e9]271        settings->beginGroup("Output");
[1fbf016a09]272        settings->setValue("ShowMatrix", cbShowMatrix->isChecked());
273        settings->setValue("UseShowMatrixLimit", cbShowMatrix->isChecked() && cbCitiesLimit->isChecked());
274        if (cbCitiesLimit->isChecked())
[946f442ab0]275                settings->setValue("ShowMatrixLimit", spinCitiesLimit->value());
[f1fb54b9f7]276        settings->setValue("ScrollToEnd", cbScrollToEnd->isChecked());
[11086c2def]277        if (_newFont)
[1fbf016a09]278                settings->setValue("Font", font);
[11086c2def]279        if (_newColor)
[1fbf016a09]280                settings->setValue("Color", color);
[430bd7f7e9]281        settings->endGroup();
282        QDialog::accept();
283}
[5515c2c2a7]284
[aecdf994f9]285void SettingsDialog::buttonColorClicked()
286{
[e7f7d3854d]287QColor color = QColorDialog::getColor(this->color,this);
[430bd7f7e9]288        if (color.isValid() && (this->color != color)) {
[e7f7d3854d]289                this->color = color;
[11086c2def]290                _newColor = true;
[430bd7f7e9]291        }
[aecdf994f9]292}
293
[e0fcac5f2c]294void SettingsDialog::buttonFontClicked()
[430bd7f7e9]295{
[e0fcac5f2c]296bool ok;
297QFont font = QFontDialog::getFont(&ok,this->font,this);
298        if (ok && (this->font != font)) {
299                this->font = font;
[11086c2def]300                _newFont = true;
[e0fcac5f2c]301        }
[430bd7f7e9]302}
303
[b2bf8e3b6b]304#ifdef Q_OS_WINCE
305void SettingsDialog::desktopResized(int screen)
306{
307        if (screen != 0)
308                return;
309
310QRect availableGeometry = QApplication::desktop()->availableGeometry(0);
311        if (currentGeometry != availableGeometry) {
[278bc7818f]312                QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
[b2bf8e3b6b]313                /*!
314                 * \hack HACK: This hack checks whether \link QDesktopWidget::availableGeometry() availableGeometry()\endlink's \c top + \c hegiht = \link QDesktopWidget::screenGeometry() screenGeometry()\endlink's \c height.
315                 *  If \c true, the window gets maximized. If we used \c setGeometry() in this case, the bottom of the
316                 *  window would end up being behind the soft buttons. Is this a bug in Qt or Windows Mobile?
317                 */
318                if ((availableGeometry.top() + availableGeometry.height()) == QApplication::desktop()->screenGeometry().height()) {
319                        setWindowState(windowState() | Qt::WindowMaximized);
320                } else {
321                        if (windowState() & Qt::WindowMaximized)
322                                setWindowState(windowState() ^ Qt::WindowMaximized);
323                        setGeometry(availableGeometry);
324                }
[278bc7818f]325                currentGeometry = availableGeometry;
326                QApplication::restoreOverrideCursor();
[b2bf8e3b6b]327        }
[278bc7818f]328}
329
330void SettingsDialog::showEvent(QShowEvent *ev)
331{
332        desktopResized(0);
333
334        QWidget::showEvent(ev);
[b2bf8e3b6b]335}
336#endif // Q_OS_WINCE
337
[caef58b531]338void SettingsDialog::spinRandMinValueChanged(int val) {
339        spinRandMax->setMinimum(val);
340}
341
[6beb157497]342#if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN)
[430bd7f7e9]343bool SettingsDialog::event(QEvent *ev)
344{
345        // Checking for StatusTip event and if tip text is not empty string
346        // setting it as text of the dialog hint label. Otherwise, setting
347        // dialog hint label text to own status tip text.
348        if (ev->type() == QEvent::StatusTip) {
349QString tip = static_cast<QStatusTipEvent *>(ev)->tip();
350                if (tip.length() != 0)
351                        labelHint->setText(tip);
352                else
353                        labelHint->setText(labelHint->statusTip());
354                return true;
355        } else
356                return QDialog::event(ev);
[5354a01311]357}
[430bd7f7e9]358#endif // Q_OS_WINCE
Note: See TracBrowser for help on using the repository browser.