144 } |
144 } |
145 |
145 |
146 |
146 |
147 void EditProduct::addImage_clicked() |
147 void EditProduct::addImage_clicked() |
148 { |
148 { |
149 QString fileName; |
149 QString fileName, dirName; |
150 QByteArray imageByteArray; |
150 QByteArray imageByteArray; |
151 QSqlQuery query; |
151 QSqlQuery query; |
152 QSettings settings(QSettings::IniFormat, QSettings::UserScope, "mbse", "bmsapp"); |
152 QSettings settings(QSettings::IniFormat, QSettings::UserScope, "mbse", "bmsapp"); |
153 |
153 |
|
154 /* |
|
155 * First check if the directory stored in the settings file exists. |
|
156 * It might be on a removable media that was last used ... |
|
157 * If so, fallback to the user's home directory. |
|
158 */ |
|
159 dirName = settings.value("paths/images").toString(); |
|
160 if (! QDir(dirName).exists()) { |
|
161 dirName = QDir::homePath(); |
|
162 } |
|
163 |
154 QFileDialog dialog1(this, tr("Open File")); |
164 QFileDialog dialog1(this, tr("Open File")); |
155 dialog1.setDirectory(settings.value("paths/images").toString()); |
165 dialog1.setDirectory(dirName); |
156 /* Only a few image formats are valid */ |
|
157 QStringList mimeTypeFilters ({ "image/bmp", "image/gif", "image/jpeg", "image/png", "image/svg+xml" }); |
|
158 dialog1.setMimeTypeFilters(mimeTypeFilters); |
|
159 dialog1.setNameFilter("Images (*.bmp *.BMP *.gif *.GIF *.jpg *.JPG *.jpeg *.JPEG *.png *.PNG *.svg *.SVG)"); |
166 dialog1.setNameFilter("Images (*.bmp *.BMP *.gif *.GIF *.jpg *.JPG *.jpeg *.JPEG *.png *.PNG *.svg *.SVG)"); |
160 dialog1.setAcceptMode(QFileDialog::AcceptOpen); |
167 dialog1.setAcceptMode(QFileDialog::AcceptOpen); |
161 // dialog1.setOption(QFileDialog::DontUseNativeDialog); |
|
162 if (dialog1.exec() != QDialog::Accepted) |
168 if (dialog1.exec() != QDialog::Accepted) |
163 return; |
169 return; |
164 |
|
165 /* |
170 /* |
166 * Save our current path |
171 * Save our current path |
167 */ |
172 */ |
168 settings.setValue("paths/images", dialog1.directory().absolutePath()); |
173 settings.setValue("paths/images", dialog1.directory().absolutePath()); |
169 |
|
170 fileName = dialog1.selectedFiles().constFirst(); |
174 fileName = dialog1.selectedFiles().constFirst(); |
|
175 |
171 QImageReader reader(fileName); |
176 QImageReader reader(fileName); |
172 reader.setAutoTransform(true); |
177 reader.setAutoTransform(true); |
173 const QImage newImage = reader.read(); |
178 const QImage newImage = reader.read(); |
174 if (newImage.isNull()) { |
179 if (newImage.isNull()) { |
175 QMessageBox::information(this, QGuiApplication::applicationDisplayName(), tr("Cannot load %1: %2") |
180 QMessageBox::information(this, QGuiApplication::applicationDisplayName(), tr("Cannot load %1: %2") |