If a new image for a product is loaded, check if the path is available. If not, use the home directory.

Mon, 23 Jan 2023 17:23:10 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Mon, 23 Jan 2023 17:23:10 +0100
changeset 472
db8ad1c2112b
parent 471
0836bfcc312c
child 473
0d7441b39d41

If a new image for a product is loaded, check if the path is available. If not, use the home directory.

src/EditProductTab13.cpp file | annotate | diff | comparison | revisions
--- a/src/EditProductTab13.cpp	Sun Jan 22 19:02:54 2023 +0100
+++ b/src/EditProductTab13.cpp	Mon Jan 23 17:23:10 2023 +0100
@@ -146,28 +146,33 @@
 
 void EditProduct::addImage_clicked()
 {
-    QString fileName;
+    QString fileName, dirName;
     QByteArray imageByteArray;
     QSqlQuery query;
     QSettings settings(QSettings::IniFormat, QSettings::UserScope, "mbse", "bmsapp");
 
+    /*
+     * First check if the directory stored in the settings file exists.
+     * It might be on a removable media that was last used ...
+     * If so, fallback to the user's home directory.
+     */
+    dirName = settings.value("paths/images").toString();
+    if (! QDir(dirName).exists()) {
+	dirName = QDir::homePath();
+    }
+
     QFileDialog dialog1(this, tr("Open File"));
-    dialog1.setDirectory(settings.value("paths/images").toString());
-    /* Only a few image formats are valid */
-    QStringList mimeTypeFilters ({ "image/bmp", "image/gif", "image/jpeg", "image/png", "image/svg+xml" });
-    dialog1.setMimeTypeFilters(mimeTypeFilters);
+    dialog1.setDirectory(dirName);
     dialog1.setNameFilter("Images (*.bmp *.BMP *.gif *.GIF *.jpg *.JPG *.jpeg *.JPEG *.png *.PNG *.svg *.SVG)");
     dialog1.setAcceptMode(QFileDialog::AcceptOpen);
-//    dialog1.setOption(QFileDialog::DontUseNativeDialog);
     if (dialog1.exec() != QDialog::Accepted)
 	return;
-
     /*
      * Save our current path
      */
     settings.setValue("paths/images", dialog1.directory().absolutePath());
+    fileName = dialog1.selectedFiles().constFirst();
 
-    fileName = dialog1.selectedFiles().constFirst();
     QImageReader reader(fileName);
     reader.setAutoTransform(true);
     const QImage newImage = reader.read();

mercurial