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

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

+ Added SIP show/hide handling under wince.

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