source: tspsg/src/settingsdialog.cpp @ f1fb54b9f7

0.1.3.145-beta1-symbian0.1.4.170-beta2-bb10appveyorimgbotreadme
Last change on this file since f1fb54b9f7 was f1fb54b9f7, checked in by Oleksii Serdiuk, 14 years ago

+ Added the ability to generate fractional random numbers.
+ Added "Scroll to the end of solution output after solving" option.

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