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

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

Fixed some compilation errors on some platforms intorduced in the previous build...

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