Debian Codegraph @

src/Gui/ViewProviderTextDocument.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2017 Markus Hovorka <[email protected]> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23
24 #include <QMenu>
25 #include <QPlainTextEdit>
26
27
28 #include <App/Application.h>
29 #include <Base/Type.h>
30
31 #include "ViewProviderTextDocument.h"
32 #include "ActionFunction.h"
33 #include "Document.h"
34 #include "MainWindow.h"
35 #include "PythonEditor.h"
36 #include "TextDocumentEditorView.h"
37 #include "ViewProviderDocumentObject.h"
38
39
40 using namespace Gui;
41
42 PROPERTY_SOURCE(Gui::ViewProviderTextDocument, Gui::ViewProviderDocumentObject)
43 const char* ViewProviderTextDocument::SyntaxEnums[] = {"None", "Python", nullptr};
44
45 ViewProviderTextDocument::ViewProviderTextDocument()
46 {
47 sPixmap = "TextDocument";
48
49 ADD_PROPERTY_TYPE(
50 ReadOnly,
51 (false),
52 "Editor",
53 App::Prop_None,
54 "Defines whether the content can be edited."
55 );
56
57 QFont font;
58 font.setFamily(
59 QString::fromLatin1(
60 App::GetApplication()
61 .GetUserParameter()
62 .GetGroup("BaseApp")
63 ->GetGroup("Preferences")
64 ->GetGroup("Editor")
65 ->GetASCII("Font", font.family().toLatin1())
66 .c_str()
67 )
68 );
69 font.setPointSize(
70 App::GetApplication()
71 .GetUserParameter()
72 .GetGroup("BaseApp")
73 ->GetGroup("Preferences")
74 ->GetGroup("Editor")
75 ->GetInt("FontSize", font.pointSize())
76 );
77
78 ADD_PROPERTY_TYPE(FontSize, (font.pointSize()), "Editor", App::Prop_None, "Font size");
79 ADD_PROPERTY_TYPE(
80 FontName,
81 ((const char*)font.family().toLatin1()),
82 "Editor",
83 App::Prop_None,
84 "Font name"
85 );
86
87 ADD_PROPERTY_TYPE(
88 SyntaxHighlighter,
89 (static_cast<long>(0)),
90 "Editor",
91 App::Prop_None,
92 "Syntax highlighting"
93 );
94 SyntaxHighlighter.setEnums(SyntaxEnums);
95
96 DisplayMode.setStatus(App::Property::Hidden, true);
97 OnTopWhenSelected.setStatus(App::Property::Hidden, true);
98 SelectionStyle.setStatus(App::Property::Hidden, true);
99 Visibility.setStatus(App::Property::Hidden, true);
100
101 setToggleVisibility(ToggleVisibilityMode::NoToggleVisibility);
102 }
103
104 void ViewProviderTextDocument::setupContextMenu(QMenu* menu, QObject* receiver, const char* member)
105 {
106 auto func = new Gui::ActionFunction(menu);
107 QAction* act = menu->addAction(QObject::tr("Edit Text"));
108 func->trigger(act, [this]() { this->doubleClicked(); });
109
110 ViewProviderDocumentObject::setupContextMenu(menu, receiver, member);
111 }
112
113 bool ViewProviderTextDocument::doubleClicked()
114 {
115 if (!activateView()) {
116 editorWidget = new QPlainTextEdit {};
117 editorWidget->setReadOnly(ReadOnly.getValue());
118 FontName.touch();
119 SyntaxHighlighter.touch();
120
121 getMainWindow()->addWindow(
122 new TextDocumentEditorView {getObject<App::TextDocument>(), editorWidget, getMainWindow()}
123 );
124 }
125 return true;
126 }
127
128 void ViewProviderTextDocument::onChanged(const App::Property* prop)
129 {
130 if (editorWidget) {
131 if (prop == &ReadOnly) {
132 editorWidget->setReadOnly(ReadOnly.getValue());
133 }
134 else if (prop == &FontSize || prop == &FontName) {
135 QFont font(QString::fromLatin1(this->FontName.getValue()), (int)this->FontSize.getValue());
136 editorWidget->setFont(font);
137 }
138 else if (prop == &SyntaxHighlighter) {
139 long value = SyntaxHighlighter.getValue();
140 if (value == 1) {
141 auto pythonSyntax = new PythonSyntaxHighlighter(editorWidget);
142 pythonSyntax->setDocument(editorWidget->document());
143 }
144 else {
145 auto shl = editorWidget->findChild<QSyntaxHighlighter*>();
146 if (shl) {
147 shl->deleteLater();
148 }
149 }
150 }
151 }
152 ViewProviderDocumentObject::onChanged(prop);
153 }
154
155 MDIView* ViewProviderTextDocument::getMDIView() const
156 {
157 auto views = getDocument()->getMDIViewsOfType(TextDocumentEditorView::getClassTypeId());
158 for (auto v : views) {
159 auto textView = static_cast<TextDocumentEditorView*>(v);
160 if (textView->getTextObject() == getObject()) {
161 return textView;
162 }
163 }
164 return nullptr;
165 }
166
167 bool ViewProviderTextDocument::activateView() const
168 {
169 auto views = getDocument()->getMDIViewsOfType(TextDocumentEditorView::getClassTypeId());
170 for (auto v : views) {
171 auto textView = static_cast<TextDocumentEditorView*>(v);
172 if (textView->getTextObject() == getObject()) {
173 getMainWindow()->setActiveWindow(textView);
174 return true;
175 }
176 }
177 return false;
178 }