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