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

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

+ Added SStep::next that indicates what branch was selected for the next step.
+ Added "Show solution graph" option.
+ New CTSPSolver::getTotalSteps() method that returns a total number of steps in the current solution.

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