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

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

+ Added Installation Guide.

  • Updated translations.
  • Updated documentaion.

NB: This will be the second public alpha release.

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