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

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

View File

@ -54,4 +54,19 @@ void FileReader::getTextContent(const QString &path, QString &textContent, const
break;
}
}
}
}
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
#include <extractor-manager.h>
#include <QImage>
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();

View File

@ -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"

View File

@ -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;
};
}