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

View File

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

View File

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

View File

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

View File

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

View File

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