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

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