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