1 | /* |
---|
2 | * TSPSG: TSP Solver and Generator |
---|
3 | * Copyright (C) 2007-2010 Lёppa <contacts[at]oleksii[dot]name> |
---|
4 | * |
---|
5 | * $Id: settingsdialog.cpp 104 2010-04-19 20:33:02Z 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 | */ |
---|
32 | SettingsDialog::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 | QPalette 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 |
---|
53 | QVBoxLayout *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. |
---|
59 | QScrollArea *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 |
---|
83 | QVBoxLayout *vbox1, *vbox2; |
---|
84 | QHBoxLayout *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 | 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() && cbCitiesLimit->isChecked()); |
---|
200 | spinCitiesLimit->setValue(settings->value("ShowMatrixLimit", DEF_SHOW_MATRIX_LIMIT).toInt()); |
---|
201 | spinCitiesLimit->setMaximum(MAX_NUM_CITIES); |
---|
202 | cbScrollToEnd->setChecked(settings->value("ScrollToEnd", DEF_SCROLL_TO_END).toBool()); |
---|
203 | |
---|
204 | font = settings->value("Font",QFont(DEF_FONT_FAMILY,DEF_FONT_SIZE)).value<QFont>(); |
---|
205 | color = settings->value("Colors/Text", DEF_TEXT_COLOR).value<QColor>(); |
---|
206 | settings->endGroup(); |
---|
207 | |
---|
208 | #ifndef Q_OS_WINCE_WM |
---|
209 | adjustSize(); |
---|
210 | #endif // Q_OS_WINCE_WM |
---|
211 | } |
---|
212 | |
---|
213 | /*! |
---|
214 | * \brief Indicates whether or not the font color has been changed. |
---|
215 | * \return \c true if font color changed, otherwise \c false. |
---|
216 | */ |
---|
217 | bool SettingsDialog::colorChanged() const |
---|
218 | { |
---|
219 | return _newColor; |
---|
220 | } |
---|
221 | |
---|
222 | /*! |
---|
223 | * \brief Indicates whether or not the font properties have been changed. |
---|
224 | * \return \c true if font properties changed, otherwise \c false. |
---|
225 | */ |
---|
226 | bool SettingsDialog::fontChanged() const |
---|
227 | { |
---|
228 | return _newFont; |
---|
229 | } |
---|
230 | |
---|
231 | /*! |
---|
232 | * \brief Indicates whether and how the translucency setting was changed |
---|
233 | * \retval -1 the translucency was \em disabled. |
---|
234 | * \retval 0 the translucency was <em>not changed</em>. |
---|
235 | * \retval 1 the translucency was \em enabled. |
---|
236 | */ |
---|
237 | qint8 SettingsDialog::translucencyChanged() const |
---|
238 | { |
---|
239 | return _translucency; |
---|
240 | } |
---|
241 | |
---|
242 | /* Privates **********************************************************/ |
---|
243 | |
---|
244 | void SettingsDialog::accept() |
---|
245 | { |
---|
246 | if (QApplication::keyboardModifiers() & Qt::ShiftModifier) { |
---|
247 | 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) { |
---|
248 | _newFont = (font != QFont(DEF_FONT_FAMILY, DEF_FONT_SIZE)); |
---|
249 | _newColor = (color != DEF_TEXT_COLOR); |
---|
250 | settings->remove(""); |
---|
251 | settings->setValue("SettingsReset", true); |
---|
252 | QDialog::accept(); |
---|
253 | QMessageBox::information(this, tr("Settings Reset"), tr("All settings where successfully reset to their defaults.\nIt is recommended to restart the application now.")); |
---|
254 | return; |
---|
255 | } else |
---|
256 | return; |
---|
257 | } |
---|
258 | #ifndef HANDHELD |
---|
259 | settings->setValue("SavePos", cbSaveState->isChecked()); |
---|
260 | #endif // HANDHELD |
---|
261 | #ifdef Q_OS_WIN32 |
---|
262 | if (QtWin::isCompositionEnabled()) { |
---|
263 | bool old = settings->value("UseTranslucency", DEF_USE_TRANSLUCENCY).toBool(); |
---|
264 | if ((!old && cbUseTranslucency->isChecked()) || (old && !cbUseTranslucency->isChecked())) { |
---|
265 | _translucency = old ? -1 : 1; |
---|
266 | } else |
---|
267 | _translucency = 0; |
---|
268 | settings->setValue("UseTranslucency", cbUseTranslucency->isChecked()); |
---|
269 | } |
---|
270 | #endif // Q_OS_WIN32 |
---|
271 | settings->setValue("UseNativeDialogs", cbUseNativeDialogs->isChecked()); |
---|
272 | |
---|
273 | settings->beginGroup("Task"); |
---|
274 | settings->setValue("SymmetricMode", cbSymmetricMode->isChecked()); |
---|
275 | settings->setValue("FractionalAccuracy", spinFractionalAccuracy->value()); |
---|
276 | settings->setValue("RandMin", spinRandMin->value()); |
---|
277 | settings->setValue("RandMax", spinRandMax->value()); |
---|
278 | settings->setValue("FractionalRandom", cbFractionalRandom->isChecked()); |
---|
279 | settings->endGroup(); |
---|
280 | |
---|
281 | settings->beginGroup("Output"); |
---|
282 | settings->setValue("ShowMatrix", cbShowMatrix->isChecked()); |
---|
283 | settings->setValue("UseShowMatrixLimit", cbShowMatrix->isChecked() && cbCitiesLimit->isChecked()); |
---|
284 | if (cbCitiesLimit->isChecked()) |
---|
285 | settings->setValue("ShowMatrixLimit", spinCitiesLimit->value()); |
---|
286 | settings->setValue("ScrollToEnd", cbScrollToEnd->isChecked()); |
---|
287 | if (_newFont) |
---|
288 | settings->setValue("Font", font); |
---|
289 | if (_newColor) |
---|
290 | settings->setValue("Colors/Text", color); |
---|
291 | settings->endGroup(); |
---|
292 | QDialog::accept(); |
---|
293 | } |
---|
294 | |
---|
295 | void SettingsDialog::buttonColorClicked() |
---|
296 | { |
---|
297 | QColor color = QColorDialog::getColor(this->color,this); |
---|
298 | if (color.isValid() && (this->color != color)) { |
---|
299 | this->color = color; |
---|
300 | _newColor = true; |
---|
301 | } |
---|
302 | } |
---|
303 | |
---|
304 | void SettingsDialog::buttonFontClicked() |
---|
305 | { |
---|
306 | bool ok; |
---|
307 | QFont font = QFontDialog::getFont(&ok,this->font,this); |
---|
308 | if (ok && (this->font != font)) { |
---|
309 | this->font = font; |
---|
310 | _newFont = true; |
---|
311 | } |
---|
312 | } |
---|
313 | |
---|
314 | #ifdef Q_OS_WINCE_WM |
---|
315 | void SettingsDialog::desktopResized(int screen) |
---|
316 | { |
---|
317 | if (screen != 0) |
---|
318 | return; |
---|
319 | |
---|
320 | QRect availableGeometry = QApplication::desktop()->availableGeometry(0); |
---|
321 | if (currentGeometry != availableGeometry) { |
---|
322 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
323 | /*! |
---|
324 | * \hack HACK: This hack checks whether \link QDesktopWidget::availableGeometry() availableGeometry()\endlink's \c top + \c hegiht = \link QDesktopWidget::screenGeometry() screenGeometry()\endlink's \c height. |
---|
325 | * If \c true, the window gets maximized. If we used \c setGeometry() in this case, the bottom of the |
---|
326 | * window would end up being behind the soft buttons. Is this a bug in Qt or Windows Mobile? |
---|
327 | */ |
---|
328 | if ((availableGeometry.top() + availableGeometry.height()) == QApplication::desktop()->screenGeometry().height()) { |
---|
329 | setWindowState(windowState() | Qt::WindowMaximized); |
---|
330 | } else { |
---|
331 | if (windowState() & Qt::WindowMaximized) |
---|
332 | setWindowState(windowState() ^ Qt::WindowMaximized); |
---|
333 | setGeometry(availableGeometry); |
---|
334 | } |
---|
335 | currentGeometry = availableGeometry; |
---|
336 | QApplication::restoreOverrideCursor(); |
---|
337 | } |
---|
338 | } |
---|
339 | |
---|
340 | void SettingsDialog::showEvent(QShowEvent *ev) |
---|
341 | { |
---|
342 | desktopResized(0); |
---|
343 | |
---|
344 | QWidget::showEvent(ev); |
---|
345 | } |
---|
346 | #endif // Q_OS_WINCE_WM |
---|
347 | |
---|
348 | void SettingsDialog::spinRandMinValueChanged(int val) { |
---|
349 | spinRandMax->setMinimum(val); |
---|
350 | } |
---|
351 | |
---|
352 | #ifndef HANDHELD |
---|
353 | bool SettingsDialog::event(QEvent *ev) |
---|
354 | { |
---|
355 | // Checking for StatusTip event and if tip text is not empty string |
---|
356 | // setting it as text of the dialog hint label. Otherwise, setting |
---|
357 | // dialog hint label text to own status tip text. |
---|
358 | if (ev->type() == QEvent::StatusTip) { |
---|
359 | QString tip = static_cast<QStatusTipEvent *>(ev)->tip(); |
---|
360 | if (tip.length() != 0) |
---|
361 | labelHint->setText(tip); |
---|
362 | else |
---|
363 | labelHint->setText(labelHint->statusTip()); |
---|
364 | return true; |
---|
365 | } else |
---|
366 | return QDialog::event(ev); |
---|
367 | } |
---|
368 | #endif // HANDHELD |
---|