ImageViewer: Proliferate FilePath use

Get rid of a few .toFileInfo() and related calls.

Change-Id: I85a384848294e045fe07f621f06ebb17e567b444
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
hjk
2023-06-07 09:14:28 +02:00
parent 003b43aee7
commit 9dabb9d0aa
6 changed files with 63 additions and 53 deletions

View File

@@ -24,8 +24,9 @@
#include <QSpinBox>
#include <QToolButton>
namespace ImageViewer {
namespace Internal {
using namespace Utils;
namespace ImageViewer::Internal {
enum { exportMinimumSize = 1, exportMaximumSize = 2000 };
@@ -98,10 +99,10 @@ void ExportDialog::accept()
QMessageBox::warning(this, windowTitle(), m_pathChooser->errorMessage());
return;
}
const QString fileName = exportFileName();
if (QFileInfo::exists(fileName)) {
const FilePath filePath = exportFileName();
if (filePath.exists()) {
const QString question = Tr::tr("%1 already exists.\nWould you like to overwrite it?")
.arg(QDir::toNativeSeparators(fileName));
.arg(filePath.toUserOutput());
if (QMessageBox::question(this, windowTitle(), question, QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
return;
}
@@ -156,14 +157,14 @@ void ExportDialog::exportHeightChanged(int height)
setExportWidthBlocked(square ? height : qRound(qreal(height) * m_aspectRatio));
}
QString ExportDialog::exportFileName() const
FilePath ExportDialog::exportFileName() const
{
return m_pathChooser->filePath().toString();
return m_pathChooser->filePath();
}
void ExportDialog::setExportFileName(const QString &f)
void ExportDialog::setExportFileName(const FilePath &f)
{
m_pathChooser->setFilePath(Utils::FilePath::fromString(f));
m_pathChooser->setFilePath(f);
}
ExportData ExportDialog::exportData() const
@@ -171,5 +172,4 @@ ExportData ExportDialog::exportData() const
return {exportFileName(), exportSize()};
}
} // namespace Internal
} // namespace ImageViewer
} // ImageViewer::Internal

View File

