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

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

+ Added the ability to select in what format to save the graph when saving solution as HTML.

  • Moved all installation and deployment rules to a separate install.pri file.
  • Property svn:keywords set to Id URL
File size: 14.7 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 109 2010-04-27 07:12: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
[104]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
[100]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");
[107]196        cbShowGraph->setChecked(settings->value("ShowGraph", DEF_SHOW_GRAPH).toBool());
[109]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
[100]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>();
[104]226        color = settings->value("Colors/Text", DEF_TEXT_COLOR).value<QColor>();
[100]227        settings->endGroup();
228
229#ifndef Q_OS_WINCE_WM
230        adjustSize();
231#endif // Q_OS_WINCE_WM
232}
233
234/*!
235 * \brief Indicates whether or not the font color has been changed.
236 * \return \c true if font color changed, otherwise \c false.
237 */
238bool SettingsDialog::colorChanged() const
239{
240        return _newColor;
241}
242
243/*!
244 * \brief Indicates whether or not the font properties have been changed.
245 * \return \c true if font properties changed, otherwise \c false.
246 */
247bool SettingsDialog::fontChanged() const
248{
249        return _newFont;
250}
251
252/*!
253 * \brief Indicates whether and how the translucency setting was changed
254 * \retval -1 the translucency was \em disabled.
255 * \retval  0 the translucency was <em>not changed</em>.
256 * \retval  1 the translucency was \em enabled.
257 */
258qint8 SettingsDialog::translucencyChanged() const
259{
260        return _translucency;
261}
262
263/* Privates **********************************************************/
264
265void SettingsDialog::accept()
266{
267        if (QApplication::keyboardModifiers() & Qt::ShiftModifier) {
268                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) {
269                        _newFont = (font != QFont(DEF_FONT_FAMILY, DEF_FONT_SIZE));
[104]270                        _newColor = (color != DEF_TEXT_COLOR);
[100]271                        settings->remove("");
272                        settings->setValue("SettingsReset", true);
273                        QDialog::accept();
[109]274                        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]275                        return;
276                } else
277                        return;
278        }
279#ifndef HANDHELD
280        settings->setValue("SavePos", cbSaveState->isChecked());
281#endif // HANDHELD
282#ifdef Q_OS_WIN32
283        if (QtWin::isCompositionEnabled()) {
284bool old = settings->value("UseTranslucency", DEF_USE_TRANSLUCENCY).toBool();
285                if ((!old && cbUseTranslucency->isChecked()) || (old && !cbUseTranslucency->isChecked())) {
286                        _translucency = old ? -1 : 1;
287                } else
288                        _translucency = 0;
289                settings->setValue("UseTranslucency", cbUseTranslucency->isChecked());
290        }
291#endif // Q_OS_WIN32
292        settings->setValue("UseNativeDialogs", cbUseNativeDialogs->isChecked());
293
294        settings->beginGroup("Task");
295        settings->setValue("SymmetricMode", cbSymmetricMode->isChecked());
296        settings->setValue("FractionalAccuracy", spinFractionalAccuracy->value());
297        settings->setValue("RandMin", spinRandMin->value());
298        settings->setValue("RandMax", spinRandMax->value());
299        settings->setValue("FractionalRandom", cbFractionalRandom->isChecked());
300        settings->endGroup();
301
302        settings->beginGroup("Output");
[107]303        settings->setValue("ShowGraph", cbShowGraph->isChecked());
[109]304        if (cbShowGraph->isChecked()) {
305                if (comboGraphImageFormat->currentIndex() >= 0)
306                        settings->setValue("GraphImageFormat", comboGraphImageFormat->currentText());
307                else
308                        settings->setValue("GraphImageFormat", DEF_GRAPH_IMAGE_FORMAT);
309        }
[100]310        settings->setValue("ShowMatrix", cbShowMatrix->isChecked());
311        settings->setValue("UseShowMatrixLimit", cbShowMatrix->isChecked() && cbCitiesLimit->isChecked());
312        if (cbCitiesLimit->isChecked())
313                settings->setValue("ShowMatrixLimit", spinCitiesLimit->value());
314        settings->setValue("ScrollToEnd", cbScrollToEnd->isChecked());
315        if (_newFont)
316                settings->setValue("Font", font);
317        if (_newColor)
[104]318                settings->setValue("Colors/Text", color);
[100]319        settings->endGroup();
320        QDialog::accept();
321}
322
323void SettingsDialog::buttonColorClicked()
324{
325QColor color = QColorDialog::getColor(this->color,this);
326        if (color.isValid() && (this->color != color)) {
327                this->color = color;
328                _newColor = true;
329        }
330}
331
332void SettingsDialog::buttonFontClicked()
333{
334bool ok;
335QFont font = QFontDialog::getFont(&ok,this->font,this);
336        if (ok && (this->font != font)) {
337                this->font = font;
338                _newFont = true;
339        }
340}
341
342#ifdef Q_OS_WINCE_WM
343void SettingsDialog::desktopResized(int screen)
344{
345        if (screen != 0)
346                return;
347
348QRect availableGeometry = QApplication::desktop()->availableGeometry(0);
349        if (currentGeometry != availableGeometry) {
350                QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
351                /*!
352                 * \hack HACK: This hack checks whether \link QDesktopWidget::availableGeometry() availableGeometry()\endlink's \c top + \c hegiht = \link QDesktopWidget::screenGeometry() screenGeometry()\endlink's \c height.
353                 *  If \c true, the window gets maximized. If we used \c setGeometry() in this case, the bottom of the
354                 *  window would end up being behind the soft buttons. Is this a bug in Qt or Windows Mobile?
355                 */
356                if ((availableGeometry.top() + availableGeometry.height()) == QApplication::desktop()->screenGeometry().height()) {
357                        setWindowState(windowState() | Qt::WindowMaximized);
358                } else {
359                        if (windowState() & Qt::WindowMaximized)
360                                setWindowState(windowState() ^ Qt::WindowMaximized);
361                        setGeometry(availableGeometry);
362                }
363                currentGeometry = availableGeometry;
364                QApplication::restoreOverrideCursor();
365        }
366}
367
368void SettingsDialog::showEvent(QShowEvent *ev)
369{
370        desktopResized(0);
371
372        QWidget::showEvent(ev);
373}
374#endif // Q_OS_WINCE_WM
375
376void SettingsDialog::spinRandMinValueChanged(int val) {
377        spinRandMax->setMinimum(val);
378}
379
380#ifndef HANDHELD
381bool SettingsDialog::event(QEvent *ev)
382{
383        // Checking for StatusTip event and if tip text is not empty string
384        // setting it as text of the dialog hint label. Otherwise, setting
385        // dialog hint label text to own status tip text.
386        if (ev->type() == QEvent::StatusTip) {
387QString tip = static_cast<QStatusTipEvent *>(ev)->tip();
388                if (tip.length() != 0)
389                        labelHint->setText(tip);
390                else
391                        labelHint->setText(labelHint->statusTip());
392                return true;
393        } else
394                return QDialog::event(ev);
395}
396#endif // HANDHELD
Note: See TracBrowser for help on using the repository browser.