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

@@ -40,15 +40,14 @@ TextDocument::TextDocument()
ADD_PROPERTY_TYPE(
Text, (""), 0, App::Prop_Hidden,
"Content of the document.");
ADD_PROPERTY_TYPE(
ReadOnly, (false), 0, App::Prop_None,
"Defines whether the content can be edited.");
}
void TextDocument::onChanged(const Property* prop)
{
if (prop == &Text)
textChanged();
else if (prop == &Label)
labelChanged();
DocumentObject::onChanged(prop);
}
@@ -57,7 +56,12 @@ const char* TextDocument::getViewProviderName() const
return "Gui::ViewProviderTextDocument";
}
boost::signals2::connection TextDocument::connect(const TextSlot &sub)
boost::signals2::connection TextDocument::connectText(const TextSlot &sub)
{
return textChanged.connect(sub);
}
boost::signals2::connection TextDocument::connectLabel(const TextSlot &sub)
{
return labelChanged.connect(sub);
}

View File

@@ -42,7 +42,6 @@ public:
using TextSlot = TextSignal::slot_type;
PropertyString Text;
PropertyBool ReadOnly;
TextDocument();
~TextDocument() {}
@@ -50,9 +49,12 @@ public:
void onChanged(const Property* prop);
const char* getViewProviderName() const;
boost::signals2::connection connect(const TextSlot &sub);
boost::signals2::connection connectText(const TextSlot &sub);
boost::signals2::connection connectLabel(const TextSlot &sub);
private:
TextSignal textChanged;
TextSignal labelChanged;
};
}

View File

@@ -73,7 +73,6 @@ void TextDocumentEditorView::setupEditor()
{
connect(getEditor()->document(), SIGNAL(modificationChanged(bool)),
this, SLOT(setWindowModified(bool)));
getEditor()->setReadOnly(textDocument->ReadOnly.getValue());
setWindowTitle(QString::fromUtf8(textDocument->Label.getValue())
+ QString::fromLatin1("[*]"));
getEditor()->setPlainText(
@@ -82,8 +81,10 @@ void TextDocumentEditorView::setupEditor()
void TextDocumentEditorView::setupConnection()
{
textConnection = textDocument->connect(
textConnection = textDocument->connectText(
boost::bind(&TextDocumentEditorView::sourceChanged, this));
labelConnection = textDocument->connectLabel(
boost::bind(&TextDocumentEditorView::labelChanged, this));
}
void TextDocumentEditorView::sourceChanged()
@@ -96,6 +97,12 @@ void TextDocumentEditorView::sourceChanged()
}
}
void TextDocumentEditorView::labelChanged()
{
setWindowTitle(QString::fromUtf8(textDocument->Label.getValue())
+ QString::fromLatin1("[*]"));
}
void TextDocumentEditorView::refresh()
{
QString text = QString::fromUtf8(

View File

@@ -55,16 +55,21 @@ public:
QPlainTextEdit* getEditor() const { return editor; }
App::TextDocument* getTextObject() const { return textDocument; }
private:
void setupEditor();
void setupConnection();
void saveToObject();
void sourceChanged();
void labelChanged();
void refresh();
bool isEditorModified() const;
private:
QPlainTextEdit *const editor;
App::TextDocument *const textDocument;
boost::signals2::connection textConnection;
boost::signals2::connection labelConnection;
bool sourceModified = false;
};

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(

View File

@@ -25,9 +25,10 @@
#define GUI_ViewProviderTextDocument_H
#include "PreCompiled.h"
#include "ViewProviderDocumentObject.h"
#include <QPointer>
class QPlainTextEdit;
namespace Gui {
@@ -37,11 +38,23 @@ public:
ViewProviderTextDocument();
~ViewProviderTextDocument() {}
App::PropertyBool ReadOnly;
App::PropertyFloat FontSize;
App::PropertyFont FontName;
App::PropertyEnumeration SyntaxHighlighter;
bool doubleClicked();
void setupContextMenu(QMenu* menu, QObject* receiver, const char* member);
bool isShow() const { return true; }
void onChanged(const App::Property* prop);
private:
bool activateView() const;
private:
QPointer<QPlainTextEdit> editorWidget;
static const char* SyntaxEnums[];
};
}