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

This commit is contained in:
cemycc 2011-08-12 15:00:22 +03:00
parent f1e55dae35
commit 49d892724a
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; editor_type = Constants::ED_PHRASE;
current_file = filename; current_file = filename;
connect(text_edit->document(), SIGNAL(contentsChanged()), this, SLOT(docContentsChanged())); connect(text_edit->document(), SIGNAL(contentsChanged()), this, SLOT(docContentsChanged()));
connect(text_edit->document(), SIGNAL(undoCommandAdded()), this, SLOT(newUndoCommandAdded()));
} else { } else {
QErrorMessage error; QErrorMessage error;
error.showMessage("This file is not a phrase file."); 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) /* void CTextEdit::keyPressEvent(QKeyEvent *event)
{ {
QString chars = event->text(); QString chars = event->text();

View file

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