Files
QTeletextMaker/levelonecommands.cpp

949 lines
34 KiB
C++
Raw Permalink Normal View History

2020-11-10 18:48:12 +00:00
/*
2024-01-01 00:12:17 +00:00
* Copyright (C) 2020-2024 Gavin MacGregor
2020-11-10 18:48:12 +00:00
*
* This file is part of QTeletextMaker.
*
* QTeletextMaker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QTeletextMaker is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QTeletextMaker. If not, see <https://www.gnu.org/licenses/>.
*/
2021-05-03 22:17:51 +01:00
#include <QApplication>
#include <QByteArray>
#include <QByteArrayList>
#include <QClipboard>
#include <QImage>
2021-05-03 22:17:51 +01:00
#include <QMimeData>
#include <QRegularExpression>
#include <QSet>
2021-05-03 22:17:51 +01:00
2020-11-10 18:48:12 +00:00
#include "levelonecommands.h"
#include "document.h"
#include "keymap.h"
2020-11-10 18:48:12 +00:00
LevelOneCommand::LevelOneCommand(TeletextDocument *teletextDocument, QUndoCommand *parent) : QUndoCommand(parent)
2020-11-10 18:48:12 +00:00
{
m_teletextDocument = teletextDocument;
m_subPageIndex = teletextDocument->currentSubPageIndex();
m_row = teletextDocument->cursorRow();
m_column = teletextDocument->cursorColumn();
m_firstDo = true;
}
2024-07-09 22:16:58 +01:00
QByteArrayList LevelOneCommand::storeCharacters(int topRow, int leftColumn, int bottomRow, int rightColumn)
{
2024-07-09 22:16:58 +01:00
QByteArrayList result;
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);
2024-07-09 22:16:58 +01:00
result.append(rowArray);
}
2024-07-09 22:16:58 +01:00
return result;
}
2024-07-09 22:16:58 +01:00
void LevelOneCommand::retrieveCharacters(int topRow, int leftColumn, const QByteArrayList &storedChars)
{
2024-07-09 22:16:58 +01:00
const int bottomRow = topRow + storedChars.size() - 1;
const int rightColumn = leftColumn + storedChars.at(0).size() - 1;
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) {
2024-07-09 22:16:58 +01:00
m_teletextDocument->currentSubPage()->setCharacter(r, c, storedChars.at(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;
m_newCharacter = newCharacter;
m_insertMode = insertMode;
for (int c=0; c<40; c++)
m_oldRowContents[c] = m_newRowContents[c] = m_teletextDocument->currentSubPage()->character(m_row, c);
if (m_insertMode)
setText(QObject::tr("insert character"));
else
setText(QObject::tr("overwrite character"));
2020-11-10 18:48:12 +00:00
}
void TypeCharacterCommand::redo()
2020-11-10 18:48:12 +00:00
{
m_teletextDocument->selectSubPageIndex(m_subPageIndex);
// Only apply the typed character on the first do, m_newRowContents will remember it if we redo
if (m_firstDo) {
if (m_insertMode) {
// Insert - Move characters rightwards
for (int c=39; c>m_columnEnd; c--)
m_newRowContents[c] = m_newRowContents[c-1];
}
m_newRowContents[m_columnEnd] = m_newCharacter;
m_firstDo = false;
}
for (int c=0; c<40; c++)
m_teletextDocument->currentSubPage()->setCharacter(m_row, c, m_newRowContents[c]);
m_teletextDocument->moveCursor(m_row, m_columnEnd);
2020-11-10 18:48:12 +00:00
m_teletextDocument->cursorRight();
2023-09-12 13:50:00 +01:00
emit m_teletextDocument->contentsChanged();
2020-11-10 18:48:12 +00:00
}
void TypeCharacterCommand::undo()
2020-11-10 18:48:12 +00:00
{
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_columnStart);
2023-09-12 13:50:00 +01:00
emit m_teletextDocument->contentsChanged();
2020-11-10 18:48:12 +00:00
}
bool TypeCharacterCommand::mergeWith(const QUndoCommand *command)
{
const TypeCharacterCommand *newerCommand = static_cast<const TypeCharacterCommand *>(command);
// Has to be the next typed column on the same row
if (m_subPageIndex != newerCommand->m_subPageIndex || m_row != newerCommand->m_row || m_columnEnd != newerCommand->m_columnEnd-1)
return false;
m_columnEnd = newerCommand->m_columnEnd;
for (int c=0; c<40; c++)
m_newRowContents[c] = newerCommand->m_newRowContents[c];
return true;
}
2020-11-10 18:48:12 +00:00
ToggleMosaicBitCommand::ToggleMosaicBitCommand(TeletextDocument *teletextDocument, unsigned char bitToToggle, QUndoCommand *parent) : LevelOneCommand(teletextDocument, parent)
2020-11-10 18:48:12 +00:00
{
m_oldCharacter = teletextDocument->currentSubPage()->character(m_row, m_column);
2024-07-07 19:41:39 +01:00
2020-11-10 18:48:12 +00:00
if (bitToToggle == 0x20 || bitToToggle == 0x7f)
2024-07-07 19:41:39 +01:00
// Clear or fill the whole mosaic character
2020-11-10 18:48:12 +00:00
m_newCharacter = bitToToggle;
else if (bitToToggle == 0x66)
2024-07-07 19:41:39 +01:00
// Dither
m_newCharacter = (m_row & 1) ? 0x66 : 0x39;
2024-07-07 19:41:39 +01:00
else if (m_oldCharacter & 0x20)
// Previous character was mosaic, just toggle the bit(s)
2020-11-10 18:48:12 +00:00
m_newCharacter = m_oldCharacter ^ bitToToggle;
2024-07-07 19:41:39 +01:00
else
// Previous character was blast-through, change to mosaic and set bit alone
m_newCharacter = bitToToggle | 0x20;
setText(QObject::tr("mosaic"));
2020-11-10 18:48:12 +00:00
}
void ToggleMosaicBitCommand::redo()
{
m_teletextDocument->selectSubPageIndex(m_subPageIndex);
m_teletextDocument->currentSubPage()->setCharacter(m_row, m_column, m_newCharacter);
2020-11-10 18:48:12 +00:00
m_teletextDocument->moveCursor(m_row, m_column);
2023-09-12 13:50:00 +01:00
emit m_teletextDocument->contentsChanged();
2020-11-10 18:48:12 +00:00
}
void ToggleMosaicBitCommand::undo()
{
m_teletextDocument->selectSubPageIndex(m_subPageIndex);
m_teletextDocument->currentSubPage()->setCharacter(m_row, m_column, m_oldCharacter);
2020-11-10 18:48:12 +00:00
m_teletextDocument->moveCursor(m_row, m_column);
2023-09-12 13:50:00 +01:00
emit m_teletextDocument->contentsChanged();
2020-11-10 18:48:12 +00:00
}
bool ToggleMosaicBitCommand::mergeWith(const QUndoCommand *command)
{
const ToggleMosaicBitCommand *newerCommand = static_cast<const ToggleMosaicBitCommand *>(command);
2020-11-10 18:48:12 +00:00
if (m_subPageIndex != newerCommand->m_subPageIndex || m_row != newerCommand->m_row || m_column != newerCommand->m_column)
2020-11-10 18:48:12 +00:00
return false;
m_newCharacter = newerCommand->m_newCharacter;
2020-11-10 18:48:12 +00:00
return true;
}
BackspaceKeyCommand::BackspaceKeyCommand(TeletextDocument *teletextDocument, bool insertMode, QUndoCommand *parent) : LevelOneCommand(teletextDocument, parent)
2020-11-10 18:48:12 +00:00
{
m_columnStart = m_column - 1;
if (m_columnStart == -1) {
m_columnStart = 39;
2020-11-10 18:48:12 +00:00
if (--m_row == 0)
m_row = 24;
}
m_columnEnd = m_columnStart;
m_insertMode = insertMode;
for (int c=0; c<40; c++)
m_oldRowContents[c] = m_newRowContents[c] = m_teletextDocument->currentSubPage()->character(m_row, c);
setText(QObject::tr("backspace"));
2020-11-10 18:48:12 +00:00
}
void BackspaceKeyCommand::redo()
2020-11-10 18:48:12 +00:00
{
m_teletextDocument->selectSubPageIndex(m_subPageIndex);
if (m_firstDo) {
if (m_insertMode) {
// Insert - Move characters leftwards and put a space on the far right
for (int c=m_columnEnd; c<39; c++)
m_newRowContents[c] = m_newRowContents[c+1];
m_newRowContents[39] = 0x20;
} else
// Replace - Overwrite backspaced character with a space
m_newRowContents[m_columnEnd] = 0x20;
m_firstDo = false;
}
for (int c=0; c<40; c++)
m_teletextDocument->currentSubPage()->setCharacter(m_row, c, m_newRowContents[c]);
m_teletextDocument->moveCursor(m_row, m_columnEnd);
2023-09-12 13:50:00 +01:00
emit m_teletextDocument->contentsChanged();
2020-11-10 18:48:12 +00:00
}
void BackspaceKeyCommand::undo()
2020-11-10 18:48:12 +00:00
{
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_columnStart);
2020-11-10 18:48:12 +00:00
m_teletextDocument->cursorRight();
2023-09-12 13:50:00 +01:00
emit m_teletextDocument->contentsChanged();
2020-11-10 18:48:12 +00:00
}
bool BackspaceKeyCommand::mergeWith(const QUndoCommand *command)
{
const BackspaceKeyCommand *newerCommand = static_cast<const BackspaceKeyCommand *>(command);
// Has to be the next backspaced column on the same row
if (m_subPageIndex != newerCommand->m_subPageIndex || m_row != newerCommand->m_row || m_columnEnd != newerCommand->m_columnEnd+1)
return false;
// For backspacing m_columnStart is where we began backspacing and m_columnEnd is where we ended backspacing
// so m_columnEnd will be less than m_columnStart
m_columnEnd = newerCommand->m_columnEnd;
for (int c=0; c<40; c++)
m_newRowContents[c] = newerCommand->m_newRowContents[c];
return true;
}
2020-11-10 18:48:12 +00:00
DeleteKeyCommand::DeleteKeyCommand(TeletextDocument *teletextDocument, QUndoCommand *parent) : LevelOneCommand(teletextDocument, parent)
2021-02-12 21:33:06 +00:00
{
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);
// Move characters leftwards and put a space on the far right
2021-02-12 21:33:06 +00:00
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);
2023-09-12 13:50:00 +01:00
emit m_teletextDocument->contentsChanged();
2021-02-12 21:33:06 +00:00
}
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);
2023-09-12 13:50:00 +01:00
emit m_teletextDocument->contentsChanged();
2021-02-12 21:33:06 +00:00
}
bool DeleteKeyCommand::mergeWith(const QUndoCommand *command)
{
const DeleteKeyCommand *newerCommand = static_cast<const DeleteKeyCommand *>(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;
}
ShiftMosaicsCommand::ShiftMosaicsCommand(TeletextDocument *teletextDocument, const QSet<QPair<int, int>> &mosaicList, QUndoCommand *parent) : LevelOneCommand(teletextDocument, parent)
{
m_selectionTopRow = m_teletextDocument->selectionTopRow();
m_selectionLeftColumn = m_teletextDocument->selectionLeftColumn();
m_selectionBottomRow = m_teletextDocument->selectionBottomRow();
m_selectionRightColumn = m_teletextDocument->selectionRightColumn();
m_selectionCornerRow = m_teletextDocument->selectionCornerRow();
m_selectionCornerColumn = m_teletextDocument->selectionCornerColumn();
m_mosaicList = mosaicList;
m_oldCharacters = storeCharacters(m_selectionTopRow, m_selectionLeftColumn, m_selectionBottomRow, m_selectionRightColumn);
m_newCharacters = m_oldCharacters;
}
void ShiftMosaicsCommand::redo()
{
m_teletextDocument->selectSubPageIndex(m_subPageIndex);
retrieveCharacters(m_selectionTopRow, m_selectionLeftColumn, m_newCharacters);
emit m_teletextDocument->contentsChanged();
m_teletextDocument->setSelectionCorner(m_selectionCornerRow, m_selectionCornerColumn);
m_teletextDocument->moveCursor(m_row, m_column, true);
}
void ShiftMosaicsCommand::undo()
{
m_teletextDocument->selectSubPageIndex(m_subPageIndex);
retrieveCharacters(m_selectionTopRow, m_selectionLeftColumn, m_oldCharacters);
emit m_teletextDocument->contentsChanged();
m_teletextDocument->setSelectionCorner(m_selectionCornerRow, m_selectionCornerColumn);
m_teletextDocument->moveCursor(m_row, m_column, true);
}
bool ShiftMosaicsCommand::mergeWith(const QUndoCommand *command)
{
const ShiftMosaicsCommand *newerCommand = static_cast<const ShiftMosaicsCommand *>(command);
if (m_subPageIndex != newerCommand->m_subPageIndex || m_selectionTopRow != newerCommand->m_selectionTopRow || m_selectionLeftColumn != newerCommand->m_selectionLeftColumn || m_selectionBottomRow != newerCommand->m_selectionBottomRow || m_selectionRightColumn != newerCommand->m_selectionRightColumn)
return false;
m_newCharacters = newerCommand->m_newCharacters;
return true;
}
ShiftMosaicsUpCommand::ShiftMosaicsUpCommand(TeletextDocument *teletextDocument, const QSet<QPair<int, int>> &mosaicList, QUndoCommand *parent) : ShiftMosaicsCommand(teletextDocument, mosaicList, parent)
{
for (int r=m_selectionTopRow; r<=m_selectionBottomRow; r++)
for (int c=m_selectionLeftColumn; c<=m_selectionRightColumn; c++)
if (m_mosaicList.contains(qMakePair(r, c))) {
const int lr = r - m_selectionTopRow;
const int lc = c - m_selectionLeftColumn;
unsigned char mosaicWrap = 0x00;
for (int sr=r+1; sr<=m_selectionBottomRow; sr++)
if (m_mosaicList.contains(qMakePair(sr, c))) {
mosaicWrap = m_newCharacters[sr - m_selectionTopRow][lc];
mosaicWrap = ((mosaicWrap & 0x01) << 4) | ((mosaicWrap & 0x02) << 5);
break;
}
m_newCharacters[lr][lc] = ((m_newCharacters[lr][lc] >> 2) & 0x07) | ((m_newCharacters[lr][lc] & 0x40) >> 3) | mosaicWrap | 0x20;
}
setText(QObject::tr("shift mosaics up"));
}
ShiftMosaicsDownCommand::ShiftMosaicsDownCommand(TeletextDocument *teletextDocument, const QSet<QPair<int, int>> &mosaicList, QUndoCommand *parent) : ShiftMosaicsCommand(teletextDocument, mosaicList, parent)
{
for (int r=m_selectionBottomRow; r>=m_selectionTopRow; r--)
for (int c=m_selectionLeftColumn; c<=m_selectionRightColumn; c++)
if (m_mosaicList.contains(qMakePair(r, c))) {
const int lr = r - m_selectionTopRow;
const int lc = c - m_selectionLeftColumn;
unsigned char mosaicWrap = 0x00;
for (int sr=r-1; sr>=m_selectionTopRow; sr--)
if (m_mosaicList.contains(qMakePair(sr, c))) {
mosaicWrap = m_newCharacters[sr - m_selectionTopRow][lc];
mosaicWrap = ((mosaicWrap & 0x10) >> 4) | ((mosaicWrap & 0x40) >> 5);
break;
}
m_newCharacters[lr][lc] = ((m_newCharacters[lr][lc] & 0x07) << 2) | ((m_newCharacters[lr][lc] & 0x08) << 3) | mosaicWrap | 0x20;
}
setText(QObject::tr("shift mosaics down"));
}
ShiftMosaicsLeftCommand::ShiftMosaicsLeftCommand(TeletextDocument *teletextDocument, const QSet<QPair<int, int>> &mosaicList, QUndoCommand *parent) : ShiftMosaicsCommand(teletextDocument, mosaicList, parent)
{
for (int c=m_selectionLeftColumn; c<=m_selectionRightColumn; c++)
for (int r=m_selectionTopRow; r<=m_selectionBottomRow; r++)
if (m_mosaicList.contains(qMakePair(r, c))) {
const int lr = r - m_selectionTopRow;
const int lc = c - m_selectionLeftColumn;
unsigned char mosaicWrap = 0x00;
for (int sc=c+1; sc<=m_selectionRightColumn; sc++)
if (m_mosaicList.contains(qMakePair(r, sc))) {
mosaicWrap = m_newCharacters[lr][sc - m_selectionLeftColumn];
mosaicWrap = ((mosaicWrap & 0x05) << 1) | ((mosaicWrap & 0x10) << 2);
break;
}
m_newCharacters[lr][lc] = ((m_newCharacters[lr][lc] & 0x0a) >> 1) | ((m_newCharacters[lr][lc] & 0x40) >> 2) | mosaicWrap | 0x20;
}
setText(QObject::tr("shift mosaics left"));
}
ShiftMosaicsRightCommand::ShiftMosaicsRightCommand(TeletextDocument *teletextDocument, const QSet<QPair<int, int>> &mosaicList, QUndoCommand *parent) : ShiftMosaicsCommand(teletextDocument, mosaicList, parent)
{
for (int c=m_selectionRightColumn; c>=m_selectionLeftColumn; c--)
for (int r=m_selectionTopRow; r<=m_selectionBottomRow; r++)
if (m_mosaicList.contains(qMakePair(r, c))) {
const int lr = r - m_selectionTopRow;
const int lc = c - m_selectionLeftColumn;
unsigned char mosaicWrap = 0x00;
for (int sc=c-1; sc>=m_selectionLeftColumn; sc--)
if (m_mosaicList.contains(qMakePair(r, sc))) {
mosaicWrap = m_newCharacters[lr][sc - m_selectionLeftColumn];
mosaicWrap = ((mosaicWrap & 0x0a) >> 1) | ((mosaicWrap & 0x40) >> 2);
break;
}
m_newCharacters[lr][lc] = ((m_newCharacters[lr][lc] & 0x05) << 1) | ((m_newCharacters[lr][lc] & 0x10) << 2) | mosaicWrap | 0x20;
}
setText(QObject::tr("shift mosaics right"));
}
InsertRowCommand::InsertRowCommand(TeletextDocument *teletextDocument, bool copyRow, QUndoCommand *parent) : LevelOneCommand(teletextDocument, parent)
2020-11-10 18:48:12 +00:00
{
m_copyRow = copyRow;
if (m_copyRow)
setText(QObject::tr("insert copy row"));
else
setText(QObject::tr("insert blank row"));
2020-11-10 18:48:12 +00:00
}
void InsertRowCommand::redo()
{
m_teletextDocument->selectSubPageIndex(m_subPageIndex);
m_teletextDocument->moveCursor(m_row, -1);
// Store copy of the bottom row we're about to push out, for undo
for (int c=0; c<40; c++)
m_deletedBottomRow[c] = m_teletextDocument->currentSubPage()->character(23, c);
// Move lines below the inserting row downwards without affecting the FastText row
2020-11-10 18:48:12 +00:00
for (int r=22; r>=m_row; r--)
for (int c=0; c<40; c++)
m_teletextDocument->currentSubPage()->setCharacter(r+1, c, m_teletextDocument->currentSubPage()->character(r, c));
if (!m_copyRow)
// The above movement leaves a duplicate of the current row, so blank it if requested
2020-11-10 18:48:12 +00:00
for (int c=0; c<40; c++)
m_teletextDocument->currentSubPage()->setCharacter(m_row, c, ' ');
2023-09-12 13:50:00 +01:00
emit m_teletextDocument->contentsChanged();
2020-11-10 18:48:12 +00:00
}
void InsertRowCommand::undo()
{
m_teletextDocument->selectSubPageIndex(m_subPageIndex);
m_teletextDocument->moveCursor(m_row, -1);
// Move lines below the deleting row upwards without affecting the FastText row
2020-11-10 18:48:12 +00:00
for (int r=m_row; r<23; r++)
for (int c=0; c<40; c++)
m_teletextDocument->currentSubPage()->setCharacter(r, c, m_teletextDocument->currentSubPage()->character(r+1, c));
// Now repair the bottom row we pushed out
for (int c=0; c<40; c++)
m_teletextDocument->currentSubPage()->setCharacter(23, c, m_deletedBottomRow[c]);
2023-09-12 13:50:00 +01:00
emit m_teletextDocument->contentsChanged();
2020-11-10 18:48:12 +00:00
}
DeleteRowCommand::DeleteRowCommand(TeletextDocument *teletextDocument, QUndoCommand *parent) : LevelOneCommand(teletextDocument, parent)
2020-11-10 18:48:12 +00:00
{
setText(QObject::tr("delete row"));
2020-11-10 18:48:12 +00:00
}
void DeleteRowCommand::redo()
{
m_teletextDocument->selectSubPageIndex(m_subPageIndex);
m_teletextDocument->moveCursor(m_row, -1);
// Store copy of the row we're going to delete, for undo
for (int c=0; c<40; c++)
m_deletedRow[c] = m_teletextDocument->currentSubPage()->character(m_row, c);
// Move lines below the deleting row upwards without affecting the FastText row
2020-11-10 18:48:12 +00:00
for (int r=m_row; r<23; r++)
for (int c=0; c<40; c++)
m_teletextDocument->currentSubPage()->setCharacter(r, c, m_teletextDocument->currentSubPage()->character(r+1, c));
// If we deleted the FastText row blank that row, otherwise blank the last row
int blankingRow = (m_row < 24) ? 23 : 24;
for (int c=0; c<40; c++)
m_teletextDocument->currentSubPage()->setCharacter(blankingRow, c, ' ');
2023-09-12 13:50:00 +01:00
emit m_teletextDocument->contentsChanged();
2020-11-10 18:48:12 +00:00
}
void DeleteRowCommand::undo()
{
m_teletextDocument->selectSubPageIndex(m_subPageIndex);
m_teletextDocument->moveCursor(m_row, -1);
// Move lines below the inserting row downwards without affecting the FastText row
2020-11-10 18:48:12 +00:00
for (int r=22; r>=m_row; r--)
for (int c=0; c<40; c++)
m_teletextDocument->currentSubPage()->setCharacter(r+1, c, m_teletextDocument->currentSubPage()->character(r, c));
// Now repair the row we deleted
for (int c=0; c<40; c++)
m_teletextDocument->currentSubPage()->setCharacter(m_row, c, m_deletedRow[c]);
2023-09-12 13:50:00 +01:00
emit m_teletextDocument->contentsChanged();
2020-11-10 18:48:12 +00:00
}
2021-05-03 22:17:51 +01:00
#ifndef QT_NO_CLIPBOARD
2024-07-09 22:16:58 +01:00
CutCommand::CutCommand(TeletextDocument *teletextDocument, QUndoCommand *parent) : LevelOneCommand(teletextDocument, parent)
2021-05-03 22:17:51 +01:00
{
m_selectionTopRow = m_teletextDocument->selectionTopRow();
m_selectionBottomRow = m_teletextDocument->selectionBottomRow();
m_selectionLeftColumn = m_teletextDocument->selectionLeftColumn();
m_selectionRightColumn = m_teletextDocument->selectionRightColumn();
m_selectionCornerRow = m_teletextDocument->selectionCornerRow();
m_selectionCornerColumn = m_teletextDocument->selectionCornerColumn();
2024-07-09 22:16:58 +01:00
m_oldCharacters = storeCharacters(m_selectionTopRow, m_selectionLeftColumn, m_selectionBottomRow, m_selectionRightColumn);
2021-05-03 22:17:51 +01:00
setText(QObject::tr("cut"));
}
void CutCommand::redo()
{
m_teletextDocument->selectSubPageIndex(m_subPageIndex);
for (int r=m_selectionTopRow; r<=m_selectionBottomRow; r++) {
for (int c=m_selectionLeftColumn; c<=m_selectionRightColumn; c++)
m_teletextDocument->currentSubPage()->setCharacter(r, c, 0x20);
}
emit m_teletextDocument->contentsChanged();
2021-05-03 22:17:51 +01:00
}
void CutCommand::undo()
{
m_teletextDocument->selectSubPageIndex(m_subPageIndex);
2024-07-09 22:16:58 +01:00
retrieveCharacters(m_selectionTopRow, m_selectionLeftColumn, m_oldCharacters);
2021-05-03 22:17:51 +01:00
emit m_teletextDocument->contentsChanged();
2021-05-03 22:17:51 +01:00
m_teletextDocument->setSelectionCorner(m_selectionCornerRow, m_selectionCornerColumn);
m_teletextDocument->moveCursor(m_row, m_column, true);
}
2024-07-09 22:16:58 +01:00
PasteCommand::PasteCommand(TeletextDocument *teletextDocument, int pageCharSet, QUndoCommand *parent) : LevelOneCommand(teletextDocument, parent)
2021-05-03 22:17:51 +01:00
{
const QClipboard *clipboard = QApplication::clipboard();
const QMimeData *mimeData = clipboard->mimeData();
QByteArray nativeData;
m_selectionActive = m_teletextDocument->selectionActive();
if (m_selectionActive) {
m_selectionCornerRow = m_teletextDocument->selectionCornerRow();
m_selectionCornerColumn = m_teletextDocument->selectionCornerColumn();
m_pasteTopRow = m_teletextDocument->selectionTopRow();
m_pasteBottomRow = m_teletextDocument->selectionBottomRow();
m_pasteLeftColumn = m_teletextDocument->selectionLeftColumn();
m_pasteRightColumn = m_teletextDocument->selectionRightColumn();
} else {
m_pasteTopRow = m_row;
m_pasteLeftColumn = m_column;
// m_pasteBottomRow and m_pasteRightColumn will be filled in later
// when the size of the clipboard data is known
2021-05-03 22:17:51 +01:00
}
// Zero size here represents invalid or empty clipboard data
2021-05-03 22:17:51 +01:00
m_clipboardDataHeight = m_clipboardDataWidth = 0;
// Try to get something from the clipboard
// FIXME is this a correct "custom" mime type? Or should we use vnd?
2021-05-03 22:17:51 +01:00
nativeData = mimeData->data("application/x-teletext");
if (nativeData.size() > 2) {
// Native clipboard data: we put it there ourselves
m_plainText = false;
2021-05-03 22:17:51 +01:00
m_clipboardDataHeight = nativeData.at(0);
m_clipboardDataWidth = nativeData.at(1);
// Guard against invalid dimensions or total size not matching stated dimensions
if (m_clipboardDataHeight > 0 && m_clipboardDataWidth > 0 && m_clipboardDataHeight <= 25 && m_clipboardDataWidth <= 40 && nativeData.size() == m_clipboardDataHeight * m_clipboardDataWidth + 2)
for (int r=0; r<m_clipboardDataHeight; r++)
m_pastingCharacters.append(nativeData.mid(2 + r * m_clipboardDataWidth, m_clipboardDataWidth));
else {
2021-05-03 22:17:51 +01:00
// Invalidate
m_clipboardDataHeight = m_clipboardDataWidth = 0;
return;
}
if (!m_selectionActive) {
m_pasteBottomRow = m_row + m_clipboardDataHeight - 1;
m_pasteRightColumn = m_column + m_clipboardDataWidth - 1;
}
} else if (mimeData->hasText()) {
// Plain text
m_plainText = true;
const int rightColumn = m_selectionActive ? m_pasteRightColumn : 39;
// Parse line-feeds in the clipboard data
QStringList plainTextData = mimeData->text().split(QRegularExpression("\n|\r\n|\r"));
// "if" statement will be false if clipboard data is a single line of text
// that will fit from the cursor position
if (plainTextData.size() != 1 || m_pasteLeftColumn + plainTextData.at(0).size() - 1 > rightColumn) {
bool wrappingNeeded = false;
if (!m_selectionActive) {
// If selection is NOT active, use the full width of the page to paste.
// The second and subsequent lines will start at column 1
m_pasteLeftColumn = 1;
// Check if first word in the first line will fit from the cursor position
bool firstWordFits = true;
const int firstSpace = plainTextData.at(0).indexOf(' ');
if (firstSpace == -1 && m_column + plainTextData.at(0).size() > 40)
firstWordFits = false; // Only one word in first line, and it won't fit
else if (m_column + firstSpace > 40)
firstWordFits = false; // First word in first line won't fit
// If the first word WILL fit at the cursor position, pad the first line
// to match the cursor position using null characters.
// In the QString null characters represent character cells in the
// pasting rectangle that won't overwrite what's on the page.
// If the first word WON'T fit, start pasting at the beginning of the next row.
if (firstWordFits)
plainTextData[0] = QString(m_column-m_pasteLeftColumn, QChar::Null) + plainTextData.at(0);
else if (m_pasteTopRow < 24)
m_pasteTopRow++;
else
return;
}
const int pasteWidth = rightColumn - m_pasteLeftColumn + 1;
// Find out if we need to word-wrap
for (int i=0; i<plainTextData.size(); i++)
if (plainTextData.at(i).size() > pasteWidth) {
wrappingNeeded = true;
break;
}
if (wrappingNeeded) {
QStringList wrappedText;
for (int i=0; i<plainTextData.size(); i++) {
// Split this line into individual words
QStringList lineWords = plainTextData.at(i).split(' ');
// If there's any words which are too long to fit,
// break them across multiple lines
for (int j=0; j<lineWords.size(); j++)
if (lineWords.at(j).size() > pasteWidth) {
lineWords.insert(j+1, lineWords.at(j).mid(pasteWidth));
lineWords[j].truncate(pasteWidth);
}
// Now reassemble the words into lines that will fit
QString currentLine = lineWords.at(0);
for (int j=1; j<lineWords.size(); j++)
if (currentLine.size() + 1 + lineWords.at(j).size() <= pasteWidth)
currentLine.append(' ' + lineWords.at(j));
else {
wrappedText.append(currentLine);
currentLine = lineWords.at(j);
}
wrappedText.append(currentLine);
}
plainTextData.swap(wrappedText);
}
}
m_clipboardDataHeight = plainTextData.size();
m_clipboardDataWidth = 0;
// Convert the unicode clipboard text into teletext bytes matching the current Level 1
// character set of this page
for (int r=0; r<m_clipboardDataHeight; r++) {
m_pastingCharacters.append(QByteArray());
for (int c=0; c<plainTextData.at(r).size(); c++) {
char convertedChar;
const QChar charToConvert = plainTextData.at(r).at(c);
// Map a null character in the QString to 0xff (or -1)
// In the QByteArray 0xff bytes represent character cells in the pasting rectangle
// that won't overwrite what's on the page
if (charToConvert == QChar::Null)
convertedChar = -1;
2023-08-20 19:01:53 +01:00
else if (charToConvert >= QChar(0x01) && charToConvert <= QChar(0x1f))
convertedChar = ' ';
else if (keymapping[pageCharSet].contains(charToConvert))
// Remapped character or non-Latin character converted successfully
convertedChar = keymapping[pageCharSet].value(charToConvert);
else {
// Either a Latin character or non-Latin character that can't be converted
// See if it's a Latin character
convertedChar = charToConvert.toLatin1();
if (convertedChar <= 0)
// Couldn't convert - make it a block character so it doesn't need to be inserted-between later on
convertedChar = 0x7f;
}
m_pastingCharacters[r].append(convertedChar);
}
m_clipboardDataWidth = qMax(m_pastingCharacters.at(r).size(), m_clipboardDataWidth);
}
// Pad the end of short lines with spaces to make a box
for (int r=0; r<m_clipboardDataHeight; r++)
m_pastingCharacters[r] = m_pastingCharacters.at(r).leftJustified(m_clipboardDataWidth);
if (!m_selectionActive) {
m_pasteBottomRow = m_pasteTopRow + m_clipboardDataHeight - 1;
m_pasteRightColumn = m_pasteLeftColumn + m_clipboardDataWidth - 1;
}
} else if (mimeData->hasImage()) {
QImage imageData = qvariant_cast<QImage>(mimeData->imageData());
m_plainText = false;
// Round up when dividing pixel size into character cell size
m_clipboardDataHeight = (imageData.height() + 2) / 3;
m_clipboardDataWidth = (imageData.width() + 1) / 2;
// Format_MonoLSB reverses the bits which makes them easier to shuffle into sixels
if (imageData.depth() == 1)
imageData.convertTo(QImage::Format_MonoLSB);
else
// Only pure black and white images convert reliably this way...
imageData = imageData.convertToFormat(QImage::Format_MonoLSB, QVector<QRgb>{0x000000ff, 0xffffffff});
for (int r=0; r<m_clipboardDataHeight; r++)
m_pastingCharacters.append(QByteArray(m_clipboardDataWidth, 0x00));
// Directly read the pixel-bits and convert them to sixels with some funky bit manipulation
for (int y=0; y<imageData.height(); y++) {
const unsigned char *bytePointer = imageData.constScanLine(y);
// Three rows of sixels per character cell
const int r = y / 3;
// Where to shuffle the bits into the top, middle or bottom row of sixels
// Yes it does put the bottom right sixel into bit 5 instead of bit 6;
// this gets remedied further on
const int yShift = (y % 3) * 2;
// The loop does eight horizontal pixels into four character cells at a time
for (int x=0; x<imageData.width(); x+=8) {
const unsigned char byteScanned = *bytePointer;
const int c = x / 2;
m_pastingCharacters[r][c] = m_pastingCharacters[r][c] | ((byteScanned & 0x03) << yShift);
// Since we're doing four character cells at a time, we need to exit the loop
// early before we go out of bounds.
// Yes it does leave an undefined last column of sixels from images that are an
// odd numbered number of pixels wide; this gets remedied further on
if (x + 2 >= imageData.width())
continue;
m_pastingCharacters[r][c+1] = m_pastingCharacters[r][c+1] | (((byteScanned >> 2) & 0x03) << yShift);
if (x + 4 >= imageData.width())
continue;
m_pastingCharacters[r][c+2] = m_pastingCharacters[r][c+2] | (((byteScanned >> 4) & 0x03) << yShift);
if (x + 6 >= imageData.width())
continue;
m_pastingCharacters[r][c+3] = m_pastingCharacters[r][c+3] | (((byteScanned >> 6) & 0x03) << yShift);
bytePointer++;
}
}
for (int r=0; r<m_clipboardDataHeight; r++) {
for (int c=0; c<m_clipboardDataWidth; c++)
if (m_pastingCharacters.at(r).at(c) & 0x20)
// If bit 5 was set, correct this to bit 6
// but we keep bit 5 set as all mosaic characters have bit 5 set
m_pastingCharacters[r][c] = m_pastingCharacters.at(r).at(c) | 0x40;
else
// Set bit 5 to have it recognised as a mosaic character
m_pastingCharacters[r][c] = m_pastingCharacters.at(r).at(c) | 0x20;
// If image was an odd numbered width, neutralise the undefined sixels
// on the right half
if (imageData.width() & 0x01)
m_pastingCharacters[r][m_clipboardDataWidth-1] = m_pastingCharacters.at(r).at(m_clipboardDataWidth-1) & 0x35;
}
if (!m_selectionActive) {
m_pasteBottomRow = m_row + m_clipboardDataHeight - 1;
m_pasteRightColumn = m_column + m_clipboardDataWidth - 1;
}
2021-05-03 22:17:51 +01:00
}
if (m_clipboardDataWidth == 0 || m_clipboardDataHeight == 0)
2021-05-03 22:17:51 +01:00
return;
2024-07-09 22:16:58 +01:00
m_oldCharacters = storeCharacters(m_pasteTopRow, m_pasteLeftColumn, m_pasteBottomRow, m_pasteRightColumn);
2021-05-03 22:17:51 +01:00
setText(QObject::tr("paste"));
}
void PasteCommand::redo()
{
if (m_clipboardDataWidth == 0 || m_clipboardDataHeight == 0)
2021-05-03 22:17:51 +01:00
return;
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) {
// Check for 0xff bytes using "-1"
// gcc complains about "comparision always true due to limited range"
if (m_pastingCharacters.at(arrayR).at(arrayC) != -1)
m_teletextDocument->currentSubPage()->setCharacter(r, c, m_pastingCharacters.at(arrayR).at(arrayC));
arrayC++;
2021-05-03 22:17:51 +01:00
// If paste area is wider than clipboard data, repeat the pattern
// if it wasn't plain text
if (arrayC == m_clipboardDataWidth) {
if (!m_plainText)
arrayC = 0;
else
break;
}
2021-05-03 22:17:51 +01:00
}
arrayR++;
// If paste area is taller than clipboard data, repeat the pattern
// if it wasn't plain text
if (arrayR == m_clipboardDataHeight) {
if (!m_plainText)
arrayR = 0;
else
break;
}
2021-05-03 22:17:51 +01:00
}
emit m_teletextDocument->contentsChanged();
2021-05-03 22:17:51 +01:00
if (m_selectionActive) {
m_teletextDocument->setSelectionCorner(m_selectionCornerRow, m_selectionCornerColumn);
m_teletextDocument->moveCursor(m_row, m_column, true);
} else {
m_teletextDocument->moveCursor(m_row, qMin(m_column+m_clipboardDataWidth-1, 39));
2021-05-03 22:17:51 +01:00
m_teletextDocument->cursorRight();
}
}
void PasteCommand::undo()
{
if (m_clipboardDataWidth == 0 || m_clipboardDataHeight == 0)
2021-05-03 22:17:51 +01:00
return;
m_teletextDocument->selectSubPageIndex(m_subPageIndex);
2024-07-09 22:16:58 +01:00
retrieveCharacters(m_pasteTopRow, m_pasteLeftColumn, m_oldCharacters);
emit m_teletextDocument->contentsChanged();
if (!m_selectionActive)
m_teletextDocument->moveCursor(m_row, m_column);
2021-05-03 22:17:51 +01:00
}
#endif // !QT_NO_CLIPBOARD
InsertSubPageCommand::InsertSubPageCommand(TeletextDocument *teletextDocument, bool afterCurrentSubPage, bool copySubPage, QUndoCommand *parent) : LevelOneCommand(teletextDocument, parent)
2020-11-10 18:48:12 +00:00
{
m_newSubPageIndex = m_subPageIndex + afterCurrentSubPage;
2020-11-10 18:48:12 +00:00
m_copySubPage = copySubPage;
setText(QObject::tr("insert subpage"));
2020-11-10 18:48:12 +00:00
}
void InsertSubPageCommand::redo()
{
m_teletextDocument->insertSubPage(m_newSubPageIndex, m_copySubPage);
2020-11-10 18:48:12 +00:00
m_teletextDocument->selectSubPageIndex(m_newSubPageIndex, true);
}
void InsertSubPageCommand::undo()
{
m_teletextDocument->deleteSubPage(m_newSubPageIndex);
//TODO should we always wrench to "subpage viewed when we inserted"? Or just if subpage viewed is being deleted?
m_teletextDocument->selectSubPageIndex(qMin(m_newSubPageIndex, m_teletextDocument->numberOfSubPages()-1), true);
2020-11-10 18:48:12 +00:00
}
DeleteSubPageCommand::DeleteSubPageCommand(TeletextDocument *teletextDocument, QUndoCommand *parent) : LevelOneCommand(teletextDocument, parent)
2020-12-15 21:57:42 +00:00
{
setText(QObject::tr("delete subpage"));
}
void DeleteSubPageCommand::redo()
{
m_teletextDocument->deleteSubPageToRecycle(m_subPageIndex);
m_teletextDocument->selectSubPageIndex(qMin(m_subPageIndex, m_teletextDocument->numberOfSubPages()-1), true);
2020-12-15 21:57:42 +00:00
}
void DeleteSubPageCommand::undo()
{
m_teletextDocument->unDeleteSubPageFromRecycle(m_subPageIndex);
m_teletextDocument->selectSubPageIndex(m_subPageIndex, true);
2020-12-15 21:57:42 +00:00
}