[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 65 2009-10-20 19:38:01Z 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 |
---|
| 37 | #ifdef Q_OS_WINCE |
---|
| 38 | // Layout helper elements |
---|
| 39 | QVBoxLayout *vbox1, *vbox2; |
---|
| 40 | QHBoxLayout *hbox1, *hbox2; |
---|
| 41 | QSpacerItem *spacer; |
---|
| 42 | |
---|
| 43 | labelRandMin->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Preferred); |
---|
| 44 | labelRandMax->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Preferred); |
---|
| 45 | |
---|
| 46 | // Top part (with white bg) |
---|
| 47 | vbox2 = new QVBoxLayout(bgWhite); |
---|
| 48 | vbox2->addWidget(groupRandomSettings); |
---|
| 49 | vbox2->addWidget(groupOutputSettings); |
---|
| 50 | spacer = new QSpacerItem(0,0,QSizePolicy::Minimum,QSizePolicy::Expanding); |
---|
| 51 | vbox2->addItem(spacer); |
---|
[57] | 52 | vbox2->addWidget(cbAutosize); |
---|
[56] | 53 | |
---|
| 54 | // Output settings group |
---|
| 55 | hbox1 = new QHBoxLayout(groupOutputSettings); |
---|
| 56 | hbox1->addWidget(buttonFont); |
---|
| 57 | hbox1->addWidget(buttonColor); |
---|
| 58 | |
---|
| 59 | // Bottom part (with grey bg) |
---|
| 60 | hbox2 = new QHBoxLayout(bgGrey); |
---|
| 61 | hbox2->setMargin(6); |
---|
| 62 | hbox2->setSpacing(6); |
---|
| 63 | hbox2->addWidget(buttonHelp); |
---|
| 64 | spacer = new QSpacerItem(0,0,QSizePolicy::Expanding); |
---|
| 65 | hbox2->addItem(spacer); |
---|
| 66 | hbox2->addWidget(buttonOK); |
---|
| 67 | hbox2->addWidget(buttonCancel); |
---|
| 68 | |
---|
| 69 | // Central layout |
---|
| 70 | vbox1 = new QVBoxLayout(this); |
---|
| 71 | vbox1->setMargin(0); |
---|
| 72 | vbox1->setSpacing(0); |
---|
| 73 | vbox1->addWidget(bgWhite); |
---|
| 74 | vbox1->addWidget(lineHorizontal); |
---|
| 75 | vbox1->addWidget(bgGrey); |
---|
| 76 | #else |
---|
| 77 | // Layout helper elements |
---|
| 78 | QVBoxLayout *vbox1, *vbox2, *vbox3; |
---|
| 79 | QHBoxLayout *hbox1, *hbox2, *hbox3; |
---|
| 80 | QSpacerItem *spacer; |
---|
| 81 | |
---|
| 82 | cbSaveState = new QCheckBox(bgWhite); |
---|
| 83 | cbSaveState->setObjectName("cbSaveState"); |
---|
| 84 | #ifndef QT_NO_STATUSTIP |
---|
| 85 | cbSaveState->setStatusTip(trUtf8("Restore main window state and position on application restart")); |
---|
| 86 | #endif // QT_NO_STATUSTIP |
---|
| 87 | cbSaveState->setText(trUtf8("Save main window state and position")); |
---|
| 88 | cbSaveState->setCursor(QCursor(Qt::PointingHandCursor)); |
---|
| 89 | |
---|
| 90 | imgIcon = new QLabel(this); |
---|
| 91 | imgIcon->setObjectName("imgIcon"); |
---|
[58] | 92 | imgIcon->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Expanding); |
---|
| 93 | imgIcon->setFrameShape(QFrame::Panel); |
---|
[56] | 94 | imgIcon->setLineWidth(0); |
---|
| 95 | imgIcon->setPixmap(QPixmap(QString::fromUtf8(":/images/icons/preferences_system.png"))); |
---|
| 96 | imgIcon->setStyleSheet("background-color: #0080C0;"); |
---|
| 97 | imgIcon->setAlignment(Qt::AlignCenter); |
---|
| 98 | |
---|
| 99 | labelHint = new QLabel(bgGrey); |
---|
| 100 | labelHint->setObjectName("labelHint"); |
---|
| 101 | labelHint->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Preferred); |
---|
[58] | 102 | // labelHint->setMinimumSize(QSize(190,28)); |
---|
| 103 | labelHint->setMinimumSize(QSize(0,28)); |
---|
| 104 | labelHint->setMaximumSize(QSize(QWIDGETSIZE_MAX,28)); |
---|
[56] | 105 | labelHint->setTextFormat(Qt::PlainText); |
---|
| 106 | // labelHint->setAlignment(Qt::AlignLeft | Qt::AlignTop); |
---|
| 107 | labelHint->setWordWrap(true); |
---|
| 108 | #ifndef QT_NO_STATUSTIP |
---|
| 109 | labelHint->setStatusTip(trUtf8("Hover mouse pointer over dialog elements to get additional help")); |
---|
| 110 | #endif // QT_NO_STATUSTIP |
---|
| 111 | |
---|
| 112 | lineVertical = new QFrame(this); |
---|
| 113 | lineVertical->setObjectName("lineVertical"); |
---|
| 114 | lineVertical->setFrameShadow(QFrame::Plain); |
---|
| 115 | lineVertical->setFrameShape(QFrame::VLine); |
---|
| 116 | lineVertical->setLineWidth(2); |
---|
| 117 | |
---|
| 118 | // Top line |
---|
| 119 | hbox1 = new QHBoxLayout(); |
---|
| 120 | hbox1->addWidget(imgIcon); |
---|
| 121 | hbox1->addWidget(lineVertical); |
---|
| 122 | hbox1->addWidget(bgWhite); |
---|
| 123 | |
---|
| 124 | // Output settings group |
---|
| 125 | vbox3 = new QVBoxLayout(groupOutputSettings); |
---|
| 126 | vbox3->addWidget(buttonFont); |
---|
| 127 | vbox3->addWidget(buttonColor); |
---|
| 128 | |
---|
| 129 | // Random and Output settings groups |
---|
| 130 | hbox2 = new QHBoxLayout(); |
---|
| 131 | hbox2->addWidget(groupRandomSettings); |
---|
| 132 | hbox2->addWidget(groupOutputSettings); |
---|
| 133 | spacer = new QSpacerItem(0,0,QSizePolicy::Expanding); |
---|
| 134 | hbox2->addItem(spacer); |
---|
| 135 | |
---|
| 136 | // Top right part (with white bg) |
---|
| 137 | vbox2 = new QVBoxLayout(bgWhite); |
---|
| 138 | spacer = new QSpacerItem(0,0,QSizePolicy::Minimum,QSizePolicy::Expanding); |
---|
| 139 | vbox2->addItem(spacer); |
---|
| 140 | vbox2->addLayout(hbox2); |
---|
[57] | 141 | vbox2->addWidget(cbAutosize); |
---|
[56] | 142 | vbox2->addWidget(cbSaveState); |
---|
| 143 | spacer = new QSpacerItem(0,0,QSizePolicy::Minimum,QSizePolicy::Expanding); |
---|
| 144 | vbox2->addItem(spacer); |
---|
| 145 | |
---|
| 146 | // Bottom part (with grey bg) |
---|
| 147 | hbox3 = new QHBoxLayout(bgGrey); |
---|
| 148 | hbox3->setMargin(6); |
---|
| 149 | hbox3->setSpacing(6); |
---|
| 150 | hbox3->addWidget(buttonHelp); |
---|
| 151 | hbox3->addWidget(labelHint); |
---|
| 152 | hbox3->addWidget(buttonOK); |
---|
| 153 | hbox3->addWidget(buttonCancel); |
---|
| 154 | |
---|
| 155 | // Central layout |
---|
| 156 | vbox1 = new QVBoxLayout(this); |
---|
| 157 | vbox1->setMargin(0); |
---|
| 158 | vbox1->setSpacing(0); |
---|
| 159 | vbox1->addLayout(hbox1); |
---|
| 160 | vbox1->addWidget(lineHorizontal); |
---|
| 161 | vbox1->addWidget(bgGrey); |
---|
| 162 | #endif // Q_OS_WINCE |
---|
[1] | 163 | connect(buttonOK,SIGNAL(clicked()),this,SLOT(accept())); |
---|
| 164 | connect(buttonCancel,SIGNAL(clicked()),this,SLOT(reject())); |
---|
| 165 | connect(spinRandMin,SIGNAL(valueChanged(int)),this,SLOT(spinRandMinValueChanged(int))); |
---|
[17] | 166 | connect(buttonFont,SIGNAL(clicked()),this,SLOT(buttonFontClicked())); |
---|
[21] | 167 | connect(buttonColor,SIGNAL(clicked()),this,SLOT(buttonColorClicked())); |
---|
[1] | 168 | // setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::MSWindowsFixedSizeDialogHint); |
---|
| 169 | setWindowFlags(windowFlags() ^ Qt::WindowContextHelpButtonHint); |
---|
[17] | 170 | #ifndef Q_OS_WINCE |
---|
[10] | 171 | // Setting initial text of dialog hint label to own status tip |
---|
| 172 | // text. |
---|
[9] | 173 | labelHint->setText(labelHint->statusTip()); |
---|
[17] | 174 | #endif // Q_OS_WINCE |
---|
[23] | 175 | settings = new QSettings(QSettings::IniFormat,QSettings::UserScope,"TSPSG","tspsg"); |
---|
[57] | 176 | spinRandMin->setMaximum(MAX_RAND_VALUE); |
---|
[21] | 177 | spinRandMin->setValue(settings->value("MinCost",DEF_RAND_MIN).toInt()); |
---|
[57] | 178 | spinRandMax->setMaximum(MAX_RAND_VALUE); |
---|
[21] | 179 | spinRandMax->setValue(settings->value("MaxCost",DEF_RAND_MAX).toInt()); |
---|
[57] | 180 | cbAutosize->setChecked(settings->value("Autosize",true).toBool()); |
---|
[21] | 181 | #ifndef Q_OS_WINCE |
---|
| 182 | cbSaveState->setChecked(settings->value("SavePos",false).toBool()); |
---|
| 183 | #endif // Q_OS_WINCE |
---|
[42] | 184 | settings->beginGroup("Output"); |
---|
[21] | 185 | font = settings->value("Font",QFont(DEF_FONT_FAMILY,DEF_FONT_SIZE)).value<QFont>(); |
---|
| 186 | color = settings->value("Color",DEF_FONT_COLOR).value<QColor>(); |
---|
[42] | 187 | settings->endGroup(); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | void SettingsDialog::accept() |
---|
| 191 | { |
---|
[21] | 192 | #ifndef Q_OS_WINCE |
---|
[42] | 193 | settings->setValue("SavePos",cbSaveState->isChecked()); |
---|
[21] | 194 | #endif // Q_OS_WINCE |
---|
[57] | 195 | settings->setValue("Autosize",cbAutosize->isChecked()); |
---|
[42] | 196 | settings->setValue("MinCost",spinRandMin->value()); |
---|
| 197 | settings->setValue("MaxCost",spinRandMax->value()); |
---|
| 198 | settings->beginGroup("Output"); |
---|
| 199 | if (newFont) |
---|
| 200 | settings->setValue("Font",font); |
---|
| 201 | if (newColor) |
---|
| 202 | settings->setValue("Color",color); |
---|
[21] | 203 | settings->endGroup(); |
---|
[42] | 204 | QDialog::accept(); |
---|
[1] | 205 | } |
---|
| 206 | |
---|
[42] | 207 | void SettingsDialog::buttonFontClicked() |
---|
| 208 | { |
---|
| 209 | bool ok; |
---|
| 210 | QFont font = QFontDialog::getFont(&ok,this->font,this); |
---|
| 211 | if (ok && (this->font != font)) { |
---|
| 212 | this->font = font; |
---|
| 213 | newFont = true; |
---|
| 214 | } |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | void SettingsDialog::buttonColorClicked() |
---|
| 218 | { |
---|
| 219 | QColor color = QColorDialog::getColor(this->color,this); |
---|
| 220 | if (color.isValid() && (this->color != color)) { |
---|
| 221 | this->color = color; |
---|
| 222 | newColor = true; |
---|
| 223 | } |
---|
| 224 | } |
---|
| 225 | |
---|
[65] | 226 | /*! |
---|
| 227 | * \brief Indicates whether or not the font color has been changed. |
---|
| 228 | */ |
---|
[42] | 229 | bool SettingsDialog::colorChanged() const |
---|
| 230 | { |
---|
| 231 | return newColor; |
---|
| 232 | } |
---|
| 233 | |
---|
[65] | 234 | /*! |
---|
| 235 | * \brief Indicates whether or not the font properties have been changed. |
---|
| 236 | */ |
---|
[42] | 237 | bool SettingsDialog::fontChanged() const |
---|
| 238 | { |
---|
| 239 | return newFont; |
---|
| 240 | } |
---|
| 241 | |
---|
[65] | 242 | void SettingsDialog::spinRandMinValueChanged(int val) { |
---|
| 243 | spinRandMax->setMinimum(val); |
---|
| 244 | } |
---|
| 245 | |
---|
[17] | 246 | #ifndef Q_OS_WINCE |
---|
[9] | 247 | bool SettingsDialog::event(QEvent *ev) |
---|
| 248 | { |
---|
| 249 | // Checking for StatusTip event and if tip text is not empty string |
---|
| 250 | // setting it as text of the dialog hint label. Otherwise, setting |
---|
[10] | 251 | // dialog hint label text to own status tip text. |
---|
[9] | 252 | if (ev->type() == QEvent::StatusTip) { |
---|
| 253 | QString tip = static_cast<QStatusTipEvent *>(ev)->tip(); |
---|
| 254 | if (tip.length() != 0) |
---|
| 255 | labelHint->setText(tip); |
---|
| 256 | else |
---|
| 257 | labelHint->setText(labelHint->statusTip()); |
---|
| 258 | return true; |
---|
[58] | 259 | // Making imgIcon square. |
---|
| 260 | } else if (ev->type() == QEvent::Show) { |
---|
| 261 | bool result = QDialog::event(ev); |
---|
| 262 | if (result) |
---|
| 263 | imgIcon->setMinimumWidth(imgIcon->height()); |
---|
| 264 | return result; |
---|
[9] | 265 | } else |
---|
| 266 | return QDialog::event(ev); |
---|
| 267 | } |
---|
[17] | 268 | #endif // Q_OS_WINCE |
---|