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 0d66caba6..3921e32f2 100644 --- a/apps/opencs/model/prefs/state.cpp +++ b/apps/opencs/model/prefs/state.cpp @@ -328,6 +328,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 22beb0889..8cb4494bb 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) { @@ -90,6 +92,16 @@ CSVWorld::ScriptEdit::ScriptEdit( connect(this, SIGNAL(selectionChanged()), this, SLOT(markOccurrences())); + mCommentAction = new QAction (tr ("Comment Selection"), this); + connect(mCommentAction, SIGNAL (triggered()), this, SLOT (commentSelection())); + CSMPrefs::Shortcut *commentShortcut = new CSMPrefs::Shortcut("script-editor-comment", this); + commentShortcut->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())); @@ -317,6 +329,65 @@ void CSVWorld::ScriptEdit::markOccurrences() mMarkOccurrencesRunning = false; } } + +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) { @@ -326,6 +397,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 eb57aed1d..b1e70a4c8 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" @@ -56,6 +57,8 @@ namespace CSVWorld int mTabCharCount; bool mMarkOccurrences; bool mMarkOccurrencesRunning; + QAction *mCommentAction; + QAction *mUncommentAction; protected: @@ -79,6 +82,8 @@ namespace CSVWorld virtual void resizeEvent(QResizeEvent *e); + virtual void contextMenuEvent(QContextMenuEvent *event); + private: QVector mAllowedTypes; @@ -115,6 +120,11 @@ namespace CSVWorld void updateLineNumberArea(const QRect &, int); void markOccurrences(); + + void commentSelection(); + + void uncommentSelection(); + }; class LineNumberArea : public QWidget diff --git a/apps/opencs/view/world/scripterrortable.cpp b/apps/opencs/view/world/scripterrortable.cpp index c22bcf199..ca7cbd159 100644 --- a/apps/opencs/view/world/scripterrortable.cpp +++ b/apps/opencs/view/world/scripterrortable.cpp @@ -52,6 +52,13 @@ void CSVWorld::ScriptErrorTable::addMessage (const std::string& message, columnItem->setFlags (columnItem->flags() ^ Qt::ItemIsEditable); setItem (row, 3, columnItem); } + else + { + QTableWidgetItem *lineItem = new QTableWidgetItem; + lineItem->setData (Qt::DisplayRole, "-"); + lineItem->setFlags (lineItem->flags() ^ Qt::ItemIsEditable); + setItem (row, 1, lineItem); + } QTableWidgetItem *messageItem = new QTableWidgetItem (QString::fromUtf8 (message.c_str())); messageItem->setFlags (messageItem->flags() ^ Qt::ItemIsEditable); @@ -141,7 +148,7 @@ void CSVWorld::ScriptErrorTable::settingChanged (const CSMPrefs::Setting *settin void CSVWorld::ScriptErrorTable::cellClicked (int row, int column) { - if (item (row, 1)) + if (item (row, 3)) { int scriptLine = item (row, 1)->data (Qt::DisplayRole).toInt(); int scriptColumn = item (row, 3)->data (Qt::DisplayRole).toInt();