Changed: #1307 Added undo/redo for Phrase Editor. Works great

--HG--
branch : gsoc2011-translationovqt
This commit is contained in:
cemycc 2011-08-12 15:00:22 +03:00
parent a983374b2a
commit ba7dfeb907
2 changed files with 22 additions and 55 deletions

View file

@ -60,6 +60,7 @@ void CEditorPhrase::open(QString filename)
editor_type = Constants::ED_PHRASE;
current_file = filename;
connect(text_edit->document(), SIGNAL(contentsChanged()), this, SLOT(docContentsChanged()));
connect(text_edit->document(), SIGNAL(undoCommandAdded()), this, SLOT(newUndoCommandAdded()));
} else {
QErrorMessage error;
error.showMessage("This file is not a phrase file.");
@ -67,6 +68,11 @@ void CEditorPhrase::open(QString filename)
}
}
void CEditorPhrase::newUndoCommandAdded()
{
current_stack->push(new CUndoPhraseNewCommand(text_edit));
}
/* void CTextEdit::keyPressEvent(QKeyEvent *event)
{
QString chars = event->text();

View file

@ -43,7 +43,10 @@ class CTextEdit : public QTextEdit
private:
QUndoStack* m_undoStack;
public:
CTextEdit(QWidget* parent = 0) : QTextEdit(parent) { }
CTextEdit(QWidget* parent = 0) : QTextEdit(parent)
{
setUndoRedoEnabled(true);
}
//void keyPressEvent(QKeyEvent *event);
void setUndoStack(QUndoStack* undoStack)
{
@ -66,73 +69,31 @@ public:
void closeEvent(QCloseEvent *event);
public Q_SLOTS:
void docContentsChanged();
void newUndoCommandAdded();
};
class CUndoPhraseInsertCommand : public QUndoCommand
class CUndoPhraseNewCommand : public QUndoCommand
{
public:
CUndoPhraseInsertCommand(int index, const QString &chars, QTextEdit *document, QUndoCommand *parent = 0) : QUndoCommand("Insert characters", parent),
m_index(index),
m_chars(chars),
m_document(document)
{ }
virtual void redo()
{
QString text = m_document->toPlainText();
text.insert(m_index, m_chars);
m_document->clear();
m_document->setPlainText(text);
}
virtual void undo()
{
QString text = m_document->toPlainText();
text.remove(m_index, m_chars.length());
m_document->clear();
m_document->setPlainText(text);
m_document->undo();
}
private:
int m_index;
QString m_chars;
QTextEdit* m_document;
};
class CUndoPhraseRemoveCommand : public QUndoCommand
{
public:
CUndoPhraseRemoveCommand(int index, int count, QTextEdit *document, QUndoCommand *parent = 0) : QUndoCommand("Remove characters", parent),
m_index(index),
m_count(count),
m_document(document)
CUndoPhraseNewCommand(CTextEdit *textEdit, QUndoCommand *parent = 0)
: QUndoCommand("Inserting/Removing characters", parent),
m_textEdit(textEdit)
{ }
virtual void redo()
~CUndoPhraseNewCommand() {}
void undo()
{
QString text = m_document->toPlainText();
m_removedChars = text.mid(m_index, m_count);
text.remove(m_index, m_count);
m_document->clear();
m_document->setPlainText(text);
m_textEdit->undo();
}
virtual void undo()
void redo()
{
QString text = m_document->toPlainText();
text.insert(m_index, m_removedChars);
m_document->clear();
m_document->setPlainText(text);
m_textEdit->redo();
}
private:
int m_index;
int m_count;
QString m_removedChars;
QTextEdit* m_document;
CTextEdit* m_textEdit;
};
class SyntaxHighlighter : public QSyntaxHighlighter