Skip to content

Commit

Permalink
Add raw data preview support (#10)
Browse files Browse the repository at this point in the history
- Also adds psc as valid text preview
  • Loading branch information
Silarn authored Jul 11, 2024
1 parent 1bfa648 commit f47d910
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
33 changes: 24 additions & 9 deletions src/previewbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ bool PreviewBase::init(IOrganizer *moInfo)
const QStringList& blacklist = m_MOInfo->pluginSetting(name(), "blacklisted_extensions").toString().toLower().split(',');

// set up image reader to be used for all image types qt (the current installation) supports
auto imageReader = std::bind(&PreviewBase::genImagePreview, this, std::placeholders::_1, std::placeholders::_2);
auto imageReader = std::bind(&PreviewBase::genImagePreview, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);

foreach(const QByteArray & fileType, QImageReader::supportedImageFormats()) {
auto strFileType = QString(fileType).toLower();
Expand All @@ -56,9 +56,9 @@ bool PreviewBase::init(IOrganizer *moInfo)
m_PreviewGenerators[strFileType] = imageReader;
}

const QStringList supportedTextFormats = { "txt", "ini", "json", "log", "cfg" };
const QStringList supportedTextFormats = { "txt", "ini", "json", "log", "cfg", "psc" };

auto textReader = std::bind(&PreviewBase::genTxtPreview, this, std::placeholders::_1, std::placeholders::_2);
auto textReader = std::bind(&PreviewBase::genTxtPreview, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);

foreach(const QString fileType, supportedTextFormats) {

Expand Down Expand Up @@ -116,20 +116,31 @@ std::set<QString> PreviewBase::supportedExtensions() const
return extensions;
}

QWidget *PreviewBase::genFilePreview(const QString &fileName, const QSize &maxSize) const
QWidget* PreviewBase::genFilePreview(const QString &fileName, const QSize &maxSize) const
{
return genDataPreview(nullptr, fileName, maxSize);
}

QWidget* PreviewBase::genDataPreview(const QByteArray& fileData, const QString& fileName,
const QSize& maxSize) const
{
auto iter = m_PreviewGenerators.find(QFileInfo(fileName).suffix().toLower());
if (iter != m_PreviewGenerators.end()) {
return iter->second(fileName, maxSize);
return iter->second(fileName, maxSize, fileData);
} else {
return nullptr;
}
}

QWidget *PreviewBase::genImagePreview(const QString &fileName, const QSize&) const
QWidget *PreviewBase::genImagePreview(const QString &fileName, const QSize&, const QByteArray& fileData) const
{
QLabel *label = new QLabel();
QPixmap pic = QPixmap(fileName);
QPixmap pic;
if (fileData == nullptr) {
pic = QPixmap(fileName);
} else {
pic.loadFromData(fileData);
}
QSize screenSize = QApplication::primaryScreen()->geometry().size();
// ensure the output image is no more than 80% of the screen height.
// If the aspect ratio is higher than that of the screen this would still allow the image to extend
Expand All @@ -142,10 +153,14 @@ QWidget *PreviewBase::genImagePreview(const QString &fileName, const QSize&) con
return label;
}

QWidget *PreviewBase::genTxtPreview(const QString &fileName, const QSize&) const
QWidget *PreviewBase::genTxtPreview(const QString &fileName, const QSize&, const QByteArray& fileData) const
{
QTextEdit *edit = new QTextEdit();
edit->setText(MOBase::readFileText(fileName));
if (fileData == nullptr) {
edit->setText(MOBase::readFileText(fileName));
} else {
edit->setText(MOBase::decodeTextData(fileData));
}
edit->setReadOnly(true);
return edit;
}
Expand Down
12 changes: 7 additions & 5 deletions src/previewbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,17 @@ class PreviewBase : public MOBase::IPluginPreview

public:
virtual std::set<QString> supportedExtensions() const;
virtual QWidget *genFilePreview(const QString &fileName, const QSize &maxSize) const;
virtual bool supportsArchives() const override { return true; }
virtual QWidget* genFilePreview(const QString &fileName, const QSize &maxSize) const;
virtual QWidget* genDataPreview(const QByteArray& fileData, const QString& fileName,
const QSize& maxSize) const override;

private:

QWidget *genImagePreview(const QString &fileName, const QSize &maxSize) const;
QWidget *genTxtPreview(const QString &fileName, const QSize &maxSize) const;
QWidget *genImagePreview(const QString &fileName, const QSize &maxSize, const QByteArray& fileData) const;
QWidget* genTxtPreview(const QString& fileName, const QSize& maxSize, const QByteArray& fileData) const;

private:
std::map<QString, std::function<QWidget*(const QString&, const QSize&)> > m_PreviewGenerators;
std::map<QString, std::function<QWidget*(const QString&, const QSize&, const QByteArray&)> > m_PreviewGenerators;

private:
const MOBase::IOrganizer* m_MOInfo;
Expand Down

0 comments on commit f47d910

Please sign in to comment.