From 83ff7d162c64b22558839b0d7130e9f62df7d916 Mon Sep 17 00:00:00 2001 From: PlutonicOverkill Date: Tue, 25 Apr 2017 16:12:59 +1200 Subject: [PATCH 1/6] Add comment/uncomment actions to the context menu --- apps/opencs/view/world/scriptedit.cpp | 21 +++++++++++++++++++++ apps/opencs/view/world/scriptedit.hpp | 6 ++++++ 2 files changed, 27 insertions(+) diff --git a/apps/opencs/view/world/scriptedit.cpp b/apps/opencs/view/world/scriptedit.cpp index 6f27d56569..2367ede99a 100644 --- a/apps/opencs/view/world/scriptedit.cpp +++ b/apps/opencs/view/world/scriptedit.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include "../../model/doc/document.hpp" @@ -284,6 +285,16 @@ void CSVWorld::ScriptEdit::updateLineNumberArea(const QRect &rect, int dy) updateLineNumberAreaWidth(0); } +void CSVWorld::ScriptEdit::commentSelection() +{ + +} + +void CSVWorld::ScriptEdit::uncommentSelection() +{ + +} + void CSVWorld::ScriptEdit::resizeEvent(QResizeEvent *e) { QPlainTextEdit::resizeEvent(e); @@ -292,6 +303,16 @@ void CSVWorld::ScriptEdit::resizeEvent(QResizeEvent *e) mLineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height())); } +void CSVWorld::ScriptEdit::contextMenuEvent(QContextMenuEvent *event) +{ + QMenu *menu = createStandardContextMenu(); + menu->addAction("Comment Selection", this, SLOT(commentSelection())); + menu->addAction("Uncomment Selection", this, SLOT(uncommentSelection())); + + menu->exec(event->globalPos()); + delete menu; +} + void CSVWorld::ScriptEdit::lineNumberAreaPaintEvent(QPaintEvent *event) { QPainter painter(mLineNumberArea); diff --git a/apps/opencs/view/world/scriptedit.hpp b/apps/opencs/view/world/scriptedit.hpp index 4977ed8e07..27e2cd17e5 100644 --- a/apps/opencs/view/world/scriptedit.hpp +++ b/apps/opencs/view/world/scriptedit.hpp @@ -77,6 +77,8 @@ namespace CSVWorld virtual void resizeEvent(QResizeEvent *e); + virtual void contextMenuEvent(QContextMenuEvent *event); + private: QVector mAllowedTypes; @@ -111,6 +113,10 @@ namespace CSVWorld void updateLineNumberAreaWidth(int newBlockCount); void updateLineNumberArea(const QRect &, int); + + void commentSelection(); + + void uncommentSelection(); }; class LineNumberArea : public QWidget From 61e374fdfcfa0b5666977dcd1329b2d2e1cc34d8 Mon Sep 17 00:00:00 2001 From: PlutonicOverkill Date: Tue, 25 Apr 2017 19:12:18 +1200 Subject: [PATCH 2/6] Implement commentSelection() and uncommentSelection() --- apps/opencs/view/world/scriptedit.cpp | 38 +++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/apps/opencs/view/world/scriptedit.cpp b/apps/opencs/view/world/scriptedit.cpp index 2367ede99a..2a2165e1f7 100644 --- a/apps/opencs/view/world/scriptedit.cpp +++ b/apps/opencs/view/world/scriptedit.cpp @@ -287,12 +287,50 @@ void CSVWorld::ScriptEdit::updateLineNumberArea(const QRect &rect, int dy) void CSVWorld::ScriptEdit::commentSelection() { + QTextCursor begin = textCursor(); + QTextCursor end = begin; + begin.setPosition(begin.selectionStart()); + begin.movePosition(QTextCursor::StartOfLine); + end.setPosition(end.selectionEnd()); + end.movePosition(QTextCursor::StartOfLine); + + for (; begin <= end; begin.movePosition(QTextCursor::StartOfLine), begin.movePosition(QTextCursor::Down)) + { + begin.insertText(";"); + } } void CSVWorld::ScriptEdit::uncommentSelection() { + QTextCursor begin = textCursor(); + QTextCursor end = begin; + begin.setPosition(begin.selectionStart()); + begin.movePosition(QTextCursor::StartOfLine); + end.setPosition(end.selectionEnd()); + end.movePosition(QTextCursor::StartOfLine); + + for (; begin <= end; begin.movePosition(QTextCursor::StartOfLine), begin.movePosition(QTextCursor::Down)) { + // loop through line until a nonspace character is reached + begin.select(QTextCursor::LineUnderCursor); + QString line = begin.selectedText(); + + // get first nonspace character in line + int index; + for (index = 0; index != line.size(); ++index) + { + if (!line[index].isSpace()) + break; + } + + if (index != line.size() && line[index] == ';') + { + line.remove(index, 1); + } + + begin.insertText(line); + } } void CSVWorld::ScriptEdit::resizeEvent(QResizeEvent *e) From 1f699552f71f5dd9e0aa96f96e55a508926904ce Mon Sep 17 00:00:00 2001 From: PlutonicOverkill Date: Wed, 26 Apr 2017 15:49:39 +1200 Subject: [PATCH 3/6] Fix infinite recursion bug in uncommentSelection() --- apps/opencs/view/world/scriptedit.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/apps/opencs/view/world/scriptedit.cpp b/apps/opencs/view/world/scriptedit.cpp index 2a2165e1f7..eb9757f601 100644 --- a/apps/opencs/view/world/scriptedit.cpp +++ b/apps/opencs/view/world/scriptedit.cpp @@ -293,9 +293,9 @@ void CSVWorld::ScriptEdit::commentSelection() begin.movePosition(QTextCursor::StartOfLine); end.setPosition(end.selectionEnd()); - end.movePosition(QTextCursor::StartOfLine); + end.movePosition(QTextCursor::EndOfLine); - for (; begin <= end; begin.movePosition(QTextCursor::StartOfLine), begin.movePosition(QTextCursor::Down)) + for (; begin < end; begin.movePosition(QTextCursor::StartOfLine), begin.movePosition(QTextCursor::Down)) { begin.insertText(";"); } @@ -309,13 +309,15 @@ void CSVWorld::ScriptEdit::uncommentSelection() begin.movePosition(QTextCursor::StartOfLine); end.setPosition(end.selectionEnd()); - end.movePosition(QTextCursor::StartOfLine); + end.movePosition(QTextCursor::EndOfLine); - for (; begin <= end; begin.movePosition(QTextCursor::StartOfLine), begin.movePosition(QTextCursor::Down)) { - // loop through line until a nonspace character is reached + for (; begin < end; begin.movePosition(QTextCursor::StartOfLine), begin.movePosition(QTextCursor::Down)) { begin.select(QTextCursor::LineUnderCursor); QString line = begin.selectedText(); + if (line.size() == 0) + continue; + // get first nonspace character in line int index; for (index = 0; index != line.size(); ++index) @@ -326,10 +328,11 @@ void CSVWorld::ScriptEdit::uncommentSelection() if (index != line.size() && line[index] == ';') { + // remove the semicolon line.remove(index, 1); + // put the line back + begin.insertText(line); } - - begin.insertText(line); } } From 3d1e640388d4c1260f8c6f8a9f0f5aef4a92f394 Mon Sep 17 00:00:00 2001 From: PlutonicOverkill Date: Wed, 26 Apr 2017 19:42:03 +1200 Subject: [PATCH 4/6] Add setting to change keyboard shortcut and fix another crash --- apps/opencs/model/prefs/state.cpp | 4 ++++ apps/opencs/view/world/scriptedit.cpp | 26 ++++++++++++++++++-------- apps/opencs/view/world/scriptedit.hpp | 1 + 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/apps/opencs/model/prefs/state.cpp b/apps/opencs/model/prefs/state.cpp index b8d6102acc..3de90e468b 100644 --- a/apps/opencs/model/prefs/state.cpp +++ b/apps/opencs/model/prefs/state.cpp @@ -326,6 +326,10 @@ void CSMPrefs::State::declare() declareShortcut ("orbit-roll-right", "Roll Right", QKeySequence(Qt::Key_E)); declareShortcut ("orbit-speed-mode", "Toggle Speed Mode", QKeySequence(Qt::Key_F)); declareShortcut ("orbit-center-selection", "Center On Selected", QKeySequence(Qt::Key_C)); + + declareSubcategory ("Script Editor"); + declareShortcut ("script-editor-comment", "Comment Selection", QKeySequence()); + declareShortcut ("script-editor-uncomment", "Uncomment Selection", QKeySequence()); } void CSMPrefs::State::declareCategory (const std::string& key) diff --git a/apps/opencs/view/world/scriptedit.cpp b/apps/opencs/view/world/scriptedit.cpp index eb9757f601..531c732ea6 100644 --- a/apps/opencs/view/world/scriptedit.cpp +++ b/apps/opencs/view/world/scriptedit.cpp @@ -14,6 +14,7 @@ #include "../../model/world/universalid.hpp" #include "../../model/world/tablemimedata.hpp" #include "../../model/prefs/state.hpp" +#include "../../model/prefs/shortcut.hpp" CSVWorld::ScriptEdit::ChangeLock::ChangeLock (ScriptEdit& edit) : mEdit (edit) { @@ -87,6 +88,20 @@ CSVWorld::ScriptEdit::ScriptEdit( <associateAction(comment); + mContextMenu->addAction(comment); + + QAction* uncomment = new QAction (tr ("Uncomment Selection"), this); + connect(uncomment, SIGNAL (triggered()), this, SLOT (uncommentSelection())); + CSMPrefs::Shortcut* uncommentShortcut = new CSMPrefs::Shortcut("script-editor-uncomment", this); + uncommentShortcut->associateAction(uncomment); + mContextMenu->addAction(uncomment); + mHighlighter = new ScriptHighlighter (document.getData(), mode, ScriptEdit::document()); connect (&document.getData(), SIGNAL (idListChanged()), this, SLOT (idListChanged())); @@ -295,7 +310,7 @@ void CSVWorld::ScriptEdit::commentSelection() end.setPosition(end.selectionEnd()); end.movePosition(QTextCursor::EndOfLine); - for (; begin < end; begin.movePosition(QTextCursor::StartOfLine), begin.movePosition(QTextCursor::Down)) + for (; begin < end; begin.movePosition(QTextCursor::EndOfLine), begin.movePosition(QTextCursor::Right)) { begin.insertText(";"); } @@ -311,7 +326,7 @@ void CSVWorld::ScriptEdit::uncommentSelection() end.setPosition(end.selectionEnd()); end.movePosition(QTextCursor::EndOfLine); - for (; begin < end; begin.movePosition(QTextCursor::StartOfLine), begin.movePosition(QTextCursor::Down)) { + for (; begin < end; begin.movePosition(QTextCursor::EndOfLine), begin.movePosition(QTextCursor::Right)) { begin.select(QTextCursor::LineUnderCursor); QString line = begin.selectedText(); @@ -346,12 +361,7 @@ void CSVWorld::ScriptEdit::resizeEvent(QResizeEvent *e) void CSVWorld::ScriptEdit::contextMenuEvent(QContextMenuEvent *event) { - QMenu *menu = createStandardContextMenu(); - menu->addAction("Comment Selection", this, SLOT(commentSelection())); - menu->addAction("Uncomment Selection", this, SLOT(uncommentSelection())); - - menu->exec(event->globalPos()); - delete menu; + mContextMenu->exec(event->globalPos()); } void CSVWorld::ScriptEdit::lineNumberAreaPaintEvent(QPaintEvent *event) diff --git a/apps/opencs/view/world/scriptedit.hpp b/apps/opencs/view/world/scriptedit.hpp index 27e2cd17e5..1ae93b62e7 100644 --- a/apps/opencs/view/world/scriptedit.hpp +++ b/apps/opencs/view/world/scriptedit.hpp @@ -54,6 +54,7 @@ namespace CSVWorld QFont mDefaultFont; QFont mMonoFont; int mTabCharCount; + QMenu *mContextMenu; protected: From 6063d8e31b27e52ca12f2cce6088992a386b78ab Mon Sep 17 00:00:00 2001 From: PlutonicOverkill Date: Thu, 27 Apr 2017 16:31:45 +1200 Subject: [PATCH 5/6] Fix existing context menu in script editor --- apps/opencs/view/world/scriptedit.cpp | 27 ++++++++++++++------------- apps/opencs/view/world/scriptedit.hpp | 4 +++- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/apps/opencs/view/world/scriptedit.cpp b/apps/opencs/view/world/scriptedit.cpp index 531c732ea6..8804c960ad 100644 --- a/apps/opencs/view/world/scriptedit.cpp +++ b/apps/opencs/view/world/scriptedit.cpp @@ -88,19 +88,15 @@ CSVWorld::ScriptEdit::ScriptEdit( <associateAction(mCommentAction); - QAction* comment = new QAction (tr ("Comment Selection"), this); - connect(comment, SIGNAL (triggered()), this, SLOT (commentSelection())); - CSMPrefs::Shortcut* commentShortcut = new CSMPrefs::Shortcut("script-editor-comment", this); - commentShortcut->associateAction(comment); - mContextMenu->addAction(comment); - - QAction* uncomment = new QAction (tr ("Uncomment Selection"), this); - connect(uncomment, SIGNAL (triggered()), this, SLOT (uncommentSelection())); - CSMPrefs::Shortcut* uncommentShortcut = new CSMPrefs::Shortcut("script-editor-uncomment", this); - uncommentShortcut->associateAction(uncomment); - mContextMenu->addAction(uncomment); + mUncommentAction = new QAction (tr ("Uncomment Selection"), this); + connect(mUncommentAction, SIGNAL (triggered()), this, SLOT (uncommentSelection())); + CSMPrefs::Shortcut *uncommentShortcut = new CSMPrefs::Shortcut("script-editor-uncomment", this); + uncommentShortcut->associateAction(mUncommentAction); mHighlighter = new ScriptHighlighter (document.getData(), mode, ScriptEdit::document()); @@ -361,7 +357,12 @@ void CSVWorld::ScriptEdit::resizeEvent(QResizeEvent *e) void CSVWorld::ScriptEdit::contextMenuEvent(QContextMenuEvent *event) { - mContextMenu->exec(event->globalPos()); + QMenu *menu = createStandardContextMenu(); + menu->addAction(mCommentAction); + menu->addAction(mUncommentAction); + + menu->exec(event->globalPos()); + delete menu; } void CSVWorld::ScriptEdit::lineNumberAreaPaintEvent(QPaintEvent *event) diff --git a/apps/opencs/view/world/scriptedit.hpp b/apps/opencs/view/world/scriptedit.hpp index 1ae93b62e7..a788eaccf6 100644 --- a/apps/opencs/view/world/scriptedit.hpp +++ b/apps/opencs/view/world/scriptedit.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include "../../model/world/universalid.hpp" @@ -54,7 +55,8 @@ namespace CSVWorld QFont mDefaultFont; QFont mMonoFont; int mTabCharCount; - QMenu *mContextMenu; + QAction *mCommentAction; + QAction *mUncommentAction; protected: From e685de0f8432358c5ec02afc42ae402005035489 Mon Sep 17 00:00:00 2001 From: PlutonicOverkill Date: Thu, 27 Apr 2017 19:48:01 +1200 Subject: [PATCH 6/6] Fix script editor undo/redo context menu actions --- apps/opencs/view/world/scriptedit.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/apps/opencs/view/world/scriptedit.cpp b/apps/opencs/view/world/scriptedit.cpp index 8804c960ad..bfd07035c9 100644 --- a/apps/opencs/view/world/scriptedit.cpp +++ b/apps/opencs/view/world/scriptedit.cpp @@ -306,10 +306,14 @@ void CSVWorld::ScriptEdit::commentSelection() end.setPosition(end.selectionEnd()); end.movePosition(QTextCursor::EndOfLine); + begin.beginEditBlock(); + for (; begin < end; begin.movePosition(QTextCursor::EndOfLine), begin.movePosition(QTextCursor::Right)) { begin.insertText(";"); } + + begin.endEditBlock(); } void CSVWorld::ScriptEdit::uncommentSelection() @@ -322,6 +326,8 @@ void CSVWorld::ScriptEdit::uncommentSelection() end.setPosition(end.selectionEnd()); end.movePosition(QTextCursor::EndOfLine); + begin.beginEditBlock(); + for (; begin < end; begin.movePosition(QTextCursor::EndOfLine), begin.movePosition(QTextCursor::Right)) { begin.select(QTextCursor::LineUnderCursor); QString line = begin.selectedText(); @@ -345,6 +351,8 @@ void CSVWorld::ScriptEdit::uncommentSelection() begin.insertText(line); } } + + begin.endEditBlock(); } void CSVWorld::ScriptEdit::resizeEvent(QResizeEvent *e) @@ -358,6 +366,16 @@ void CSVWorld::ScriptEdit::resizeEvent(QResizeEvent *e) void CSVWorld::ScriptEdit::contextMenuEvent(QContextMenuEvent *event) { QMenu *menu = createStandardContextMenu(); + + // remove redo/undo since they are disabled + QList menuActions = menu->actions(); + for (QList::iterator i = menuActions.begin(); i < menuActions.end(); ++i) + { + if ((*i)->text().contains("Undo") || (*i)->text().contains("Redo")) + { + (*i)->setVisible(false); + } + } menu->addAction(mCommentAction); menu->addAction(mUncommentAction);