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

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

+ Added Wait Cursor on city number change, because it can take some time (e.g. when changing from 5 to 50 cities).

  • Moved platform specific action definitions from .ui to source.
  • Changed random generation to include defined maximum (it was up to maximum previously).
  • Reverted from native to Qt open/save file dialog under WinCE, because it is more convenient than the native one.
  • English translation is now always loaded at application start before any other language is loaded. This is done to support plurals and some special characters that Qt translation system doesn't "like".
  • Updated translations to reflect recent changes.
  • Property svn:keywords set to Id URL
File size: 9.9 KB
RevLine 
[1]1/*
[42]2 *  TSPSG: TSP Solver and Generator
[17]3 *  Copyright (C) 2007-2009 Lёppa <contacts[at]oleksii[dot]name>
[1]4 *
[6]5 *  $Id: settingsdialog.cpp 80 2009-12-22 19:56:21Z 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)
[42]33        : QDialog(parent), newFont(false), newColor(false)
[1]34{
35        setupUi(this);
[56]36        // Laying out elements
[78]37        layoutCitiesLimit = new QHBoxLayout();
38        layoutCitiesLimit->setMargin(0);
39        layoutCitiesLimit->setSpacing(0);
40        layoutCitiesLimit->addSpacing(10);
41        layoutCitiesLimit->addWidget(cbCitiesLimit);
42        layoutCitiesLimit->addWidget(spinCitiesLimit);
43        layoutCitiesLimit->addStretch();
44
45        buttonBox->button(QDialogButtonBox::Save)->setIcon(QIcon(":/images/icons/button_ok.png"));
46        buttonBox->button(QDialogButtonBox::Save)->setStatusTip(trUtf8("Save new preferences"));
47        buttonBox->button(QDialogButtonBox::Save)->setCursor(QCursor(Qt::PointingHandCursor));
48        buttonBox->button(QDialogButtonBox::Cancel)->setIcon(QIcon(":/images/icons/button_cancel.png"));
49        buttonBox->button(QDialogButtonBox::Cancel)->setStatusTip(trUtf8("Close without saving preferences"));
50        buttonBox->button(QDialogButtonBox::Cancel)->setCursor(QCursor(Qt::PointingHandCursor));
51
[56]52#ifdef Q_OS_WINCE
53        // Layout helper elements
54QVBoxLayout *vbox1, *vbox2;
55QHBoxLayout *hbox1, *hbox2;
56
57        labelRandMin->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Preferred);
58        labelRandMax->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Preferred);
59
60        // Top part (with white bg)
61        vbox2 = new QVBoxLayout(bgWhite);
62        vbox2->addWidget(groupRandomSettings);
63        vbox2->addWidget(groupOutputSettings);
[78]64        vbox2->addStretch();
65        vbox2->addWidget(cbShowMatrix);
66        vbox2->addLayout(layoutCitiesLimit);
[57]67        vbox2->addWidget(cbAutosize);
[56]68
69        // Output settings group
70        hbox1 = new QHBoxLayout(groupOutputSettings);
71        hbox1->addWidget(buttonFont);
72        hbox1->addWidget(buttonColor);
73
74        // Bottom part (with grey bg)
75        hbox2 = new QHBoxLayout(bgGrey);
76        hbox2->setMargin(6);
77        hbox2->setSpacing(6);
78        hbox2->addWidget(buttonHelp);
[78]79        hbox2->addStretch();
80        hbox2->addWidget(buttonBox);
[56]81
82        // Central layout
83        vbox1 = new QVBoxLayout(this);
84        vbox1->setMargin(0);
85        vbox1->setSpacing(0);
86        vbox1->addWidget(bgWhite);
87        vbox1->addWidget(lineHorizontal);
88        vbox1->addWidget(bgGrey);
89#else
90        // Layout helper elements
91QVBoxLayout *vbox1, *vbox2, *vbox3;
92QHBoxLayout *hbox1, *hbox2, *hbox3;
93
94        cbSaveState = new QCheckBox(bgWhite);
95        cbSaveState->setObjectName("cbSaveState");
96#ifndef QT_NO_STATUSTIP
97        cbSaveState->setStatusTip(trUtf8("Restore main window state and position on application restart"));
98#endif // QT_NO_STATUSTIP
99        cbSaveState->setText(trUtf8("Save main window state and position"));
100        cbSaveState->setCursor(QCursor(Qt::PointingHandCursor));
101
102        imgIcon = new QLabel(this);
103        imgIcon->setObjectName("imgIcon");
[58]104        imgIcon->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Expanding);
105        imgIcon->setFrameShape(QFrame::Panel);
[56]106        imgIcon->setLineWidth(0);
[78]107        imgIcon->setPixmap(QPixmap(":/images/icons/preferences_system.png"));
[56]108        imgIcon->setStyleSheet("background-color: #0080C0;");
109        imgIcon->setAlignment(Qt::AlignCenter);
[78]110        imgIcon->setMinimumWidth(150);
[56]111
112        labelHint = new QLabel(bgGrey);
113        labelHint->setObjectName("labelHint");
114        labelHint->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Preferred);
[58]115//      labelHint->setMinimumSize(QSize(190,28));
116        labelHint->setMinimumSize(QSize(0,28));
117        labelHint->setMaximumSize(QSize(QWIDGETSIZE_MAX,28));
[56]118        labelHint->setTextFormat(Qt::PlainText);
119//      labelHint->setAlignment(Qt::AlignLeft | Qt::AlignTop);
120        labelHint->setWordWrap(true);
121#ifndef QT_NO_STATUSTIP
122        labelHint->setStatusTip(trUtf8("Hover mouse pointer over dialog elements to get additional help"));
123#endif // QT_NO_STATUSTIP
124
125        lineVertical = new QFrame(this);
126        lineVertical->setObjectName("lineVertical");
127        lineVertical->setFrameShadow(QFrame::Plain);
128        lineVertical->setFrameShape(QFrame::VLine);
129        lineVertical->setLineWidth(2);
130
131        // Top line
132        hbox1 = new QHBoxLayout();
133        hbox1->addWidget(imgIcon);
134        hbox1->addWidget(lineVertical);
135        hbox1->addWidget(bgWhite);
136
137        // Output settings group
138        vbox3 = new QVBoxLayout(groupOutputSettings);
139        vbox3->addWidget(buttonFont);
140        vbox3->addWidget(buttonColor);
141
142        // Random and Output settings groups
143        hbox2 = new QHBoxLayout();
144        hbox2->addWidget(groupRandomSettings);
145        hbox2->addWidget(groupOutputSettings);
[78]146        hbox2->addStretch();
[56]147
148        // Top right part (with white bg)
149        vbox2 = new QVBoxLayout(bgWhite);
[78]150        vbox2->addStretch();
[56]151        vbox2->addLayout(hbox2);
[78]152        vbox2->addWidget(cbShowMatrix);
153        vbox2->addLayout(layoutCitiesLimit);
[57]154        vbox2->addWidget(cbAutosize);
[56]155        vbox2->addWidget(cbSaveState);
[78]156        vbox2->addStretch();
[56]157
158        // Bottom part (with grey bg)
159        hbox3 = new QHBoxLayout(bgGrey);
160        hbox3->setMargin(6);
161        hbox3->setSpacing(6);
162        hbox3->addWidget(buttonHelp);
163        hbox3->addWidget(labelHint);
[78]164        hbox3->addWidget(buttonBox);
[56]165
166        // Central layout
167        vbox1 = new QVBoxLayout(this);
168        vbox1->setMargin(0);
169        vbox1->setSpacing(0);
170        vbox1->addLayout(hbox1);
171        vbox1->addWidget(lineHorizontal);
172        vbox1->addWidget(bgGrey);
173#endif // Q_OS_WINCE
[1]174        connect(spinRandMin,SIGNAL(valueChanged(int)),this,SLOT(spinRandMinValueChanged(int)));
[17]175        connect(buttonFont,SIGNAL(clicked()),this,SLOT(buttonFontClicked()));
[21]176        connect(buttonColor,SIGNAL(clicked()),this,SLOT(buttonColorClicked()));
[1]177//      setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::MSWindowsFixedSizeDialogHint);
178        setWindowFlags(windowFlags() ^ Qt::WindowContextHelpButtonHint);
[17]179#ifndef Q_OS_WINCE
[78]180        // Setting initial text of dialog hint label to own status tip text.
[9]181        labelHint->setText(labelHint->statusTip());
[17]182#endif // Q_OS_WINCE
[80]183
184        settings = new QSettings(QSettings::IniFormat, QSettings::UserScope, "TSPSG", "tspsg", this);
185
[57]186        spinRandMin->setMaximum(MAX_RAND_VALUE);
[21]187        spinRandMin->setValue(settings->value("MinCost",DEF_RAND_MIN).toInt());
[57]188        spinRandMax->setMaximum(MAX_RAND_VALUE);
[21]189        spinRandMax->setValue(settings->value("MaxCost",DEF_RAND_MAX).toInt());
[57]190        cbAutosize->setChecked(settings->value("Autosize",true).toBool());
[21]191#ifndef Q_OS_WINCE
192        cbSaveState->setChecked(settings->value("SavePos",false).toBool());
193#endif // Q_OS_WINCE
[78]194
[42]195        settings->beginGroup("Output");
[78]196        cbShowMatrix->setChecked(settings->value("ShowMatrix", DEF_SHOW_MATRIX).toBool());
197        cbCitiesLimit->setEnabled(cbShowMatrix->isChecked());
198        cbCitiesLimit->setChecked(settings->value("UseShowMatrixLimit", DEF_USE_SHOW_MATRIX_LIMIT && cbShowMatrix->isChecked()).toBool());
199        spinCitiesLimit->setEnabled(cbShowMatrix->isChecked());
200        spinCitiesLimit->setValue(settings->value("ShowMatrixCitiesLimit", DEF_SHOW_MATRIX_CITY_LIMIT).toInt());
201        spinCitiesLimit->setMaximum(MAX_NUM_CITIES);
202
[21]203        font = settings->value("Font",QFont(DEF_FONT_FAMILY,DEF_FONT_SIZE)).value<QFont>();
204        color = settings->value("Color",DEF_FONT_COLOR).value<QColor>();
[42]205        settings->endGroup();
[78]206
207        adjustSize();
[42]208}
209
[67]210/*!
211 * \brief Indicates whether or not the font color has been changed.
212 * \return \c true if font color changed, otherwise \c false.
213 */
214bool SettingsDialog::colorChanged() const
215{
216        return newColor;
217}
218
219/*!
220 * \brief Indicates whether or not the font properties have been changed.
221 * \return \c true if font properties changed, otherwise \c false.
222 */
223bool SettingsDialog::fontChanged() const
224{
225        return newFont;
226}
227
228/* Privates **********************************************************/
229
[42]230void SettingsDialog::accept()
231{
[21]232#ifndef Q_OS_WINCE
[78]233        settings->setValue("SavePos", cbSaveState->isChecked());
[21]234#endif // Q_OS_WINCE
[78]235        settings->setValue("Autosize", cbAutosize->isChecked());
236        settings->setValue("MinCost", spinRandMin->value());
237        settings->setValue("MaxCost", spinRandMax->value());
238
[42]239        settings->beginGroup("Output");
[78]240        settings->setValue("ShowMatrix", cbShowMatrix->isChecked());
241        settings->setValue("UseShowMatrixLimit", cbShowMatrix->isChecked() && cbCitiesLimit->isChecked());
242        if (cbCitiesLimit->isChecked())
243                settings->setValue("ShowMatrixCitiesLimit", spinCitiesLimit->value());
[42]244        if (newFont)
[78]245                settings->setValue("Font", font);
[42]246        if (newColor)
[78]247                settings->setValue("Color", color);
[21]248        settings->endGroup();
[42]249        QDialog::accept();
[1]250}
251
[42]252void SettingsDialog::buttonColorClicked()
253{
254QColor color = QColorDialog::getColor(this->color,this);
255        if (color.isValid() && (this->color != color)) {
256                this->color = color;
257                newColor = true;
258        }
259}
260
[67]261void SettingsDialog::buttonFontClicked()
[42]262{
[67]263bool ok;
264QFont font = QFontDialog::getFont(&ok,this->font,this);
265        if (ok && (this->font != font)) {
266                this->font = font;
267                newFont = true;
268        }
[42]269}
270
[65]271void SettingsDialog::spinRandMinValueChanged(int val) {
272        spinRandMax->setMinimum(val);
273}
274
[17]275#ifndef Q_OS_WINCE
[9]276bool SettingsDialog::event(QEvent *ev)
277{
278        // Checking for StatusTip event and if tip text is not empty string
279        // setting it as text of the dialog hint label. Otherwise, setting
[10]280        // dialog hint label text to own status tip text.
[9]281        if (ev->type() == QEvent::StatusTip) {
282QString tip = static_cast<QStatusTipEvent *>(ev)->tip();
283                if (tip.length() != 0)
284                        labelHint->setText(tip);
285                else
286                        labelHint->setText(labelHint->statusTip());
287                return true;
288        } else
289                return QDialog::event(ev);
290}
[17]291#endif // Q_OS_WINCE
Note: See TracBrowser for help on using the repository browser.