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

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