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

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

+ Translatable About dialog.

  • Improved installation rules in .pro file.
  • Disabled SVG and JPEG support. They aren't needed at this point.
  • Fixed a bug when labels were hardly visible under Windows Vista/7 whith translucency on and a dark background behind the application.
  • Fixed a bug when Main Window area was blank if the application was compiled using Qt < 4.6.0.
  • Property svn:keywords set to Id URL
File size: 13.5 KB
RevLine 
[1]1/*
[42]2 *  TSPSG: TSP Solver and Generator
[87]3 *  Copyright (C) 2007-2010 Lёppa <contacts[at]oleksii[dot]name>
[1]4 *
[6]5 *  $Id: settingsdialog.cpp 98 2010-03-12 19:28:42Z laleppa $
6 *  $URL: https://tspsg.svn.sourceforge.net/svnroot/tspsg/trunk/src/settingsdialog.cpp $
[3]7 *
[6]8 *  This file is part of TSPSG.
[1]9 *
[6]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.
[1]14 *
[6]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.
[1]19 *
[6]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/>.
[1]22 */
23
24#include "settingsdialog.h"
25
[65]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 */
[1]32SettingsDialog::SettingsDialog(QWidget *parent)
[92]33        : QDialog(parent), _newFont(false), _newColor(false), _translucency(0)
[1]34{
35        setupUi(this);
[87]36
[78]37        buttonBox->button(QDialogButtonBox::Save)->setIcon(QIcon(":/images/icons/button_ok.png"));
[87]38        buttonBox->button(QDialogButtonBox::Save)->setStatusTip(tr("Save new preferences"));
[78]39        buttonBox->button(QDialogButtonBox::Save)->setCursor(QCursor(Qt::PointingHandCursor));
40        buttonBox->button(QDialogButtonBox::Cancel)->setIcon(QIcon(":/images/icons/button_cancel.png"));
[87]41        buttonBox->button(QDialogButtonBox::Cancel)->setStatusTip(tr("Close without saving preferences"));
[78]42        buttonBox->button(QDialogButtonBox::Cancel)->setCursor(QCursor(Qt::PointingHandCursor));
43
[93]44#if defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN)
[94]45QVBoxLayout *vbox1; // Layout helper
[56]46
[94]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);
[95]56#else
57        buttons->insertStretch(buttons->indexOf(buttonHelp) + 1);
[94]58#endif // Q_OS_WINCE
59
[85]60        bgWhite->layout()->setMargin(0);
[56]61
62        // Central layout
63        vbox1 = new QVBoxLayout(this);
64        vbox1->setMargin(0);
65        vbox1->setSpacing(0);
[94]66#ifdef Q_OS_WINCE
67        vbox1->addWidget(scrollArea);
68#else
[56]69        vbox1->addWidget(bgWhite);
[94]70#endif // Q_OS_WINCE
[56]71        vbox1->addWidget(bgGrey);
[94]72        setLayout(vbox1);
[98]73#else // Q_OS_WINCE || Q_OS_SYMBIAN
[56]74        // Layout helper elements
[85]75QVBoxLayout *vbox1, *vbox2;
[94]76QHBoxLayout *hbox1;
[56]77
[96]78#ifdef Q_OS_WIN32
[92]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        }
[96]88#endif // Q_OS_WIN32
[92]89
[56]90        cbSaveState = new QCheckBox(bgWhite);
91        cbSaveState->setObjectName("cbSaveState");
92#ifndef QT_NO_STATUSTIP
[87]93        cbSaveState->setStatusTip(tr("Restore main window state and position on application restart"));
[56]94#endif // QT_NO_STATUSTIP
[87]95        cbSaveState->setText(tr("Save main window state and position"));
[56]96        cbSaveState->setCursor(QCursor(Qt::PointingHandCursor));
97
98        imgIcon = new QLabel(this);
99        imgIcon->setObjectName("imgIcon");
[58]100        imgIcon->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Expanding);
[94]101        imgIcon->setFrameShape(QFrame::NoFrame);
[78]102        imgIcon->setPixmap(QPixmap(":/images/icons/preferences_system.png"));
[82]103        imgIcon->setStyleSheet("background-color: #0080C0; padding-top: 11px;");
104        imgIcon->setAlignment(Qt::AlignTop | Qt::AlignHCenter);
[78]105        imgIcon->setMinimumWidth(150);
[56]106
107        labelHint = new QLabel(bgGrey);
108        labelHint->setObjectName("labelHint");
109        labelHint->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Preferred);
[58]110//      labelHint->setMinimumSize(QSize(190,28));
111        labelHint->setMinimumSize(QSize(0,28));
112        labelHint->setMaximumSize(QSize(QWIDGETSIZE_MAX,28));
[56]113        labelHint->setTextFormat(Qt::PlainText);
114//      labelHint->setAlignment(Qt::AlignLeft | Qt::AlignTop);
115        labelHint->setWordWrap(true);
116#ifndef QT_NO_STATUSTIP
[87]117        labelHint->setStatusTip(tr("Hover mouse pointer over dialog elements to get additional help"));
[56]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
[85]132        vbox1 = static_cast<QVBoxLayout *>(tabGeneral->layout());
[87]133        vbox1->insertWidget(vbox1->indexOf(cbUseNativeDialogs) + 1, cbSaveState);
[92]134#ifdef Q_OS_WIN32
135        if (QtWin::isCompositionEnabled())
136                vbox1->insertWidget(vbox1->indexOf(cbUseNativeDialogs) + 1, cbUseTranslucency);
137#endif // Q_OS_WIN32
[56]138
[94]139        // Inserting label for hints to the bottom part (with grey bg)
140        buttons->insertWidget(buttons->indexOf(buttonHelp) + 1, labelHint, 1);
[56]141
142        // Central layout
[85]143        vbox2 = new QVBoxLayout(this);
144        vbox2->setMargin(0);
145        vbox2->setSpacing(0);
146        vbox2->addLayout(hbox1);
147        vbox2->addWidget(bgGrey);
[94]148        setLayout(vbox2);
[56]149#endif // Q_OS_WINCE
[85]150
[94]151#ifdef Q_OS_WINCE
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
[1]155        connect(spinRandMin,SIGNAL(valueChanged(int)),this,SLOT(spinRandMinValueChanged(int)));
[17]156        connect(buttonFont,SIGNAL(clicked()),this,SLOT(buttonFontClicked()));
[21]157        connect(buttonColor,SIGNAL(clicked()),this,SLOT(buttonColorClicked()));
[1]158        setWindowFlags(windowFlags() ^ Qt::WindowContextHelpButtonHint);
[93]159#if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN)
[78]160        // Setting initial text of dialog hint label to own status tip text.
[9]161        labelHint->setText(labelHint->statusTip());
[17]162#endif // Q_OS_WINCE
[80]163
164        settings = new QSettings(QSettings::IniFormat, QSettings::UserScope, "TSPSG", "tspsg", this);
[95]165        settings->remove("SettingsReset");
[80]166
[82]167        cbAutosize->setChecked(settings->value("Autosize", DEF_AUTOSIZE).toBool());
168        cbUseNativeDialogs->setChecked(settings->value("UseNativeDialogs", DEF_USE_NATIVE_DIALOGS).toBool());
[92]169#ifdef Q_OS_WIN32
170        if (QtWin::isCompositionEnabled())
171                cbUseTranslucency->setChecked(settings->value("UseTranslucency", DEF_USE_TRANSLUCENCY).toBool());
172#endif // Q_OS_WIN32
[93]173#if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN)
[82]174        cbSaveState->setChecked(settings->value("SavePos", DEF_SAVEPOS).toBool());
[21]175#endif // Q_OS_WINCE
[78]176
[87]177        settings->beginGroup("Task");
[95]178        cbSymmetricMode->setChecked(settings->value("SymmetricMode", DEF_SYMMETRIC_MODE).toBool());
[87]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
[42]187        settings->beginGroup("Output");
[78]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());
[82]192        spinCitiesLimit->setValue(settings->value("ShowMatrixLimit", DEF_SHOW_MATRIX_LIMIT).toInt());
[78]193        spinCitiesLimit->setMaximum(MAX_NUM_CITIES);
[81]194        cbScrollToEnd->setChecked(settings->value("ScrollToEnd", DEF_SCROLL_TO_END).toBool());
[78]195
[21]196        font = settings->value("Font",QFont(DEF_FONT_FAMILY,DEF_FONT_SIZE)).value<QFont>();
197        color = settings->value("Color",DEF_FONT_COLOR).value<QColor>();
[42]198        settings->endGroup();
[78]199
[95]200#ifndef Q_OS_WINCE
[78]201        adjustSize();
[95]202#endif // Q_OS_WINCE
[42]203}
204
[67]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{
[92]211        return _newColor;
[67]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{
[92]220        return _newFont;
[67]221}
222
[92]223/*!
[94]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.
[92]228 */
229qint8 SettingsDialog::translucencyChanged() const
230{
231        return _translucency;
232}
233
[67]234/* Privates **********************************************************/
235
[42]236void SettingsDialog::accept()
237{
[95]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        }
[93]250#if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN)
[78]251        settings->setValue("SavePos", cbSaveState->isChecked());
[21]252#endif // Q_OS_WINCE
[92]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
[82]263        settings->setValue("UseNativeDialogs", cbUseNativeDialogs->isChecked());
[87]264
265        settings->beginGroup("Task");
[95]266        settings->setValue("SymmetricMode", cbSymmetricMode->isChecked());
[87]267        settings->setValue("FractionalAccuracy", spinFractionalAccuracy->value());
268        settings->setValue("RandMin", spinRandMin->value());
269        settings->setValue("RandMax", spinRandMax->value());
[81]270        settings->setValue("FractionalRandom", cbFractionalRandom->isChecked());
[87]271        settings->endGroup();
[78]272
[42]273        settings->beginGroup("Output");
[78]274        settings->setValue("ShowMatrix", cbShowMatrix->isChecked());
275        settings->setValue("UseShowMatrixLimit", cbShowMatrix->isChecked() && cbCitiesLimit->isChecked());
276        if (cbCitiesLimit->isChecked())
[82]277                settings->setValue("ShowMatrixLimit", spinCitiesLimit->value());
[81]278        settings->setValue("ScrollToEnd", cbScrollToEnd->isChecked());
[92]279        if (_newFont)
[78]280                settings->setValue("Font", font);
[92]281        if (_newColor)
[78]282                settings->setValue("Color", color);
[21]283        settings->endGroup();
[42]284        QDialog::accept();
[1]285}
286
[42]287void SettingsDialog::buttonColorClicked()
288{
289QColor color = QColorDialog::getColor(this->color,this);
290        if (color.isValid() && (this->color != color)) {
291                this->color = color;
[92]292                _newColor = true;
[42]293        }
294}
295
[67]296void SettingsDialog::buttonFontClicked()
[42]297{
[67]298bool ok;
299QFont font = QFontDialog::getFont(&ok,this->font,this);
300        if (ok && (this->font != font)) {
301                this->font = font;
[92]302                _newFont = true;
[67]303        }
[42]304}
305
[94]306#ifdef Q_OS_WINCE
307void SettingsDialog::desktopResized(int screen)
308{
309        if (screen != 0)
310                return;
311
312QRect availableGeometry = QApplication::desktop()->availableGeometry(0);
313        if (currentGeometry != availableGeometry) {
[95]314                QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
[94]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                }
[95]327                currentGeometry = availableGeometry;
328                QApplication::restoreOverrideCursor();
[94]329        }
330}
[95]331
332void SettingsDialog::showEvent(QShowEvent *ev)
333{
334        desktopResized(0);
335
336        QWidget::showEvent(ev);
337}
[94]338#endif // Q_OS_WINCE
339
[65]340void SettingsDialog::spinRandMinValueChanged(int val) {
341        spinRandMax->setMinimum(val);
342}
343
[93]344#if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN)
[9]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
[10]349        // dialog hint label text to own status tip text.
[9]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}
[17]360#endif // Q_OS_WINCE
Note: See TracBrowser for help on using the repository browser.