Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

Commit

Permalink
Merge pull request #263 from sandsmark/master
Browse files Browse the repository at this point in the history
Fix some regressions from Shawn's changes (I should've tested those more before pulling them in), and some minor exiv2 stuff
  • Loading branch information
oferkv committed May 21, 2020
2 parents 9995384 + b2a743a commit 6b2198b
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 29 deletions.
44 changes: 36 additions & 8 deletions ImageViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,40 @@
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))

namespace { // anonymous, not visible outside of this file
Q_DECLARE_LOGGING_CATEGORY(PHOTOTONIC_EXIV2_LOG)
Q_LOGGING_CATEGORY(PHOTOTONIC_EXIV2_LOG, "phototonic.exif", QtCriticalMsg)

struct Exiv2LogHandler {
static void handleMessage(int level, const char *message) {
switch(level) {
case Exiv2::LogMsg::debug:
qCDebug(PHOTOTONIC_EXIV2_LOG) << message;
break;
case Exiv2::LogMsg::info:
qCInfo(PHOTOTONIC_EXIV2_LOG) << message;
break;
case Exiv2::LogMsg::warn:
case Exiv2::LogMsg::error:
case Exiv2::LogMsg::mute:
qCWarning(PHOTOTONIC_EXIV2_LOG) << message;
break;
default:
qCWarning(PHOTOTONIC_EXIV2_LOG) << "unhandled log level" << level << message;
break;
}
}

Exiv2LogHandler() {
Exiv2::LogMsg::setHandler(&Exiv2LogHandler::handleMessage);
}
};
}

