Skip to content

Commit

Permalink
feat: handling of broken image paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Onetchou committed Aug 17, 2024
1 parent 5a6980f commit f80d57a
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 37 deletions.
28 changes: 1 addition & 27 deletions src/app/seamly2d/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand Down Expand Up @@ -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
//---------------------------------------------------------------------------------------------------------------------
/**
Expand Down
1 change: 0 additions & 1 deletion src/app/seamly2d/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,6 @@ private slots:
void handlePieceMenu();
void handleLayoutMenu();
void handleImagesMenu();
QString getImageFilename();


void CancelTool();
Expand Down
1 change: 1 addition & 0 deletions src/app/seamly2d/xml/vpattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
81 changes: 72 additions & 9 deletions src/libs/tools/images/image_tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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 <br/><br/> <b>%1</b> <br/><br/> 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();
Expand Down
3 changes: 3 additions & 0 deletions src/libs/tools/images/image_tool.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
#include "../ifc/xml/vabstractpattern.h"
#include "vmaingraphicsscene.h"

QString getImageFilename(QWidget *parent);


class ImageTool : public QObject
{
Q_OBJECT
Expand Down

0 comments on commit f80d57a

Please sign in to comment.