SVG Viewer Example▲
Sélectionnez
/**
**************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
***************************************************************************
*/
#include
"exportdialog.h"
#include <QApplication>
#include <QDesktopWidget>
#include <QDialogButtonBox>
#include <QFileDialog>
#include <QFormLayout>
#include <QHBoxLayout>
#include <QMessageBox>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QSpinBox>
#include <QToolButton>
#include <QVBoxLayout>
#include <QImageWriter>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
enum
{
exportMinimumSize =
1
, exportMaximumSize =
2000
}
;
ExportDialog::
ExportDialog(QWidget *
parent)
:
QDialog(parent)
, m_fileNameLineEdit(new
QLineEdit(this
))
, m_widthSpinBox(new
QSpinBox(this
))
, m_heightSpinBox(new
QSpinBox(this
))
, m_aspectRatio(1
)
{
typedef
void
(QSpinBox::
*
QSpinBoxIntSignal)(int
);
setWindowFlags(windowFlags() &
amp; ~
Qt::
WindowContextHelpButtonHint);
setWindowTitle(tr("Export"
));
QFormLayout *
formLayout =
new
QFormLayout(this
);
QHBoxLayout *
fileLayout =
new
QHBoxLayout;
fileLayout-&
gt;addWidget(m_fileNameLineEdit);
m_fileNameLineEdit-&
gt;setMinimumWidth(QApplication::
desktop()-&
gt;availableGeometry(this
).width() /
6
);
QPushButton *
browseButton =
new
QPushButton(tr("Browse..."
), this
);
fileLayout-&
gt;addWidget(browseButton);
connect(browseButton, &
amp;QAbstractButton::
clicked, this
, &
amp;ExportDialog::
browse);
formLayout-&
gt;addRow(tr("File:"
), fileLayout);
QHBoxLayout *
sizeLayout =
new
QHBoxLayout;
sizeLayout-&
gt;addStretch();
m_widthSpinBox-&
gt;setMinimum(exportMinimumSize);
m_widthSpinBox-&
gt;setMaximum(exportMaximumSize);
connect(m_widthSpinBox, static_cast
&
lt;QSpinBoxIntSignal&
gt;(&
amp;QSpinBox::
valueChanged),
this
, &
amp;ExportDialog::
exportWidthChanged);
sizeLayout-&
gt;addWidget(m_widthSpinBox);
//: Multiplication, as in 32x32
sizeLayout-&
gt;addWidget(new
QLabel(tr("x"
)));
m_heightSpinBox-&
gt;setMinimum(exportMinimumSize);
m_heightSpinBox-&
gt;setMaximum(exportMaximumSize);
connect(m_heightSpinBox, static_cast
&
lt;QSpinBoxIntSignal&
gt;(&
amp;QSpinBox::
valueChanged),
this
, &
amp;ExportDialog::
exportHeightChanged);
sizeLayout-&
gt;addWidget(m_heightSpinBox);
QToolButton *
resetButton =
new
QToolButton(this
);
resetButton-&
gt;setIcon(QIcon(":/qt-project.org/styles/commonstyle/images/refresh-32.png"
));
sizeLayout-&
gt;addWidget(resetButton);
connect(resetButton, &
amp;QAbstractButton::
clicked, this
, &
amp;ExportDialog::
resetExportSize);
formLayout-&
gt;addRow(tr("Size:"
), sizeLayout);
QDialogButtonBox *
buttonBox =
new
QDialogButtonBox(QDialogButtonBox::
Ok |
QDialogButtonBox::
Cancel, this
);
connect(buttonBox, &
amp;QDialogButtonBox::
accepted, this
, &
amp;QDialog::
accept);
connect(buttonBox, &
amp;QDialogButtonBox::
rejected, this
, &
amp;QDialog::
reject);
formLayout-&
gt;addRow(buttonBox);
}
void
ExportDialog::
accept()
{
const
QString fileName =
exportFileName();
if
(fileName.isEmpty()) {
QMessageBox::
warning(this
, windowTitle(), tr("Please enter a file name"
));
return
;
}
QFileInfo fi(fileName);
if
(fi.exists()) {
const
QString question =
tr("%1 already exists.
\n
Would you like to overwrite it?"
).arg(QDir::
toNativeSeparators(fileName));
if
(QMessageBox::
question(this
, windowTitle(), question, QMessageBox::
Yes |
QMessageBox::
No) !=
QMessageBox::
Yes)
return
;
}
QDialog::
accept();
}
QSize ExportDialog::
exportSize() const
{
return
QSize(m_widthSpinBox-&
gt;value(), m_heightSpinBox-&
gt;value());
}
void
ExportDialog::
setExportSize(const
QSize &
amp;size)
{
m_defaultSize =
size;
QSizeF defaultSizeF(m_defaultSize);
m_aspectRatio =
defaultSizeF.width() /
defaultSizeF.height();
setExportWidthBlocked(size.width());
setExportHeightBlocked(size.height());
}
void
ExportDialog::
resetExportSize()
{
setExportWidthBlocked(m_defaultSize.width());
setExportHeightBlocked(m_defaultSize.height());
}
void
ExportDialog::
setExportWidthBlocked(int
width)
{
if
(m_widthSpinBox-&
gt;value() !=
width) {
const
bool
blockSignals =
m_widthSpinBox-&
gt;blockSignals(true
);
m_widthSpinBox-&
gt;setValue(width);
m_widthSpinBox-&
gt;blockSignals(blockSignals);
}
}
void
ExportDialog::
setExportHeightBlocked(int
height)
{
if
(m_heightSpinBox-&
gt;value() !=
height) {
const
bool
blockSignals =
m_heightSpinBox-&
gt;blockSignals(true
);
m_heightSpinBox-&
gt;setValue(height);
m_heightSpinBox-&
gt;blockSignals(blockSignals);
}
}
void
ExportDialog::
exportWidthChanged(int
width)
{
const
bool
square =
m_defaultSize.width() ==
m_defaultSize.height();
setExportHeightBlocked(square ? width : qRound(qreal(width) /
m_aspectRatio));
}
void
ExportDialog::
exportHeightChanged(int
height)
{
const
bool
square =
m_defaultSize.width() ==
m_defaultSize.height();
setExportWidthBlocked(square ? height : qRound(qreal(height) *
m_aspectRatio));
}
QString ExportDialog::
exportFileName() const
{
return
QDir::
cleanPath(m_fileNameLineEdit-&
gt;text().trimmed());
}
void
ExportDialog::
setExportFileName(const
QString &
amp;f)
{
m_fileNameLineEdit-&
gt;setText(QDir::
toNativeSeparators(f));
}
void
ExportDialog::
browse()
{
QFileDialog fileDialog(this
);
fileDialog.setAcceptMode(QFileDialog::
AcceptSave);
const
QString fileName =
exportFileName();
if
(!
fileName.isEmpty())
fileDialog.setDirectory(QFileInfo(fileName).absolutePath());
QStringList mimeTypes;
const
auto
supportedMimeTypes =
QImageWriter::
supportedMimeTypes();
for
(const
QByteArray &
amp;mimeType : supportedMimeTypes)
mimeTypes.append(QLatin1String(mimeType));
fileDialog.setMimeTypeFilters(mimeTypes);
const
int
pngIndex =
mimeTypes.indexOf("image/png"
);
if
(pngIndex &
gt;=
0
) {
fileDialog.selectMimeTypeFilter(mimeTypes.at(pngIndex));
fileDialog.setDefaultSuffix("png"
);
}
if
(fileDialog.exec() ==
QDialog::
Accepted)
setExportFileName(fileDialog.selectedFiles().constFirst());
}