From 6e81451b46453e11b3c47cac49fb162b0dae4811 Mon Sep 17 00:00:00 2001 From: joshua Date: Sat, 23 Jul 2022 21:54:23 +0200 Subject: [PATCH] Add import dxf feature Use dxf2elmt to import dxf. See https://github.com/antonioaja/dxf2elmt --- qelectrotech.pro | 6 +- sources/dxf/dxftoelmt.cpp | 102 ++++++++++++++++++ sources/dxf/dxftoelmt.h | 30 ++++++ sources/editor/ui/qetelementeditor.cpp | 29 ++++++ sources/editor/ui/qetelementeditor.h | 1 + sources/editor/ui/qetelementeditor.ui | 10 ++ sources/ui/thirdpartybinaryinstalldialog.cpp | 51 +++++++++ sources/ui/thirdpartybinaryinstalldialog.h | 42 ++++++++ sources/ui/thirdpartybinaryinstalldialog.ui | 104 +++++++++++++++++++ 9 files changed, 373 insertions(+), 2 deletions(-) create mode 100644 sources/dxf/dxftoelmt.cpp create mode 100644 sources/dxf/dxftoelmt.h create mode 100644 sources/ui/thirdpartybinaryinstalldialog.cpp create mode 100644 sources/ui/thirdpartybinaryinstalldialog.h create mode 100644 sources/ui/thirdpartybinaryinstalldialog.ui diff --git a/qelectrotech.pro b/qelectrotech.pro index 7baddbb08..5ae48a6ec 100644 --- a/qelectrotech.pro +++ b/qelectrotech.pro @@ -159,7 +159,8 @@ HEADERS += $$files(sources/*.h) $$files(sources/ui/*.h) \ $$files(sources/print/*.h) \ $$files(sources/TerminalStrip/*.h) \ $$files(sources/TerminalStrip/ui/*.h) \ - $$files(sources/TerminalStrip/UndoCommand/*.h) + $$files(sources/TerminalStrip/UndoCommand/*.h) \ + $$files(sources/dxf/*.h) SOURCES += $$files(sources/*.cpp) \ $$files(sources/editor/*.cpp) \ @@ -195,7 +196,8 @@ SOURCES += $$files(sources/*.cpp) \ $$files(sources/print/*.cpp) \ $$files(sources/TerminalStrip/*.cpp) \ $$files(sources/TerminalStrip/ui/*.cpp) \ - $$files(sources/TerminalStrip/UndoCommand/*.cpp) + $$files(sources/TerminalStrip/UndoCommand/*.cpp) \ + $$files(sources/dxf/*.cpp) # Needed for use promote QTreeWidget in terminalstripeditor.ui INCLUDEPATH += sources/TerminalStrip/ui diff --git a/sources/dxf/dxftoelmt.cpp b/sources/dxf/dxftoelmt.cpp new file mode 100644 index 000000000..5e33b4e9d --- /dev/null +++ b/sources/dxf/dxftoelmt.cpp @@ -0,0 +1,102 @@ +/* + Copyright 2006-2022 The QElectroTech Team + This file is part of QElectroTech. + + QElectroTech is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + QElectroTech is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with QElectroTech. If not, see . +*/ +#include "dxftoelmt.h" +#include "../ui/thirdpartybinaryinstalldialog.h" + +#include +#include +#include +#include + +/** + * @brief dxftoElmt + * Return the dxf at @a file_path converted to elmt. + * The returned value is a QByteArray, instead of a + * QDomDocument or QString, to let user do what they want with that. + * @param file_path + * @return + */ +QByteArray dxfToElmt(const QString &file_path) +{ + if (!dxf2ElmtIsPresent(false)) { + return QByteArray(); + } + + QProcess process_; + const QString program{dxf2ElmtBinaryPath()}; + const QStringList arguments{file_path, QStringLiteral("-v")}; + + process_.start(program, arguments); + process_.waitForFinished(); + + const auto byte_array{process_.readAll()}; + process_.close(); + return byte_array; +} + +QString dxf2ElmtDirPath() +{ +#if defined(Q_OS_WIN32) || defined(Q_OS_WIN64) + return (QDir::homePath() + QStringLiteral("/Application Data/qet/binary")); +#elif defined(Q_OS_MAC) + return (QDir::homePath() + QStringLiteral("/.qet/binary")); +#else + return (QDir::homePath() + QStringLiteral("/.qet/binary")); +#endif +} + +/** + * @brief dxf2ElmtBinaryPath + * @return the path to the dxf2elmt program + */ +QString dxf2ElmtBinaryPath() +{ +#if defined(Q_OS_WIN32) || defined(Q_OS_WIN64) + return dxf2ElmtDirPath() + QStringLiteral("/dxf2elmt.exe"); +#elif defined(Q_OS_MAC) + return dxf2ElmtDirPath() + QStringLiteral("/dxf2elmt.app"); +#else + return dxf2ElmtDirPath() + QStringLiteral("/dxf2elmt"); +#endif +} + +/** + * @brief dxf2ElmtIsPresent + * Return true if dxf2elmt is present in the system + * @param install_dialog + * True to display a dialog with the explanations + * of how to install the dxf2elmt program + * if not present in the system. + * @return + */ +bool dxf2ElmtIsPresent(bool install_dialog, QWidget *parent) +{ + const bool exist{QFile::exists(dxf2ElmtBinaryPath())}; + if (!exist && install_dialog) + { + auto string_{QStringLiteral("L'import dxf nécessite le logiciel dxf2elmt. \n" + "Veuillez télécharger celui-ci en suivant le lien ci dessous et le dézipper dans le dossier d'installation")}; + + ThirdPartyBinaryInstallDialog dialog_(string_, + QStringLiteral("https://github.com/antonioaja/dxf2elmt/releases"), + dxf2ElmtDirPath(), + parent); + dialog_.exec(); + } + return exist; +} diff --git a/sources/dxf/dxftoelmt.h b/sources/dxf/dxftoelmt.h new file mode 100644 index 000000000..97f06af19 --- /dev/null +++ b/sources/dxf/dxftoelmt.h @@ -0,0 +1,30 @@ +/* + Copyright 2006-2022 The QElectroTech Team + This file is part of QElectroTech. + + QElectroTech is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + QElectroTech is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with QElectroTech. If not, see . +*/ +#ifndef DXFTOELMT_H +#define DXFTOELMT_H + +#include + +class QWidget; + +QByteArray dxfToElmt(const QString &file_path); +QString dxf2ElmtDirPath(); +QString dxf2ElmtBinaryPath(); +bool dxf2ElmtIsPresent(bool install_dialog = true, QWidget *parent = nullptr); + +#endif // DXFTOELMT_H diff --git a/sources/editor/ui/qetelementeditor.cpp b/sources/editor/ui/qetelementeditor.cpp index 1e06360d2..4fa0e74e1 100644 --- a/sources/editor/ui/qetelementeditor.cpp +++ b/sources/editor/ui/qetelementeditor.cpp @@ -46,6 +46,7 @@ #include "dynamictextfieldeditor.h" #include "../../newelementwizard.h" #include "../editorcommands.h" +#include "../../dxf/dxftoelmt.h" #include #include @@ -1490,3 +1491,31 @@ void QETElementEditor::on_m_donate_action_triggered() { } void QETElementEditor::on_m_about_qt_action_triggered() { qApp->aboutQt(); } + +void QETElementEditor::on_m_import_dxf_triggered() +{ + if (dxf2ElmtIsPresent(true, this)) + { + QString file_path{QFileDialog::getOpenFileName(this, + QObject::tr("Importer un fichier dxf"), + "/home", + "DXF (*.dxf)")}; + if (file_path.isEmpty()) { + return; + } + + QMessageBox::information(this, tr("Avertissement"), tr("L'import d'un dxf volumineux peut prendre du temps \n" + "veuillez patienter durant l'import...")); + + const QByteArray array_{dxfToElmt(file_path)}; + if (array_.isEmpty()) { + return; + } + QDomDocument xml_; + xml_.setContent(array_); + + m_elmt_scene->fromXml(xml_); + fillPartsList(); + } +} + diff --git a/sources/editor/ui/qetelementeditor.h b/sources/editor/ui/qetelementeditor.h index faae89aed..386dc3ee2 100644 --- a/sources/editor/ui/qetelementeditor.h +++ b/sources/editor/ui/qetelementeditor.h @@ -110,6 +110,7 @@ class QETElementEditor : public QMainWindow void on_m_youtube_action_triggered(); void on_m_donate_action_triggered(); void on_m_about_qt_action_triggered(); + void on_m_import_dxf_triggered(); private: bool canClose(); diff --git a/sources/editor/ui/qetelementeditor.ui b/sources/editor/ui/qetelementeditor.ui index 07452bbb8..2c650052d 100644 --- a/sources/editor/ui/qetelementeditor.ui +++ b/sources/editor/ui/qetelementeditor.ui @@ -35,6 +35,7 @@ + @@ -501,6 +502,15 @@ Space + + + + :/ico/16x16/run-dxf.png:/ico/16x16/run-dxf.png + + + Importer un dxf + + diff --git a/sources/ui/thirdpartybinaryinstalldialog.cpp b/sources/ui/thirdpartybinaryinstalldialog.cpp new file mode 100644 index 000000000..07522803f --- /dev/null +++ b/sources/ui/thirdpartybinaryinstalldialog.cpp @@ -0,0 +1,51 @@ +/* + Copyright 2006-2022 The QElectroTech Team + This file is part of QElectroTech. + + QElectroTech is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + QElectroTech is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with QElectroTech. If not, see . +*/ +#include "thirdpartybinaryinstalldialog.h" +#include "ui_thirdpartybinaryinstalldialog.h" + +#include +#include +#include +#include +#include + +ThirdPartyBinaryInstallDialog::ThirdPartyBinaryInstallDialog(const QString &text, + const QString &downloadLink, + const QString &binaryFolderPath, + QWidget *parent) : + QDialog{parent}, + ui{new Ui::ThirdPartyBinaryInstallDialog} +{ + ui->setupUi(this); + ui->m_label->setText(text); + + connect(ui->m_download_pb, &QPushButton::clicked, [downloadLink](){ + QDesktopServices::openUrl(QUrl(downloadLink)); + }); + connect(ui->m_install_dir_pb, &QPushButton::clicked, [binaryFolderPath]() { + //Make sure the path exist + QDir dir_; + dir_.mkpath(binaryFolderPath); + QDesktopServices::openUrl(QUrl("file:///" + binaryFolderPath)); + }); +} + +ThirdPartyBinaryInstallDialog::~ThirdPartyBinaryInstallDialog() +{ + delete ui; +} diff --git a/sources/ui/thirdpartybinaryinstalldialog.h b/sources/ui/thirdpartybinaryinstalldialog.h new file mode 100644 index 000000000..6bbb93b18 --- /dev/null +++ b/sources/ui/thirdpartybinaryinstalldialog.h @@ -0,0 +1,42 @@ +/* + Copyright 2006-2022 The QElectroTech Team + This file is part of QElectroTech. + + QElectroTech is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + QElectroTech is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with QElectroTech. If not, see . +*/ +#ifndef THIRDPARTYBINARYINSTALLDIALOG_H +#define THIRDPARTYBINARYINSTALLDIALOG_H + +#include + +namespace Ui { + class ThirdPartyBinaryInstallDialog; +} + +class ThirdPartyBinaryInstallDialog : public QDialog +{ + Q_OBJECT + + public: + explicit ThirdPartyBinaryInstallDialog(const QString &text, + const QString &downloadLink, + const QString &binaryFolderPath, + QWidget *parent = nullptr); + ~ThirdPartyBinaryInstallDialog(); + + private: + Ui::ThirdPartyBinaryInstallDialog *ui; +}; + +#endif // THIRDPARTYBINARYINSTALLDIALOG_H diff --git a/sources/ui/thirdpartybinaryinstalldialog.ui b/sources/ui/thirdpartybinaryinstalldialog.ui new file mode 100644 index 000000000..899666d2c --- /dev/null +++ b/sources/ui/thirdpartybinaryinstalldialog.ui @@ -0,0 +1,104 @@ + + + ThirdPartyBinaryInstallDialog + + + + 0 + 0 + 373 + 64 + + + + Logiciel tiers requis + + + true + + + + + + Qt::Horizontal + + + QDialogButtonBox::Ok + + + + + + + Télechargement + + + + :/ico/16x16/edit-download.png:/ico/16x16/edit-download.png + + + + + + + Dossier installation + + + + :/ico/16x16/folder-open.png:/ico/16x16/folder-open.png + + + + + + + + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop + + + 5 + + + + + + + + + + + m_button_box + accepted() + ThirdPartyBinaryInstallDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + m_button_box + rejected() + ThirdPartyBinaryInstallDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + +