Split text block saving into intermediate class

This commit is contained in:
G.K.MacGregor
2024-02-02 18:00:43 +00:00
parent baa20d69b7
commit e3a8f43b52
2 changed files with 64 additions and 55 deletions

View File

@@ -38,6 +38,49 @@ LevelOneCommand::LevelOneCommand(TeletextDocument *teletextDocument, QUndoComman
m_firstDo = true;
}
StoreOldCharactersCommand::StoreOldCharactersCommand(TeletextDocument *teletextDocument, QUndoCommand *parent) : LevelOneCommand(teletextDocument, parent)
{
}
void StoreOldCharactersCommand::storeOldCharacters(int topRow, int leftColumn, int bottomRow, int rightColumn)
{
for (int r=topRow; r<=bottomRow; r++) {
QByteArray rowArray;
for (int c=leftColumn; c<=rightColumn; c++)
// Guard against size of pasted block going beyond last line or column
if (r < 25 && c < 40)
rowArray.append(m_teletextDocument->currentSubPage()->character(r, c));
else
// Gone beyond last line or column - store a filler character which we won't see
// Not sure if this is really necessary as out-of-bounds access might not occur?
rowArray.append(0x7f);
m_oldCharacters.append(rowArray);
}
}
void StoreOldCharactersCommand::retrieveOldCharacters(int topRow, int leftColumn, int bottomRow, int rightColumn)
{
int arrayR = 0;
int arrayC;
for (int r=topRow; r<=bottomRow; r++) {
arrayC = 0;
for (int c=leftColumn; c<=rightColumn; c++)
// Guard against size of pasted block going beyond last line or column
if (r < 25 && c < 40) {
m_teletextDocument->currentSubPage()->setCharacter(r, c, m_oldCharacters[arrayR].at(arrayC));
arrayC++;
}
arrayR++;
}
}
TypeCharacterCommand::TypeCharacterCommand(TeletextDocument *teletextDocument, unsigned char newCharacter, bool insertMode, QUndoCommand *parent) : LevelOneCommand(teletextDocument, parent)
{
m_columnStart = m_columnEnd = m_column;
@@ -351,7 +394,7 @@ void DeleteRowCommand::undo()
#ifndef QT_NO_CLIPBOARD
CutCommand::CutCommand(TeletextDocument *teletextDocument, QUndoCommand *parent) : LevelOneCommand(teletextDocument, parent)
CutCommand::CutCommand(TeletextDocument *teletextDocument, QUndoCommand *parent) : StoreOldCharactersCommand(teletextDocument, parent)
{
m_selectionTopRow = m_teletextDocument->selectionTopRow();
m_selectionBottomRow = m_teletextDocument->selectionBottomRow();
@@ -361,15 +404,7 @@ CutCommand::CutCommand(TeletextDocument *teletextDocument, QUndoCommand *parent)
m_selectionCornerRow = m_teletextDocument->selectionCornerRow();
m_selectionCornerColumn = m_teletextDocument->selectionCornerColumn();
// Store copy of the characters that we're about to blank
for (int r=m_selectionTopRow; r<=m_selectionBottomRow; r++) {
QByteArray rowArray;
for (int c=m_selectionLeftColumn; c<=m_selectionRightColumn; c++)
rowArray.append(m_teletextDocument->currentSubPage()->character(r, c));
m_deletedCharacters.append(rowArray);
}
storeOldCharacters(m_selectionTopRow, m_selectionLeftColumn, m_selectionBottomRow, m_selectionRightColumn);
setText(QObject::tr("cut"));
}
@@ -390,16 +425,7 @@ void CutCommand::undo()
{
m_teletextDocument->selectSubPageIndex(m_subPageIndex);
int arrayR = 0;
int arrayC;
for (int r=m_selectionTopRow; r<=m_selectionBottomRow; r++) {
arrayC = 0;
for (int c=m_selectionLeftColumn; c<=m_selectionRightColumn; c++)
m_teletextDocument->currentSubPage()->setCharacter(r, c, m_deletedCharacters[arrayR].at(arrayC++));
arrayR++;
}
retrieveOldCharacters(m_selectionTopRow, m_selectionLeftColumn, m_selectionBottomRow, m_selectionRightColumn);
emit m_teletextDocument->contentsChanged();
@@ -408,7 +434,7 @@ void CutCommand::undo()
}
PasteCommand::PasteCommand(TeletextDocument *teletextDocument, int pageCharSet, QUndoCommand *parent) : LevelOneCommand(teletextDocument, parent)
PasteCommand::PasteCommand(TeletextDocument *teletextDocument, int pageCharSet, QUndoCommand *parent) : StoreOldCharactersCommand(teletextDocument, parent)
{
const QClipboard *clipboard = QApplication::clipboard();
const QMimeData *mimeData = clipboard->mimeData();
@@ -586,21 +612,7 @@ PasteCommand::PasteCommand(TeletextDocument *teletextDocument, int pageCharSet,
if (m_clipboardDataWidth == 0 || m_clipboardDataHeight == 0)
return;
// Store copy of the characters that we're about to overwrite
for (int r=m_pasteTopRow; r<=m_pasteBottomRow; r++) {
QByteArray rowArray;
for (int c=m_pasteLeftColumn; c<=m_pasteRightColumn; c++)
// Guard against size of pasted block going beyond last line or column
if (r < 25 && c < 40)
rowArray.append(m_teletextDocument->currentSubPage()->character(r, c));
else
// Gone beyond last line or column - store a filler character which we won't see
// Not sure if this is really necessary as out-of-bounds access might not occur?
rowArray.append(0x7f);
m_deletedCharacters.append(rowArray);
}
storeOldCharacters(m_pasteTopRow, m_pasteLeftColumn, m_pasteBottomRow, m_pasteRightColumn);
setText(QObject::tr("paste"));
}
@@ -666,21 +678,7 @@ void PasteCommand::undo()
m_teletextDocument->selectSubPageIndex(m_subPageIndex);
int arrayR = 0;
int arrayC;
for (int r=m_pasteTopRow; r<=m_pasteBottomRow; r++) {
arrayC = 0;
for (int c=m_pasteLeftColumn; c<=m_pasteRightColumn; c++)
// Guard against size of pasted block going beyond last line or column
if (r < 25 && c < 40) {
m_teletextDocument->currentSubPage()->setCharacter(r, c, m_deletedCharacters[arrayR].at(arrayC));
arrayC++;
}
arrayR++;
}
retrieveOldCharacters(m_pasteTopRow, m_pasteLeftColumn, m_pasteBottomRow, m_pasteRightColumn);
emit m_teletextDocument->contentsChanged();

View File

@@ -36,6 +36,18 @@ protected:
bool m_firstDo;
};
class StoreOldCharactersCommand : public LevelOneCommand
{
public:
StoreOldCharactersCommand(TeletextDocument *teletextDocument, QUndoCommand *parent = 0);
protected:
void storeOldCharacters(int topRow, int leftColumn, int bottomRow, int rightColumn);
void retrieveOldCharacters(int topRow, int leftColumn, int bottomRow, int rightColumn);
QByteArrayList m_oldCharacters;
};
class TypeCharacterCommand : public LevelOneCommand
{
public:
@@ -152,7 +164,7 @@ private:
};
#ifndef QT_NO_CLIPBOARD
class CutCommand : public LevelOneCommand
class CutCommand : public StoreOldCharactersCommand
{
public:
CutCommand(TeletextDocument *teletextDocument, QUndoCommand *parent = 0);
@@ -161,12 +173,11 @@ public:
void undo() override;
private:
QByteArrayList m_deletedCharacters;
int m_selectionTopRow, m_selectionBottomRow, m_selectionLeftColumn, m_selectionRightColumn;
int m_selectionCornerRow, m_selectionCornerColumn;
};
class PasteCommand : public LevelOneCommand
class PasteCommand : public StoreOldCharactersCommand
{
public:
PasteCommand(TeletextDocument *teletextDocument, int pageCharSet, QUndoCommand *parent = 0);
@@ -175,7 +186,7 @@ public:
void undo() override;
private:
QByteArrayList m_deletedCharacters, m_pastingCharacters;
QByteArrayList m_pastingCharacters;
int m_pasteTopRow, m_pasteBottomRow, m_pasteLeftColumn, m_pasteRightColumn;
int m_clipboardDataHeight, m_clipboardDataWidth;
int m_selectionCornerRow, m_selectionCornerColumn;