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

Last change on this file since 114 was 114, checked in by laleppa, 15 years ago

+ Added a checkbox to the "Settings" menu to be able to show/hide Toolbar.

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