Convert storeCharacters to a function

This commit is contained in:
G.K.MacGregor
2024-07-09 22:16:58 +01:00
parent 98071d823c
commit 9045160d3a
2 changed files with 23 additions and 29 deletions

View File

@@ -39,13 +39,10 @@ LevelOneCommand::LevelOneCommand(TeletextDocument *teletextDocument, QUndoComman
m_firstDo = true; m_firstDo = true;
} }
QByteArrayList LevelOneCommand::storeCharacters(int topRow, int leftColumn, int bottomRow, int rightColumn)
StoreOldCharactersCommand::StoreOldCharactersCommand(TeletextDocument *teletextDocument, QUndoCommand *parent) : LevelOneCommand(teletextDocument, parent)
{ {
} QByteArrayList result;
void StoreOldCharactersCommand::storeOldCharacters(int topRow, int leftColumn, int bottomRow, int rightColumn)
{
for (int r=topRow; r<=bottomRow; r++) { for (int r=topRow; r<=bottomRow; r++) {
QByteArray rowArray; QByteArray rowArray;
@@ -58,12 +55,17 @@ void StoreOldCharactersCommand::storeOldCharacters(int topRow, int leftColumn, i
// Not sure if this is really necessary as out-of-bounds access might not occur? // Not sure if this is really necessary as out-of-bounds access might not occur?
rowArray.append(0x7f); rowArray.append(0x7f);
m_oldCharacters.append(rowArray); result.append(rowArray);
} }
return result;
} }
void StoreOldCharactersCommand::retrieveOldCharacters(int topRow, int leftColumn, int bottomRow, int rightColumn) void LevelOneCommand::retrieveCharacters(int topRow, int leftColumn, const QByteArrayList &storedChars)
{ {
const int bottomRow = topRow + storedChars.size() - 1;
const int rightColumn = leftColumn + storedChars.at(0).size() - 1;
int arrayR = 0; int arrayR = 0;
int arrayC; int arrayC;
@@ -72,7 +74,7 @@ void StoreOldCharactersCommand::retrieveOldCharacters(int topRow, int leftColumn
for (int c=leftColumn; c<=rightColumn; c++) for (int c=leftColumn; c<=rightColumn; c++)
// Guard against size of pasted block going beyond last line or column // Guard against size of pasted block going beyond last line or column
if (r < 25 && c < 40) { if (r < 25 && c < 40) {
m_teletextDocument->currentSubPage()->setCharacter(r, c, m_oldCharacters[arrayR].at(arrayC)); m_teletextDocument->currentSubPage()->setCharacter(r, c, storedChars.at(arrayR).at(arrayC));
arrayC++; arrayC++;
} }
@@ -402,7 +404,7 @@ void DeleteRowCommand::undo()
#ifndef QT_NO_CLIPBOARD #ifndef QT_NO_CLIPBOARD
CutCommand::CutCommand(TeletextDocument *teletextDocument, QUndoCommand *parent) : StoreOldCharactersCommand(teletextDocument, parent) CutCommand::CutCommand(TeletextDocument *teletextDocument, QUndoCommand *parent) : LevelOneCommand(teletextDocument, parent)
{ {
m_selectionTopRow = m_teletextDocument->selectionTopRow(); m_selectionTopRow = m_teletextDocument->selectionTopRow();
m_selectionBottomRow = m_teletextDocument->selectionBottomRow(); m_selectionBottomRow = m_teletextDocument->selectionBottomRow();
@@ -412,7 +414,7 @@ CutCommand::CutCommand(TeletextDocument *teletextDocument, QUndoCommand *parent)
m_selectionCornerRow = m_teletextDocument->selectionCornerRow(); m_selectionCornerRow = m_teletextDocument->selectionCornerRow();
m_selectionCornerColumn = m_teletextDocument->selectionCornerColumn(); m_selectionCornerColumn = m_teletextDocument->selectionCornerColumn();
storeOldCharacters(m_selectionTopRow, m_selectionLeftColumn, m_selectionBottomRow, m_selectionRightColumn); m_oldCharacters = storeCharacters(m_selectionTopRow, m_selectionLeftColumn, m_selectionBottomRow, m_selectionRightColumn);
setText(QObject::tr("cut")); setText(QObject::tr("cut"));
} }
@@ -433,7 +435,7 @@ void CutCommand::undo()
{ {
m_teletextDocument->selectSubPageIndex(m_subPageIndex); m_teletextDocument->selectSubPageIndex(m_subPageIndex);
retrieveOldCharacters(m_selectionTopRow, m_selectionLeftColumn, m_selectionBottomRow, m_selectionRightColumn); retrieveCharacters(m_selectionTopRow, m_selectionLeftColumn, m_oldCharacters);
emit m_teletextDocument->contentsChanged(); emit m_teletextDocument->contentsChanged();
@@ -442,7 +444,7 @@ void CutCommand::undo()
} }
PasteCommand::PasteCommand(TeletextDocument *teletextDocument, int pageCharSet, QUndoCommand *parent) : StoreOldCharactersCommand(teletextDocument, parent) PasteCommand::PasteCommand(TeletextDocument *teletextDocument, int pageCharSet, QUndoCommand *parent) : LevelOneCommand(teletextDocument, parent)
{ {
const QClipboard *clipboard = QApplication::clipboard(); const QClipboard *clipboard = QApplication::clipboard();
const QMimeData *mimeData = clipboard->mimeData(); const QMimeData *mimeData = clipboard->mimeData();
@@ -687,7 +689,7 @@ PasteCommand::PasteCommand(TeletextDocument *teletextDocument, int pageCharSet,
if (m_clipboardDataWidth == 0 || m_clipboardDataHeight == 0) if (m_clipboardDataWidth == 0 || m_clipboardDataHeight == 0)
return; return;
storeOldCharacters(m_pasteTopRow, m_pasteLeftColumn, m_pasteBottomRow, m_pasteRightColumn); m_oldCharacters = storeCharacters(m_pasteTopRow, m_pasteLeftColumn, m_pasteBottomRow, m_pasteRightColumn);
setText(QObject::tr("paste")); setText(QObject::tr("paste"));
} }
@@ -753,7 +755,7 @@ void PasteCommand::undo()
m_teletextDocument->selectSubPageIndex(m_subPageIndex); m_teletextDocument->selectSubPageIndex(m_subPageIndex);
retrieveOldCharacters(m_pasteTopRow, m_pasteLeftColumn, m_pasteBottomRow, m_pasteRightColumn); retrieveCharacters(m_pasteTopRow, m_pasteLeftColumn, m_oldCharacters);
emit m_teletextDocument->contentsChanged(); emit m_teletextDocument->contentsChanged();

View File

@@ -31,23 +31,14 @@ public:
LevelOneCommand(TeletextDocument *teletextDocument, QUndoCommand *parent = 0); LevelOneCommand(TeletextDocument *teletextDocument, QUndoCommand *parent = 0);
protected: protected:
QByteArrayList storeCharacters(int topRow, int leftColumn, int bottomRow, int rightColumn);
void retrieveCharacters(int topRow, int leftColumn, const QByteArrayList &oldChars);
TeletextDocument *m_teletextDocument; TeletextDocument *m_teletextDocument;
int m_subPageIndex, m_row, m_column; int m_subPageIndex, m_row, m_column;
bool m_firstDo; 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 class TypeCharacterCommand : public LevelOneCommand
{ {
public: public:
@@ -164,7 +155,7 @@ private:
}; };
#ifndef QT_NO_CLIPBOARD #ifndef QT_NO_CLIPBOARD
class CutCommand : public StoreOldCharactersCommand class CutCommand : public LevelOneCommand
{ {
public: public:
CutCommand(TeletextDocument *teletextDocument, QUndoCommand *parent = 0); CutCommand(TeletextDocument *teletextDocument, QUndoCommand *parent = 0);
@@ -173,11 +164,12 @@ public:
void undo() override; void undo() override;
private: private:
QByteArrayList m_oldCharacters;
int m_selectionTopRow, m_selectionBottomRow, m_selectionLeftColumn, m_selectionRightColumn; int m_selectionTopRow, m_selectionBottomRow, m_selectionLeftColumn, m_selectionRightColumn;
int m_selectionCornerRow, m_selectionCornerColumn; int m_selectionCornerRow, m_selectionCornerColumn;
}; };
class PasteCommand : public StoreOldCharactersCommand class PasteCommand : public LevelOneCommand
{ {
public: public:
PasteCommand(TeletextDocument *teletextDocument, int pageCharSet, QUndoCommand *parent = 0); PasteCommand(TeletextDocument *teletextDocument, int pageCharSet, QUndoCommand *parent = 0);
@@ -186,7 +178,7 @@ public:
void undo() override; void undo() override;
private: private:
QByteArrayList m_pastingCharacters; QByteArrayList m_oldCharacters, m_pastingCharacters;
int m_pasteTopRow, m_pasteBottomRow, m_pasteLeftColumn, m_pasteRightColumn; int m_pasteTopRow, m_pasteBottomRow, m_pasteLeftColumn, m_pasteRightColumn;
int m_clipboardDataHeight, m_clipboardDataWidth; int m_clipboardDataHeight, m_clipboardDataWidth;
int m_selectionCornerRow, m_selectionCornerColumn; int m_selectionCornerRow, m_selectionCornerColumn;