From f80d57ab646473e83360473834b164064d9b423e Mon Sep 17 00:00:00 2001 From: Onetchou <83351185+Onetchou@users.noreply.github.com> Date: Sat, 17 Aug 2024 17:42:07 +0200 Subject: [PATCH] feat: handling of broken image paths --- src/app/seamly2d/mainwindow.cpp | 28 +--------- src/app/seamly2d/mainwindow.h | 1 - src/app/seamly2d/xml/vpattern.cpp | 1 + src/libs/tools/images/image_tool.cpp | 81 ++++++++++++++++++++++++---- src/libs/tools/images/image_tool.h | 3 ++ 5 files changed, 77 insertions(+), 37 deletions(-) diff --git a/src/app/seamly2d/mainwindow.cpp b/src/app/seamly2d/mainwindow.cpp index dbe78cc31184..60d55265f28b 100644 --- a/src/app/seamly2d/mainwindow.cpp +++ b/src/app/seamly2d/mainwindow.cpp @@ -1739,7 +1739,7 @@ void MainWindow::handleImageTool() ui->draft_ToolBox->setCurrentWidget(ui->backgroundImage_Page); ui->importImage_ToolButton->setChecked(true); - QString filename = getImageFilename(); + QString filename = getImageFilename(this); if(!filename.isEmpty()) { @@ -1769,32 +1769,6 @@ void MainWindow::handleImageTool() } -//--------------------------------------------------------------------------------------------------------------------- -QString MainWindow::getImageFilename() -{ - const QString filter = tr("Images") + QLatin1String(" (*.bmp *.jpg *.png *.svg *.tf);;") + - "BMP" + QLatin1String(" (*.bmp);;") + - "JPG" + QLatin1String(" (*.jpg);;") + - "PNG" + QLatin1String(" (*.png);;") + - "SVG" + QLatin1String(" (*.svg);;") + - "TIF" + QLatin1String(" (*.tf)"); - - const QString path = qApp->Seamly2DSettings()->getImageFilePath(); - - bool usedNotExistedDir = false; - QDir directory(path); - if (!directory.exists()) - { - usedNotExistedDir = directory.mkpath("."); - } - - const QString filename = QFileDialog::getOpenFileName(this, tr("Open Image File"), path, filter, nullptr, - QFileDialog::DontUseNativeDialog); - - return filename; -} - - //Pieces //--------------------------------------------------------------------------------------------------------------------- /** diff --git a/src/app/seamly2d/mainwindow.h b/src/app/seamly2d/mainwindow.h index 60a24ef57586..f5beef18e6dc 100644 --- a/src/app/seamly2d/mainwindow.h +++ b/src/app/seamly2d/mainwindow.h @@ -382,7 +382,6 @@ private slots: void handlePieceMenu(); void handleLayoutMenu(); void handleImagesMenu(); - QString getImageFilename(); void CancelTool(); diff --git a/src/app/seamly2d/xml/vpattern.cpp b/src/app/seamly2d/xml/vpattern.cpp index 599eb809eaa5..9a4069dd0af2 100644 --- a/src/app/seamly2d/xml/vpattern.cpp +++ b/src/app/seamly2d/xml/vpattern.cpp @@ -979,6 +979,7 @@ void VPattern::parseImageElement(QDomElement &domElement, const Document &parse) else { image_tool->deleteLater(); + ParentNodeById(image.id).removeChild(domElement); //this way the broken image is not light-parsed in the future } } else diff --git a/src/libs/tools/images/image_tool.cpp b/src/libs/tools/images/image_tool.cpp index 21af1b6d46e2..308a4c239360 100644 --- a/src/libs/tools/images/image_tool.cpp +++ b/src/libs/tools/images/image_tool.cpp @@ -44,6 +44,32 @@ Q_LOGGING_CATEGORY(vImageTool, "v.imageTool") +//--------------------------------------------------------------------------------------------------------------------- +QString getImageFilename(QWidget *parent) +{ + const QString filter = QObject::tr("Images") + QLatin1String(" (*.bmp *.jpg *.png *.svg *.tf);;") + + "BMP" + QLatin1String(" (*.bmp);;") + + "JPG" + QLatin1String(" (*.jpg);;") + + "PNG" + QLatin1String(" (*.png);;") + + "SVG" + QLatin1String(" (*.svg);;") + + "TIF" + QLatin1String(" (*.tf)"); + + const QString path = qApp->Seamly2DSettings()->getImageFilePath(); + + bool usedNotExistedDir = false; + QDir directory(path); + if (!directory.exists()) + { + usedNotExistedDir = directory.mkpath("."); + } + + const QString filename = QFileDialog::getOpenFileName(parent, QObject::tr("Open Image File"), path, filter, nullptr, + QFileDialog::DontUseNativeDialog); + + return filename; +} + + //Constructor called from GUI ImageTool::ImageTool(QObject *parent, VAbstractPattern *doc, VMainGraphicsScene *draftScene, QString filename) : QObject(parent), @@ -71,26 +97,63 @@ ImageTool::ImageTool(QObject *parent, VAbstractPattern *doc, VMainGraphicsScene } //Constructor called when file is full-parsed -ImageTool::ImageTool(QObject *parent, VAbstractPattern *doc, VMainGraphicsScene *draftScene, DraftImage image) +ImageTool::ImageTool(QObject *parent, VAbstractPattern *doc, VMainGraphicsScene *draftScene, DraftImage img) : QObject(parent), - image(image), + image(img), m_doc(doc), m_draftScene(draftScene) { qCDebug(vImageTool, "Image filename: %s", qUtf8Printable(image.filename)); - if (image.filename.isEmpty()) + QFileInfo fileInfo(image.filename); + + if (!fileInfo.exists()) { - qCDebug(vImageTool, "Can't load image"); - QMessageBox::critical(nullptr, tr("Import Image"), tr("Could not load the image."), QMessageBox::Ok); - return; + const QString text = tr("The image