@@ -7,7 +7,10 @@
QT_FORWARD_DECLARE_CLASS(QSpinBox)
namespace Utils { class PathChooser; }
namespace Utils {
class FilePath;
class PathChooser;
} // Utils
namespace ImageViewer::Internal {
@@ -21,8 +24,8 @@ public:
QSize exportSize() const;
void setExportSize(const QSize &);
QString exportFileName() const;
void setExportFileName(const QString &);
Utils::FilePath exportFileName() const;
void setExportFileName(const Utils::FilePath &);
ExportData exportData() const;

View File

@@ -40,6 +40,8 @@ const char kSettingsBackground[] = "ShowBackground";
const char kSettingsOutline[] = "ShowOutline";
const char kSettingsFitToScreen[] = "FitToScreen";
using namespace Utils;
namespace ImageViewer {
namespace Constants {
const qreal DEFAULT_SCALE_FACTOR = 1.2;
@@ -157,25 +159,24 @@ QImage ImageView::renderSvg(const QSize &imageSize) const
bool ImageView::exportSvg(const ExportData &ed)
{
const bool result = renderSvg(ed.size).save(ed.fileName);
const bool result = renderSvg(ed.size).save(ed.filePath.toFSPathString());
if (result) {
const QString message = Tr::tr("Exported \"%1\", %2x%3, %4 bytes")
.arg(QDir::toNativeSeparators(ed.fileName))
.arg(ed.filePath.toUserOutput())
.arg(ed.size.width()).arg(ed.size.height())
.arg(QFileInfo(ed.fileName).size());
.arg(ed.filePath.fileSize());
Core::MessageManager::writeDisrupting(message);
} else {
const QString message = Tr::tr("Could not write file \"%1\".").arg(QDir::toNativeSeparators(ed.fileName));
const QString message = Tr::tr("Could not write file \"%1\".").arg(ed.filePath.toUserOutput());
QMessageBox::critical(this, Tr::tr("Export Image"), message);
}
return result;
}
#ifndef QT_NO_SVG
static QString suggestedExportFileName(const QFileInfo &fi)
static FilePath suggestedExportFileName(const FilePath &fi)
{
return fi.absolutePath() + QLatin1Char('/') + fi.baseName()
+ QStringLiteral(".png");
return fi.absolutePath().pathAppended(fi.baseName() + ".png");
}
#endif
@@ -195,11 +196,11 @@ void ImageView::exportImage()
auto svgItem = qgraphicsitem_cast<QGraphicsSvgItem *>(m_imageItem);
QTC_ASSERT(svgItem, return);
const QFileInfo origFi = m_file->filePath().toFileInfo();
const FilePath origPath = m_file->filePath();
ExportDialog exportDialog(this);
exportDialog.setWindowTitle(Tr::tr("Export %1").arg(origFi.fileName()));
exportDialog.setWindowTitle(Tr::tr("Export %1").arg(origPath.fileName()));
exportDialog.setExportSize(svgSize());
exportDialog.setExportFileName(suggestedExportFileName(origFi));
exportDialog.setExportFileName(suggestedExportFileName(origPath));
while (exportDialog.exec() == QDialog::Accepted && !exportSvg(exportDialog.exportData())) {}
#endif // !QT_NO_SVG
@@ -210,14 +211,13 @@ void ImageView::exportMultiImages()
#ifndef QT_NO_SVG
QTC_ASSERT(qgraphicsitem_cast<QGraphicsSvgItem *>(m_imageItem), return);
const QFileInfo origFi = m_file->filePath().toFileInfo();
const FilePath origPath = m_file->filePath();
const QSize size = svgSize();
const QString title =
Tr::tr("Export a Series of Images from %1 (%2x%3)")
.arg(origFi.fileName()).arg(size.width()).arg(size.height());
const QString title = Tr::tr("Export a Series of Images from %1 (%2x%3)")
.arg(origPath.fileName()).arg(size.width()).arg(size.height());
MultiExportDialog multiExportDialog;
multiExportDialog.setWindowTitle(title);
multiExportDialog.setExportFileName(suggestedExportFileName(origFi));
multiExportDialog.setExportFileName(suggestedExportFileName(origPath));
multiExportDialog.setSvgSize(size);
multiExportDialog.suggestSizes();

View File

@@ -4,6 +4,8 @@
#pragma once
#include <utils/filepath.h>
#include <QGraphicsView>
QT_FORWARD_DECLARE_CLASS(QImage)
@@ -17,7 +19,7 @@ namespace ImageViewer::Internal {
class ImageViewerFile;
struct ExportData {
QString fileName;
Utils::FilePath filePath;
QSize size;
};

View File

@@ -15,8 +15,6 @@
#include <utils/utilsicons.h>
#include <QDialogButtonBox>
#include <QDir>
#include <QFileInfo>
#include <QFormLayout>
#include <QLabel>
#include <QLineEdit>
@@ -27,6 +25,8 @@
#include <QToolButton>
#include <QWidgetAction>
using namespace Utils;
namespace ImageViewer::Internal {
static const int standardIconSizesValues[] = {16, 24, 32, 48, 64, 128, 256};
@@ -95,11 +95,11 @@ static QVector<QSize> stringToSizes(const QString &s)
return result;
}
static QString fileNameForSize(QString pattern, const QSize &s)
static FilePath fileNameForSize(QString pattern, const QSize &s)
{
pattern.replace("%1", QString::number(s.width()));
pattern.replace("%2", QString::number(s.height()));
return pattern;
return FilePath::fromString(pattern);
}
// Helpers for writing/reading the user-specified size specifications
@@ -236,7 +236,7 @@ void MultiExportDialog::suggestSizes()
QVector<ExportData> MultiExportDialog::exportData() const
{
const QVector<QSize> sizeList = sizes();
const QString pattern = exportFileName();
const QString pattern = exportFileName().toString();
QVector<ExportData> result;
result.reserve(sizeList.size());
for (const QSize &s : sizeList)
@@ -268,7 +268,7 @@ void MultiExportDialog::accept()
Tr::tr("Invalid size specification: %1").arg(sizeSpec));
return;
}
if (data.size() > 1 && data.at(0).fileName == data.at(1).fileName) {
if (data.size() > 1 && data.at(0).filePath == data.at(1).filePath) {
QMessageBox::warning(this, windowTitle(),
Tr::tr("The file name must contain one of the placeholders %1, %2.")
.arg(QString("%1"), QString("%2")));
@@ -277,17 +277,17 @@ void MultiExportDialog::accept()
writeSettings(m_svgSize, sizeSpec);
QStringList existingFiles;
FilePaths existingFiles;
for (const ExportData &d : data) {
if (QFileInfo::exists(d.fileName))
existingFiles.append(d.fileName);
if (d.filePath.exists())
existingFiles.append(d.filePath);
}
if (!existingFiles.isEmpty()) {
const QString message = existingFiles.size() == 1
? Tr::tr("The file %1 already exists.\nWould you like to overwrite it?")
.arg(QDir::toNativeSeparators(existingFiles.constFirst()))
.arg(existingFiles.constFirst().toUserOutput())
: Tr::tr("The files %1 already exist.\nWould you like to overwrite them?")
.arg(QDir::toNativeSeparators(existingFiles.join(", ")));
.arg(FilePath::formatFilePaths(existingFiles, ", "));
QMessageBox messageBox(QMessageBox::Question, windowTitle(), message,
QMessageBox::Yes | QMessageBox::No, this);
if (messageBox.exec() != QMessageBox::Yes)
@@ -297,17 +297,21 @@ void MultiExportDialog::accept()
QDialog::accept();
}
QString MultiExportDialog::exportFileName() const
FilePath MultiExportDialog::exportFileName() const
{
return m_pathChooser->filePath().toString();
return m_pathChooser->filePath();
}
void MultiExportDialog::setExportFileName(QString f)
void MultiExportDialog::setExportFileName(const FilePath &filePath)
{
const int lastDot = f.lastIndexOf('.');
if (lastDot != -1)
f.insert(lastDot, "-%1");
m_pathChooser->setFilePath(Utils::FilePath::fromString(f));
FilePath f = filePath;
QString ff = f.path();
const int lastDot = ff.lastIndexOf('.');
if (lastDot != -1) {
ff.insert(lastDot, "-%1");
f = f.withNewPath(ff);
};
m_pathChooser->setFilePath(f);
}
} // ImageViewer:Internal

View File

@@ -4,14 +4,16 @@
#pragma once
#include <QDialog>
#include <QPair>
#include <QSize>
#include <QVector>
QT_FORWARD_DECLARE_CLASS(QLineEdit)
namespace Utils { class PathChooser; }
namespace Utils {
class FilePath;
class PathChooser;
} // Utils
namespace ImageViewer::Internal {
@@ -19,12 +21,11 @@ struct ExportData;
class MultiExportDialog : public QDialog
{
Q_OBJECT
public:
explicit MultiExportDialog(QWidget *parent = nullptr);
QString exportFileName() const;
void setExportFileName(QString);
Utils::FilePath exportFileName() const;
void setExportFileName(const Utils::FilePath &);
void accept() override;