mirror of
https://github.com/JakubMelka/PDF4QT.git
synced 2025-06-05 21:59:17 +02:00
File attachments
This commit is contained in:
@ -234,6 +234,7 @@ public:
|
||||
PageLayout getPageLayout() const { return m_pageLayout; }
|
||||
PageMode getPageMode() const { return m_pageMode; }
|
||||
const QByteArray& getBaseURI() const { return m_baseURI; }
|
||||
const std::map<QByteArray, PDFFileSpecification>& getEmbeddedFiles() const { return m_embeddedFiles; }
|
||||
|
||||
/// Returns destination using the key. If destination with the key is not found,
|
||||
/// then nullptr is returned.
|
||||
|
@ -32,6 +32,11 @@ public:
|
||||
explicit PDFEmbeddedFile() = default;
|
||||
|
||||
bool isValid() const { return m_stream.isStream(); }
|
||||
const QByteArray& getSubtype() const { return m_subtype; }
|
||||
PDFInteger getSize() const { return m_size; }
|
||||
const QDateTime& getCreationDate() const { return m_creationDate; }
|
||||
const QDateTime& getModifiedDate() const { return m_modifiedDate; }
|
||||
const QByteArray& getChecksum() const { return m_checksum; }
|
||||
|
||||
static PDFEmbeddedFile parse(const PDFDocument* document, PDFObject object);
|
||||
|
||||
|
@ -19,7 +19,10 @@
|
||||
#include "pdfdocument.h"
|
||||
|
||||
#include <QFont>
|
||||
#include <QStyle>
|
||||
#include <QApplication>
|
||||
#include <QMimeDatabase>
|
||||
#include <QFileIconProvider>
|
||||
|
||||
namespace pdf
|
||||
{
|
||||
@ -437,4 +440,152 @@ const PDFAction* PDFOutlineTreeItemModel::getAction(const QModelIndex& index) co
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int PDFAttachmentsTreeItemModel::columnCount(const QModelIndex& parent) const
|
||||
{
|
||||
Q_UNUSED(parent);
|
||||
return EndColumn;
|
||||
}
|
||||
|
||||
QVariant PDFAttachmentsTreeItemModel::data(const QModelIndex& index, int role) const
|
||||
{
|
||||
if (!index.isValid())
|
||||
{
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
const PDFAttachmentsTreeItem* item = static_cast<const PDFAttachmentsTreeItem*>(index.internalPointer());
|
||||
switch (role)
|
||||
{
|
||||
case Qt::DisplayRole:
|
||||
{
|
||||
switch (index.column())
|
||||
{
|
||||
case Title:
|
||||
return item->getTitle();
|
||||
|
||||
case Description:
|
||||
return item->getDescription();
|
||||
|
||||
default:
|
||||
Q_ASSERT(false);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case Qt::DecorationRole:
|
||||
{
|
||||
switch (index.column())
|
||||
{
|
||||
case Title:
|
||||
return item->getIcon();
|
||||
|
||||
case Description:
|
||||
return QVariant();
|
||||
|
||||
default:
|
||||
Q_ASSERT(false);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
void PDFAttachmentsTreeItemModel::update()
|
||||
{
|
||||
beginResetModel();
|
||||
|
||||
m_rootItem.reset();
|
||||
if (m_document)
|
||||
{
|
||||
const std::map<QByteArray, PDFFileSpecification>& embeddedFiles = m_document->getCatalog()->getEmbeddedFiles();
|
||||
if (!embeddedFiles.empty())
|
||||
{
|
||||
QMimeDatabase mimeDatabase;
|
||||
QFileIconProvider fileIconProvider;
|
||||
fileIconProvider.setOptions(QFileIconProvider::DontUseCustomDirectoryIcons);
|
||||
PDFAttachmentsTreeItem* root = new PDFAttachmentsTreeItem(nullptr, QIcon(), QString(), QString(), nullptr);
|
||||
m_rootItem.reset(root);
|
||||
|
||||
std::map<QString, PDFAttachmentsTreeItem*> subroots;
|
||||
|
||||
for (const auto& embeddedFile : embeddedFiles)
|
||||
{
|
||||
const PDFFileSpecification* specification = &embeddedFile.second;
|
||||
const PDFEmbeddedFile* file = specification->getPlatformFile();
|
||||
if (!file)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
QString fileName = specification->getPlatformFileName();
|
||||
QString description = specification->getDescription();
|
||||
|
||||
// Jakub Melka: try to obtain mime type from subtype, if it fails, then form file name.
|
||||
// We do not obtain type from data, because it can be slow (files can be large).
|
||||
QMimeType type = mimeDatabase.mimeTypeForName(file->getSubtype());
|
||||
if (!type.isValid())
|
||||
{
|
||||
type = mimeDatabase.mimeTypeForFile(fileName, QMimeDatabase::MatchExtension);
|
||||
}
|
||||
|
||||
// Get icon and select folder, to which file belongs
|
||||
QIcon icon;
|
||||
QString fileTypeName = "@GENERIC";
|
||||
QString fileTypeDescription = tr("Files");
|
||||
if (type.isValid())
|
||||
{
|
||||
icon = QIcon::fromTheme(type.iconName());
|
||||
if (icon.isNull())
|
||||
{
|
||||
icon = QIcon::fromTheme(type.genericIconName());
|
||||
}
|
||||
|
||||
fileTypeName = type.name();
|
||||
fileTypeDescription = type.comment();
|
||||
}
|
||||
|
||||
if (icon.isNull())
|
||||
{
|
||||
icon = fileIconProvider.icon(QFileInfo(fileName));
|
||||
}
|
||||
if (icon.isNull())
|
||||
{
|
||||
icon = QApplication::style()->standardIcon(QStyle::SP_FileIcon);
|
||||
}
|
||||
|
||||
// Create subroot
|
||||
PDFAttachmentsTreeItem* subroot = nullptr;
|
||||
auto it = subroots.find(fileTypeName);
|
||||
if (it == subroots.cend())
|
||||
{
|
||||
subroot = new PDFAttachmentsTreeItem(nullptr, icon, fileTypeDescription, QString(), nullptr);
|
||||
root->addCreatedChild(subroot);
|
||||
subroots[fileTypeName] = subroot;
|
||||
}
|
||||
else
|
||||
{
|
||||
subroot = it->second;
|
||||
}
|
||||
|
||||
// Create item
|
||||
subroot->addCreatedChild(new PDFAttachmentsTreeItem(nullptr, qMove(icon), qMove(fileName), qMove(description), specification));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
Qt::ItemFlags PDFAttachmentsTreeItemModel::flags(const QModelIndex& index) const
|
||||
{
|
||||
return PDFTreeItemModel::flags(index);
|
||||
}
|
||||
|
||||
} // namespace pdf
|
||||
|
@ -29,6 +29,7 @@ namespace pdf
|
||||
class PDFAction;
|
||||
class PDFDocument;
|
||||
class PDFOutlineItem;
|
||||
class PDFFileSpecification;
|
||||
class PDFOptionalContentActivity;
|
||||
|
||||
/// Represents tree item in the GUI tree
|
||||
@ -166,6 +167,54 @@ private:
|
||||
QIcon m_icon;
|
||||
};
|
||||
|
||||
class PDFAttachmentsTreeItem : public PDFTreeItem
|
||||
{
|
||||
public:
|
||||
explicit PDFAttachmentsTreeItem(PDFAttachmentsTreeItem* parent, QIcon icon, QString title, QString description, const PDFFileSpecification* fileSpecification) :
|
||||
PDFTreeItem(parent),
|
||||
m_icon(qMove(icon)),
|
||||
m_title(qMove(title)),
|
||||
m_description(qMove(description)),
|
||||
m_fileSpecification(fileSpecification)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
const QIcon& getIcon() const { return m_icon; }
|
||||
const QString& getTitle() const { return m_title; }
|
||||
const QString& getDescription() const { return m_description; }
|
||||
const PDFFileSpecification* getFileSpecification() const { return m_fileSpecification; }
|
||||
|
||||
private:
|
||||
QIcon m_icon;
|
||||
QString m_title;
|
||||
QString m_description;
|
||||
const PDFFileSpecification* m_fileSpecification;
|
||||
};
|
||||
|
||||
class PDFFORQTLIBSHARED_EXPORT PDFAttachmentsTreeItemModel : public PDFTreeItemModel
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
PDFAttachmentsTreeItemModel(QObject* parent) :
|
||||
PDFTreeItemModel(parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
enum Column
|
||||
{
|
||||
Title,
|
||||
Description,
|
||||
EndColumn
|
||||
};
|
||||
|
||||
virtual int columnCount(const QModelIndex& parent) const override;
|
||||
virtual QVariant data(const QModelIndex& index, int role) const override;
|
||||
virtual void update() override;
|
||||
virtual Qt::ItemFlags flags(const QModelIndex& index) const override;
|
||||
};
|
||||
|
||||
} // namespace pdf
|
||||
|
||||
#endif // PDFITEMMODELS_H
|
||||
|
Reference in New Issue
Block a user