%1

could not be found. Do you " + "want to update the file location?").arg(image.filename); + QMessageBox::StandardButton result = QMessageBox::question(nullptr, tr("Loading image"), text, + QMessageBox::Yes | QMessageBox::No, + QMessageBox::Yes); + if (result == QMessageBox::No) + { + return; + } + else + { + bool askAgain = true; + while(askAgain) + { + image.filename = getImageFilename(nullptr); + if (image.filename.isEmpty()) + { + qCDebug(vImageTool, "No image selected"); + QMessageBox::critical(nullptr, tr("Import Image"), tr("No image was selected..."), QMessageBox::Ok); + continue; + } + + QImageReader imageReader(image.filename); + + if(!imageReader.canRead()) + { + qCDebug(vImageTool, "Can't read image"); + QMessageBox::critical(nullptr, tr("Import Image"), tr("Could not read the image.") + "\n" + tr("File may be corrupted..."), QMessageBox::Ok); + continue; + } + askAgain = false; + } + image.name = fileInfo.baseName(); + + QDomElement domElement = m_doc->elementById(image.id, VAbstractPattern::TagDraftImage); + QDomElement newDomElement = domElement.cloneNode().toElement(); + m_doc->SetAttribute(newDomElement, VAbstractPattern::AttrName, image.name); + m_doc->SetAttribute(newDomElement, VAbstractPattern::AttrSource, image.filename); + domElement.parentNode().replaceChild(newDomElement, domElement); + } } - QFileInfo f(image.filename); - if (image.name.isEmpty()) { - image.name = f.baseName(); + image.name = fileInfo.baseName(); } addImage(); diff --git a/src/libs/tools/images/image_tool.h b/src/libs/tools/images/image_tool.h index 5d0c8857935b..d1ed6c50f7aa 100644 --- a/src/libs/tools/images/image_tool.h +++ b/src/libs/tools/images/image_tool.h @@ -36,6 +36,9 @@ #include "../ifc/xml/vabstractpattern.h" #include "vmaingraphicsscene.h" +QString getImageFilename(QWidget *parent); + + class ImageTool : public QObject { Q_OBJECT