ImageViewer::ImageViewer(QWidget *parent, MetadataCache *metadataCache) : QWidget(parent) {
// This is a threadsafe way to ensure that we only register it once
static Exiv2LogHandler handler;

this->phototonic = (Phototonic *) parent;
this->metadataCache = metadataCache;
cursorIsHidden = false;
Expand All @@ -44,7 +77,7 @@ ImageViewer::ImageViewer(QWidget *parent, MetadataCache *metadataCache) : QWidge
scrollArea->horizontalScrollBar()->blockSignals(true);
scrollArea->setFrameStyle(0);
scrollArea->setWidget(imageWidget);
scrollArea->setWidgetResizable(true);
scrollArea->setWidgetResizable(false);
setBackgroundColor();

QVBoxLayout *scrollLayout = new QVBoxLayout;
Expand Down Expand Up @@ -118,7 +151,7 @@ void ImageViewer::resizeImage() {

int imageViewWidth = this->size().width();
int imageViewHeight = this->size().height();
QSize imageSize = isAnimation ? animation->currentPixmap().size() : imageWidget->size();
QSize imageSize = isAnimation ? animation->currentPixmap().size() : imageWidget->imageSize();

if (tempDisableResize) {
imageSize.scale(imageSize.width(), imageSize.height(), Qt::KeepAspectRatio);
Expand Down Expand Up @@ -239,12 +272,7 @@ void ImageViewer::showEvent(QShowEvent *event) {
}

void ImageViewer::centerImage(QSize &imgSize) {
int newX = (this->size().width() - imgSize.width()) / 2;
int newY = (this->size().height() - imgSize.height()) / 2;

if (newX != imageWidget->pos().x() || newY != imageWidget->pos().y()) {
imageWidget->move(newX, newY);
}
scrollArea->ensureVisible(imgSize.width()/2, imgSize.height()/2, width()/2, height()/2);
}

void ImageViewer::rotateByExifRotation(QImage &image, QString &imageFullPath) {
Expand Down
11 changes: 7 additions & 4 deletions ImageWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,19 @@ QSize ImageWidget::sizeHint() const

void ImageWidget::paintEvent(QPaintEvent *)
{
float scale = qMax(float(width()) / m_image.width(), float(height()) / m_image.height());

QPainter painter(this);
painter.scale(scale, scale);
painter.setRenderHint(QPainter::Antialiasing);
QPoint center(width() / 2, height() / 2);
painter.translate(center);
painter.rotate(m_rotation);
painter.translate(center * -1);
QPoint upperLeft;
if (width() > m_image.width())
upperLeft.setX(center.x() - m_image.width() / 2);
if (height() > m_image.height())
upperLeft.setY(center.y() - m_image.height() / 2);
if (width() > m_image.width() * scale)
upperLeft.setX(center.x() - scale*m_image.width() / 2);
if (height() > m_image.height() * scale)
upperLeft.setY(center.y() - scale*m_image.height() / 2);
painter.drawImage(upperLeft, m_image);
}
32 changes: 21 additions & 11 deletions MetadataCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,29 +72,39 @@ bool MetadataCache::loadImageMetadata(const QString &imageFullPath) {
exifImage = Exiv2::ImageFactory::open(imageFullPath.toStdString());
exifImage->readMetadata();
} catch (Exiv2::Error &error) {
qWarning() << "Error loading image for reading metadata" << error.what();
return false;
}

try {
Exiv2::ExifData &exifData = exifImage->exifData();
if (!exifData.empty()) {
orientation = exifData["Exif.Image.Orientation"].value().toLong();
if (!exifImage->good()) {
return false;
}

if (exifImage->supportsMetadata(Exiv2::mdExif)) try {
Exiv2::ExifData::const_iterator it = Exiv2::orientation(exifImage->exifData());
if (it != exifImage->exifData().end()) {
orientation = it->toLong();
}
} catch (Exiv2::Error &error) {
qWarning() << "Failed to read Exif metadata";
qWarning() << "Failed to read Exif metadata" << error.what();
}

try {
if (exifImage->supportsMetadata(Exiv2::mdIptc)) try {
Exiv2::IptcData &iptcData = exifImage->iptcData();
if (!iptcData.empty()) {
QString key;
Exiv2::IptcData::iterator end = iptcData.end();
for (Exiv2::IptcData::iterator iptcIt = iptcData.begin(); iptcIt != end; ++iptcIt) {
if (iptcIt->tagName() == "Keywords") {
QString tagName = QString::fromUtf8(iptcIt->toString().c_str());
tags.insert(tagName);
Settings::knownTags.insert(tagName);

// Finds the first ID, but we need to loop over the rest in case there are more
Exiv2::IptcData::iterator iptcIt = iptcData.findId(Exiv2::IptcDataSets::Keywords);
for (; iptcIt != end; ++iptcIt) {
if (iptcIt->tag() != Exiv2::IptcDataSets::Keywords) {
continue;
}

QString tagName = QString::fromUtf8(iptcIt->toString().c_str());
tags.insert(tagName);
Settings::knownTags.insert(tagName);
}
}
} catch (Exiv2::Error &error) {
Expand Down
18 changes: 12 additions & 6 deletions ThumbsViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ void ThumbsViewer::loadSubDirectories() {
return;
}
}
QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
QApplication::processEvents();
}

onSelectionChanged();
Expand Down Expand Up @@ -596,7 +596,7 @@ void ThumbsViewer::loadDuplicates()
goto finish;
}
}
QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
QApplication::processEvents();
}
}

Expand Down Expand Up @@ -662,7 +662,7 @@ void ThumbsViewer::initThumbs() {
++thumbsAddedCounter;
if (thumbsAddedCounter > 100) {
thumbsAddedCounter = 1;
QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
QApplication::processEvents();
}
}

Expand Down Expand Up @@ -754,7 +754,7 @@ void ThumbsViewer::findDupes(bool resetCounters)
++processEventsCounter;
if (processEventsCounter > 9) {
processEventsCounter = 0;
QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
QApplication::processEvents();
}

updateFoundDupesState(foundDups, totalFiles, originalImages);
Expand Down Expand Up @@ -791,7 +791,12 @@ void ThumbsViewer::loadAllThumbs() {
if (thumbsViewerModel->item(i)->data(LoadedRole).toBool())
continue;
loadThumb(i);
QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);

if (isAbortThumbsLoading) {
break;
}

QApplication::processEvents();
}
}

Expand Down Expand Up @@ -820,7 +825,8 @@ void ThumbsViewer::loadThumbsRange() {
continue;

loadThumb(currThumb);
QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);

QApplication::processEvents();
}

isInProgress = false;
Expand Down

0 comments on commit 6b2198b

Please sign in to comment.