From cbb2b8b119e214123432760c024b234a5d638604 Mon Sep 17 00:00:00 2001 From: PlutonicOverkill Date: Fri, 28 Apr 2017 19:57:49 +1200 Subject: [PATCH] Add setting so feature can be turned off --- apps/opencs/model/prefs/state.cpp | 1 + apps/opencs/view/world/scriptedit.cpp | 25 ++++++++++++++------ apps/opencs/view/world/scriptedit.hpp | 1 + apps/opencs/view/world/scripthighlighter.cpp | 7 +++++- apps/opencs/view/world/scripthighlighter.hpp | 3 +++ 5 files changed, 29 insertions(+), 8 deletions(-) diff --git a/apps/opencs/model/prefs/state.cpp b/apps/opencs/model/prefs/state.cpp index 6bef55c44..0d66caba6 100644 --- a/apps/opencs/model/prefs/state.cpp +++ b/apps/opencs/model/prefs/state.cpp @@ -152,6 +152,7 @@ void CSMPrefs::State::declare() setRange (0, 10000); declareInt ("error-height", "Initial height of the error panel", 100). setRange (100, 10000); + declareBool ("highlight-occurrences", "Highlight other occurrences of selected names", true); declareSeparator(); declareColour ("colour-int", "Highlight Colour: Integer Literals", QColor ("darkmagenta")); declareColour ("colour-float", "Highlight Colour: Float Literals", QColor ("magenta")); diff --git a/apps/opencs/view/world/scriptedit.cpp b/apps/opencs/view/world/scriptedit.cpp index 1581c3e34..22beb0889 100644 --- a/apps/opencs/view/world/scriptedit.cpp +++ b/apps/opencs/view/world/scriptedit.cpp @@ -49,6 +49,7 @@ CSVWorld::ScriptEdit::ScriptEdit( mDefaultFont(font()), mMonoFont(QFont("Monospace")), mTabCharCount(4), + mMarkOccurrences(true), mMarkOccurrencesRunning(false), mDocument(document), mWhiteListQoutes("^[a-z|_]{1}[a-z|0-9|_]{0,}$", Qt::CaseInsensitive) @@ -233,6 +234,13 @@ void CSVWorld::ScriptEdit::settingChanged(const CSMPrefs::Setting *setting) mTabCharCount = setting->toInt(); setTabWidth(); } + else if (*setting == "Scripts/highlight-occurrences") + { + mMarkOccurrences = setting->isTrue(); + mHighlighter->setMarkedWord(""); + updateHighlighting(); + mHighlighter->setMarkOccurrences(mMarkOccurrences); + } } void CSVWorld::ScriptEdit::idListChanged() @@ -295,16 +303,19 @@ void CSVWorld::ScriptEdit::markOccurrences() if (mMarkOccurrencesRunning) return; - mMarkOccurrencesRunning = true; + if (mMarkOccurrences) + { + mMarkOccurrencesRunning = true; - QTextCursor cursor = textCursor(); - cursor.select(QTextCursor::WordUnderCursor); - QString word = cursor.selectedText(); + QTextCursor cursor = textCursor(); + cursor.select(QTextCursor::WordUnderCursor); + QString word = cursor.selectedText(); - mHighlighter->setMarkedWord(word.toStdString()); - mHighlighter->rehighlight(); + mHighlighter->setMarkedWord(word.toStdString()); + mHighlighter->rehighlight(); - mMarkOccurrencesRunning = false; + mMarkOccurrencesRunning = false; + } } void CSVWorld::ScriptEdit::resizeEvent(QResizeEvent *e) diff --git a/apps/opencs/view/world/scriptedit.hpp b/apps/opencs/view/world/scriptedit.hpp index 5c78470b3..eb57aed1d 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; + bool mMarkOccurrences; bool mMarkOccurrencesRunning; protected: diff --git a/apps/opencs/view/world/scripthighlighter.cpp b/apps/opencs/view/world/scripthighlighter.cpp index 3041eff2d..7e6ea5844 100644 --- a/apps/opencs/view/world/scripthighlighter.cpp +++ b/apps/opencs/view/world/scripthighlighter.cpp @@ -73,7 +73,7 @@ void CSVWorld::ScriptHighlighter::highlight (const Compiler::TokenLoc& loc, Type index -= length; QTextCharFormat scheme = mScheme[type]; - if (loc.mLiteral == mMarkedWord) + if (mMarkOccurrences && loc.mLiteral == mMarkedWord) scheme.merge(mScheme[Type_Highlight]); setFormat (index, length, scheme); @@ -109,6 +109,11 @@ void CSVWorld::ScriptHighlighter::highlightBlock (const QString& text) catch (...) {} // ignore syntax errors } +void CSVWorld::ScriptHighlighter::setMarkOccurrences(bool flag) +{ + mMarkOccurrences = flag; +} + void CSVWorld::ScriptHighlighter::setMarkedWord(const std::string& name) { mMarkedWord = name; diff --git a/apps/opencs/view/world/scripthighlighter.hpp b/apps/opencs/view/world/scripthighlighter.hpp index d55cf4839..a7d0fc2a1 100644 --- a/apps/opencs/view/world/scripthighlighter.hpp +++ b/apps/opencs/view/world/scripthighlighter.hpp @@ -49,6 +49,7 @@ namespace CSVWorld CSMWorld::ScriptContext mContext; std::map mScheme; Mode mMode; + bool mMarkOccurrences; std::string mMarkedWord; private: @@ -94,6 +95,8 @@ namespace CSVWorld virtual void highlightBlock (const QString& text); + void setMarkOccurrences(bool); + void setMarkedWord(const std::string& name); void invalidateIds();