Debian Codegraph @

src/Gui/ModuleIO.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2024 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 <QMessageBox>
27
28
29 #include "ModuleIO.h"
30 #include "Application.h"
31 #include "FileDialog.h"
32 #include "MainWindow.h"
33
34
35 using namespace Gui;
36
37 /* TRANSLATOR Gui::ModuleIO */
38
39 bool ModuleIO::verifyFile(const QString& filename)
40 {
41 QFileInfo fi(filename);
42 if (!fi.exists() || !fi.isFile()) {
43 QMessageBox::critical(
44 Gui::getMainWindow(),
45 tr("File not found"),
46 tr("The file '%1' cannot be opened.").arg(filename)
47 );
48 return false;
49 }
50
51 return true;
52 }
53
54 void ModuleIO::openFile(const QString& filename)
55 {
56 // invokes appendFile()
57 Gui::SelectModule::Dict dict = Gui::SelectModule::importHandler(filename);
58 for (Gui::SelectModule::Dict::iterator it = dict.begin(); it != dict.end(); ++it) {
59 Gui::Application::Instance->open(it.key().toUtf8(), it.value().toLatin1());
60 break;
61 }
62 }
63
64 void ModuleIO::verifyAndOpenFile(const QString& filename)
65 {
66 if (verifyFile(filename)) {
67 openFile(filename);
68 }
69 }
70
71 void ModuleIO::importFiles(const QStringList& filenames, const char* document)
72 {
73 SelectModule::Dict dict = SelectModule::importHandler(filenames);
74 // load the files with the associated modules
75 for (SelectModule::Dict::iterator it = dict.begin(); it != dict.end(); ++it) {
76 // if the passed document name doesn't exist the module should create it, if needed
77 Application::Instance->importFrom(it.key().toUtf8(), document, it.value().toLatin1());
78 }
79 }