Skip to content

Commit

Permalink
Merge pull request #302 from plc-user/master
Browse files Browse the repository at this point in the history
integrate "QET_ElementScaler" as external software
  • Loading branch information
scorpio810 authored Aug 19, 2024
2 parents 1baf9db + 5d725f6 commit 6aebbfe
Show file tree
Hide file tree
Showing 9 changed files with 376 additions and 118 deletions.
3 changes: 3 additions & 0 deletions cmake/qet_compilation_vars.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ set(QET_SRC_FILES
${QET_DIR}/sources/dxf/dxftoelmt.cpp
${QET_DIR}/sources/dxf/dxftoelmt.h

${QET_DIR}/sources/qet_elementscaler/qet_elementscaler.cpp
${QET_DIR}/sources/qet_elementscaler/qet_elementscaler.h

${QET_DIR}/sources/editor/arceditor.cpp
${QET_DIR}/sources/editor/arceditor.h
${QET_DIR}/sources/editor/editorcommands.cpp
Expand Down
Binary file modified lang/qet_de.qm
Binary file not shown.
267 changes: 152 additions & 115 deletions lang/qet_de.ts

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions qelectrotech.pro
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ HEADERS += $$files(sources/*.h) \
$$files(sources/TerminalStrip/GraphicsItem/*.h) \
$$files(sources/TerminalStrip/GraphicsItem/properties/*.h) \
$$files(sources/xml/*.h) \
$$files(sources/dxf/*.h)
$$files(sources/dxf/*.h) \
$$files(sources/qet_elementscaler/*.h)

SOURCES += $$files(sources/*.cpp) \
$$files(sources/editor/*.cpp) \
Expand Down Expand Up @@ -208,7 +209,8 @@ SOURCES += $$files(sources/*.cpp) \
$$files(sources/TerminalStrip/GraphicsItem/*.cpp) \
$$files(sources/TerminalStrip/GraphicsItem/properties/*.cpp) \
$$files(sources/xml/*.cpp) \
$$files(sources/dxf/*.cpp)
$$files(sources/dxf/*.cpp) \
$$files(sources/qet_elementscaler/*.cpp)

# Needed for use promote QTreeWidget in terminalstripeditor.ui
INCLUDEPATH += sources/TerminalStrip/ui
Expand Down
44 changes: 43 additions & 1 deletion sources/editor/ui/qetelementeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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));
}
}

2 changes: 2 additions & 0 deletions sources/editor/ui/qetelementeditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
10 changes: 10 additions & 0 deletions sources/editor/ui/qetelementeditor.ui
Original file line number Diff line number Diff line change
Expand Up @@ -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"/>
Expand Down Expand Up @@ -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"/>
Expand Down
132 changes: 132 additions & 0 deletions sources/qet_elementscaler/qet_elementscaler.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
30 changes: 30 additions & 0 deletions sources/qet_elementscaler/qet_elementscaler.h
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6aebbfe

Please sign in to comment.