Skip to content

Commit 4aebdf9

Browse files
committed
Allow showing larger images and show error if too big
1 parent 3e39ad9 commit 4aebdf9

File tree

3 files changed

+152
-102
lines changed

3 files changed

+152
-102
lines changed

src/viewer/ascent_viewer.cpp

+37-1
Original file line numberDiff line numberDiff line change
@@ -490,9 +490,30 @@ void AscentViewer::changeToPhoto(int photoIndex, bool saveDescriptionFirst)
490490
}
491491
else {
492492
QString filepath = photos.at(currentPhotoIndex).filepath;
493+
QImageReader::setAllocationLimit(512);
494+
495+
// Set up error message capturing
496+
imageLoadErrorMessage.clear();
497+
qInstallMessageHandler([] (QtMsgType type, const QMessageLogContext& context, const QString& msg) {
498+
Q_UNUSED(type);
499+
Q_UNUSED(context);
500+
if (!msg.startsWith("QImageIOHandler: ")) return;
501+
QString trimmedMessage = msg;
502+
trimmedMessage.remove("QImageIOHandler: ");
503+
AscentViewer::imageErrorMessageOccurred(trimmedMessage);
504+
});
505+
493506
QImageReader reader = QImageReader(filepath);
494507
reader.setAutoTransform(true);
495508
QImage image = reader.read();
509+
510+
// Fetch printed error message
511+
qInstallMessageHandler(nullptr);
512+
QString extraErrorMessage = imageLoadErrorMessage;
513+
imageLoadErrorMessage.clear();
514+
if (!extraErrorMessage.isEmpty()) {
515+
extraErrorMessage = tr("\nMore details: %1.").arg(extraErrorMessage);
516+
}
496517

497518
if (!image.isNull()) { // Image loaded
498519
// Prepare image frame to show image
@@ -504,8 +525,9 @@ void AscentViewer::changeToPhoto(int photoIndex, bool saveDescriptionFirst)
504525
QString labelText = tr(
505526
"This image file cannot be shown:\n%1"
506527
"\nReason: %2."
528+
"%3"
507529
"\n\nYou can remove the image, replace the file, or mass relocate image files in the whole database.")
508-
.arg(filepath, reader.errorString()); // Error string is already translated
530+
.arg(filepath, reader.errorString(), extraErrorMessage); // Error string is already translated
509531
imageErrorLabel->setText(labelText);
510532

511533
// Show image error box
@@ -1011,6 +1033,20 @@ void AscentViewer::handle_filesDropped(QStringList filepaths)
10111033

10121034

10131035

1036+
/**
1037+
* Globally accessible static function for receiving error messages from QImageReader.
1038+
*
1039+
* This function is used because qInstallMessageHandler() does not allow capturing in lambdas.
1040+
*
1041+
* @param message The error message with its prefix already removed.
1042+
*/
1043+
void AscentViewer::imageErrorMessageOccurred(const QString& message)
1044+
{
1045+
imageLoadErrorMessage = message;
1046+
}
1047+
1048+
1049+
10141050
/**
10151051
* Spawns the info area context menu at the given position and controls the enabled state of its
10161052
* actions.

src/viewer/ascent_viewer.h

+7
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ class AscentViewer : public QDialog, public Ui_AscentViewer {
9999
/** Context menu action for editing the current ascent's trip. */
100100
QAction* editTripAction;
101101

102+
private:
103+
/** Temporary global static variable for error messages printed when loading images. */
104+
inline static QString imageLoadErrorMessage = QString();
105+
102106
public:
103107
AscentViewer(MainWindow* parent, Database* db, const ItemTypesHandler* typesHandler, ViewRowIndex viewRowIndex);
104108
virtual ~AscentViewer();
@@ -172,6 +176,9 @@ private slots:
172176
// Files dropped on image frame
173177
void handle_filesDropped(QStringList filepaths);
174178

179+
// Error message capture
180+
static void imageErrorMessageOccurred(const QString& message);
181+
175182
private:
176183
// Helpers
177184
void popupInfoContextMenu(QPoint pos);

0 commit comments

Comments
 (0)