Debian Codegraph @

src/Gui/FileHandler.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2023 Werner Mayer <wmayer[at]users.sourceforge.net> *
5 * *
6 * This file is part of FreeCAD. *
7 * *
8 * FreeCAD is free software: you can redistribute it and/or modify it *
9 * under the terms of the GNU Lesser General Public License as *
10 * published by the Free Software Foundation, either version 2.1 of the *
11 * License, or (at your option) any later version. *
12 * *
13 * FreeCAD is distributed in the hope that it will be useful, but *
14 * WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16 * Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with FreeCAD. If not, see *
20 * <https://www.gnu.org/licenses/>. *
21 * *
22 **************************************************************************/
23
24
25 #include <QFileInfo>
26 #include <QImageReader>
27 #include <QStringList>
28 #include <Inventor/SoInput.h>
29
30
31 #include "FileHandler.h"
32 #include "Application.h"
33 #include "BitmapFactory.h"
34 #include "CommandT.h"
35 #include "EditorView.h"
36 #include "PythonEditor.h"
37 #include "MainWindow.h"
38 #include <App/Application.h>
39 #include <Base/Tools.h>
40
41 using namespace Gui;
42
43 FileHandler::FileHandler(const QString& filename)
44 : filename(filename)
45 {}
46
47 bool FileHandler::openFile()
48 {
49 docname.clear();
50 return openInternal();
51 }
52
53 bool FileHandler::importFile(const std::string& document)
54 {
55 docname = document;
56 return openInternal();
57 }
58
59 QString FileHandler::extension() const
60 {
61 QFileInfo fi;
62 fi.setFile(filename);
63 return fi.suffix().toLower();
64 }
65
66 App::Document* FileHandler::getOrCreateDocument()
67 {
68 App::Document* doc = App::GetApplication().getActiveDocument();
69 if (!doc) {
70 doc = App::GetApplication().newDocument();
71 }
72
73 return doc;
74 }
75
76 App::Document* FileHandler::getOrCreateDocument(const std::string& document)
77 {
78 App::Document* doc = nullptr;
79 if (!document.empty()) {
80 doc = App::GetApplication().getDocument(document.c_str());
81 }
82 else {
83 doc = App::GetApplication().getActiveDocument();
84 }
85
86 if (!doc) {
87 doc = App::GetApplication().newDocument(document.c_str());
88 }
89
90 return doc;
91 }
92
93 App::Document* FileHandler::createDocumentIfNeeded()
94 {
95 if (docname.empty()) {
96 return getOrCreateDocument();
97 }
98
99 return getOrCreateDocument(docname);
100 }
101
102 bool FileHandler::activateEditor()
103 {
104 QList<EditorView*> views = getMainWindow()->findChildren<EditorView*>();
105 for (const auto& it : views) {
106 if (it->fileName() == filename) {
107 it->setFocus();
108 return true;
109 }
110 }
111
112 return false;
113 }
114
115 bool FileHandler::openInternal()
116 {
117 if (activateEditor()) {
118 return true;
119 }
120
121 QFileInfo fi;
122 fi.setFile(filename);
123 QString ext = fi.suffix().toLower();
124
125 auto hasExtension = [ext](const QStringList& suffixes) {
126 return suffixes.contains(ext);
127 };
128
129 if (hasExtension(QStringList() << QLatin1String("iv"))) {
130 openInventor();
131 return true;
132 }
133
134 if (hasExtension(
135 QStringList() << QLatin1String("wrl") << QLatin1String("wrz") << QLatin1String("vrml")
136 )) {
137 openVRML();
138 return true;
139 }
140
141 if (hasExtension(
142 QStringList() << QLatin1String("py") << QLatin1String("fcmacro")
143 << QLatin1String("fcscript")
144 )) {
145 openPython();
146 return true;
147 }
148
149 QStringList supportedFormats;
150 auto imageFormats = QImageReader::supportedImageFormats();
151 std::transform(
152 imageFormats.cbegin(),
153 imageFormats.cend(),
154 std::back_inserter(supportedFormats),
155 [](const QByteArray& format) { return QString::fromLatin1(format); }
156 );
157
158 if (hasExtension(supportedFormats)) {
159 openImage();
160 return true;
161 }
162
163 return false;
164 }
165
166 void FileHandler::openInternal(const char* type, const char* prop)
167 {
168 App::Document* doc = createDocumentIfNeeded();
169
170 QFileInfo fi;
171 fi.setFile(filename);
172
173 QString encBase = Base::Tools::escapeEncodeString(fi.baseName());
174 QString encPath = Base::Tools::escapeEncodeString(fi.absoluteFilePath());
175
176 Gui::cmdAppDocumentArgs(doc, "addObject('%s', '%s')", type, encBase.toStdString());
177 Gui::cmdAppDocumentArgs(doc, "ActiveObject.%s = '%s'", prop, encPath.toStdString());
178 Gui::cmdAppDocumentArgs(doc, "ActiveObject.Label = '%s'", encBase.toStdString());
179 Gui::cmdAppDocument(doc, "recompute()");
180 }
181
182 void FileHandler::openInventor()
183 {
184 openInternal("App::InventorObject", "FileName");
185 }
186
187 void FileHandler::openVRML()
188 {
189 QFileInfo fi;
190 fi.setFile(filename);
191
192 // Add this to the search path in order to read inline files (#0002029)
193 QByteArray path = fi.absolutePath().toUtf8();
194 SoInput::addDirectoryFirst(path.constData());
195
196 openInternal("App::VRMLObject", "VrmlFile");
197
198 SoInput::removeDirectory(path.constData());
199 }
200
201 void FileHandler::openImage()
202 {
203 openInternal("Image::ImagePlane", "ImageFile");
204 }
205
206 void FileHandler::openPython()
207 {
208 auto editor = new PythonEditor();
209 editor->setWindowIcon(Gui::BitmapFactory().iconFromTheme("applications-python"));
210 auto edit = new PythonEditorView(editor, getMainWindow());
211 edit->open(filename);
212 edit->resize(400, 300);
213 getMainWindow()->addWindow(edit);
214 }