integrate "QET_ElementScaler" as external software

This commit is contained in:
plc-user
2024-08-19 09:19:28 +02:00
parent 1baf9db799
commit 85d10fc0e6
9 changed files with 376 additions and 118 deletions

View File

@@ -47,6 +47,7 @@
#include "../../newelementwizard.h"
#include "../editorcommands.h"
#include "../../dxf/dxftoelmt.h"
#include "../../qet_elementscaler/qet_elementscaler.h"
#include "../UndoCommand/openelmtcommand.h"
#include <QSettings>
@@ -1332,6 +1333,24 @@ void QETElementEditor::on_m_open_dxf_action_triggered()
DXF -> start(program,arguments);
}
void QETElementEditor::on_m_open_scaled_element_action_triggered()
{
#ifdef TODO_LIST
# pragma message("@TODO Merge 'Element-Scaling' code into QET")
# pragma message("https://github.com/plc-user/QET_ElementScaler")
#endif
#if defined(Q_OS_WIN32) || defined(Q_OS_WIN64)
QString program = (QDir::homePath() + "/Application Data/qet/QET_ElementScaler.exe");
#elif defined(Q_OS_MAC)
QString program = (QDir::homePath() + "/.qet/QET_ElementScaler.app");
#else
QString program = (QDir::homePath() + "/.qet/QET_ElementScaler");
#endif
QStringList arguments;
QProcess *ES = new QProcess(qApp);
ES -> start(program,arguments);
}
bool QETElementEditor::on_m_save_as_file_action_triggered()
{
// Check element before writing
@@ -1504,7 +1523,7 @@ void QETElementEditor::on_m_import_dxf_triggered()
{
QString file_path{QFileDialog::getOpenFileName(this,
QObject::tr("Importer un fichier dxf"),
"/home",
QDir::homePath(),
"DXF (*.dxf)")};
if (file_path.isEmpty()) {
return;
@@ -1524,3 +1543,26 @@ void QETElementEditor::on_m_import_dxf_triggered()
}
}
void QETElementEditor::on_m_import_scaled_element_triggered()
{
if (ElementScalerIsPresent(true, this))
{
QString file_path{QFileDialog::getOpenFileName(this,
tr("Importer un élément à redimensionner"),
QDir::homePath(),
tr("Éléments QElectroTech (*.elmt)"))};
if (file_path.isEmpty()) {
return;
}
const QByteArray array_{ElementScaler(file_path, this)};
if (array_.isEmpty()) {
return;
}
QDomDocument xml_;
xml_.setContent(array_);
m_elmt_scene->undoStack().push(new OpenElmtCommand(xml_, m_elmt_scene));
}
}

View File

@@ -87,6 +87,7 @@ class QETElementEditor : public QMainWindow
void on_m_open_action_triggered();
void on_m_open_from_file_action_triggered();
void on_m_open_dxf_action_triggered();
void on_m_open_scaled_element_action_triggered();
bool on_m_save_as_file_action_triggered();
void on_m_reload_action_triggered();
void on_m_quit_action_triggered();
@@ -111,6 +112,7 @@ class QETElementEditor : public QMainWindow
void on_m_donate_action_triggered();
void on_m_about_qt_action_triggered();
void on_m_import_dxf_triggered();
void on_m_import_scaled_element_triggered();
private:
bool canClose();

View File

@@ -35,6 +35,7 @@
<addaction name="m_new_action"/>
<addaction name="m_open_action"/>
<addaction name="m_open_from_file_action"/>
<addaction name="m_import_scaled_element"/>
<addaction name="m_import_dxf"/>
<addaction name="m_open_dxf_action"/>
<addaction name="m_save_action"/>
@@ -514,6 +515,15 @@
<string>Importer un dxf</string>
</property>
</action>
<action name="m_import_scaled_element">
<property name="icon">
<iconset resource="../../../qelectrotech.qrc">
<normaloff>:/ico/22x22/folder-open.png</normaloff>:/ico/22x22/folder-open.png</iconset>
</property>
<property name="text">
<string>importer un élément à redimensionner</string>
</property>
</action>
</widget>
<resources>
<include location="../../../qelectrotech.qrc"/>

View File

@@ -0,0 +1,132 @@
/*
Copyright 2024 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 <http://www.gnu.org/licenses/>.
*/
#include "qet_elementscaler.h"
#include "../ui/thirdpartybinaryinstalldialog.h"
#include <QFile>
#include <QProcess>
#include <QInputDialog>
#include <QDir>
/**
* @brief QET_ElementScaler
* Return the scaled element from @a file_path.
* The returned value is a QByteArray, instead of a
* QDomDocument or QString, to let user do what he/she wants.
* If something goes wrong the QByteArray returned is empty.
* @param file_path
* @return
*/
QByteArray ElementScaler(const QString &file_path, QWidget *parent)
{
if (!ElementScalerIsPresent(false)) {
return QByteArray();
}
bool ok;
double fx = QInputDialog::getDouble(parent, QObject::tr("Entrer le facteur d'échelle"),
QObject::tr("Facteur X:"), 1.0, 0.1, 100, 5, &ok,
Qt::WindowFlags(), 1);
QString sFactorX = "1.0";
if (ok)
sFactorX = (QString::number(fx, 'f', 5));
else
return QByteArray();
double fy = QInputDialog::getDouble(parent, QObject::tr("Entrer le facteur d'échelle"),
QObject::tr("Facteur Y:"), fx, 0.1, 100, 5, &ok,
Qt::WindowFlags(), 1);
QString sFactorY = "1.0";
if (ok)
sFactorY = (QString::number(fy, 'f', 5));
else
return QByteArray();
QProcess process_;
const QString program{ElementScalerBinaryPath()};
const QStringList arguments{QStringLiteral("-x"), sFactorX,
QStringLiteral("-y"), sFactorY,
QStringLiteral("-o"), QStringLiteral("-f"), file_path};
process_.start(program, arguments);
if (process_.waitForFinished())
{
const auto byte_array{process_.readAll()};
process_.close();
return byte_array;
}
else
{
//If something went wrong we return an empty QByteArray
return QByteArray();
}
}
QString ElementScalerDirPath()
{
#if defined(Q_OS_WIN32) || defined(Q_OS_WIN64)
return (QDir::homePath() + QStringLiteral("/Application Data/qet/binary"));
#elif defined(Q_OS_MACOS)
return (QDir::homePath() + QStringLiteral("/.qet/binary"));
#else
return (QDir::homePath() + QStringLiteral("/.qet/binary"));
#endif
}
/**
* @brief ElementScalerBinaryPath
* @return the path to the QET_ElementScaler program
*/
QString ElementScalerBinaryPath()
{
#if defined(Q_OS_WIN32) || defined(Q_OS_WIN64)
return ElementScalerDirPath() + QStringLiteral("/QET_ElementScaler.exe");
#elif defined(Q_OS_MACOS)
return ElementScalerDirPath() + QStringLiteral("/./QET_ElementScaler");
#else
return ElementScalerDirPath() + QStringLiteral("/QET_ElementScaler");
#endif
}
/**
* @brief ElementScalerIsPresent
* Return true if QET_ElementScaler is present in the system
* @param install_dialog
* True to display a dialog with the explanations
* of how to install the QET_ElementScaler program
* if not present in the system.
* @return
*/
bool ElementScalerIsPresent(bool install_dialog, QWidget *parent)
{
const bool exist{QFile::exists(ElementScalerBinaryPath())};
if (!exist && install_dialog)
{
auto string_{QObject::tr("Le logiciel QET_ElementScaler est nécessaire pour mettre les éléments à l'échelle.\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/plc-user/QET_ElementScaler/releases"),
ElementScalerDirPath(),
parent);
dialog_.exec();
}
return exist;
}

View File

@@ -0,0 +1,30 @@
/*
Copyright 2024 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 <http://www.gnu.org/licenses/>.
*/
#ifndef QET_ELEMENTSCALER_H
#define QET_ELEMENTSCALER_H
#include <QByteArray>
class QWidget;
QByteArray ElementScaler(const QString &file_path, QWidget *parent);
QString ElementScalerDirPath();
QString ElementScalerBinaryPath();
bool ElementScalerIsPresent(bool install_dialog = true, QWidget *parent = nullptr);
#endif // QET_ELEMENTSCALER_H