Debian Codegraph @

src/Gui/Dialogs/DlgSettingsImageImp.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 <QRegularExpression>
24 #include <QRegularExpressionMatch>
25
26 #include "Dialogs/DlgSettingsImageImp.h"
27 #include "ui_DlgSettingsImage.h"
28
29
30 using namespace Gui::Dialog;
31 using namespace std;
32
33 /* TRANSLATOR Gui::Dialog::DlgSettingsImageImp */
34
35 /**
36 * Constructs a DlgSettingsImageImp as a child of 'parent', with the
37 * name 'name' and widget flags set to 'f'.
38 */
39 DlgSettingsImageImp::DlgSettingsImageImp(QWidget* parent)
40 : QWidget(parent)
41 , ui(new Ui_DlgSettingsImage)
42 {
43 ui->setupUi(this);
44 setupConnections();
45
46 SbVec2s res = SoOffscreenRenderer::getMaximumResolution();
47 ui->spinWidth->setMaximum((int)res[0]);
48 ui->spinHeight->setMaximum((int)res[1]);
49
50 _width = width();
51 _height = height();
52 _fRatio = (float)_width / (float)_height;
53
54 ui->comboMethod->addItem(tr("Offscreen (new)"), QByteArray("QtOffscreenRenderer"));
55 ui->comboMethod->addItem(tr("Offscreen (old)"), QByteArray("CoinOffscreenRenderer"));
56 ui->comboMethod->addItem(tr("Framebuffer (custom)"), QByteArray("FramebufferObject"));
57 ui->comboMethod->addItem(tr("Framebuffer (as is)"), QByteArray("GrabFramebuffer"));
58 }
59
60 /**
61 * Destroys the object and frees any allocated resources
62 */
63 DlgSettingsImageImp::~DlgSettingsImageImp() = default;
64
65 void DlgSettingsImageImp::setupConnections()
66 {
67 // clang-format off
68 connect(ui->buttonRatioScreen, &QToolButton::clicked,
69 this, &DlgSettingsImageImp::onButtonRatioScreenClicked);
70 connect(ui->buttonRatio4x3, &QToolButton::clicked,
71 this, &DlgSettingsImageImp::onButtonRatio4x3Clicked);
72 connect(ui->buttonRatio16x9, &QToolButton::clicked,
73 this, &DlgSettingsImageImp::onButtonRatio16x9Clicked);
74 connect(ui->buttonRatio1x1, &QToolButton::clicked,
75 this, &DlgSettingsImageImp::onButtonRatio1x1Clicked);
76 connect(ui->standardSizeBox, qOverload<int>(&QComboBox::activated),
77 this, &DlgSettingsImageImp::onStandardSizeBoxActivated);
78 connect(ui->comboMethod, qOverload<int>(&QComboBox::activated),
79 this, &DlgSettingsImageImp::onComboMethodActivated);
80 // clang-format on
81 }
82
83 void DlgSettingsImageImp::changeEvent(QEvent* e)
84 {
85 if (e->type() == QEvent::LanguageChange) {
86 ui->retranslateUi(this);
87 }
88 QWidget::changeEvent(e);
89 }
90
91 /**
92 * Sets the image size to (\a w, \a h).
93 */
94 void DlgSettingsImageImp::setImageSize(int w, int h)
95 {
96 // set current screen size
97 ui->standardSizeBox->setItemData(0, QSize(w, h));
98
99 ui->spinWidth->setValue(w);
100 ui->spinHeight->setValue(h);
101
102 // As the image size is in pixel why shouldn't _width and _height be integers?
103 _width = w;
104 _height = h;
105 _fRatio = (float)_width / (float)_height;
106 }
107
108 /**
109 * Sets the image size to \a s.
110 */
111 void DlgSettingsImageImp::setImageSize(const QSize& s)
112 {
113 // set current screen size
114 ui->standardSizeBox->setItemData(0, s);
115
116 ui->spinWidth->setValue(s.width());
117 ui->spinHeight->setValue(s.height());
118
119 // As the image size is in pixel why shouldn't _width and _height be integers?
120 _width = s.width();
121 _height = s.height();
122 _fRatio = (float)_width / (float)_height;
123 }
124
125 /**
126 * Returns the currently set image size.
127 */
128 QSize DlgSettingsImageImp::imageSize() const
129 {
130 return {ui->spinWidth->value(), ui->spinHeight->value()};
131 }
132
133 /**
134 * Returns the currently set image width.
135 */
136 int DlgSettingsImageImp::imageWidth() const
137 {
138 return ui->spinWidth->value();
139 }
140
141 /**
142 * Returns the currently set image height.
143 */
144 int DlgSettingsImageImp::imageHeight() const
145 {
146 return ui->spinHeight->value();
147 }
148
149 /**
150 * Returns the comment of the picture. If for the currently selected image format no comments are
151 * supported QString() is returned.
152 */
153 QString DlgSettingsImageImp::comment() const
154 {
155 if (!ui->textEditComment->isEnabled()) {
156 return {};
157 }
158 else {
159 return ui->textEditComment->toPlainText();
160 }
161 }
162
163 int DlgSettingsImageImp::backgroundType() const
164 {
165 return ui->comboBackground->currentIndex();
166 }
167
168 /**
169 * Sets the image size to (\a w, \a h).
170 */
171 void DlgSettingsImageImp::setBackgroundType(int t)
172 {
173 if (t < ui->comboBackground->count()) {
174 ui->comboBackground->setCurrentIndex(t);
175 }
176 }
177
178 bool DlgSettingsImageImp::addWatermark() const
179 {
180 return ui->checkWatermark->isChecked();
181 }
182
183 void DlgSettingsImageImp::onSelectedFilter(const QString& filter)
184 {
185 bool ok
186 = (filter.startsWith(QLatin1String("JPG")) || filter.startsWith(QLatin1String("JPEG"))
187 || filter.startsWith(QLatin1String("PNG")));
188 ui->buttonGroupComment->setEnabled(ok);
189 }
190
191 void DlgSettingsImageImp::adjustImageSize(float fRatio)
192 {
193 // if width has changed then adjust height and vice versa, if both has changed then adjust width
194 if (_height != ui->spinHeight->value()) {
195 _height = ui->spinHeight->value();
196 _width = (int)((float)_height * fRatio);
197 ui->spinWidth->setValue((int)_width);
198 }
199 else // if( _width != spinWidth->value() )
200 {
201 _width = ui->spinWidth->value();
202 _height = (int)((float)_width / fRatio);
203 ui->spinHeight->setValue((int)_height);
204 }
205 }
206
207 void DlgSettingsImageImp::onButtonRatioScreenClicked()
208 {
209 adjustImageSize(_fRatio);
210 }
211
212 void DlgSettingsImageImp::onButtonRatio4x3Clicked()
213 {
214 adjustImageSize(4.0f / 3.0f);
215 }
216
217 void DlgSettingsImageImp::onButtonRatio16x9Clicked()
218 {
219 adjustImageSize(16.0f / 9.0f);
220 }
221
222 void DlgSettingsImageImp::onButtonRatio1x1Clicked()
223 {
224 adjustImageSize(1.0f);
225 }
226
227 void DlgSettingsImageImp::onStandardSizeBoxActivated(int index)
228 {
229 if (index == 0) {
230 // we have set the user data for the 1st item
231 QSize s = ui->standardSizeBox->itemData(0).toSize();
232 ui->spinWidth->setValue(s.width());
233 ui->spinHeight->setValue(s.height());
234 }
235 else {
236 // try to extract from the string
237 QString text = ui->standardSizeBox->itemText(index);
238 QRegularExpression rx(QLatin1String(R"(\b\d{2,5}\b)"));
239 int pos = 0;
240 auto match = rx.match(text, pos);
241 if (match.hasMatch()) {
242 pos = match.capturedStart();
243 QString width = text.mid(pos, match.capturedLength());
244 ui->spinWidth->setValue(width.toInt());
245 pos += match.capturedLength();
246 }
247
248 match = rx.match(text, pos);
249 if (match.hasMatch()) {
250 pos = match.capturedStart();
251 QString height = text.mid(pos, match.capturedLength());
252 ui->spinHeight->setValue(height.toInt());
253 }
254 }
255 }
256
257 void DlgSettingsImageImp::setMethod(const QByteArray& m)
258 {
259 int index = ui->comboMethod->findData(m);
260 if (index >= 0) {
261 ui->comboMethod->setCurrentIndex(index);
262 }
263 }
264
265 QByteArray DlgSettingsImageImp::method() const
266 {
267 return ui->comboMethod->currentData().toByteArray();
268 }
269
270 void DlgSettingsImageImp::onComboMethodActivated(int index)
271 {
272 QByteArray data = ui->comboMethod->itemData(index).toByteArray();
273 if (data == QByteArray("GrabFramebuffer")) {
274 ui->comboBackground->setEnabled(false);
275 }
276 else {
277 ui->comboBackground->setEnabled(true);
278 }
279 }
280
281 #include "moc_DlgSettingsImageImp.cpp"