gui: PreviewResult: Add serialization() methods for IPC

This commit is contained in:
Albert S. 2022-05-27 09:29:28 +02:00
förälder ee18142e36
incheckning 3bdcb76d8e
6 ändrade filer med 70 tillägg och 2 borttagningar

Visa fil

@ -1,5 +1,4 @@
#include "previewresult.h"
PreviewResult::PreviewResult()
{
}
@ -33,3 +32,11 @@ unsigned int PreviewResult::getPage() const
{
return this->page;
}
QByteArray PreviewResult::serialize() const
{
QByteArray result;
QDataStream stream{&result, QIODevice::WriteOnly};
stream << 0 << this->documentPath << this->page;
return result;
}

Visa fil

@ -2,6 +2,12 @@
#define PREVIEWRESULT_H
#include "clicklabel.h"
enum PreviewResultType
{
PDF = 1,
PlainText
};
class PreviewResult
{
protected:
@ -17,6 +23,7 @@ class PreviewResult
virtual bool hasPreview();
QString getDocumentPath() const;
unsigned int getPage() const;
virtual QByteArray serialize() const;
};
#endif // PREVIEWRESULT_H

Visa fil

@ -1,5 +1,4 @@
#include "previewresultpdf.h"
PreviewResultPdf::PreviewResultPdf(const PreviewResult &o)
{
this->documentPath = o.getDocumentPath();
@ -19,3 +18,27 @@ bool PreviewResultPdf::hasPreview()
bool result = !this->previewImage.isNull();
return result;
}
QByteArray PreviewResultPdf::serialize() const
{
QByteArray result;
QDataStream stream{&result, QIODevice::WriteOnly};
PreviewResultType type = PreviewResultType::PDF;
stream << type << this->documentPath << this->page << this->previewImage;
return result;
}
QSharedPointer<PreviewResultPdf> PreviewResultPdf::deserialize(QByteArray &ba)
{
PreviewResultPdf *result = new PreviewResultPdf();
PreviewResultType type;
QDataStream stream{&ba, QIODevice::ReadOnly};
stream >> type;
if(type != PreviewResultType::PDF)
{
throw std::runtime_error("Invalid byte array: Not a pdf preview");
}
stream >> result->documentPath >> result->page >> result->previewImage;
return QSharedPointer<PreviewResultPdf>(result);
}

Visa fil

@ -12,6 +12,10 @@ class PreviewResultPdf : public PreviewResult
QWidget *createPreviewWidget() override;
bool hasPreview() override;
QByteArray serialize() const;
static QSharedPointer<PreviewResultPdf> deserialize(QByteArray &ba);
};
#endif // PREVIEWRESULTPDF_H

Visa fil

@ -28,3 +28,27 @@ void PreviewResultPlainText::setText(QString text)
{
this->text = text;
}
QByteArray PreviewResultPlainText::serialize() const
{
QByteArray result;
QDataStream stream{&result, QIODevice::WriteOnly};
PreviewResultType type = PreviewResultType::PlainText;
stream << type << this->documentPath << this->page << this->text;
return result;
}
QSharedPointer<PreviewResultPlainText> PreviewResultPlainText::deserialize(QByteArray &ba)
{
PreviewResultPlainText *result = new PreviewResultPlainText();
PreviewResultType type;
QDataStream stream{&ba, QIODevice::ReadOnly};
stream >> type;
if(type != PreviewResultType::PlainText)
{
throw std::runtime_error("Invalid byte array: Not a pdf preview");
}
stream >> result->documentPath >> result->page >> result->text;
return QSharedPointer<PreviewResultPlainText>(result);
}

Visa fil

@ -15,6 +15,9 @@ class PreviewResultPlainText : public PreviewResult
bool hasPreview() override;
void setText(QString text);
QByteArray serialize() const;
static QSharedPointer<PreviewResultPlainText> deserialize(QByteArray &ba);
};
#endif // PREVIEWRESULTPLAINTEXT_H