Debian Codegraph @

src/Gui/Dialogs/DlgSettingsColorGradientImp.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2005 Werner Mayer <wmayer[at]users.sourceforge.net> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23 #include <cmath>
24 #include <limits>
25 #include <QButtonGroup>
26 #include <QDoubleValidator>
27 #include <QFontMetrics>
28 #include <QLocale>
29 #include <QMessageBox>
30
31 #include "Dialogs/DlgSettingsColorGradientImp.h"
32 #include "ui_DlgSettingsColorGradient.h"
33 #include "SpinBox.h"
34 #include "Tools.h"
35
36
37 using namespace Gui::Dialog;
38
39 /* TRANSLATOR Gui::Dialog::DlgSettingsColorGradientImp */
40
41 /**
42 * Constructs a DlgSettingsColorGradientImp as a child of 'parent', with the
43 * name 'name' and widget flags set to 'f'.
44 */
45 DlgSettingsColorGradientImp::DlgSettingsColorGradientImp(
46 const App::ColorGradient& cg,
47 QWidget* parent,
48 Qt::WindowFlags fl
49 )
50 : QDialog(parent, fl)
51 , validator(nullptr)
52 , ui(new Ui_DlgSettingsColorGradient)
53 {
54 ui->setupUi(this);
55 ui->spinBoxLabel->setRange(5, 30);
56 ui->spinBoxDecimals->setMaximum(std::numeric_limits<float>::digits10);
57
58 // remove the automatic help button in dialog title since we don't use it
59 setWindowFlag(Qt::WindowContextHelpButtonHint, false);
60
61 // the elementary charge is 1.6e-19, since such values might be the result of
62 // simulations, use this as boundary for a scientific validator
63 validator = new QDoubleValidator(-2e19, 2e19, ui->spinBoxDecimals->maximum(), this);
64 validator->setNotation(QDoubleValidator::ScientificNotation);
65 ui->floatLineEditMax->setValidator(validator);
66 ui->floatLineEditMin->setValidator(validator);
67
68 // assure that the LineEdit is as wide to contain numbers with 4 digits and 6 decimals
69 QFontMetrics fm(ui->floatLineEditMax->font());
70 ui->floatLineEditMax->setMinimumWidth(
71 QtTools::horizontalAdvance(fm, QStringLiteral("-8000.000000"))
72 );
73
74 setColorModelNames(cg.getColorModelNames());
75 setProfile(cg.getProfile());
76 setupConnections();
77 }
78
79 /**
80 * Destroys the object and frees any allocated resources
81 */
82 DlgSettingsColorGradientImp::~DlgSettingsColorGradientImp() = default;
83
84 void DlgSettingsColorGradientImp::setupConnections()
85 {
86 // clang-format off
87 auto group = new QButtonGroup(this);
88 group->setExclusive(true);
89 group->addButton(ui->radioButtonFlow);
90 group->addButton(ui->radioButtonZero);
91
92 connect(group, &QButtonGroup::idClicked,
93 this, &DlgSettingsColorGradientImp::colorModelChanged);
94
95 connect(ui->comboBoxModel, qOverload<int>(&QComboBox::currentIndexChanged),
96 this, &DlgSettingsColorGradientImp::colorModelChanged);
97
98 connect(ui->checkBoxGrayed, &QCheckBox::toggled,
99 this, &DlgSettingsColorGradientImp::colorModelChanged);
100
101 connect(ui->checkBoxInvisible, &QCheckBox::toggled,
102 this, &DlgSettingsColorGradientImp::colorModelChanged);
103
104 connect(ui->floatLineEditMax, &QLineEdit::editingFinished,
105 this, &DlgSettingsColorGradientImp::colorModelChanged);
106
107 connect(ui->floatLineEditMin, &QLineEdit::editingFinished,
108 this, &DlgSettingsColorGradientImp::colorModelChanged);
109
110 connect(ui->spinBoxLabel, qOverload<int>(&QSpinBox::valueChanged),
111 this, &DlgSettingsColorGradientImp::colorModelChanged);
112
113 connect(ui->spinBoxDecimals, qOverload<int>(&QSpinBox::valueChanged),
114 this, &DlgSettingsColorGradientImp::colorModelChanged);
115 // clang-format on
116 }
117
118 App::ColorGradientProfile DlgSettingsColorGradientImp::getProfile() const
119 {
120 App::ColorGradientProfile profile;
121 profile.tColorModel = colorModel();
122 profile.tStyle = colorStyle();
123 profile.visibility.setFlag(App::Visibility::Grayed, isOutGrayed());
124 profile.visibility.setFlag(App::Visibility::Invisible, isOutInvisible());
125 profile.ctColors = numberOfLabels();
126 getRange(profile.fMin, profile.fMax);
127 return profile;
128 }
129
130 void DlgSettingsColorGradientImp::setProfile(const App::ColorGradientProfile& pro)
131 {
132 setColorModel(pro.tColorModel);
133 setColorStyle(pro.tStyle);
134 setOutGrayed(pro.visibility.testFlag(App::Visibility::Grayed));
135 setOutInvisible(pro.visibility.testFlag(App::Visibility::Invisible));
136 setNumberOfLabels(pro.ctColors);
137 setRange(pro.fMin, pro.fMax);
138 }
139
140 void DlgSettingsColorGradientImp::setColorModel(std::size_t index)
141 {
142 ui->comboBoxModel->setCurrentIndex(index);
143 }
144
145 std::size_t DlgSettingsColorGradientImp::colorModel() const
146 {
147 return static_cast<std::size_t>(ui->comboBoxModel->currentIndex());
148 }
149
150 void DlgSettingsColorGradientImp::setColorModelNames(const std::vector<std::string>& names)
151 {
152 ui->comboBoxModel->clear();
153 for (const auto& it : names) {
154 ui->comboBoxModel->addItem(QString::fromStdString(it));
155 }
156 }
157
158 void DlgSettingsColorGradientImp::setColorStyle(App::ColorBarStyle tStyle)
159 {
160 switch (tStyle) {
161 case App::ColorBarStyle::FLOW:
162 ui->radioButtonFlow->setChecked(true);
163 break;
164 case App::ColorBarStyle::ZERO_BASED:
165 ui->radioButtonZero->setChecked(true);
166 break;
167 }
168 }
169
170 App::ColorBarStyle DlgSettingsColorGradientImp::colorStyle() const
171 {
172 return ui->radioButtonZero->isChecked() ? App::ColorBarStyle::ZERO_BASED
173 : App::ColorBarStyle::FLOW;
174 }
175
176 void DlgSettingsColorGradientImp::setOutGrayed(bool grayed)
177 {
178 ui->checkBoxGrayed->setChecked(grayed);
179 }
180
181 bool DlgSettingsColorGradientImp::isOutGrayed() const
182 {
183 return ui->checkBoxGrayed->isChecked();
184 }
185
186 void DlgSettingsColorGradientImp::setOutInvisible(bool invisible)
187 {
188 ui->checkBoxInvisible->setChecked(invisible);
189 }
190
191 bool DlgSettingsColorGradientImp::isOutInvisible() const
192 {
193 return ui->checkBoxInvisible->isChecked();
194 }
195
196 void DlgSettingsColorGradientImp::setRange(float fMin, float fMax)
197 {
198 auto toString = [this](float value, int decimals) {
199 int pos = 0;
200 while (decimals > 0) {
201 QString str = QLocale().toString(value, 'g', decimals--);
202 if (validator->validate(str, pos) == QValidator::Acceptable) {
203 return str;
204 }
205 }
206
207 return QLocale().toString(value, 'g', 1);
208 };
209
210 ui->floatLineEditMax->blockSignals(true);
211 ui->floatLineEditMax->setText(toString(fMax, numberOfDecimals()));
212 ui->floatLineEditMax->blockSignals(false);
213
214 ui->floatLineEditMin->blockSignals(true);
215 ui->floatLineEditMin->setText(toString(fMin, numberOfDecimals()));
216 ui->floatLineEditMin->blockSignals(false);
217 }
218
219 void DlgSettingsColorGradientImp::getRange(float& fMin, float& fMax) const
220 {
221 fMax = QLocale().toFloat(ui->floatLineEditMax->text());
222 fMin = QLocale().toFloat(ui->floatLineEditMin->text());
223 }
224
225 void DlgSettingsColorGradientImp::setNumberOfLabels(int val)
226 {
227 ui->spinBoxLabel->setValue(val);
228 }
229
230 int DlgSettingsColorGradientImp::numberOfLabels() const
231 {
232 return ui->spinBoxLabel->value();
233 }
234
235 void DlgSettingsColorGradientImp::setNumberOfDecimals(int val, float fMin, float fMax)
236 {
237 ui->spinBoxDecimals->setValue(val);
238 setRange(fMin, fMax);
239 }
240
241 int DlgSettingsColorGradientImp::numberOfDecimals() const
242 {
243 return ui->spinBoxDecimals->value();
244 }
245
246 void DlgSettingsColorGradientImp::accept()
247 {
248 double fMax = QLocale().toDouble(ui->floatLineEditMax->text());
249 double fMin = QLocale().toDouble(ui->floatLineEditMin->text());
250
251 if (fMax <= fMin) {
252 QMessageBox::warning(
253 this,
254 tr("Wrong parameter"),
255 tr("The maximum value must be higher than the minimum value.")
256 );
257 }
258 else {
259 QDialog::accept();
260 }
261 }
262
263 #include "moc_DlgSettingsColorGradientImp.cpp"