Gui: allow to set font family and size and syntax highlighting

This commit is contained in:
wmayer
2019-12-19 23:17:46 +01:00
parent 99e5bededb
commit d969581ff8
6 changed files with 93 additions and 10 deletions

View File

@@ -35,6 +35,7 @@
#include <Gui/MainWindow.h>
#include <Gui/Document.h>
#include <Gui/ActionFunction.h>
#include <Gui/PythonEditor.h>
#include "ViewProviderTextDocument.h"
@@ -42,10 +43,32 @@
using namespace Gui;
PROPERTY_SOURCE(Gui::ViewProviderTextDocument, Gui::ViewProviderDocumentObject)
const char* ViewProviderTextDocument::SyntaxEnums[]= {"None","Python",nullptr};
ViewProviderTextDocument::ViewProviderTextDocument()
{
sPixmap = "TextDocument";
ADD_PROPERTY_TYPE(
ReadOnly, (false), "Editor", App::Prop_None,
"Defines whether the content can be edited.");
QFont font;
font.setFamily(QString::fromLatin1(App::GetApplication().GetUserParameter().
GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Editor")->GetASCII("Font", font.family().toLatin1()).c_str()));
font.setPointSize(App::GetApplication().GetUserParameter().
GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Editor")->GetInt("FontSize", font.pointSize()));
ADD_PROPERTY_TYPE(FontSize,(font.pointSize()), "Editor", App::Prop_None, "Font size");
ADD_PROPERTY_TYPE(FontName,((const char*)font.family().toLatin1()), "Editor", App::Prop_None, "Font name");
ADD_PROPERTY_TYPE(SyntaxHighlighter,(static_cast<long>(0)), "Editor", App::Prop_None, "Syntax highlighting");
SyntaxHighlighter.setEnums(SyntaxEnums);
DisplayMode.setStatus(App::Property::Hidden, true);
OnTopWhenSelected.setStatus(App::Property::Hidden, true);
SelectionStyle.setStatus(App::Property::Hidden, true);
Visibility.setStatus(App::Property::Hidden, true);
}
void ViewProviderTextDocument::setupContextMenu(QMenu* menu, QObject* receiver, const char* member)
@@ -60,7 +83,10 @@ void ViewProviderTextDocument::setupContextMenu(QMenu* menu, QObject* receiver,
bool ViewProviderTextDocument::doubleClicked()
{
if (!activateView()) {
auto* editorWidget = new QPlainTextEdit {};
editorWidget = new QPlainTextEdit {};
editorWidget->setReadOnly(ReadOnly.getValue());
SyntaxHighlighter.touch();
getMainWindow()->addWindow(
new TextDocumentEditorView {
static_cast<App::TextDocument*>(getObject()),
@@ -69,6 +95,32 @@ bool ViewProviderTextDocument::doubleClicked()
return true;
}
void ViewProviderTextDocument::onChanged(const App::Property* prop)
{
if (editorWidget) {
if (prop == &ReadOnly) {
editorWidget->setReadOnly(ReadOnly.getValue());
}
else if (prop == &FontSize || prop == &FontName) {
QFont font(QString::fromLatin1(this->FontName.getValue()), (int)this->FontSize.getValue());
editorWidget->setFont(font);
}
else if (prop == &SyntaxHighlighter) {
long value = SyntaxHighlighter.getValue();
if (value == 1) {
PythonSyntaxHighlighter* pythonSyntax = new PythonSyntaxHighlighter(editorWidget);
pythonSyntax->setDocument(editorWidget->document());
}
else {
QSyntaxHighlighter* shl = editorWidget->findChild<QSyntaxHighlighter*>();
if (shl)
shl->deleteLater();
}
}
}
ViewProviderDocumentObject::onChanged(prop);
}
bool ViewProviderTextDocument::activateView() const
{
auto views = getDocument()->getMDIViewsOfType(