diff --git a/libsearch/index/file-reader.cpp b/libsearch/index/file-reader.cpp index ad030e7..8dd1a7c 100644 --- a/libsearch/index/file-reader.cpp +++ b/libsearch/index/file-reader.cpp @@ -54,4 +54,19 @@ void FileReader::getTextContent(const QString &path, QString &textContent, const break; } } -} \ No newline at end of file +} + +QImage FileReader::getThumbnail(const QString &path, const QSize &targetSize, qreal dpr) +{ + QString mimeType = UkuiFileMetadata::MimeUtils::strictMimeType(path, {}).name(); + QList extractors = m_extractorManager.fetchExtractors(mimeType); + FileExtractionResult result(path, mimeType, UkuiFileMetadata::ExtractionResult::Flag::ExtractThumbnail); + result.setThumbnailRequest(UkuiFileMetadata::ThumbnailRequest(targetSize, dpr)); + for(auto extractor : extractors) { + extractor->extract(&result); + if(result.thumbnail().isValid()) { + return result.thumbnail().image(); + } + } + return {}; +} diff --git a/libsearch/index/file-reader.h b/libsearch/index/file-reader.h index ef4865f..5d55cb5 100644 --- a/libsearch/index/file-reader.h +++ b/libsearch/index/file-reader.h @@ -21,12 +21,14 @@ #define FILEREADER_H #include +#include namespace UkuiSearch { class FileReader{ public: static FileReader* getInstance(); ~FileReader() = default; void getTextContent(const QString &path, QString &textContent, const QString &suffix); + QImage getThumbnail(const QString &path, const QSize &targetSize, qreal dpr); private: FileReader(); diff --git a/libsearch/index/file-search-plugin.cpp b/libsearch/index/file-search-plugin.cpp index bd60ad0..417770c 100644 --- a/libsearch/index/file-search-plugin.cpp +++ b/libsearch/index/file-search-plugin.cpp @@ -28,6 +28,7 @@ #include "file-indexer-config.h" #include "libkydate.h" #include "data-collecter.h" +#include "file-reader.h" #define OCR_ICONLABLE_WITH 352 #define OCR_ICONLABLE_HEIGHT 247 @@ -599,23 +600,23 @@ QWidget *FileContentSearchPlugin::detailPage(const ResultInfo &ri) if(ri.actionKey == m_currentActionKey) { return m_detailPage; } + if (1 == ri.type) { - auto creator = new ThumbnailCreator(ri.actionKey); - connect(creator, &ThumbnailCreator::ready, this, [&](QString uri, const QImage &image){ + auto creator = new ThumbnailCreator(ri.actionKey, QSize(OCR_ICONLABLE_WITH, OCR_ICONLABLE_HEIGHT), m_detailPage->window()->devicePixelRatio()); + connect(creator, &ThumbnailCreator::ready, this, [&, ri](const QString& uri, const QImage &image){ if(uri != m_currentActionKey) { return; } QPixmap pixmap = QPixmap::fromImage(image); if(!pixmap.isNull()) { - m_iconLabel->setPixmap(pixmap.scaled(OCR_ICONLABLE_WITH, OCR_ICONLABLE_HEIGHT, Qt::KeepAspectRatio, Qt::SmoothTransformation)); + m_iconLabel->setPixmap(pixmap); m_detailLyt->setContentsMargins(8, (OCR_ICONLABLE_HEIGHT-pixmap.height())/2 + 8, 16, 0); } else { m_iconLabel->setPixmap(ri.icon.pixmap(120, 120)); } }, Qt::QueuedConnection); m_thumbnailPool.start(creator); - - m_iconLabel->setPixmap({}); + m_iconLabel->setPixmap(ri.icon.pixmap(120, 120)); m_detailLyt->setContentsMargins(8, 50, 16, 0); m_pluginLabel->setText(tr("OCR")); m_snippetLabel->hide(); @@ -707,7 +708,7 @@ void FileContentSearchPlugin::initDetailPage() m_detailLyt->setContentsMargins(8, 50, 16, 0); m_iconLabel = new QLabel(m_detailPage); m_iconLabel->setAlignment(Qt::AlignCenter); - //m_iconLabel->setFixedHeight(128); + m_iconLabel->setMinimumHeight(120); m_nameFrame = new QFrame(m_detailPage); m_nameFrameLyt = new QHBoxLayout(m_nameFrame); @@ -798,14 +799,16 @@ void FileContentSearchPlugin::initDetailPage() // return nullptr; //} -ThumbnailCreator::ThumbnailCreator(QString url, QObject *parent): QObject(parent) +ThumbnailCreator::ThumbnailCreator(const QString &url, const QSize &targetSize, qreal dpr, QObject *parent): QObject(parent) { setAutoDelete(true); m_url = url; + m_targetSize = targetSize; + m_dpr = dpr; } void ThumbnailCreator::run() { - Q_EMIT ready(m_url, QImage(m_url)); + Q_EMIT ready(m_url, FileReader::getInstance()->getThumbnail(m_url, m_targetSize, m_dpr)); } #include "file-search-plugin.moc" diff --git a/libsearch/index/file-search-plugin.h b/libsearch/index/file-search-plugin.h index 427c622..385539c 100644 --- a/libsearch/index/file-search-plugin.h +++ b/libsearch/index/file-search-plugin.h @@ -209,7 +209,7 @@ class ThumbnailCreator : public QObject, public QRunnable { Q_OBJECT public: - explicit ThumbnailCreator(QString url, QObject *parent = nullptr); + explicit ThumbnailCreator(const QString &url, const QSize &targetSize, qreal dpr, QObject *parent = nullptr); protected: void run() override; @@ -219,6 +219,8 @@ Q_SIGNALS: private: QString m_url; + QSize m_targetSize; + qreal m_dpr; }; }