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

Last change on this file since 95 was 95, checked in by laleppa, 15 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 svn:keywords set to Id URL
File size: 13.4 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 95 2010-02-27 13:13:50Z 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        buttonBox->button(QDialogButtonBox::Save)->setIcon(QIcon(":/images/icons/button_ok.png"));
38        buttonBox->button(QDialogButtonBox::Save)->setStatusTip(tr("Save new preferences"));
39        buttonBox->button(QDialogButtonBox::Save)->setCursor(QCursor(Qt::PointingHandCursor));
40        buttonBox->button(QDialogButtonBox::Cancel)->setIcon(QIcon(":/images/icons/button_cancel.png"));
41        buttonBox->button(QDialogButtonBox::Cancel)->setStatusTip(tr("Close without saving preferences"));
42        buttonBox->button(QDialogButtonBox::Cancel)->setCursor(QCursor(Qt::PointingHandCursor));
43
44#if defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN)
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);
56#else
57        buttons->insertStretch(buttons->indexOf(buttonHelp) + 1);
58#endif // Q_OS_WINCE
59
60        bgWhite->layout()->setMargin(0);
61
62        // Central layout
63        vbox1 = new QVBoxLayout(this);
64        vbox1->setMargin(0);
65        vbox1->setSpacing(0);
66#ifdef Q_OS_WINCE
67        vbox1->addWidget(scrollArea);
68#else
69        vbox1->addWidget(bgWhite);
70#endif // Q_OS_WINCE
71        vbox1->addWidget(bgGrey);
72        setLayout(vbox1);
73#else
74        // Layout helper elements
75QVBoxLayout *vbox1, *vbox2;
76QHBoxLayout *hbox1;
77
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
88        cbSaveState = new QCheckBox(bgWhite);
89        cbSaveState->setObjectName("cbSaveState");
90#ifndef QT_NO_STATUSTIP
91        cbSaveState->setStatusTip(tr("Restore main window state and position on application restart"));
92#endif // QT_NO_STATUSTIP
93        cbSaveState->setText(tr("Save main window state and position"));
94        cbSaveState->setCursor(QCursor(Qt::PointingHandCursor));
95
96        imgIcon = new QLabel(this);
97        imgIcon->setObjectName("imgIcon");
98        imgIcon->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Expanding);
99        imgIcon->setFrameShape(QFrame::NoFrame);
100        imgIcon->setPixmap(QPixmap(":/images/icons/preferences_system.png"));
101        imgIcon->setStyleSheet("background-color: #0080C0; padding-top: 11px;");
102        imgIcon->setAlignment(Qt::AlignTop | Qt::AlignHCenter);
103        imgIcon->setMinimumWidth(150);
104
105        labelHint = new QLabel(bgGrey);
106        labelHint->setObjectName("labelHint");
107        labelHint->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Preferred);
108//      labelHint->setMinimumSize(QSize(190,28));
109        labelHint->setMinimumSize(QSize(0,28));
110        labelHint->setMaximumSize(QSize(QWIDGETSIZE_MAX,28));
111        labelHint->setTextFormat(Qt::PlainText);
112//      labelHint->setAlignment(Qt::AlignLeft | Qt::AlignTop);
113        labelHint->setWordWrap(true);
114#ifndef QT_NO_STATUSTIP
115        labelHint->setStatusTip(tr("Hover mouse pointer over dialog elements to get additional help"));
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
130        vbox1 = static_cast<QVBoxLayout *>(tabGeneral->layout());
131        vbox1->insertWidget(vbox1->indexOf(cbUseNativeDialogs) + 1, cbSaveState);
132#ifdef Q_OS_WIN32
133        if (QtWin::isCompositionEnabled())
134                vbox1->insertWidget(vbox1->indexOf(cbUseNativeDialogs) + 1, cbUseTranslucency);
135#endif // Q_OS_WIN32
136
137        // Inserting label for hints to the bottom part (with grey bg)
138        buttons->insertWidget(buttons->indexOf(buttonHelp) + 1, labelHint, 1);
139
140        // Central layout
141        vbox2 = new QVBoxLayout(this);
142        vbox2->setMargin(0);
143        vbox2->setSpacing(0);
144        vbox2->addLayout(hbox1);
145        vbox2->addWidget(bgGrey);
146        setLayout(vbox2);
147#endif // Q_OS_WINCE
148
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
153        connect(spinRandMin,SIGNAL(valueChanged(int)),this,SLOT(spinRandMinValueChanged(int)));
154        connect(buttonFont,SIGNAL(clicked()),this,SLOT(buttonFontClicked()));
155        connect(buttonColor,SIGNAL(clicked()),this,SLOT(buttonColorClicked()));
156        setWindowFlags(windowFlags() ^ Qt::WindowContextHelpButtonHint);
157#if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN)
158        // Setting initial text of dialog hint label to own status tip text.
159        labelHint->setText(labelHint->statusTip());
160#endif // Q_OS_WINCE
161
162        settings = new QSettings(QSettings::IniFormat, QSettings::UserScope, "TSPSG", "tspsg", this);
163        settings->remove("SettingsReset");
164
165        cbAutosize->setChecked(settings->value("Autosize", DEF_AUTOSIZE).toBool());
166        cbUseNativeDialogs->setChecked(settings->value("UseNativeDialogs", DEF_USE_NATIVE_DIALOGS).toBool());
167#ifdef Q_OS_WIN32
168        if (QtWin::isCompositionEnabled())
169                cbUseTranslucency->setChecked(settings->value("UseTranslucency", DEF_USE_TRANSLUCENCY).toBool());
170#endif // Q_OS_WIN32
171#if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN)
172        cbSaveState->setChecked(settings->value("SavePos", DEF_SAVEPOS).toBool());
173#endif // Q_OS_WINCE
174
175        settings->beginGroup("Task");
176        cbSymmetricMode->setChecked(settings->value("SymmetricMode", DEF_SYMMETRIC_MODE).toBool());
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
185        settings->beginGroup("Output");
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());
190        spinCitiesLimit->setValue(settings->value("ShowMatrixLimit", DEF_SHOW_MATRIX_LIMIT).toInt());
191        spinCitiesLimit->setMaximum(MAX_NUM_CITIES);
192        cbScrollToEnd->setChecked(settings->value("ScrollToEnd", DEF_SCROLL_TO_END).toBool());
193
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();
197
198#ifndef Q_OS_WINCE
199        adjustSize();
200#endif // Q_OS_WINCE
201}
202
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{
209        return _newColor;
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{
218        return _newFont;
219}
220
221/*!
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.
226 */
227qint8 SettingsDialog::translucencyChanged() const
228{
229        return _translucency;
230}
231
232/* Privates **********************************************************/
233
234void SettingsDialog::accept()
235{
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        }
248#if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN)
249        settings->setValue("SavePos", cbSaveState->isChecked());
250#endif // Q_OS_WINCE
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
261        settings->setValue("UseNativeDialogs", cbUseNativeDialogs->isChecked());
262
263        settings->beginGroup("Task");
264        settings->setValue("SymmetricMode", cbSymmetricMode->isChecked());
265        settings->setValue("FractionalAccuracy", spinFractionalAccuracy->value());
266        settings->setValue("RandMin", spinRandMin->value());
267        settings->setValue("RandMax", spinRandMax->value());
268        settings->setValue("FractionalRandom", cbFractionalRandom->isChecked());
269        settings->endGroup();
270
271        settings->beginGroup("Output");
272        settings->setValue("ShowMatrix", cbShowMatrix->isChecked());
273        settings->setValue("UseShowMatrixLimit", cbShowMatrix->isChecked() && cbCitiesLimit->isChecked());
274        if (cbCitiesLimit->isChecked())
275                settings->setValue("ShowMatrixLimit", spinCitiesLimit->value());
276        settings->setValue("ScrollToEnd", cbScrollToEnd->isChecked());
277        if (_newFont)
278                settings->setValue("Font", font);
279        if (_newColor)
280                settings->setValue("Color", color);
281        settings->endGroup();
282        QDialog::accept();
283}
284
285void SettingsDialog::buttonColorClicked()
286{
287QColor color = QColorDialog::getColor(this->color,this);
288        if (color.isValid() && (this->color != color)) {
289                this->color = color;
290                _newColor = true;
291        }
292}
293
294void SettingsDialog::buttonFontClicked()
295{
296bool ok;
297QFont font = QFontDialog::getFont(&ok,this->font,this);
298        if (ok && (this->font != font)) {
299                this->font = font;
300                _newFont = true;
301        }
302}
303
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) {
312                QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
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                }
325                currentGeometry = availableGeometry;
326                QApplication::restoreOverrideCursor();
327        }
328}
329
330void SettingsDialog::showEvent(QShowEvent *ev)
331{
332        desktopResized(0);
333
334        QWidget::showEvent(ev);
335}
336#endif // Q_OS_WINCE
337
338void SettingsDialog::spinRandMinValueChanged(int val) {
339        spinRandMax->setMinimum(val);
340}
341
342#if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN)
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);
357}
358#endif // Q_OS_WINCE
Note: See TracBrowser for help on using the repository browser.