From 1a7e5aff5f54428c9e881d972313fb15fd953e41 Mon Sep 17 00:00:00 2001 From: "G.K.MacGregor" Date: Sun, 2 May 2021 11:35:02 +0100 Subject: [PATCH] Move repetitive variables into intermediate class --- levelonecommands.cpp | 69 +++++++++++++++----------------------------- levelonecommands.h | 60 +++++++++++++++++--------------------- 2 files changed, 51 insertions(+), 78 deletions(-) diff --git a/levelonecommands.cpp b/levelonecommands.cpp index c1e157e..4905b92 100644 --- a/levelonecommands.cpp +++ b/levelonecommands.cpp @@ -21,12 +21,18 @@ #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_subPageIndex = teletextDocument->currentSubPageIndex(); 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_insertMode = insertMode; @@ -37,7 +43,6 @@ TypeCharacterCommand::TypeCharacterCommand(TeletextDocument *teletextDocument, u setText(QObject::tr("insert character")); else setText(QObject::tr("overwrite character")); - m_firstDo = true; } 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); if (bitToToggle == 0x20 || bitToToggle == 0x7f) 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_subPageIndex = teletextDocument->currentSubPageIndex(); - m_row = teletextDocument->cursorRow(); - m_columnStart = teletextDocument->cursorColumn()-1; + m_columnStart = m_column - 1; + if (m_columnStart == -1) { m_columnStart = 39; 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); setText(QObject::tr("backspace")); - m_firstDo = true; } 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++) 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; 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")); } @@ -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 = teletextDocument->currentSubPageIndex()+afterCurrentSubPage; + m_newSubPageIndex = m_subPageIndex + afterCurrentSubPage; m_copySubPage = copySubPage; 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")); } void DeleteSubPageCommand::redo() { - m_teletextDocument->deleteSubPageToRecycle(m_subPageToDelete); - m_teletextDocument->selectSubPageIndex(qMin(m_subPageToDelete, m_teletextDocument->numberOfSubPages()-1), true); + m_teletextDocument->deleteSubPageToRecycle(m_subPageIndex); + m_teletextDocument->selectSubPageIndex(qMin(m_subPageIndex, m_teletextDocument->numberOfSubPages()-1), true); } void DeleteSubPageCommand::undo() { - m_teletextDocument->unDeleteSubPageFromRecycle(m_subPageToDelete); - m_teletextDocument->selectSubPageIndex(m_subPageToDelete, true); + m_teletextDocument->unDeleteSubPageFromRecycle(m_subPageIndex); + 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_oldColour = teletextDocument->currentSubPage()->CLUT(colourIndex); 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; for (int i=m_colourTable*8; icurrentSubPage()->CLUT(i); diff --git a/levelonecommands.h b/levelonecommands.h index a82dce3..9c0f32e 100644 --- a/levelonecommands.h +++ b/levelonecommands.h @@ -24,7 +24,18 @@ #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: enum { Id = 101 }; @@ -37,13 +48,12 @@ public: int id() const override { return Id; } private: - TeletextDocument *m_teletextDocument; unsigned char m_newCharacter, m_oldRowContents[40], m_newRowContents[40]; - int m_subPageIndex, m_row, m_columnStart, m_columnEnd; - bool m_firstDo, m_insertMode; + int m_columnStart, m_columnEnd; + bool m_insertMode; }; -class ToggleMosaicBitCommand : public QUndoCommand +class ToggleMosaicBitCommand : public LevelOneCommand { public: enum { Id = 102 }; @@ -56,12 +66,10 @@ public: int id() const override { return Id; } private: - TeletextDocument *m_teletextDocument; unsigned char m_oldCharacter, m_newCharacter; - int m_subPageIndex, m_row, m_column; }; -class BackspaceKeyCommand : public QUndoCommand +class BackspaceKeyCommand : public LevelOneCommand { public: enum { Id = 103 }; @@ -74,13 +82,12 @@ public: int id() const override { return Id; } private: - TeletextDocument *m_teletextDocument; unsigned char m_oldRowContents[40], m_newRowContents[40]; - int m_subPageIndex, m_row, m_columnStart, m_columnEnd; - bool m_firstDo, m_insertMode; + int m_columnStart, m_columnEnd; + bool m_insertMode; }; -class DeleteKeyCommand : public QUndoCommand +class DeleteKeyCommand : public LevelOneCommand { public: enum { Id = 104 }; @@ -93,12 +100,10 @@ public: int id() const override { return Id; } private: - TeletextDocument *m_teletextDocument; unsigned char m_oldRowContents[40], m_newRowContents[40]; - int m_subPageIndex, m_row, m_column; }; -class InsertSubPageCommand : public QUndoCommand +class InsertSubPageCommand : public LevelOneCommand { public: InsertSubPageCommand(TeletextDocument *, bool, bool, QUndoCommand *parent = 0); @@ -107,25 +112,20 @@ public: void undo() override; private: - TeletextDocument *m_teletextDocument; int m_newSubPageIndex; bool m_copySubPage; }; -class DeleteSubPageCommand : public QUndoCommand +class DeleteSubPageCommand : public LevelOneCommand { public: DeleteSubPageCommand(TeletextDocument *, QUndoCommand *parent = 0); void redo() override; void undo() override; - -private: - TeletextDocument *m_teletextDocument; - int m_subPageToDelete; }; -class InsertRowCommand : public QUndoCommand +class InsertRowCommand : public LevelOneCommand { public: InsertRowCommand(TeletextDocument *, bool, QUndoCommand *parent = 0); @@ -134,13 +134,11 @@ public: void undo() override; private: - TeletextDocument *m_teletextDocument; - int m_subPageIndex, m_row; bool m_copyRow; unsigned char m_deletedBottomRow[40]; }; -class DeleteRowCommand : public QUndoCommand +class DeleteRowCommand : public LevelOneCommand { public: DeleteRowCommand(TeletextDocument *, QUndoCommand *parent = 0); @@ -149,12 +147,10 @@ public: void undo() override; private: - TeletextDocument *m_teletextDocument; - int m_subPageIndex, m_row; unsigned char m_deletedRow[40]; }; -class SetColourCommand : public QUndoCommand +class SetColourCommand : public LevelOneCommand { public: SetColourCommand(TeletextDocument *, int, int, QUndoCommand *parent = 0); @@ -163,11 +159,10 @@ public: void undo() override; private: - TeletextDocument *m_teletextDocument; - int m_subPageIndex, m_colourIndex, m_oldColour, m_newColour; + int m_colourIndex, m_oldColour, m_newColour; }; -class ResetCLUTCommand : public QUndoCommand +class ResetCLUTCommand : public LevelOneCommand { public: ResetCLUTCommand(TeletextDocument *, int, QUndoCommand *parent = 0); @@ -176,8 +171,7 @@ public: void undo() override; private: - TeletextDocument *m_teletextDocument; - int m_subPageIndex, m_colourTable; + int m_colourTable; int m_oldColourEntry[8]; };