Skip to content

Commit

Permalink
ticket:4334 & 4344 Allow undo/redo in text view.
Browse files Browse the repository at this point in the history
  • Loading branch information
adeas31 committed Mar 23, 2017
1 parent 742ae25 commit 75a9b6e
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 66 deletions.
18 changes: 17 additions & 1 deletion OMEdit/OMEditGUI/Editors/BaseEditor.cpp
Expand Up @@ -1421,6 +1421,14 @@ void PlainTextEdit::keyPressEvent(QKeyEvent *pEvent)
// ctrl+k is pressed.
mpBaseEditor->toggleCommentSelection();
return;
} else if (pEvent->matches(QKeySequence::Undo)) {
// ctrl+z is pressed.
MainWindow::instance()->undo();
return;
} else if (pEvent->matches(QKeySequence::Redo)) {
// ctrl+y is pressed.
MainWindow::instance()->redo();
return;
} else if (shiftModifier && pEvent->key() == Qt::Key_Home) {
handleHomeKey(true);
return;
Expand Down Expand Up @@ -1776,7 +1784,15 @@ void BaseEditor::createActions()
*/
QMenu* BaseEditor::createStandardContextMenu()
{
QMenu *pMenu = mpPlainTextEdit->createStandardContextMenu();
/* ticket:4334 & 4344
* It's not possible to remove QPlainTextEdit undo/redo actions from standard context menu.
* So don't use the standard context menu.
* Added our custom undo/redo actions to the custom context menu.
*/
// QMenu *pMenu = mpPlainTextEdit->createStandardContextMenu();
QMenu *pMenu = new QMenu;
pMenu->addAction(MainWindow::instance()->getUndoAction());
pMenu->addAction(MainWindow::instance()->getRedoAction());
pMenu->addSeparator();
pMenu->addAction(mpFindReplaceAction);
pMenu->addAction(mpClearFindReplaceTextsAction);
Expand Down
10 changes: 9 additions & 1 deletion OMEdit/OMEditGUI/Editors/CompositeModelEditor.cpp
Expand Up @@ -1255,12 +1255,20 @@ void CompositeModelEditor::showContextMenu(QPoint point)
*/
void CompositeModelEditor::setPlainText(const QString &text)
{
static int init = 0;
if (text != mpPlainTextEdit->toPlainText()) {
mForceSetPlainText = true;
mXmlDocument.setContent(text);
updateAllOrientations();
// use the text from mXmlDocument so that we can map error to line numbers. We don't care about users formatting in the file.
mpPlainTextEdit->setPlainText(mXmlDocument.toString());
if (!init) {
init = 1;
mpPlainTextEdit->setPlainText(mXmlDocument.toString());
} else {
QTextCursor textCursor (mpPlainTextEdit->document());
textCursor.select(QTextCursor::Document);
textCursor.insertText(mXmlDocument.toString());
}
mForceSetPlainText = false;
mLastValidText = text;
}
Expand Down
12 changes: 9 additions & 3 deletions OMEdit/OMEditGUI/Editors/ModelicaEditor.cpp
Expand Up @@ -267,6 +267,7 @@ void ModelicaEditor::showContextMenu(QPoint point)
*/
void ModelicaEditor::setPlainText(const QString &text)
{
static int init = 0;
QMap<int, int> leadingSpacesMap;
QString contents = text;
// store and remove leading spaces
Expand All @@ -277,11 +278,16 @@ void ModelicaEditor::setPlainText(const QString &text)
// Only set the text when it is really new
if (contents != mpPlainTextEdit->toPlainText()) {
mForceSetPlainText = true;
if (mpModelWidget->getLibraryTreeItem()->isInPackageOneFile()) {
if (!init) {
init = 1;
mpPlainTextEdit->setPlainText(contents);
storeLeadingSpaces(leadingSpacesMap);
} else {
mpPlainTextEdit->setPlainText(contents);
QTextCursor textCursor (mpPlainTextEdit->document());
textCursor.select(QTextCursor::Document);
textCursor.insertText(contents);
}
if (mpModelWidget->getLibraryTreeItem()->isInPackageOneFile()) {
storeLeadingSpaces(leadingSpacesMap);
}
setTextChanged(false);
mForceSetPlainText = false;
Expand Down
10 changes: 8 additions & 2 deletions OMEdit/OMEditGUI/MainWindow.cpp
Expand Up @@ -1484,7 +1484,10 @@ void MainWindow::undo()
pModelWidget->clearSelection();
pModelWidget->getUndoStack()->undo();
pModelWidget->updateClassAnnotationIfNeeded();
pModelWidget->updateModelText();
pModelWidget->updateModelText(false);
} else if (pModelWidget && pModelWidget->getEditor() && pModelWidget->getEditor()->isVisible() &&
(pModelWidget->getEditor()->getPlainTextEdit()->document()->isUndoAvailable())) {
pModelWidget->getEditor()->getPlainTextEdit()->document()->undo();
}
}

Expand All @@ -1501,7 +1504,10 @@ void MainWindow::redo()
pModelWidget->clearSelection();
pModelWidget->getUndoStack()->redo();
pModelWidget->updateClassAnnotationIfNeeded();
pModelWidget->updateModelText();
pModelWidget->updateModelText(false);
} else if (pModelWidget && pModelWidget->getEditor() && pModelWidget->getEditor()->isVisible() &&
(pModelWidget->getEditor()->getPlainTextEdit()->document()->isRedoAvailable())) {
pModelWidget->getEditor()->getPlainTextEdit()->document()->redo();
}
}

Expand Down

0 comments on commit 75a9b6e

Please sign in to comment.