Skip to content

Commit

Permalink
feat: improve image dimension detection in property dialog
Browse files Browse the repository at this point in the history
- Add QImageReader as fallback method when properties don't contain width/height
- Optimize condition checks in imageExtenInfo function
- Add QImageReader header inclusion
- Add comments to explain the fallback logic

This change enhances the reliability of image dimension detection,
especially for image formats that don't provide dimension information
in their properties.

Log: improve image dimension detection in property dialog
Bug: https://pms.uniontech.com/bug-view-300075.html
  • Loading branch information
Lighto-Ku committed Jan 20, 2025
1 parent 866275b commit 357d792
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/plugins/common/dfmplugin-propertydialog/views/basicwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <QApplication>

Check warning on line 28 in src/plugins/common/dfmplugin-propertydialog/views/basicwidget.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QApplication> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 28 in src/plugins/common/dfmplugin-propertydialog/views/basicwidget.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QApplication> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QSet>

Check warning on line 29 in src/plugins/common/dfmplugin-propertydialog/views/basicwidget.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QSet> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 29 in src/plugins/common/dfmplugin-propertydialog/views/basicwidget.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QSet> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QDBusInterface>

Check warning on line 30 in src/plugins/common/dfmplugin-propertydialog/views/basicwidget.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDBusInterface> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 30 in src/plugins/common/dfmplugin-propertydialog/views/basicwidget.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QDBusInterface> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QImageReader>

Check warning on line 31 in src/plugins/common/dfmplugin-propertydialog/views/basicwidget.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QImageReader> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 31 in src/plugins/common/dfmplugin-propertydialog/views/basicwidget.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QImageReader> not found. Please note: Cppcheck does not need standard library headers to get proper results.

static constexpr int kSpacingHeight { 2 };
static constexpr int kLeftContentsMargins { 0 };
Expand Down Expand Up @@ -420,15 +421,29 @@ void BasicWidget::closeEvent(QCloseEvent *event)

void BasicWidget::imageExtenInfo(const QUrl &url, QMap<DFMIO::DFileInfo::AttributeExtendID, QVariant> properties)
{
if (url != currentUrl) {
return;
}

if (properties.isEmpty()) {
if (url != currentUrl || properties.isEmpty()) {
return;
}

// Try to get dimensions from properties
int width = properties[DFileInfo::AttributeExtendID::kExtendMediaWidth].toInt();
int height = properties[DFileInfo::AttributeExtendID::kExtendMediaHeight].toInt();

// not all formats of image files have width and height in properties
// if properties failed, use QImageReader as fallback
if (width == 0 || height == 0) {
QImageReader reader(url.toLocalFile());
if (reader.canRead()) {
QSize size = reader.size();
width = size.width();
height = size.height();
}
}

if (width == 0 || height == 0) {
return;
}

const QString &imgSizeStr = QString::number(width) + "x" + QString::number(height);

fileMediaResolution->setRightValue(imgSizeStr, Qt::ElideNone, Qt::AlignVCenter, true);
Expand Down

0 comments on commit 357d792

Please sign in to comment.