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

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

+ Some Windows Vista and Windows 7 eyecandy with translucent windows thanks to http://labs.trolltech.com/blogs/2009/09/15/using-blur-behind-on-windows/ :-)

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