1
/***************************************************************************
2
* Copyright (c) 2005 Werner Mayer <wmayer[at]users.sourceforge.net> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
***************************************************************************/
23
#include "PreCompiled.h"
25
# include <QRegularExpression>
26
# include <QRegularExpressionMatch>
29
#include "DlgSettingsImageImp.h"
30
#include "ui_DlgSettingsImage.h"
33
using namespace Gui::Dialog;
36
/* TRANSLATOR Gui::Dialog::DlgSettingsImageImp */
39
* Constructs a DlgSettingsImageImp as a child of 'parent', with the
40
* name 'name' and widget flags set to 'f'.
42
DlgSettingsImageImp::DlgSettingsImageImp( QWidget* parent )
44
, ui(new Ui_DlgSettingsImage)
49
SbVec2s res = SoOffscreenRenderer::getMaximumResolution();
50
ui->spinWidth->setMaximum((int)res[0]);
51
ui->spinHeight->setMaximum((int)res[1]);
55
_fRatio = (float)_width/(float)_height;
57
ui->comboMethod->addItem(tr("Offscreen (New)"), QByteArray("QtOffscreenRenderer"));
58
ui->comboMethod->addItem(tr("Offscreen (Old)"), QByteArray("CoinOffscreenRenderer"));
59
ui->comboMethod->addItem(tr("Framebuffer (custom)"), QByteArray("FramebufferObject"));
60
ui->comboMethod->addItem(tr("Framebuffer (as is)"), QByteArray("GrabFramebuffer"));
64
* Destroys the object and frees any allocated resources
66
DlgSettingsImageImp::~DlgSettingsImageImp() = default;
68
void DlgSettingsImageImp::setupConnections()
70
connect(ui->buttonRatioScreen, &QToolButton::clicked,
71
this, &DlgSettingsImageImp::onButtonRatioScreenClicked);
72
connect(ui->buttonRatio4x3, &QToolButton::clicked,
73
this, &DlgSettingsImageImp::onButtonRatio4x3Clicked);
74
connect(ui->buttonRatio16x9, &QToolButton::clicked,
75
this, &DlgSettingsImageImp::onButtonRatio16x9Clicked);
76
connect(ui->buttonRatio1x1, &QToolButton::clicked,
77
this, &DlgSettingsImageImp::onButtonRatio1x1Clicked);
78
connect(ui->standardSizeBox, qOverload<int>(&QComboBox::activated),
79
this, &DlgSettingsImageImp::onStandardSizeBoxActivated);
80
connect(ui->comboMethod, qOverload<int>(&QComboBox::activated),
81
this, &DlgSettingsImageImp::onComboMethodActivated);
84
void DlgSettingsImageImp::changeEvent(QEvent *e)
86
if (e->type() == QEvent::LanguageChange) {
87
ui->retranslateUi(this);
89
QWidget::changeEvent(e);
93
* Sets the image size to (\a w, \a h).
95
void DlgSettingsImageImp::setImageSize(int w, int h)
97
// set current screen size
98
ui->standardSizeBox->setItemData(0, QSize(w,h));
100
ui->spinWidth->setValue(w);
101
ui->spinHeight->setValue(h);
103
// As the image size is in pixel why shouldn't _width and _height be integers?
106
_fRatio = (float)_width/(float)_height;
110
* Sets the image size to \a s.
112
void DlgSettingsImageImp::setImageSize( const QSize& s )
114
// set current screen size
115
ui->standardSizeBox->setItemData(0, s);
117
ui->spinWidth->setValue( s.width() );
118
ui->spinHeight->setValue( s.height() );
120
// As the image size is in pixel why shouldn't _width and _height be integers?
122
_height = s.height();
123
_fRatio = (float)_width/(float)_height;
127
* Returns the currently set image size.
129
QSize DlgSettingsImageImp::imageSize() const
131
return { ui->spinWidth->value(), ui->spinHeight->value() };
135
* Returns the currently set image width.
137
int DlgSettingsImageImp::imageWidth() const
139
return ui->spinWidth->value();
143
* Returns the currently set image height.
145
int DlgSettingsImageImp::imageHeight() const
147
return ui->spinHeight->value();
151
* Returns the comment of the picture. If for the currently selected image format no comments are supported
152
* QString() is returned.
154
QString DlgSettingsImageImp::comment() const
156
if ( !ui->textEditComment->isEnabled() )
159
return ui->textEditComment->toPlainText();
162
int DlgSettingsImageImp::backgroundType() const
164
return ui->comboBackground->currentIndex();
168
* Sets the image size to (\a w, \a h).
170
void DlgSettingsImageImp::setBackgroundType(int t)
172
if ( t < ui->comboBackground->count() )
173
ui->comboBackground->setCurrentIndex(t);
176
bool DlgSettingsImageImp::addWatermark() const
178
return ui->checkWatermark->isChecked();
181
void DlgSettingsImageImp::onSelectedFilter(const QString& filter)
183
bool ok = (filter.startsWith(QLatin1String("JPG")) ||
184
filter.startsWith(QLatin1String("JPEG")) ||
185
filter.startsWith(QLatin1String("PNG")));
186
ui->buttonGroupComment->setEnabled( ok );
189
void DlgSettingsImageImp::adjustImageSize(float fRatio)
191
// if width has changed then adjust height and vice versa, if both has changed then adjust width
192
if (_height != ui->spinHeight->value())
194
_height = ui->spinHeight->value();
195
_width = (int)((float)_height*fRatio);
196
ui->spinWidth->setValue( (int)_width );
198
else // if( _width != spinWidth->value() )
200
_width = ui->spinWidth->value();
201
_height = (int)((float)_width/fRatio);
202
ui->spinHeight->setValue( (int)_height );
206
void DlgSettingsImageImp::onButtonRatioScreenClicked()
208
adjustImageSize(_fRatio);
211
void DlgSettingsImageImp::onButtonRatio4x3Clicked()
213
adjustImageSize(4.0f/3.0f);
216
void DlgSettingsImageImp::onButtonRatio16x9Clicked()
218
adjustImageSize(16.0f/9.0f);
221
void DlgSettingsImageImp::onButtonRatio1x1Clicked()
223
adjustImageSize(1.0f);
226
void DlgSettingsImageImp::onStandardSizeBoxActivated(int index)
229
// we have set the user data for the 1st item
230
QSize s = ui->standardSizeBox->itemData(0).toSize();
231
ui->spinWidth->setValue(s.width());
232
ui->spinHeight->setValue(s.height());
235
// try to extract from the string
236
QString text = ui->standardSizeBox->itemText(index);
237
QRegularExpression rx(QLatin1String(R"(\b\d{2,5}\b)"));
239
auto match = rx.match(text, pos);
240
if (match.hasMatch()) {
241
pos = match.capturedStart();
242
QString width = text.mid(pos, match.capturedLength());
243
ui->spinWidth->setValue(width.toInt());
244
pos += match.capturedLength();
247
match = rx.match(text, pos);
248
if (match.hasMatch()) {
249
pos = match.capturedStart();
250
QString height = text.mid(pos, match.capturedLength());
251
ui->spinHeight->setValue(height.toInt());
256
void DlgSettingsImageImp::setMethod(const QByteArray& m)
258
int index = ui->comboMethod->findData(m);
260
ui->comboMethod->setCurrentIndex(index);
263
QByteArray DlgSettingsImageImp::method() const
265
return ui->comboMethod->currentData().toByteArray();
268
void DlgSettingsImageImp::onComboMethodActivated(int index)
270
QByteArray data = ui->comboMethod->itemData(index).toByteArray();
271
if (data == QByteArray("GrabFramebuffer")) {
272
ui->comboBackground->setEnabled(false);
275
ui->comboBackground->setEnabled(true);
279
#include "moc_DlgSettingsImageImp.cpp"