Skip to content

Commit

Permalink
Gui: allow to set font family and size and syntax highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Dec 19, 2019
1 parent 99e5bed commit d969581
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 10 deletions.
12 changes: 8 additions & 4 deletions src/App/TextDocument.cpp
Expand Up @@ -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);
}

Expand All @@ -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);
}
6 changes: 4 additions & 2 deletions src/App/TextDocument.h
Expand Up @@ -42,17 +42,19 @@ class AppExport TextDocument : public App::DocumentObject {
using TextSlot = TextSignal::slot_type;

PropertyString Text;
PropertyBool ReadOnly;

TextDocument();
~TextDocument() {}

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;
};

}
Expand Down
11 changes: 9 additions & 2 deletions src/Gui/TextDocumentEditorView.cpp
Expand Up @@ -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(
Expand All @@ -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()
Expand All @@ -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(
Expand Down
5 changes: 5 additions & 0 deletions src/Gui/TextDocumentEditorView.h
Expand Up @@ -55,16 +55,21 @@ class GuiExport TextDocumentEditorView : public MDIView {

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;
};

Expand Down
54 changes: 53 additions & 1 deletion src/Gui/ViewProviderTextDocument.cpp
Expand Up @@ -35,17 +35,40 @@
#include <Gui/MainWindow.h>
#include <Gui/Document.h>
#include <Gui/ActionFunction.h>
#include <Gui/PythonEditor.h>

#include "ViewProviderTextDocument.h"


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)
Expand All @@ -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()),
Expand All @@ -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(
Expand Down
15 changes: 14 additions & 1 deletion src/Gui/ViewProviderTextDocument.h
Expand Up @@ -25,9 +25,10 @@
#define GUI_ViewProviderTextDocument_H


#include "PreCompiled.h"
#include "ViewProviderDocumentObject.h"
#include <QPointer>

class QPlainTextEdit;

namespace Gui {

Expand All @@ -37,11 +38,23 @@ class GuiExport ViewProviderTextDocument : public ViewProviderDocumentObject {
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[];
};

}
Expand Down

0 comments on commit d969581

Please sign in to comment.