From 9a997d1bfa22426f1c1a409d6399dfdc94f04ce1 Mon Sep 17 00:00:00 2001 From: "G.K.MacGregor" Date: Fri, 12 Feb 2021 21:33:06 +0000 Subject: [PATCH] Implement Delete key command --- levelonecommands.cpp | 53 ++++++++++++++++++++++++++++++++++++++++++++ levelonecommands.h | 18 +++++++++++++++ mainwidget.cpp | 4 ++++ 3 files changed, 75 insertions(+) diff --git a/levelonecommands.cpp b/levelonecommands.cpp index 9b64c79..331ab3c 100644 --- a/levelonecommands.cpp +++ b/levelonecommands.cpp @@ -185,6 +185,58 @@ bool BackspaceCommand::mergeWith(const QUndoCommand *command) } +DeleteKeyCommand::DeleteKeyCommand(TeletextDocument *teletextDocument, QUndoCommand *parent) : QUndoCommand(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); + + setText(QObject::tr("delete")); +} + +void DeleteKeyCommand::redo() +{ + m_teletextDocument->selectSubPageIndex(m_subPageIndex); + + for (int c=m_column; c<39; c++) + m_newRowContents[c] = m_newRowContents[c+1]; + m_newRowContents[39] = 0x20; + + for (int c=0; c<40; c++) + m_teletextDocument->currentSubPage()->setCharacter(m_row, c, m_newRowContents[c]); + + m_teletextDocument->moveCursor(m_row, m_column); + emit m_teletextDocument->contentsChange(m_row); +} + +void DeleteKeyCommand::undo() +{ + m_teletextDocument->selectSubPageIndex(m_subPageIndex); + + for (int c=0; c<40; c++) + m_teletextDocument->currentSubPage()->setCharacter(m_row, c, m_oldRowContents[c]); + + m_teletextDocument->moveCursor(m_row, m_column); + emit m_teletextDocument->contentsChange(m_row); +} + +bool DeleteKeyCommand::mergeWith(const QUndoCommand *command) +{ + const DeleteKeyCommand *newerCommand = static_cast(command); + + if (m_subPageIndex != newerCommand->m_subPageIndex || m_row != newerCommand->m_row || m_column != newerCommand->m_column) + return false; + + for (int c=0; c<40; c++) + m_newRowContents[c] = newerCommand->m_newRowContents[c]; + + return true; +} + + InsertRowCommand::InsertRowCommand(TeletextDocument *teletextDocument, bool copyRow, QUndoCommand *parent) : QUndoCommand(parent) { m_teletextDocument = teletextDocument; @@ -318,6 +370,7 @@ void DeleteSubPageCommand::undo() m_teletextDocument->selectSubPageIndex(m_subPageToDelete, true); } + SetColourCommand::SetColourCommand(TeletextDocument *teletextDocument, int colourIndex, int newColour, QUndoCommand *parent) : QUndoCommand(parent) { m_teletextDocument = teletextDocument; diff --git a/levelonecommands.h b/levelonecommands.h index f62b940..29a6196 100644 --- a/levelonecommands.h +++ b/levelonecommands.h @@ -80,6 +80,24 @@ private: bool m_firstDo; }; +class DeleteKeyCommand : public QUndoCommand +{ +public: + enum { Id = 104 }; + + DeleteKeyCommand(TeletextDocument *, QUndoCommand *parent = 0); + + void redo() override; + void undo() override; + bool mergeWith(const QUndoCommand *) override; + 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 { public: diff --git a/mainwidget.cpp b/mainwidget.cpp index b4f9fd0..9d09dab 100644 --- a/mainwidget.cpp +++ b/mainwidget.cpp @@ -332,6 +332,10 @@ void TeletextWidget::keyPressEvent(QKeyEvent *event) case Qt::Key_Backspace: backspaceEvent(); break; + case Qt::Key_Delete: + m_teletextDocument->undoStack()->push(new DeleteKeyCommand(m_teletextDocument)); + break; + case Qt::Key_Up: m_teletextDocument->cursorUp(); update();