[1] | 1 | /* |
---|
[42] | 2 | * TSPSG: TSP Solver and Generator |
---|
[87] | 3 | * Copyright (C) 2007-2010 Lёppa <contacts[at]oleksii[dot]name> |
---|
[1] | 4 | * |
---|
[6] | 5 | * $Id: settingsdialog.cpp 94 2010-02-25 20:48:46Z 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] | 32 | SettingsDialog::SettingsDialog(QWidget *parent) |
---|
[92] | 33 | : QDialog(parent), _newFont(false), _newColor(false), _translucency(0) |
---|
[1] | 34 | { |
---|
| 35 | setupUi(this); |
---|
[87] | 36 | |
---|
[78] | 37 | buttonBox->button(QDialogButtonBox::Save)->setIcon(QIcon(":/images/icons/button_ok.png")); |
---|
[87] | 38 | buttonBox->button(QDialogButtonBox::Save)->setStatusTip(tr("Save new preferences")); |
---|
[78] | 39 | buttonBox->button(QDialogButtonBox::Save)->setCursor(QCursor(Qt::PointingHandCursor)); |
---|
| 40 | buttonBox->button(QDialogButtonBox::Cancel)->setIcon(QIcon(":/images/icons/button_cancel.png")); |
---|
[87] | 41 | buttonBox->button(QDialogButtonBox::Cancel)->setStatusTip(tr("Close without saving preferences")); |
---|
[78] | 42 | buttonBox->button(QDialogButtonBox::Cancel)->setCursor(QCursor(Qt::PointingHandCursor)); |
---|
| 43 | |
---|
[93] | 44 | #if defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN) |
---|
[94] | 45 | QVBoxLayout *vbox1; // Layout helper |
---|
[56] | 46 | |
---|
[94] | 47 | #ifdef Q_OS_WINCE |
---|
| 48 | // On screens with small height when SIP is shown and the window is resized |
---|
| 49 | // there is not enought space for all elements. |
---|
| 50 | // So we show scrollbars to be able to access them. |
---|
| 51 | QScrollArea *scrollArea = new QScrollArea(this); |
---|
| 52 | scrollArea->setFrameShape(QFrame::NoFrame); |
---|
| 53 | scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
---|
| 54 | scrollArea->setWidgetResizable(true); |
---|
| 55 | scrollArea->setWidget(bgWhite); |
---|
| 56 | #endif // Q_OS_WINCE |
---|
| 57 | |
---|
[85] | 58 | bgWhite->layout()->setMargin(0); |
---|
[56] | 59 | |
---|
| 60 | // Central layout |
---|
| 61 | vbox1 = new QVBoxLayout(this); |
---|
| 62 | vbox1->setMargin(0); |
---|
| 63 | vbox1->setSpacing(0); |
---|
[94] | 64 | #ifdef Q_OS_WINCE |
---|
| 65 | vbox1->addWidget(scrollArea); |
---|
| 66 | #else |
---|
[56] | 67 | vbox1->addWidget(bgWhite); |
---|
[94] | 68 | #endif // Q_OS_WINCE |
---|
[56] | 69 | vbox1->addWidget(bgGrey); |
---|
[94] | 70 | setLayout(vbox1); |
---|
[56] | 71 | #else |
---|
| 72 | // Layout helper elements |
---|
[85] | 73 | QVBoxLayout *vbox1, *vbox2; |
---|
[94] | 74 | QHBoxLayout *hbox1; |
---|
[56] | 75 | |
---|
[92] | 76 | if (QtWin::isCompositionEnabled()) { |
---|
| 77 | cbUseTranslucency = new QCheckBox(bgWhite); |
---|
| 78 | cbUseTranslucency->setObjectName("cbUseTranslucency"); |
---|
| 79 | #ifndef QT_NO_STATUSTIP |
---|
| 80 | cbUseTranslucency->setStatusTip(tr("Use translucent effect on the Main Window under Windows Vista and 7")); |
---|
| 81 | #endif // QT_NO_STATUSTIP |
---|
| 82 | cbUseTranslucency->setText(tr("Use translucency effects")); |
---|
| 83 | cbUseTranslucency->setCursor(QCursor(Qt::PointingHandCursor)); |
---|
| 84 | } |
---|
| 85 | |
---|
[56] | 86 | cbSaveState = new QCheckBox(bgWhite); |
---|
| 87 | cbSaveState->setObjectName("cbSaveState"); |
---|
| 88 | #ifndef QT_NO_STATUSTIP |
---|
[87] | 89 | cbSaveState->setStatusTip(tr("Restore main window state and position on application restart")); |
---|
[56] | 90 | #endif // QT_NO_STATUSTIP |
---|
[87] | 91 | cbSaveState->setText(tr("Save main window state and position")); |
---|
[56] | 92 | cbSaveState->setCursor(QCursor(Qt::PointingHandCursor)); |
---|
| 93 | |
---|
| 94 | imgIcon = new QLabel(this); |
---|
| 95 | imgIcon->setObjectName("imgIcon"); |
---|
[58] | 96 | imgIcon->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Expanding); |
---|
[94] | 97 | imgIcon->setFrameShape(QFrame::NoFrame); |
---|
[78] | 98 | imgIcon->setPixmap(QPixmap(":/images/icons/preferences_system.png")); |
---|
[82] | 99 | imgIcon->setStyleSheet("background-color: #0080C0; padding-top: 11px;"); |
---|
| 100 | imgIcon->setAlignment(Qt::AlignTop | Qt::AlignHCenter); |
---|
[78] | 101 | imgIcon->setMinimumWidth(150); |
---|
[56] | 102 | |
---|
| 103 | labelHint = new QLabel(bgGrey); |
---|
| 104 | labelHint->setObjectName("labelHint"); |
---|
| 105 | labelHint->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Preferred); |
---|
[58] | 106 | // labelHint->setMinimumSize(QSize(190,28)); |
---|
| 107 | labelHint->setMinimumSize(QSize(0,28)); |
---|
| 108 | labelHint->setMaximumSize(QSize(QWIDGETSIZE_MAX,28)); |
---|
[56] | 109 | labelHint->setTextFormat(Qt::PlainText); |
---|
| 110 | // labelHint->setAlignment(Qt::AlignLeft | Qt::AlignTop); |
---|
| 111 | labelHint->setWordWrap(true); |
---|
| 112 | #ifndef QT_NO_STATUSTIP |
---|
[87] | 113 | labelHint->setStatusTip(tr("Hover mouse pointer over dialog elements to get additional help")); |
---|
[56] | 114 | #endif // QT_NO_STATUSTIP |
---|
| 115 | |
---|
| 116 | lineVertical = new QFrame(this); |
---|
| 117 | lineVertical->setObjectName("lineVertical"); |
---|
| 118 | lineVertical->setFrameShadow(QFrame::Plain); |
---|
| 119 | lineVertical->setFrameShape(QFrame::VLine); |
---|
| 120 | lineVertical->setLineWidth(2); |
---|
| 121 | |
---|
| 122 | // Top line |
---|
| 123 | hbox1 = new QHBoxLayout(); |
---|
| 124 | hbox1->addWidget(imgIcon); |
---|
| 125 | hbox1->addWidget(lineVertical); |
---|
| 126 | hbox1->addWidget(bgWhite); |
---|
| 127 | |
---|
[85] | 128 | vbox1 = static_cast<QVBoxLayout *>(tabGeneral->layout()); |
---|
[87] | 129 | vbox1->insertWidget(vbox1->indexOf(cbUseNativeDialogs) + 1, cbSaveState); |
---|
[92] | 130 | #ifdef Q_OS_WIN32 |
---|
| 131 | if (QtWin::isCompositionEnabled()) |
---|
| 132 | vbox1->insertWidget(vbox1->indexOf(cbUseNativeDialogs) + 1, cbUseTranslucency); |
---|
| 133 | #endif // Q_OS_WIN32 |
---|
[56] | 134 | |
---|
[94] | 135 | // Inserting label for hints to the bottom part (with grey bg) |
---|
| 136 | buttons->insertWidget(buttons->indexOf(buttonHelp) + 1, labelHint, 1); |
---|
[56] | 137 | |
---|
| 138 | // Central layout |
---|
[85] | 139 | vbox2 = new QVBoxLayout(this); |
---|
| 140 | vbox2->setMargin(0); |
---|
| 141 | vbox2->setSpacing(0); |
---|
| 142 | vbox2->addLayout(hbox1); |
---|
| 143 | vbox2->addWidget(bgGrey); |
---|
[94] | 144 | setLayout(vbox2); |
---|
[56] | 145 | #endif // Q_OS_WINCE |
---|
[85] | 146 | |
---|
[94] | 147 | #ifdef Q_OS_WINCE |
---|
| 148 | currentGeometry = QApplication::desktop()->availableGeometry(0); |
---|
| 149 | // We need to react to SIP show/hide and resize the window appropriately |
---|
| 150 | connect(QApplication::desktop(), SIGNAL(workAreaResized(int)), SLOT(desktopResized(int))); |
---|
| 151 | #endif // Q_OS_WINCE |
---|
[1] | 152 | connect(spinRandMin,SIGNAL(valueChanged(int)),this,SLOT(spinRandMinValueChanged(int))); |
---|
[17] | 153 | connect(buttonFont,SIGNAL(clicked()),this,SLOT(buttonFontClicked())); |
---|
[21] | 154 | connect(buttonColor,SIGNAL(clicked()),this,SLOT(buttonColorClicked())); |
---|
[1] | 155 | // setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::MSWindowsFixedSizeDialogHint); |
---|
| 156 | setWindowFlags(windowFlags() ^ Qt::WindowContextHelpButtonHint); |
---|
[93] | 157 | #if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN) |
---|
[78] | 158 | // Setting initial text of dialog hint label to own status tip text. |
---|
[9] | 159 | labelHint->setText(labelHint->statusTip()); |
---|
[17] | 160 | #endif // Q_OS_WINCE |
---|
[80] | 161 | |
---|
| 162 | settings = new QSettings(QSettings::IniFormat, QSettings::UserScope, "TSPSG", "tspsg", this); |
---|
| 163 | |
---|
[82] | 164 | cbAutosize->setChecked(settings->value("Autosize", DEF_AUTOSIZE).toBool()); |
---|
| 165 | cbUseNativeDialogs->setChecked(settings->value("UseNativeDialogs", DEF_USE_NATIVE_DIALOGS).toBool()); |
---|
[92] | 166 | #ifdef Q_OS_WIN32 |
---|
| 167 | if (QtWin::isCompositionEnabled()) |
---|
| 168 | cbUseTranslucency->setChecked(settings->value("UseTranslucency", DEF_USE_TRANSLUCENCY).toBool()); |
---|
| 169 | #endif // Q_OS_WIN32 |
---|
[93] | 170 | #if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN) |
---|
[82] | 171 | cbSaveState->setChecked(settings->value("SavePos", DEF_SAVEPOS).toBool()); |
---|
[21] | 172 | #endif // Q_OS_WINCE |
---|
[78] | 173 | |
---|
[87] | 174 | settings->beginGroup("Task"); |
---|
| 175 | spinFractionalAccuracy->setValue(settings->value("FractionalAccuracy", DEF_FRACTIONAL_ACCURACY).toInt()); |
---|
| 176 | spinRandMin->setMaximum(MAX_RAND_VALUE); |
---|
| 177 | spinRandMin->setValue(settings->value("RandMin",DEF_RAND_MIN).toInt()); |
---|
| 178 | spinRandMax->setMaximum(MAX_RAND_VALUE); |
---|
| 179 | spinRandMax->setValue(settings->value("RandMax",DEF_RAND_MAX).toInt()); |
---|
| 180 | cbFractionalRandom->setChecked(settings->value("FractionalRandom", DEF_FRACTIONAL_RANDOM).toBool()); |
---|
| 181 | settings->endGroup(); |
---|
| 182 | |
---|
[42] | 183 | settings->beginGroup("Output"); |
---|
[78] | 184 | cbShowMatrix->setChecked(settings->value("ShowMatrix", DEF_SHOW_MATRIX).toBool()); |
---|
| 185 | cbCitiesLimit->setEnabled(cbShowMatrix->isChecked()); |
---|
| 186 | cbCitiesLimit->setChecked(settings->value("UseShowMatrixLimit", DEF_USE_SHOW_MATRIX_LIMIT && cbShowMatrix->isChecked()).toBool()); |
---|
| 187 | spinCitiesLimit->setEnabled(cbShowMatrix->isChecked()); |
---|
[82] | 188 | spinCitiesLimit->setValue(settings->value("ShowMatrixLimit", DEF_SHOW_MATRIX_LIMIT).toInt()); |
---|
[78] | 189 | spinCitiesLimit->setMaximum(MAX_NUM_CITIES); |
---|
[81] | 190 | cbScrollToEnd->setChecked(settings->value("ScrollToEnd", DEF_SCROLL_TO_END).toBool()); |
---|
[78] | 191 | |
---|
[21] | 192 | font = settings->value("Font",QFont(DEF_FONT_FAMILY,DEF_FONT_SIZE)).value<QFont>(); |
---|
| 193 | color = settings->value("Color",DEF_FONT_COLOR).value<QColor>(); |
---|
[42] | 194 | settings->endGroup(); |
---|
[78] | 195 | |
---|
| 196 | adjustSize(); |
---|
[42] | 197 | } |
---|
| 198 | |
---|
[67] | 199 | /*! |
---|
| 200 | * \brief Indicates whether or not the font color has been changed. |
---|
| 201 | * \return \c true if font color changed, otherwise \c false. |
---|
| 202 | */ |
---|
| 203 | bool SettingsDialog::colorChanged() const |
---|
| 204 | { |
---|
[92] | 205 | return _newColor; |
---|
[67] | 206 | } |
---|
| 207 | |
---|
| 208 | /*! |
---|
| 209 | * \brief Indicates whether or not the font properties have been changed. |
---|
| 210 | * \return \c true if font properties changed, otherwise \c false. |
---|
| 211 | */ |
---|
| 212 | bool SettingsDialog::fontChanged() const |
---|
| 213 | { |
---|
[92] | 214 | return _newFont; |
---|
[67] | 215 | } |
---|
| 216 | |
---|
[92] | 217 | /*! |
---|
[94] | 218 | * \brief Indicates whether and how the translucency setting was changed |
---|
| 219 | * \retval -1 the translucency was \em disabled. |
---|
| 220 | * \retval 0 the translucency was <em>not changed</em>. |
---|
| 221 | * \retval 1 the translucency was \em enabled. |
---|
[92] | 222 | */ |
---|
| 223 | qint8 SettingsDialog::translucencyChanged() const |
---|
| 224 | { |
---|
| 225 | return _translucency; |
---|
| 226 | } |
---|
| 227 | |
---|
[67] | 228 | /* Privates **********************************************************/ |
---|
| 229 | |
---|
[42] | 230 | void SettingsDialog::accept() |
---|
| 231 | { |
---|
[93] | 232 | #if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN) |
---|
[78] | 233 | settings->setValue("SavePos", cbSaveState->isChecked()); |
---|
[21] | 234 | #endif // Q_OS_WINCE |
---|
[92] | 235 | #ifdef Q_OS_WIN32 |
---|
| 236 | if (QtWin::isCompositionEnabled()) { |
---|
| 237 | bool old = settings->value("UseTranslucency", DEF_USE_TRANSLUCENCY).toBool(); |
---|
| 238 | if ((!old && cbUseTranslucency->isChecked()) || (old && !cbUseTranslucency->isChecked())) { |
---|
| 239 | _translucency = old ? -1 : 1; |
---|
| 240 | } else |
---|
| 241 | _translucency = 0; |
---|
| 242 | settings->setValue("UseTranslucency", cbUseTranslucency->isChecked()); |
---|
| 243 | } |
---|
| 244 | #endif // Q_OS_WIN32 |
---|
[82] | 245 | settings->setValue("UseNativeDialogs", cbUseNativeDialogs->isChecked()); |
---|
[87] | 246 | |
---|
| 247 | settings->beginGroup("Task"); |
---|
| 248 | settings->setValue("FractionalAccuracy", spinFractionalAccuracy->value()); |
---|
| 249 | settings->setValue("RandMin", spinRandMin->value()); |
---|
| 250 | settings->setValue("RandMax", spinRandMax->value()); |
---|
[81] | 251 | settings->setValue("FractionalRandom", cbFractionalRandom->isChecked()); |
---|
[87] | 252 | settings->endGroup(); |
---|
[78] | 253 | |
---|
[42] | 254 | settings->beginGroup("Output"); |
---|
[78] | 255 | settings->setValue("ShowMatrix", cbShowMatrix->isChecked()); |
---|
| 256 | settings->setValue("UseShowMatrixLimit", cbShowMatrix->isChecked() && cbCitiesLimit->isChecked()); |
---|
| 257 | if (cbCitiesLimit->isChecked()) |
---|
[82] | 258 | settings->setValue("ShowMatrixLimit", spinCitiesLimit->value()); |
---|
[81] | 259 | settings->setValue("ScrollToEnd", cbScrollToEnd->isChecked()); |
---|
[92] | 260 | if (_newFont) |
---|
[78] | 261 | settings->setValue("Font", font); |
---|
[92] | 262 | if (_newColor) |
---|
[78] | 263 | settings->setValue("Color", color); |
---|
[21] | 264 | settings->endGroup(); |
---|
[42] | 265 | QDialog::accept(); |
---|
[1] | 266 | } |
---|
| 267 | |
---|
[42] | 268 | void SettingsDialog::buttonColorClicked() |
---|
| 269 | { |
---|
| 270 | QColor color = QColorDialog::getColor(this->color,this); |
---|
| 271 | if (color.isValid() && (this->color != color)) { |
---|
| 272 | this->color = color; |
---|
[92] | 273 | _newColor = true; |
---|
[42] | 274 | } |
---|
| 275 | } |
---|
| 276 | |
---|
[67] | 277 | void SettingsDialog::buttonFontClicked() |
---|
[42] | 278 | { |
---|
[67] | 279 | bool ok; |
---|
| 280 | QFont font = QFontDialog::getFont(&ok,this->font,this); |
---|
| 281 | if (ok && (this->font != font)) { |
---|
| 282 | this->font = font; |
---|
[92] | 283 | _newFont = true; |
---|
[67] | 284 | } |
---|
[42] | 285 | } |
---|
| 286 | |
---|
[94] | 287 | #ifdef Q_OS_WINCE |
---|
| 288 | void SettingsDialog::desktopResized(int screen) |
---|
| 289 | { |
---|
| 290 | if (screen != 0) |
---|
| 291 | return; |
---|
| 292 | |
---|
| 293 | QRect availableGeometry = QApplication::desktop()->availableGeometry(0); |
---|
| 294 | if (currentGeometry != availableGeometry) { |
---|
| 295 | /*! |
---|
| 296 | * \hack HACK: This hack checks whether \link QDesktopWidget::availableGeometry() availableGeometry()\endlink's \c top + \c hegiht = \link QDesktopWidget::screenGeometry() screenGeometry()\endlink's \c height. |
---|
| 297 | * If \c true, the window gets maximized. If we used \c setGeometry() in this case, the bottom of the |
---|
| 298 | * window would end up being behind the soft buttons. Is this a bug in Qt or Windows Mobile? |
---|
| 299 | */ |
---|
| 300 | if ((availableGeometry.top() + availableGeometry.height()) == QApplication::desktop()->screenGeometry().height()) { |
---|
| 301 | setWindowState(windowState() | Qt::WindowMaximized); |
---|
| 302 | } else { |
---|
| 303 | if (windowState() & Qt::WindowMaximized) |
---|
| 304 | setWindowState(windowState() ^ Qt::WindowMaximized); |
---|
| 305 | setGeometry(availableGeometry); |
---|
| 306 | } |
---|
| 307 | } |
---|
| 308 | currentGeometry = availableGeometry; |
---|
| 309 | } |
---|
| 310 | #endif // Q_OS_WINCE |
---|
| 311 | |
---|
[65] | 312 | void SettingsDialog::spinRandMinValueChanged(int val) { |
---|
| 313 | spinRandMax->setMinimum(val); |
---|
| 314 | } |
---|
| 315 | |
---|
[93] | 316 | #if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN) |
---|
[9] | 317 | bool SettingsDialog::event(QEvent *ev) |
---|
| 318 | { |
---|
| 319 | // Checking for StatusTip event and if tip text is not empty string |
---|
| 320 | // setting it as text of the dialog hint label. Otherwise, setting |
---|
[10] | 321 | // dialog hint label text to own status tip text. |
---|
[9] | 322 | if (ev->type() == QEvent::StatusTip) { |
---|
| 323 | QString tip = static_cast<QStatusTipEvent *>(ev)->tip(); |
---|
| 324 | if (tip.length() != 0) |
---|
| 325 | labelHint->setText(tip); |
---|
| 326 | else |
---|
| 327 | labelHint->setText(labelHint->statusTip()); |
---|
| 328 | return true; |
---|
| 329 | } else |
---|
| 330 | return QDialog::event(ev); |
---|
| 331 | } |
---|
[17] | 332 | #endif // Q_OS_WINCE |
---|