Debian Codegraph @

src/Gui/Dialogs/DlgProjectUtility.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2011 Werner Mayer <wmayer[at]users.sourceforge.net> *
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 #include <sstream>
24 #include <QDir>
25 #include <QMessageBox>
26
27 #include <App/Document.h>
28
29 #include "Dialogs/DlgProjectUtility.h"
30 #include "ui_DlgProjectUtility.h"
31 #include "Application.h"
32 #include "Command.h"
33
34
35 using namespace Gui::Dialog;
36
37
38 /* TRANSLATOR Gui::Dialog::DlgProjectUtility */
39
40 DlgProjectUtility::DlgProjectUtility(QWidget* parent, Qt::WindowFlags fl)
41 : QDialog(parent, fl)
42 , ui(new Ui_DlgProjectUtility)
43 {
44 ui->setupUi(this);
45 connect(ui->extractButton, &QPushButton::clicked, this, &DlgProjectUtility::extractButton);
46 connect(ui->createButton, &QPushButton::clicked, this, &DlgProjectUtility::createButton);
47 ui->extractSource->setFilter(QStringLiteral("%1 (*.FCStd)").arg(tr("Project file")));
48 }
49
50 /**
51 * Destroys the object and frees any allocated resources
52 */
53 DlgProjectUtility::~DlgProjectUtility() = default;
54
55 void DlgProjectUtility::extractButton()
56 {
57 QString source = ui->extractSource->fileName();
58 QString dest = ui->extractDest->fileName();
59 if (source.isEmpty()) {
60 QMessageBox::critical(this, tr("Empty source"), tr("No source is defined."));
61 return;
62 }
63
64 if (dest.isEmpty()) {
65 QMessageBox::critical(this, tr("Empty destination"), tr("No destination is defined."));
66 return;
67 }
68
69 tryExtractArchive(source, dest);
70 }
71
72 void DlgProjectUtility::createButton()
73 {
74 QString source = ui->createSource->fileName();
75 QString dest = ui->createDest->fileName();
76 if (source.isEmpty()) {
77 QMessageBox::critical(this, tr("Empty source"), tr("No source is defined."));
78 return;
79 }
80 if (dest.isEmpty()) {
81 QMessageBox::critical(this, tr("Empty destination"), tr("No destination is defined."));
82 return;
83 }
84
85 dest = QDir(dest).absoluteFilePath(QStringLiteral("project.fcstd"));
86
87 bool openFile = ui->checkLoadProject->isChecked();
88 tryCreateArchive(source, dest, openFile);
89 }
90
91 void DlgProjectUtility::tryExtractArchive(const QString& source, const QString& target)
92 {
93 try {
94 std::stringstream str;
95 str << "from freecad import project_utility\n";
96 str << "project_utility.extractDocument(\"" << (const char*)source.toUtf8() << "\", \""
97 << (const char*)target.toUtf8() << "\")";
98 Gui::Command::runCommand(Gui::Command::App, str.str().c_str());
99 }
100 catch (const Base::Exception& e) {
101 QMessageBox::critical(this, tr("Failed to extract document"), QString::fromLatin1(e.what()));
102 }
103 }
104
105 void DlgProjectUtility::tryCreateArchive(const QString& source, const QString& target, bool openFile)
106 {
107 try {
108 std::stringstream str;
109 str << "from freecad import project_utility\n";
110 str << "project_utility.createDocument(\"" << (const char*)source.toUtf8() << "\", \""
111 << (const char*)target.toUtf8() << "\")";
112 Gui::Command::runCommand(Gui::Command::App, str.str().c_str());
113 if (openFile) {
114 Application::Instance->open((const char*)target.toUtf8(), "FreeCAD");
115 }
116 }
117 catch (const Base::Exception& e) {
118 QMessageBox::critical(this, tr("Failed to create document"), QString::fromLatin1(e.what()));
119 }
120 }
121
122 #include "moc_DlgProjectUtility.cpp"