perf(file-content-search):使用元数据缩略图接口生成图片缩略图

This commit is contained in:
iaom 2024-01-23 15:02:35 +08:00
parent 933c2fa881
commit 72aa0b3aaf
4 changed files with 32 additions and 10 deletions

View File

@ -55,3 +55,18 @@ void FileReader::getTextContent(const QString &path, QString &textContent, const
} }
} }
} }
QImage FileReader::getThumbnail(const QString &path, const QSize &targetSize, qreal dpr)
{
QString mimeType = UkuiFileMetadata::MimeUtils::strictMimeType(path, {}).name();
QList<UkuiFileMetadata::Extractor*> 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 {};
}

View File

@ -21,12 +21,14 @@
#define FILEREADER_H #define FILEREADER_H
#include <extractor-manager.h> #include <extractor-manager.h>
#include <QImage>
namespace UkuiSearch { namespace UkuiSearch {
class FileReader{ class FileReader{
public: public:
static FileReader* getInstance(); static FileReader* getInstance();
~FileReader() = default; ~FileReader() = default;
void getTextContent(const QString &path, QString &textContent, const QString &suffix); void getTextContent(const QString &path, QString &textContent, const QString &suffix);
QImage getThumbnail(const QString &path, const QSize &targetSize, qreal dpr);
private: private:
FileReader(); FileReader();

View File

@ -28,6 +28,7 @@
#include "file-indexer-config.h" #include "file-indexer-config.h"
#include "libkydate.h" #include "libkydate.h"
#include "data-collecter.h" #include "data-collecter.h"
#include "file-reader.h"
#define OCR_ICONLABLE_WITH 352 #define OCR_ICONLABLE_WITH 352
#define OCR_ICONLABLE_HEIGHT 247 #define OCR_ICONLABLE_HEIGHT 247
@ -599,23 +600,23 @@ QWidget *FileContentSearchPlugin::detailPage(const ResultInfo &ri)
if(ri.actionKey == m_currentActionKey) { if(ri.actionKey == m_currentActionKey) {
return m_detailPage; return m_detailPage;
} }
if (1 == ri.type) { if (1 == ri.type) {
auto creator = new ThumbnailCreator(ri.actionKey); auto creator = new ThumbnailCreator(ri.actionKey, QSize(OCR_ICONLABLE_WITH, OCR_ICONLABLE_HEIGHT), m_detailPage->window()->devicePixelRatio());
connect(creator, &ThumbnailCreator::ready, this, [&](QString uri, const QImage &image){ connect(creator, &ThumbnailCreator::ready, this, [&, ri](const QString& uri, const QImage &image){
if(uri != m_currentActionKey) { if(uri != m_currentActionKey) {
return; return;
} }
QPixmap pixmap = QPixmap::fromImage(image); QPixmap pixmap = QPixmap::fromImage(image);
if(!pixmap.isNull()) { 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); m_detailLyt->setContentsMargins(8, (OCR_ICONLABLE_HEIGHT-pixmap.height())/2 + 8, 16, 0);
} else { } else {
m_iconLabel->setPixmap(ri.icon.pixmap(120, 120)); m_iconLabel->setPixmap(ri.icon.pixmap(120, 120));
} }
}, Qt::QueuedConnection); }, Qt::QueuedConnection);
m_thumbnailPool.start(creator); m_thumbnailPool.start(creator);
m_iconLabel->setPixmap(ri.icon.pixmap(120, 120));
m_iconLabel->setPixmap({});
m_detailLyt->setContentsMargins(8, 50, 16, 0); m_detailLyt->setContentsMargins(8, 50, 16, 0);
m_pluginLabel->setText(tr("OCR")); m_pluginLabel->setText(tr("OCR"));
m_snippetLabel->hide(); m_snippetLabel->hide();
@ -707,7 +708,7 @@ void FileContentSearchPlugin::initDetailPage()
m_detailLyt->setContentsMargins(8, 50, 16, 0); m_detailLyt->setContentsMargins(8, 50, 16, 0);
m_iconLabel = new QLabel(m_detailPage); m_iconLabel = new QLabel(m_detailPage);
m_iconLabel->setAlignment(Qt::AlignCenter); m_iconLabel->setAlignment(Qt::AlignCenter);
//m_iconLabel->setFixedHeight(128); m_iconLabel->setMinimumHeight(120);
m_nameFrame = new QFrame(m_detailPage); m_nameFrame = new QFrame(m_detailPage);
m_nameFrameLyt = new QHBoxLayout(m_nameFrame); m_nameFrameLyt = new QHBoxLayout(m_nameFrame);
@ -798,14 +799,16 @@ void FileContentSearchPlugin::initDetailPage()
// return nullptr; // 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); setAutoDelete(true);
m_url = url; m_url = url;
m_targetSize = targetSize;
m_dpr = dpr;
} }
void ThumbnailCreator::run() 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" #include "file-search-plugin.moc"

View File

@ -209,7 +209,7 @@ class ThumbnailCreator : public QObject, public QRunnable
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit ThumbnailCreator(QString url, QObject *parent = nullptr); explicit ThumbnailCreator(const QString &url, const QSize &targetSize, qreal dpr, QObject *parent = nullptr);
protected: protected:
void run() override; void run() override;
@ -219,6 +219,8 @@ Q_SIGNALS:
private: private:
QString m_url; QString m_url;
QSize m_targetSize;
qreal m_dpr;
}; };
} }