1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
#include "uiscriptingedit.h"
#include "common/unused.h"
#include "services/pluginmanager.h"
#include "syntaxhighlighterplugin.h"
#include "pluginservicebase.h"
#include <QPlainTextEdit>
#include <QTextEdit>
#include <QCoreApplication>
#include <QSyntaxHighlighter>
UiScriptingEdit::UiScriptingEdit()
{
}
const char* UiScriptingEdit::getPropertyName() const
{
return "scriptingEdit";
}
void UiScriptingEdit::handle(QWidget* widget, const QVariant& value)
{
if (!value.toBool())
return;
new EditUpdater(widget); // widget becomes its parent and owns it
}
UiScriptingEdit::EditUpdater::EditUpdater(QWidget* widget) :
QObject(widget), watchedWidget(widget)
{
widget->installEventFilter(this);
}
bool UiScriptingEdit::EditUpdater::eventFilter(QObject* obj, QEvent* e)
{
UNUSED(obj);
if (changingHighlighter)
return false;
if (e->type() != QEvent::DynamicPropertyChange)
return false;
if (dynamic_cast<QDynamicPropertyChangeEvent*>(e)->propertyName() != PluginServiceBase::LANG_PROPERTY_NAME)
return false;
QVariant prop = watchedWidget->property(PluginServiceBase::LANG_PROPERTY_NAME);
installNewHighlighter(prop);
return false;
}
void UiScriptingEdit::EditUpdater::installNewHighlighter(const QVariant& prop)
{
QString lang = prop.toString();
if (lang == currentLang)
return;
// When highlighter is deleted, it causes textChanged() signal and so this method is called recurrently.
// To avoid inifinite recursion, the changingHighlighter is used to ignore property changes during deletion
// of the highlighter.
changingHighlighter = true;
safe_delete(currentHighlighter);
currentLang = QString();
changingHighlighter = false;
for (SyntaxHighlighterPlugin* plugin : PLUGINS->getLoadedPlugins<SyntaxHighlighterPlugin>())
{
if (plugin->getLanguageName() != lang)
continue;
currentHighlighter = plugin->createSyntaxHighlighter(watchedWidget);
currentLang = lang;
break;
}
}
|