1
0

gui: PreviewResult: Add serialization() methods for IPC

Este cometimento está contido em:
Albert S. 2022-05-27 09:29:28 +02:00
ascendente ee18142e36
cometimento 3bdcb76d8e
6 ficheiros modificados com 70 adições e 2 eliminações

Ver ficheiro

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

Ver ficheiro

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

Ver ficheiro

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

Ver ficheiro

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

Ver ficheiro

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

Ver ficheiro

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