diff options
author | Friedemann Kleint <Friedemann.Kleint@theqtcompany.com> | 2016-03-14 14:25:23 +0100 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@theqtcompany.com> | 2016-03-15 09:52:34 +0000 |
commit | 95e0d10ddd18b15ba12886d1ec3c5c9c3e06bd55 (patch) | |
tree | f3510ddc66b1870e9cfa2c7154c54a8b2bd6dd23 /examples/svg | |
parent | 99303e92ae9cf461f99b8eff8b37b42f758118b6 (diff) | |
download | qtsvg-95e0d10ddd18b15ba12886d1ec3c5c9c3e06bd55.tar.gz |
SVG viewer: Add export option.
Add an option to export an image giving a size. This can be
used for creating High DPI icons from SVG files among other things.
Task-number: QTBUG-49374
Change-Id: I0437889961bc6e646667c5165584002f7ab4d952
Reviewed-by: aavit <eirik.aavitsland@theqtcompany.com>
Diffstat (limited to 'examples/svg')
-rw-r--r-- | examples/svg/svgviewer/exportdialog.cpp | 208 | ||||
-rw-r--r-- | examples/svg/svgviewer/exportdialog.h | 80 | ||||
-rw-r--r-- | examples/svg/svgviewer/mainwindow.cpp | 63 | ||||
-rw-r--r-- | examples/svg/svgviewer/mainwindow.h | 1 | ||||
-rw-r--r-- | examples/svg/svgviewer/svgview.cpp | 6 | ||||
-rw-r--r-- | examples/svg/svgviewer/svgview.h | 5 | ||||
-rw-r--r-- | examples/svg/svgviewer/svgviewer.pro | 6 |
7 files changed, 363 insertions, 6 deletions
diff --git a/examples/svg/svgviewer/exportdialog.cpp b/examples/svg/svgviewer/exportdialog.cpp new file mode 100644 index 0000000..04c9e12 --- /dev/null +++ b/examples/svg/svgviewer/exportdialog.cpp @@ -0,0 +1,208 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** 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() & ~Qt::WindowContextHelpButtonHint); + setWindowTitle(tr("Export")); + + QFormLayout *formLayout = new QFormLayout(this); + + QHBoxLayout *fileLayout = new QHBoxLayout; + fileLayout->addWidget(m_fileNameLineEdit); + m_fileNameLineEdit->setMinimumWidth(QApplication::desktop()->availableGeometry(this).width() / 6); + QPushButton *browseButton = new QPushButton(tr("Browse..."), this); + fileLayout->addWidget(browseButton); + connect(browseButton, &QAbstractButton::clicked, this, &ExportDialog::browse); + formLayout->addRow(tr("File:"), fileLayout); + + QHBoxLayout *sizeLayout = new QHBoxLayout; + sizeLayout->addStretch(); + m_widthSpinBox->setMinimum(exportMinimumSize); + m_widthSpinBox->setMaximum(exportMaximumSize); + connect(m_widthSpinBox, static_cast<QSpinBoxIntSignal>(&QSpinBox::valueChanged), + this, &ExportDialog::exportWidthChanged); + sizeLayout->addWidget(m_widthSpinBox); + //: Multiplication, as in 32x32 + sizeLayout->addWidget(new QLabel(tr("x"))); + m_heightSpinBox->setMinimum(exportMinimumSize); + m_heightSpinBox->setMaximum(exportMaximumSize); + connect(m_heightSpinBox, static_cast<QSpinBoxIntSignal>(&QSpinBox::valueChanged), + this, &ExportDialog::exportHeightChanged); + sizeLayout->addWidget(m_heightSpinBox); + QToolButton *resetButton = new QToolButton(this); + resetButton->setIcon(QIcon(":/qt-project.org/styles/commonstyle/images/refresh-32.png")); + sizeLayout->addWidget(resetButton); + connect(resetButton, &QAbstractButton::clicked, this, &ExportDialog::resetExportSize); + formLayout->addRow(tr("Size:"), sizeLayout); + + QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this); + connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); + connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); + formLayout->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.\nWould 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->value(), m_heightSpinBox->value()); +} + +void ExportDialog::setExportSize(const QSize &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->value() != width) { + const bool blockSignals = m_widthSpinBox->blockSignals(true); + m_widthSpinBox->setValue(width); + m_widthSpinBox->blockSignals(blockSignals); + } +} + +void ExportDialog::setExportHeightBlocked(int height) +{ + if (m_heightSpinBox->value() != height) { + const bool blockSignals = m_heightSpinBox->blockSignals(true); + m_heightSpinBox->setValue(height); + m_heightSpinBox->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->text().trimmed()); +} + +void ExportDialog::setExportFileName(const QString &f) +{ + m_fileNameLineEdit->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; + foreach (const QByteArray &mimeType, QImageWriter::supportedMimeTypes()) + mimeTypes.append(QLatin1String(mimeType)); + fileDialog.setMimeTypeFilters(mimeTypes); + const int pngIndex = mimeTypes.indexOf("image/png"); + if (pngIndex >= 0) { + fileDialog.selectMimeTypeFilter(mimeTypes.at(pngIndex)); + fileDialog.setDefaultSuffix("png"); + } + if (fileDialog.exec() == QDialog::Accepted) + setExportFileName(fileDialog.selectedFiles().constFirst()); +} diff --git a/examples/svg/svgviewer/exportdialog.h b/examples/svg/svgviewer/exportdialog.h new file mode 100644 index 0000000..71d8bdd --- /dev/null +++ b/examples/svg/svgviewer/exportdialog.h @@ -0,0 +1,80 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** 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$ +** +****************************************************************************/ + +#ifndef EXPORTDIALOG_H +#define EXPORTDIALOG_H + +#include <QDialog> + +QT_FORWARD_DECLARE_CLASS(QLineEdit) +QT_FORWARD_DECLARE_CLASS(QSpinBox) + +class ExportDialog : public QDialog +{ + Q_OBJECT +public: + explicit ExportDialog(QWidget *parent = 0); + + QSize exportSize() const; + void setExportSize(const QSize &); + + QString exportFileName() const; + void setExportFileName(const QString &); + + void accept() override; + +private slots: + void browse(); + void resetExportSize(); + void exportWidthChanged(int width); + void exportHeightChanged(int height); + +private: + void setExportWidthBlocked(int width); + void setExportHeightBlocked(int height); + + QLineEdit *m_fileNameLineEdit; + QSpinBox *m_widthSpinBox; + QSpinBox *m_heightSpinBox; + QSize m_defaultSize; + qreal m_aspectRatio; +}; + +#endif // EXPORTDIALOG_H diff --git a/examples/svg/svgviewer/mainwindow.cpp b/examples/svg/svgviewer/mainwindow.cpp index 4e94642..c63f223 100644 --- a/examples/svg/svgviewer/mainwindow.cpp +++ b/examples/svg/svgviewer/mainwindow.cpp @@ -39,18 +39,35 @@ ****************************************************************************/ #include "mainwindow.h" +#include "exportdialog.h" #include <QtWidgets> +#include <QSvgRenderer> #include "svgview.h" +static inline QString picturesLocation() +{ + return QStandardPaths::standardLocations(QStandardPaths::PicturesLocation).value(0, QDir::currentPath()); +} + MainWindow::MainWindow() : QMainWindow() , m_view(new SvgView) { + QToolBar *toolBar = new QToolBar(this); + addToolBar(Qt::TopToolBarArea, toolBar); + QMenu *fileMenu = menuBar()->addMenu(tr("&File")); - QAction *openAction = fileMenu->addAction(tr("&Open..."), this, &MainWindow::openFile); + const QIcon openIcon = QIcon::fromTheme("document-open", QIcon(":/qt-project.org/styles/commonstyle/images/standardbutton-open-32.png")); + QAction *openAction = fileMenu->addAction(openIcon, tr("&Open..."), this, &MainWindow::openFile); openAction->setShortcut(QKeySequence::Open); + toolBar->addAction(openAction); + const QIcon exportIcon = QIcon::fromTheme("document-save", QIcon(":/qt-project.org/styles/commonstyle/images/standardbutton-save-32.png")); + QAction *exportAction = fileMenu->addAction(exportIcon, tr("&Export..."), this, &MainWindow::exportImage); + exportAction->setToolTip(tr("Export Image")); + exportAction->setShortcut(Qt::CTRL + Qt::Key_E); + toolBar->addAction(exportAction); QAction *quitAction = fileMenu->addAction(tr("E&xit"), qApp, QCoreApplication::quit); quitAction->setShortcuts(QKeySequence::Quit); @@ -116,7 +133,7 @@ void MainWindow::openFile() fileDialog.setMimeTypeFilters(QStringList() << "image/svg+xml" << "image/svg+xml-compressed"); fileDialog.setWindowTitle(tr("Open SVG File")); if (m_currentPath.isEmpty()) - fileDialog.setDirectory(QStandardPaths::standardLocations(QStandardPaths::PicturesLocation).value(0, QDir::currentPath())); + fileDialog.setDirectory(picturesLocation()); while (fileDialog.exec() == QDialog::Accepted && !loadFile(fileDialog.selectedFiles().constFirst())) ; @@ -126,7 +143,7 @@ bool MainWindow::loadFile(const QString &fileName) { if (!QFileInfo::exists(fileName) || !m_view->openFile(fileName)) { QMessageBox::critical(this, tr("Open SVG File"), - QString("Could not open file '%1'.").arg(QDir::toNativeSeparators(fileName))); + tr("Could not open file '%1'.").arg(QDir::toNativeSeparators(fileName))); return false; } @@ -154,3 +171,43 @@ void MainWindow::setRenderer(int renderMode) m_highQualityAntialiasingAction->setEnabled(renderMode == SvgView::OpenGL); m_view->setRenderer(static_cast<SvgView::RendererType>(renderMode)); } + +void MainWindow::exportImage() +{ + ExportDialog exportDialog(this); + exportDialog.setExportSize(m_view->svgSize()); + QString fileName; + if (m_currentPath.isEmpty()) { + fileName = picturesLocation() + QLatin1String("/export.png"); + } else { + const QFileInfo fi(m_currentPath); + fileName = fi.absolutePath() + QLatin1Char('/') + fi.baseName() + QLatin1String(".png"); + } + exportDialog.setExportFileName(fileName); + + while (true) { + if (exportDialog.exec() != QDialog::Accepted) + break; + + const QSize imageSize = exportDialog.exportSize(); + QImage image(imageSize, QImage::Format_ARGB32); + image.fill(Qt::transparent); + QPainter painter; + painter.begin(&image); + m_view->renderer()->render(&painter, QRectF(QPointF(), QSizeF(imageSize))); + painter.end(); + + const QString fileName = exportDialog.exportFileName(); + if (image.save(fileName)) { + + const QString message = tr("Exported %1, %2x%3, %4 bytes") + .arg(QDir::toNativeSeparators(fileName)).arg(imageSize.width()).arg(imageSize.height()) + .arg(QFileInfo(fileName).size()); + statusBar()->showMessage(message); + break; + } else { + QMessageBox::critical(this, tr("Export Image"), + tr("Could not write file '%1'.").arg(QDir::toNativeSeparators(fileName))); + } + } +} diff --git a/examples/svg/svgviewer/mainwindow.h b/examples/svg/svgviewer/mainwindow.h index 215f4a6..c0973f9 100644 --- a/examples/svg/svgviewer/mainwindow.h +++ b/examples/svg/svgviewer/mainwindow.h @@ -64,6 +64,7 @@ public: public slots: void openFile(); + void exportImage(); void setRenderer(int renderMode); private: diff --git a/examples/svg/svgviewer/svgview.cpp b/examples/svg/svgviewer/svgview.cpp index dd9de1a..cbea85d 100644 --- a/examples/svg/svgviewer/svgview.cpp +++ b/examples/svg/svgviewer/svgview.cpp @@ -194,3 +194,9 @@ void SvgView::wheelEvent(QWheelEvent *event) event->accept(); } +QSvgRenderer *SvgView::renderer() const +{ + if (m_svgItem) + return m_svgItem->renderer(); + return nullptr; +} diff --git a/examples/svg/svgviewer/svgview.h b/examples/svg/svgviewer/svgview.h index 41c0ca5..645fb49 100644 --- a/examples/svg/svgviewer/svgview.h +++ b/examples/svg/svgviewer/svgview.h @@ -44,6 +44,8 @@ #include <QGraphicsView> QT_BEGIN_NAMESPACE +class QGraphicsSvgItem; +class QSvgRenderer; class QWheelEvent; class QPaintEvent; QT_END_NAMESPACE @@ -62,6 +64,7 @@ public: void drawBackground(QPainter *p, const QRectF &rect) override; QSize svgSize() const; + QSvgRenderer *renderer() const; public slots: void setHighQualityAntialiasing(bool highQualityAntialiasing); @@ -75,7 +78,7 @@ protected: private: RendererType m_renderer; - QGraphicsItem *m_svgItem; + QGraphicsSvgItem *m_svgItem; QGraphicsRectItem *m_backgroundItem; QGraphicsRectItem *m_outlineItem; diff --git a/examples/svg/svgviewer/svgviewer.pro b/examples/svg/svgviewer/svgviewer.pro index 157af4d..6eea56a 100644 --- a/examples/svg/svgviewer/svgviewer.pro +++ b/examples/svg/svgviewer/svgviewer.pro @@ -1,9 +1,11 @@ HEADERS = mainwindow.h \ - svgview.h + svgview.h \ + exportdialog.h RESOURCES = svgviewer.qrc SOURCES = main.cpp \ mainwindow.cpp \ - svgview.cpp + svgview.cpp \ + exportdialog.cpp QT += widgets svg qtHaveModule(opengl): QT += opengl |