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

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

Added an option to select graph quality, because higher graph quality looked uglier on the screen while looked nicer when printing.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id URL
File size: 16.3 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 127 2010-09-01 18:31:16Z 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(QIcon::fromTheme("preferences-system", QIcon(":/images/icons/preferences-system.png")));
38
39        buttonBox->button(QDialogButtonBox::Ok)->setIcon(QIcon::fromTheme("dialog-ok", QIcon(":/images/icons/dialog-ok.png")));
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(QIcon::fromTheme("dialog-cancel", QIcon(":/images/icons/dialog-cancel.png")));
43        buttonBox->button(QDialogButtonBox::Cancel)->setStatusTip(tr("Close without saving preferences"));
44        buttonBox->button(QDialogButtonBox::Cancel)->setCursor(QCursor(Qt::PointingHandCursor));
45
46        buttonColor->setIcon(QIcon::fromTheme("format-text-color", QIcon(":/images/icons/format-text-color.png")));
47        buttonFont->setIcon(QIcon::fromTheme("preferences-desktop-font", QIcon(":/images/icons/preferences-desktop-font.png")));
48        buttonHelp->setIcon(QIcon::fromTheme("help-hint", QIcon(":/images/icons/help-hint.png")));
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(QIcon::fromTheme("preferences-system", QIcon(":/images/icons/preferences-system.png")).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        cbUseNativeDialogs->setChecked(settings->value("UseNativeDialogs", DEF_USE_NATIVE_DIALOGS).toBool());
203#ifdef Q_OS_WIN32
204        if (QtWin::isCompositionEnabled())
205                cbUseTranslucency->setChecked(settings->value("UseTranslucency", DEF_USE_TRANSLUCENCY).toBool());
206#endif // Q_OS_WIN32
207#ifndef HANDHELD
208        cbSaveState->setChecked(settings->value("SavePos", DEF_SAVEPOS).toBool());
209#endif // HANDHELD
210
211        settings->beginGroup("Task");
212        cbSymmetricMode->setChecked(settings->value("SymmetricMode", DEF_SYMMETRIC_MODE).toBool());
213        spinFractionalAccuracy->setValue(settings->value("FractionalAccuracy", DEF_FRACTIONAL_ACCURACY).toInt());
214        spinRandMin->setMaximum(MAX_RAND_VALUE);
215        spinRandMin->setValue(settings->value("RandMin",DEF_RAND_MIN).toInt());
216        spinRandMax->setMaximum(MAX_RAND_VALUE);
217        spinRandMax->setValue(settings->value("RandMax",DEF_RAND_MAX).toInt());
218        cbFractionalRandom->setChecked(settings->value("FractionalRandom", DEF_FRACTIONAL_RANDOM).toBool());
219        settings->endGroup();
220
221        settings->beginGroup("Output");
222        cbShowGraph->setChecked(settings->value("ShowGraph", DEF_SHOW_GRAPH).toBool());
223
224#ifndef QT_NO_PRINTER
225        cbHQGraph->setEnabled(cbShowGraph->isChecked());
226        cbHQGraph->setChecked(settings->value("HQGraph", DEF_HQ_GRAPH && cbShowGraph->isChecked()).toBool());
227#endif
228
229#if !defined(NOSVG) && (QT_VERSION >= 0x040500)
230        comboGraphImageFormat->addItem("svg");
231#endif // NOSVG && QT_VERSION >= 0x040500
232// We create a whitelist of formats, supported by the most popular web browsers according to
233//  http://en.wikipedia.org/wiki/Comparison_of_web_browsers#Image_format_support
234//  + TIFF format (there are plugins to support it).
235QStringList whitelist;
236        whitelist << "bmp" << "jpeg" << "png" << "tiff" << "xbm";
237        foreach (QByteArray format, QImageWriter::supportedImageFormats()) {
238                if (whitelist.contains(format))
239                        comboGraphImageFormat->addItem(format);
240        }
241        comboGraphImageFormat->model()->sort(0);
242        comboGraphImageFormat->setCurrentIndex(comboGraphImageFormat->findText(settings->value("GraphImageFormat", DEF_GRAPH_IMAGE_FORMAT).toString(), Qt::MatchFixedString));
243        if (comboGraphImageFormat->currentIndex() < 0)
244                comboGraphImageFormat->setCurrentIndex(comboGraphImageFormat->findText(DEF_GRAPH_IMAGE_FORMAT, Qt::MatchFixedString));
245        labelGraphImageFormat->setEnabled(cbShowGraph->isChecked());
246        comboGraphImageFormat->setEnabled(cbShowGraph->isChecked());
247
248        cbShowMatrix->setChecked(settings->value("ShowMatrix", DEF_SHOW_MATRIX).toBool());
249        cbCitiesLimit->setEnabled(cbShowMatrix->isChecked());
250        cbCitiesLimit->setChecked(settings->value("UseShowMatrixLimit", DEF_USE_SHOW_MATRIX_LIMIT && cbShowMatrix->isChecked()).toBool());
251        spinCitiesLimit->setEnabled(cbShowMatrix->isChecked() && cbCitiesLimit->isChecked());
252        spinCitiesLimit->setValue(settings->value("ShowMatrixLimit", DEF_SHOW_MATRIX_LIMIT).toInt());
253        spinCitiesLimit->setMaximum(MAX_NUM_CITIES);
254        cbScrollToEnd->setChecked(settings->value("ScrollToEnd", DEF_SCROLL_TO_END).toBool());
255
256        font = settings->value("Font", QFont(getDefaultFont(), DEF_FONT_SIZE)).value<QFont>();
257        color = settings->value("Colors/Text", DEF_TEXT_COLOR).value<QColor>();
258        settings->endGroup();
259
260#ifdef HANDHELD
261        setWindowState(Qt::WindowMaximized);
262#else
263        adjustSize();
264#endif // Q_OS_WINCE_WM
265}
266
267/*!
268 * \brief Indicates whether or not the font color has been changed.
269 * \return \c true if font color changed, otherwise \c false.
270 */
271bool SettingsDialog::colorChanged() const
272{
273        return _newColor;
274}
275
276/*!
277 * \brief Indicates whether or not the font properties have been changed.
278 * \return \c true if font properties changed, otherwise \c false.
279 */
280bool SettingsDialog::fontChanged() const
281{
282        return _newFont;
283}
284
285/*!
286 * \brief Indicates whether and how the translucency setting was changed
287 * \retval -1 the translucency was \em disabled.
288 * \retval  0 the translucency was <em>not changed</em>.
289 * \retval  1 the translucency was \em enabled.
290 */
291qint8 SettingsDialog::translucencyChanged() const
292{
293        return _translucency;
294}
295
296/* Privates **********************************************************/
297
298void SettingsDialog::accept()
299{
300        if (QApplication::keyboardModifiers() & Qt::ShiftModifier) {
301                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) {
302                        _newFont = (font != QFont(getDefaultFont(), DEF_FONT_SIZE));
303                        _newColor = (color != DEF_TEXT_COLOR);
304                        settings->remove("");
305                        settings->setValue("SettingsReset", true);
306                        QDialog::accept();
307                        QMessageBox::information(this->parentWidget(), tr("Settings Reset"), tr("All settings where successfully reset to their defaults.\nIt is recommended to restart the application now."));
308                        return;
309                } else
310                        return;
311        }
312#ifndef HANDHELD
313        settings->setValue("SavePos", cbSaveState->isChecked());
314#endif // HANDHELD
315#ifdef Q_OS_WIN32
316        if (QtWin::isCompositionEnabled()) {
317bool old = settings->value("UseTranslucency", DEF_USE_TRANSLUCENCY).toBool();
318                if ((!old && cbUseTranslucency->isChecked()) || (old && !cbUseTranslucency->isChecked())) {
319                        _translucency = old ? -1 : 1;
320                } else
321                        _translucency = 0;
322                settings->setValue("UseTranslucency", cbUseTranslucency->isChecked());
323        }
324#endif // Q_OS_WIN32
325        settings->setValue("Autosize", cbAutosize->isChecked());
326        settings->setValue("UseNativeDialogs", cbUseNativeDialogs->isChecked());
327
328        settings->beginGroup("Task");
329        settings->setValue("SymmetricMode", cbSymmetricMode->isChecked());
330        settings->setValue("FractionalAccuracy", spinFractionalAccuracy->value());
331        settings->setValue("RandMin", spinRandMin->value());
332        settings->setValue("RandMax", spinRandMax->value());
333        settings->setValue("FractionalRandom", cbFractionalRandom->isChecked());
334        settings->endGroup();
335
336        settings->beginGroup("Output");
337        settings->setValue("ShowGraph", cbShowGraph->isChecked());
338#ifndef QT_NO_PRINTER
339        settings->setValue("HQGraph", cbShowGraph->isChecked() && cbHQGraph->isChecked());
340#endif
341        if (cbShowGraph->isChecked()) {
342                if (comboGraphImageFormat->currentIndex() >= 0)
343                        settings->setValue("GraphImageFormat", comboGraphImageFormat->currentText());
344                else
345                        settings->setValue("GraphImageFormat", DEF_GRAPH_IMAGE_FORMAT);
346        }
347        settings->setValue("ShowMatrix", cbShowMatrix->isChecked());
348        settings->setValue("UseShowMatrixLimit", cbShowMatrix->isChecked() && cbCitiesLimit->isChecked());
349        if (cbCitiesLimit->isChecked())
350                settings->setValue("ShowMatrixLimit", spinCitiesLimit->value());
351        settings->setValue("ScrollToEnd", cbScrollToEnd->isChecked());
352        if (_newFont)
353                settings->setValue("Font", font);
354        if (_newColor)
355                settings->setValue("Colors/Text", color);
356        settings->endGroup();
357        QDialog::accept();
358}
359
360void SettingsDialog::buttonColorClicked()
361{
362QColor color = QColorDialog::getColor(this->color,this);
363        if (color.isValid() && (this->color != color)) {
364                this->color = color;
365                _newColor = true;
366        }
367}
368
369void SettingsDialog::buttonFontClicked()
370{
371bool ok;
372QFont font = QFontDialog::getFont(&ok,this->font,this);
373        if (ok && (this->font != font)) {
374                this->font = font;
375                _newFont = true;
376        }
377}
378
379#ifdef Q_OS_WINCE_WM
380void SettingsDialog::desktopResized(int screen)
381{
382        if (screen != 0)
383                return;
384
385QRect availableGeometry = QApplication::desktop()->availableGeometry(0);
386        if (currentGeometry != availableGeometry) {
387                QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
388                /*!
389                 * \hack HACK: This hack checks whether \link QDesktopWidget::availableGeometry() availableGeometry()\endlink's \c top + \c hegiht = \link QDesktopWidget::screenGeometry() screenGeometry()\endlink's \c height.
390                 *  If \c true, the window gets maximized. If we used \c setGeometry() in this case, the bottom of the
391                 *  window would end up being behind the soft buttons. Is this a bug in Qt or Windows Mobile?
392                 */
393                if ((availableGeometry.top() + availableGeometry.height()) == QApplication::desktop()->screenGeometry().height()) {
394                        setWindowState(windowState() | Qt::WindowMaximized);
395                } else {
396                        if (windowState() & Qt::WindowMaximized)
397                                setWindowState(windowState() ^ Qt::WindowMaximized);
398                        setGeometry(availableGeometry);
399                }
400                currentGeometry = availableGeometry;
401                QApplication::restoreOverrideCursor();
402        }
403}
404
405void SettingsDialog::showEvent(QShowEvent *ev)
406{
407        desktopResized(0);
408
409        QWidget::showEvent(ev);
410}
411#endif // Q_OS_WINCE_WM
412
413void SettingsDialog::spinRandMinValueChanged(int val) {
414        spinRandMax->setMinimum(val);
415}
416
417#ifndef HANDHELD
418bool SettingsDialog::event(QEvent *ev)
419{
420        // Checking for StatusTip event and if tip text is not empty string
421        // setting it as text of the dialog hint label. Otherwise, setting
422        // dialog hint label text to own status tip text.
423        if (ev->type() == QEvent::StatusTip) {
424QString tip = static_cast<QStatusTipEvent *>(ev)->tip();
425                if (tip.length() != 0)
426                        labelHint->setText(tip);
427                else
428                        labelHint->setText(labelHint->statusTip());
429                return true;
430        } else
431                return QDialog::event(ev);
432}
433#endif // HANDHELD
Note: See TracBrowser for help on using the repository browser.