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