Move repetitive variables into intermediate class

This commit is contained in:
G.K.MacGregor
2021-05-02 11:35:02 +01:00
parent c24a6b1fa1
commit 1a7e5aff5f
2 changed files with 51 additions and 78 deletions

View File

@@ -21,12 +21,18 @@
#include "document.h" #include "document.h"
TypeCharacterCommand::TypeCharacterCommand(TeletextDocument *teletextDocument, unsigned char newCharacter, bool insertMode, QUndoCommand *parent) : QUndoCommand(parent) LevelOneCommand::LevelOneCommand(TeletextDocument *teletextDocument, QUndoCommand *parent) : QUndoCommand(parent)
{ {
m_teletextDocument = teletextDocument; m_teletextDocument = teletextDocument;
m_subPageIndex = teletextDocument->currentSubPageIndex(); m_subPageIndex = teletextDocument->currentSubPageIndex();
m_row = teletextDocument->cursorRow(); m_row = teletextDocument->cursorRow();
m_columnStart = m_columnEnd = teletextDocument->cursorColumn(); m_column = teletextDocument->cursorColumn();
m_firstDo = true;
}
TypeCharacterCommand::TypeCharacterCommand(TeletextDocument *teletextDocument, unsigned char newCharacter, bool insertMode, QUndoCommand *parent) : LevelOneCommand(teletextDocument, parent)
{
m_columnStart = m_columnEnd = m_column;
m_newCharacter = newCharacter; m_newCharacter = newCharacter;
m_insertMode = insertMode; m_insertMode = insertMode;
@@ -37,7 +43,6 @@ TypeCharacterCommand::TypeCharacterCommand(TeletextDocument *teletextDocument, u
setText(QObject::tr("insert character")); setText(QObject::tr("insert character"));
else else
setText(QObject::tr("overwrite character")); setText(QObject::tr("overwrite character"));
m_firstDo = true;
} }
void TypeCharacterCommand::redo() void TypeCharacterCommand::redo()
@@ -89,12 +94,8 @@ bool TypeCharacterCommand::mergeWith(const QUndoCommand *command)
} }
ToggleMosaicBitCommand::ToggleMosaicBitCommand(TeletextDocument *teletextDocument, unsigned char bitToToggle, QUndoCommand *parent) : QUndoCommand(parent) ToggleMosaicBitCommand::ToggleMosaicBitCommand(TeletextDocument *teletextDocument, unsigned char bitToToggle, QUndoCommand *parent) : LevelOneCommand(teletextDocument, parent)
{ {
m_teletextDocument = teletextDocument;
m_subPageIndex = teletextDocument->currentSubPageIndex();
m_row = teletextDocument->cursorRow();
m_column = teletextDocument->cursorColumn();
m_oldCharacter = teletextDocument->currentSubPage()->character(m_row, m_column); m_oldCharacter = teletextDocument->currentSubPage()->character(m_row, m_column);
if (bitToToggle == 0x20 || bitToToggle == 0x7f) if (bitToToggle == 0x20 || bitToToggle == 0x7f)
m_newCharacter = bitToToggle; m_newCharacter = bitToToggle;
@@ -134,12 +135,10 @@ bool ToggleMosaicBitCommand::mergeWith(const QUndoCommand *command)
} }
BackspaceKeyCommand::BackspaceKeyCommand(TeletextDocument *teletextDocument, bool insertMode, QUndoCommand *parent) : QUndoCommand(parent) BackspaceKeyCommand::BackspaceKeyCommand(TeletextDocument *teletextDocument, bool insertMode, QUndoCommand *parent) : LevelOneCommand(teletextDocument, parent)
{ {
m_teletextDocument = teletextDocument; m_columnStart = m_column - 1;
m_subPageIndex = teletextDocument->currentSubPageIndex();
m_row = teletextDocument->cursorRow();
m_columnStart = teletextDocument->cursorColumn()-1;
if (m_columnStart == -1) { if (m_columnStart == -1) {
m_columnStart = 39; m_columnStart = 39;
if (--m_row == 0) if (--m_row == 0)
@@ -152,7 +151,6 @@ BackspaceKeyCommand::BackspaceKeyCommand(TeletextDocument *teletextDocument, boo
m_oldRowContents[c] = m_newRowContents[c] = m_teletextDocument->currentSubPage()->character(m_row, c); m_oldRowContents[c] = m_newRowContents[c] = m_teletextDocument->currentSubPage()->character(m_row, c);
setText(QObject::tr("backspace")); setText(QObject::tr("backspace"));
m_firstDo = true;
} }
void BackspaceKeyCommand::redo() void BackspaceKeyCommand::redo()
@@ -208,13 +206,8 @@ bool BackspaceKeyCommand::mergeWith(const QUndoCommand *command)
} }
DeleteKeyCommand::DeleteKeyCommand(TeletextDocument *teletextDocument, QUndoCommand *parent) : QUndoCommand(parent) DeleteKeyCommand::DeleteKeyCommand(TeletextDocument *teletextDocument, QUndoCommand *parent) : LevelOneCommand(teletextDocument, parent)
{ {
m_teletextDocument = teletextDocument;
m_subPageIndex = teletextDocument->currentSubPageIndex();
m_row = teletextDocument->cursorRow();
m_column = teletextDocument->cursorColumn();
for (int c=0; c<40; c++) for (int c=0; c<40; c++)
m_oldRowContents[c] = m_newRowContents[c] = m_teletextDocument->currentSubPage()->character(m_row, c); m_oldRowContents[c] = m_newRowContents[c] = m_teletextDocument->currentSubPage()->character(m_row, c);
@@ -262,11 +255,8 @@ bool DeleteKeyCommand::mergeWith(const QUndoCommand *command)
} }
InsertRowCommand::InsertRowCommand(TeletextDocument *teletextDocument, bool copyRow, QUndoCommand *parent) : QUndoCommand(parent) InsertRowCommand::InsertRowCommand(TeletextDocument *teletextDocument, bool copyRow, QUndoCommand *parent) : LevelOneCommand(teletextDocument, parent)
{ {
m_teletextDocument = teletextDocument;
m_subPageIndex = teletextDocument->currentSubPageIndex();
m_row = teletextDocument->cursorRow();
m_copyRow = copyRow; m_copyRow = copyRow;
if (m_copyRow) if (m_copyRow)
@@ -310,12 +300,8 @@ void InsertRowCommand::undo()
} }
DeleteRowCommand::DeleteRowCommand(TeletextDocument *teletextDocument, QUndoCommand *parent) : QUndoCommand(parent) DeleteRowCommand::DeleteRowCommand(TeletextDocument *teletextDocument, QUndoCommand *parent) : LevelOneCommand(teletextDocument, parent)
{ {
m_teletextDocument = teletextDocument;
m_subPageIndex = teletextDocument->currentSubPageIndex();
m_row = teletextDocument->cursorRow();
setText(QObject::tr("delete row")); setText(QObject::tr("delete row"));
} }
@@ -354,10 +340,9 @@ void DeleteRowCommand::undo()
} }
InsertSubPageCommand::InsertSubPageCommand(TeletextDocument *teletextDocument, bool afterCurrentSubPage, bool copySubPage, QUndoCommand *parent) : QUndoCommand(parent) InsertSubPageCommand::InsertSubPageCommand(TeletextDocument *teletextDocument, bool afterCurrentSubPage, bool copySubPage, QUndoCommand *parent) : LevelOneCommand(teletextDocument, parent)
{ {
m_teletextDocument = teletextDocument; m_newSubPageIndex = m_subPageIndex + afterCurrentSubPage;
m_newSubPageIndex = teletextDocument->currentSubPageIndex()+afterCurrentSubPage;
m_copySubPage = copySubPage; m_copySubPage = copySubPage;
setText(QObject::tr("insert subpage")); setText(QObject::tr("insert subpage"));
@@ -380,30 +365,26 @@ void InsertSubPageCommand::undo()
} }
DeleteSubPageCommand::DeleteSubPageCommand(TeletextDocument *teletextDocument, QUndoCommand *parent) : QUndoCommand(parent) DeleteSubPageCommand::DeleteSubPageCommand(TeletextDocument *teletextDocument, QUndoCommand *parent) : LevelOneCommand(teletextDocument, parent)
{ {
m_teletextDocument = teletextDocument;
m_subPageToDelete = teletextDocument->currentSubPageIndex();
setText(QObject::tr("delete subpage")); setText(QObject::tr("delete subpage"));
} }
void DeleteSubPageCommand::redo() void DeleteSubPageCommand::redo()
{ {
m_teletextDocument->deleteSubPageToRecycle(m_subPageToDelete); m_teletextDocument->deleteSubPageToRecycle(m_subPageIndex);
m_teletextDocument->selectSubPageIndex(qMin(m_subPageToDelete, m_teletextDocument->numberOfSubPages()-1), true); m_teletextDocument->selectSubPageIndex(qMin(m_subPageIndex, m_teletextDocument->numberOfSubPages()-1), true);
} }
void DeleteSubPageCommand::undo() void DeleteSubPageCommand::undo()
{ {
m_teletextDocument->unDeleteSubPageFromRecycle(m_subPageToDelete); m_teletextDocument->unDeleteSubPageFromRecycle(m_subPageIndex);
m_teletextDocument->selectSubPageIndex(m_subPageToDelete, true); m_teletextDocument->selectSubPageIndex(m_subPageIndex, true);
} }
SetColourCommand::SetColourCommand(TeletextDocument *teletextDocument, int colourIndex, int newColour, QUndoCommand *parent) : QUndoCommand(parent) SetColourCommand::SetColourCommand(TeletextDocument *teletextDocument, int colourIndex, int newColour, QUndoCommand *parent) : LevelOneCommand(teletextDocument, parent)
{ {
m_teletextDocument = teletextDocument;
m_subPageIndex = teletextDocument->currentSubPageIndex();
m_colourIndex = colourIndex; m_colourIndex = colourIndex;
m_oldColour = teletextDocument->currentSubPage()->CLUT(colourIndex); m_oldColour = teletextDocument->currentSubPage()->CLUT(colourIndex);
m_newColour = newColour; m_newColour = newColour;
@@ -431,10 +412,8 @@ void SetColourCommand::undo()
} }
ResetCLUTCommand::ResetCLUTCommand(TeletextDocument *teletextDocument, int colourTable, QUndoCommand *parent) : QUndoCommand(parent) ResetCLUTCommand::ResetCLUTCommand(TeletextDocument *teletextDocument, int colourTable, QUndoCommand *parent) : LevelOneCommand(teletextDocument, parent)
{ {
m_teletextDocument = teletextDocument;
m_subPageIndex = teletextDocument->currentSubPageIndex();
m_colourTable = colourTable; m_colourTable = colourTable;
for (int i=m_colourTable*8; i<m_colourTable*8+8; i++) for (int i=m_colourTable*8; i<m_colourTable*8+8; i++)
m_oldColourEntry[i&7] = teletextDocument->currentSubPage()->CLUT(i); m_oldColourEntry[i&7] = teletextDocument->currentSubPage()->CLUT(i);

View File

@@ -24,7 +24,18 @@
#include "document.h" #include "document.h"
class TypeCharacterCommand : public QUndoCommand class LevelOneCommand : public QUndoCommand
{
public:
LevelOneCommand(TeletextDocument *, QUndoCommand *parent = 0);
protected:
TeletextDocument *m_teletextDocument;
int m_subPageIndex, m_row, m_column;
bool m_firstDo;
};
class TypeCharacterCommand : public LevelOneCommand
{ {
public: public:
enum { Id = 101 }; enum { Id = 101 };
@@ -37,13 +48,12 @@ public:
int id() const override { return Id; } int id() const override { return Id; }
private: private:
TeletextDocument *m_teletextDocument;
unsigned char m_newCharacter, m_oldRowContents[40], m_newRowContents[40]; unsigned char m_newCharacter, m_oldRowContents[40], m_newRowContents[40];
int m_subPageIndex, m_row, m_columnStart, m_columnEnd; int m_columnStart, m_columnEnd;
bool m_firstDo, m_insertMode; bool m_insertMode;
}; };
class ToggleMosaicBitCommand : public QUndoCommand class ToggleMosaicBitCommand : public LevelOneCommand
{ {
public: public:
enum { Id = 102 }; enum { Id = 102 };
@@ -56,12 +66,10 @@ public:
int id() const override { return Id; } int id() const override { return Id; }
private: private:
TeletextDocument *m_teletextDocument;
unsigned char m_oldCharacter, m_newCharacter; unsigned char m_oldCharacter, m_newCharacter;
int m_subPageIndex, m_row, m_column;
}; };
class BackspaceKeyCommand : public QUndoCommand class BackspaceKeyCommand : public LevelOneCommand
{ {
public: public:
enum { Id = 103 }; enum { Id = 103 };
@@ -74,13 +82,12 @@ public:
int id() const override { return Id; } int id() const override { return Id; }
private: private:
TeletextDocument *m_teletextDocument;
unsigned char m_oldRowContents[40], m_newRowContents[40]; unsigned char m_oldRowContents[40], m_newRowContents[40];
int m_subPageIndex, m_row, m_columnStart, m_columnEnd; int m_columnStart, m_columnEnd;
bool m_firstDo, m_insertMode; bool m_insertMode;
}; };
class DeleteKeyCommand : public QUndoCommand class DeleteKeyCommand : public LevelOneCommand
{ {
public: public:
enum { Id = 104 }; enum { Id = 104 };
@@ -93,12 +100,10 @@ public:
int id() const override { return Id; } int id() const override { return Id; }
private: private:
TeletextDocument *m_teletextDocument;
unsigned char m_oldRowContents[40], m_newRowContents[40]; unsigned char m_oldRowContents[40], m_newRowContents[40];
int m_subPageIndex, m_row, m_column;
}; };
class InsertSubPageCommand : public QUndoCommand class InsertSubPageCommand : public LevelOneCommand
{ {
public: public:
InsertSubPageCommand(TeletextDocument *, bool, bool, QUndoCommand *parent = 0); InsertSubPageCommand(TeletextDocument *, bool, bool, QUndoCommand *parent = 0);
@@ -107,25 +112,20 @@ public:
void undo() override; void undo() override;
private: private:
TeletextDocument *m_teletextDocument;
int m_newSubPageIndex; int m_newSubPageIndex;
bool m_copySubPage; bool m_copySubPage;
}; };
class DeleteSubPageCommand : public QUndoCommand class DeleteSubPageCommand : public LevelOneCommand
{ {
public: public:
DeleteSubPageCommand(TeletextDocument *, QUndoCommand *parent = 0); DeleteSubPageCommand(TeletextDocument *, QUndoCommand *parent = 0);
void redo() override; void redo() override;
void undo() override; void undo() override;
private:
TeletextDocument *m_teletextDocument;
int m_subPageToDelete;
}; };
class InsertRowCommand : public QUndoCommand class InsertRowCommand : public LevelOneCommand
{ {
public: public:
InsertRowCommand(TeletextDocument *, bool, QUndoCommand *parent = 0); InsertRowCommand(TeletextDocument *, bool, QUndoCommand *parent = 0);
@@ -134,13 +134,11 @@ public:
void undo() override; void undo() override;
private: private:
TeletextDocument *m_teletextDocument;
int m_subPageIndex, m_row;
bool m_copyRow; bool m_copyRow;
unsigned char m_deletedBottomRow[40]; unsigned char m_deletedBottomRow[40];
}; };
class DeleteRowCommand : public QUndoCommand class DeleteRowCommand : public LevelOneCommand
{ {
public: public:
DeleteRowCommand(TeletextDocument *, QUndoCommand *parent = 0); DeleteRowCommand(TeletextDocument *, QUndoCommand *parent = 0);
@@ -149,12 +147,10 @@ public:
void undo() override; void undo() override;
private: private:
TeletextDocument *m_teletextDocument;
int m_subPageIndex, m_row;
unsigned char m_deletedRow[40]; unsigned char m_deletedRow[40];
}; };
class SetColourCommand : public QUndoCommand class SetColourCommand : public LevelOneCommand
{ {
public: public:
SetColourCommand(TeletextDocument *, int, int, QUndoCommand *parent = 0); SetColourCommand(TeletextDocument *, int, int, QUndoCommand *parent = 0);
@@ -163,11 +159,10 @@ public:
void undo() override; void undo() override;
private: private:
TeletextDocument *m_teletextDocument; int m_colourIndex, m_oldColour, m_newColour;
int m_subPageIndex, m_colourIndex, m_oldColour, m_newColour;
}; };
class ResetCLUTCommand : public QUndoCommand class ResetCLUTCommand : public LevelOneCommand
{ {
public: public:
ResetCLUTCommand(TeletextDocument *, int, QUndoCommand *parent = 0); ResetCLUTCommand(TeletextDocument *, int, QUndoCommand *parent = 0);
@@ -176,8 +171,7 @@ public:
void undo() override; void undo() override;
private: private:
TeletextDocument *m_teletextDocument; int m_colourTable;
int m_subPageIndex, m_colourTable;
int m_oldColourEntry[8]; int m_oldColourEntry[8];
}; };