2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2016-03-17 16:02:20 +01:00
|
|
|
|
|
|
|
|
#include "exportdialog.h"
|
2022-07-08 18:51:09 +02:00
|
|
|
|
2018-01-12 15:32:10 +01:00
|
|
|
#include "imageview.h" // ExportData
|
2022-07-08 18:51:09 +02:00
|
|
|
#include "imageviewertr.h"
|
2016-03-17 16:02:20 +01:00
|
|
|
|
|
|
|
|
#include <coreplugin/coreicons.h>
|
|
|
|
|
|
|
|
|
|
#include <utils/pathchooser.h>
|
|
|
|
|
|
|
|
|
|
#include <QDialogButtonBox>
|
2020-07-29 16:24:21 +02:00
|
|
|
#include <QDir>
|
|
|
|
|
#include <QFileInfo>
|
2016-03-17 16:02:20 +01:00
|
|
|
#include <QFormLayout>
|
|
|
|
|
#include <QHBoxLayout>
|
2020-07-29 16:24:21 +02:00
|
|
|
#include <QImageWriter>
|
2016-03-17 16:02:20 +01:00
|
|
|
#include <QLabel>
|
|
|
|
|
#include <QLineEdit>
|
2020-07-29 16:24:21 +02:00
|
|
|
#include <QMessageBox>
|
|
|
|
|
#include <QMimeDatabase>
|
|
|
|
|
#include <QScreen>
|
2016-03-17 16:02:20 +01:00
|
|
|
#include <QSpinBox>
|
|
|
|
|
#include <QToolButton>
|
|
|
|
|
|
2023-06-07 09:14:28 +02:00
|
|
|
using namespace Utils;
|
|
|
|
|
|
|
|
|
|
namespace ImageViewer::Internal {
|
2016-03-17 16:02:20 +01:00
|
|
|
|
|
|
|
|
enum { exportMinimumSize = 1, exportMaximumSize = 2000 };
|
|
|
|
|
|
2018-01-12 15:32:10 +01:00
|
|
|
QString ExportDialog::imageNameFilterString()
|
2016-03-17 16:02:20 +01:00
|
|
|
{
|
|
|
|
|
static QString result;
|
|
|
|
|
if (result.isEmpty()) {
|
|
|
|
|
QMimeDatabase mimeDatabase;
|
2022-07-08 18:51:09 +02:00
|
|
|
const QString separator = ";;";
|
2022-10-05 13:47:56 +02:00
|
|
|
const QList<QByteArray> mimeTypes = QImageWriter::supportedMimeTypes();
|
|
|
|
|
for (const QByteArray &mimeType : mimeTypes) {
|
2016-03-17 16:02:20 +01:00
|
|
|
const QString filter = mimeDatabase.mimeTypeForName(QLatin1String(mimeType)).filterString();
|
|
|
|
|
if (!filter.isEmpty()) {
|
|
|
|
|
if (mimeType == QByteArrayLiteral("image/png")) {
|
|
|
|
|
if (!result.isEmpty())
|
|
|
|
|
result.prepend(separator);
|
|
|
|
|
result.prepend(filter);
|
|
|
|
|
} else {
|
|
|
|
|
if (!result.isEmpty())
|
|
|
|
|
result.append(separator);
|
|
|
|
|
result.append(filter);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ExportDialog::ExportDialog(QWidget *parent)
|
|
|
|
|
: QDialog(parent)
|
|
|
|
|
, m_pathChooser(new Utils::PathChooser(this))
|
|
|
|
|
, m_widthSpinBox(new QSpinBox(this))
|
|
|
|
|
, m_heightSpinBox(new QSpinBox(this))
|
|
|
|
|
, m_aspectRatio(1)
|
|
|
|
|
{
|
2018-11-11 12:24:06 +01:00
|
|
|
auto formLayout = new QFormLayout(this);
|
2016-03-17 16:02:20 +01:00
|
|
|
|
2020-07-29 16:24:21 +02:00
|
|
|
m_pathChooser->setMinimumWidth(screen()->availableGeometry().width() / 5);
|
2016-03-17 16:02:20 +01:00
|
|
|
m_pathChooser->setExpectedKind(Utils::PathChooser::SaveFile);
|
|
|
|
|
m_pathChooser->setPromptDialogFilter(imageNameFilterString());
|
2022-07-08 18:51:09 +02:00
|
|
|
formLayout->addRow(Tr::tr("File:"), m_pathChooser);
|
2016-03-17 16:02:20 +01:00
|
|
|
|
2018-11-11 12:24:06 +01:00
|
|
|
auto sizeLayout = new QHBoxLayout;
|
2016-03-17 16:02:20 +01:00
|
|
|
m_widthSpinBox->setMinimum(exportMinimumSize);
|
|
|
|
|
m_widthSpinBox->setMaximum(exportMaximumSize);
|
2022-07-19 23:43:58 +02:00
|
|
|
connect(m_widthSpinBox, &QSpinBox::valueChanged, this, &ExportDialog::exportWidthChanged);
|
2016-03-17 16:02:20 +01:00
|
|
|
sizeLayout->addWidget(m_widthSpinBox);
|
|
|
|
|
//: Multiplication, as in 32x32
|
2022-07-08 18:51:09 +02:00
|
|
|
sizeLayout->addWidget(new QLabel(Tr::tr("x")));
|
2016-03-17 16:02:20 +01:00
|
|
|
m_heightSpinBox->setMinimum(exportMinimumSize);
|
|
|
|
|
m_heightSpinBox->setMaximum(exportMaximumSize);
|
2022-07-19 23:43:58 +02:00
|
|
|
connect(m_heightSpinBox, &QSpinBox::valueChanged, this, &ExportDialog::exportHeightChanged);
|
2016-03-17 16:02:20 +01:00
|
|
|
sizeLayout->addWidget(m_heightSpinBox);
|
2018-11-11 12:24:06 +01:00
|
|
|
auto resetButton = new QToolButton(this);
|
2022-07-08 18:51:09 +02:00
|
|
|
resetButton->setIcon(QIcon(":/qt-project.org/styles/commonstyle/images/refresh-32.png"));
|
2016-03-17 16:02:20 +01:00
|
|
|
sizeLayout->addWidget(resetButton);
|
|
|
|
|
sizeLayout->addStretch();
|
|
|
|
|
connect(resetButton, &QAbstractButton::clicked, this, &ExportDialog::resetExportSize);
|
2022-07-08 18:51:09 +02:00
|
|
|
formLayout->addRow(Tr::tr("Size:"), sizeLayout);
|
2016-03-17 16:02:20 +01:00
|
|
|
|
2018-11-11 12:24:06 +01:00
|
|
|
auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
|
2016-03-17 16:02:20 +01:00
|
|
|
connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
|
|
|
|
|
connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
|
|
|
|
|
formLayout->addRow(buttonBox);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ExportDialog::accept()
|
|
|
|
|
{
|
|
|
|
|
if (!m_pathChooser->isValid()) {
|
|
|
|
|
QMessageBox::warning(this, windowTitle(), m_pathChooser->errorMessage());
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-06-07 09:14:28 +02:00
|
|
|
const FilePath filePath = exportFileName();
|
|
|
|
|
if (filePath.exists()) {
|
2022-07-08 18:51:09 +02:00
|
|
|
const QString question = Tr::tr("%1 already exists.\nWould you like to overwrite it?")
|
2023-06-07 09:14:28 +02:00
|
|
|
.arg(filePath.toUserOutput());
|
2016-03-17 16:02:20 +01:00
|
|
|
if (QMessageBox::question(this, windowTitle(), question, QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
QDialog::accept();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QSize ExportDialog::exportSize() const
|
|
|
|
|
{
|
2018-11-11 12:24:06 +01:00
|
|
|
return {m_widthSpinBox->value(), m_heightSpinBox->value()};
|
2016-03-17 16:02:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ExportDialog::setExportSize(const QSize &size)
|
|
|
|
|
{
|
|
|
|
|
m_defaultSize = size;
|
|
|
|
|
const 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) {
|
2017-09-30 07:12:57 +02:00
|
|
|
QSignalBlocker blocker(m_widthSpinBox);
|
2016-03-17 16:02:20 +01:00
|
|
|
m_widthSpinBox->setValue(width);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ExportDialog::setExportHeightBlocked(int height)
|
|
|
|
|
{
|
|
|
|
|
if (m_heightSpinBox->value() != height) {
|
2017-09-30 07:12:57 +02:00
|
|
|
QSignalBlocker blocker(m_heightSpinBox);
|
2016-03-17 16:02:20 +01:00
|
|
|
m_heightSpinBox->setValue(height);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-07 09:14:28 +02:00
|
|
|
FilePath ExportDialog::exportFileName() const
|
2016-03-17 16:02:20 +01:00
|
|
|
{
|
2023-06-07 09:14:28 +02:00
|
|
|
return m_pathChooser->filePath();
|
2016-03-17 16:02:20 +01:00
|
|
|
}
|
|
|
|
|
|
2023-06-07 09:14:28 +02:00
|
|
|
void ExportDialog::setExportFileName(const FilePath &f)
|
2016-03-17 16:02:20 +01:00
|
|
|
{
|
2023-06-07 09:14:28 +02:00
|
|
|
m_pathChooser->setFilePath(f);
|
2016-03-17 16:02:20 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-12 15:32:10 +01:00
|
|
|
ExportData ExportDialog::exportData() const
|
|
|
|
|
{
|
|
|
|
|
return {exportFileName(), exportSize()};
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-07 09:14:28 +02:00
|
|
|
} // ImageViewer::Internal
|