Refactor loading and saving code

The saving code has been refactored into one class per format with common
methods for each part of the saving process. This should make it easier to
add further formats, and inheriting a format class can allow implementing a
different format that is largely based on an existing format.

The loading code is also in one class per format but is largely the same as
what it was before.

Both classes have the ability to warn the user if any issues or errors are
or will be encountered when loading or saving.

TTI files are now written with CR/LF line endings on all platforms as a
result of using binary file writing for all formats, previously Linux builds
would save TTI files with just LF line endings. TTI files are still loaded
with readLine() and trimmed() which can cope with either type of line ending.

Experimental support for loading and exporting EP1 and HMS SD-Teletext htt
formats has been added. The htt format inherits from the t42 format as the
format appears to be largely the same except for the bits being reversed
within each byte and the clock run-in and framing code added before each
packet.
This commit is contained in:
Gavin MacGregor
2025-03-02 21:56:11 +00:00
parent 42176f2fc0
commit c9b797cff4
9 changed files with 1678 additions and 879 deletions

View File

@@ -0,0 +1,126 @@
/*
* Copyright (C) 2020-2025 Gavin MacGregor
*
* This file is part of QTeletextMaker.
*
* QTeletextMaker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QTeletextMaker is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QTeletextMaker. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef LOADFORMATS_H
#define LOADFORMATS_H
#include <QByteArray>
#include <QDataStream>
#include <QFile>
#include <QString>
#include <QStringList>
#include <QTextStream>
#include "document.h"
#include "levelonepage.h"
#include "pagebase.h"
class LoadFormat
{
public:
virtual ~LoadFormat() {};
virtual bool load(QFile *inFile, TeletextDocument *document) =0;
virtual QString description() const =0;
virtual QStringList extensions() const =0;
QString fileDialogFilter() const { return QString(description() + " (*." + extensions().join(" *.") + ')'); };
QStringList warningStrings() const { return m_warnings; };
QString errorString() const { return m_error; };
protected:
TeletextDocument const *m_document;
QStringList m_warnings;
QString m_error;
};
class LoadTTIFormat : public LoadFormat
{
public:
bool load(QFile *inFile, TeletextDocument *document) override;
QString description() const override { return QString("MRG Systems TTI"); };
QStringList extensions() const override { return QStringList { "tti", "ttix" }; };
};
class LoadT42Format : public LoadFormat
{
public:
bool load(QFile *inFile, TeletextDocument *document) override;
QString description() const override { return QString("t42 packet stream"); };
QStringList extensions() const override { return QStringList { "t42" }; };
protected:
virtual bool readPacket();
QFile *m_inFile;
unsigned char m_inLine[42];
};
class LoadHTTFormat : public LoadT42Format
{
public:
QString description() const override { return QString("HMS SD-Teletext HTT"); };
QStringList extensions() const override { return QStringList { "htt" }; };
protected:
bool readPacket() override;
};
class LoadEP1Format : public LoadFormat
{
public:
bool load(QFile *inFile, TeletextDocument *document) override;
QString description() const override { return QString("Softel EP1"); };
QStringList extensions() const override { return QStringList { "ep1", "epx" }; };
protected:
// Language codes unique to EP1
// FIXME duplicated in saveformats.h
const QMap<int, int> m_languageCode {
{ 0x00, 0x09 }, { 0x01, 0x0d }, { 0x02, 0x18 }, { 0x03, 0x11 }, { 0x04, 0x0b }, { 0x05, 0x17 }, { 0x06, 0x07 },
{ 0x08, 0x14 }, { 0x09, 0x0d }, { 0x0a, 0x18 }, { 0x0b, 0x11 }, { 0x0c, 0x0b }, { 0x0e, 0x07 },
{ 0x10, 0x09 }, { 0x11, 0x0d }, { 0x12, 0x18 }, { 0x13, 0x11 }, { 0x14, 0x0b }, { 0x15, 0x17 }, { 0x16, 0x1c },
{ 0x1d, 0x1e }, { 0x1f, 0x16 },
{ 0x21, 0x0d }, { 0x22, 0xff }, { 0x23, 0xff }, { 0x26, 0x07 },
{ 0x36, 0x1c }, { 0x37, 0x0e },
{ 0x40, 0x09 }, { 0x44, 0x0b }
};
};
class LoadFormats
{
public:
LoadFormats();
~LoadFormats();
LoadFormat *findFormat(const QString &suffix) const;
QString filters() const { return s_filters; };
private:
static const inline int s_size = 4;
static int s_instances;
inline static LoadFormat *s_fileFormat[s_size];
inline static QString s_filters;
};
#endif