diff --git a/AUTHORS.md b/AUTHORS.md index a37975d24..158e16ee6 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -117,6 +117,7 @@ Programmers Pi03k Pieter van der Kloet (pvdk) pkubik + PlutonicOverkill Radu-Marius Popovici (rpopovici) Rafael Moura (dhustkoder) rdimesio diff --git a/apps/opencs/model/prefs/state.cpp b/apps/opencs/model/prefs/state.cpp index b8d6102ac..3de90e468 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 6f27d5656..bfd07035c 100644 --- a/apps/opencs/view/world/scriptedit.cpp +++ b/apps/opencs/view/world/scriptedit.cpp @@ -7,12 +7,14 @@ #include #include #include +#include #include "../../model/doc/document.hpp" #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) { @@ -86,6 +88,16 @@ CSVWorld::ScriptEdit::ScriptEdit( <associateAction(mCommentAction); + + 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()); connect (&document.getData(), SIGNAL (idListChanged()), this, SLOT (idListChanged())); @@ -284,6 +296,65 @@ void CSVWorld::ScriptEdit::updateLineNumberArea(const QRect &rect, int dy) updateLineNumberAreaWidth(0); } +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::EndOfLine); + + begin.beginEditBlock(); + + for (; begin < end; begin.movePosition(QTextCursor::EndOfLine), begin.movePosition(QTextCursor::Right)) + { + begin.insertText(";"); + } + + begin.endEditBlock(); +} + +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::EndOfLine); + + begin.beginEditBlock(); + + for (; begin < end; begin.movePosition(QTextCursor::EndOfLine), begin.movePosition(QTextCursor::Right)) { + 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) + { + if (!line[index].isSpace()) + break; + } + + if (index != line.size() && line[index] == ';') + { + // remove the semicolon + line.remove(index, 1); + // put the line back + begin.insertText(line); + } + } + + begin.endEditBlock(); +} + void CSVWorld::ScriptEdit::resizeEvent(QResizeEvent *e) { QPlainTextEdit::resizeEvent(e); @@ -292,6 +363,26 @@ 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(); + + // 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); + + 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 4977ed8e0..a788eaccf 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,6 +55,8 @@ namespace CSVWorld QFont mDefaultFont; QFont mMonoFont; int mTabCharCount; + QAction *mCommentAction; + QAction *mUncommentAction; protected: @@ -77,6 +80,8 @@ namespace CSVWorld virtual void resizeEvent(QResizeEvent *e); + virtual void contextMenuEvent(QContextMenuEvent *event); + private: QVector mAllowedTypes; @@ -111,6 +116,10 @@ namespace CSVWorld void updateLineNumberAreaWidth(int newBlockCount); void updateLineNumberArea(const QRect &, int); + + void commentSelection(); + + void uncommentSelection(); }; class LineNumberArea : public QWidget