From: DUC ANH HOANG Date: Mon, 29 Apr 2024 08:37:07 +0000 (+0200) Subject: cmake for compilation of AppNameCustomize X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=4a4eb5e96ac181c52b3abb757be6e6a2e21a1e5a;p=modules%2Fgui.git cmake for compilation of AppNameCustomize --- diff --git a/src/LightApp/AppNameCustomize.cxx b/src/LightApp/AppNameCustomize.cxx new file mode 100644 index 000000000..7ad9ad8f6 --- /dev/null +++ b/src/LightApp/AppNameCustomize.cxx @@ -0,0 +1,138 @@ +#include "LightApp_Msg.h" + +using namespace LightApp_Msg; + +bool qmTots(const QString &qmFilePath, const QString &tsFilePath) { + QProcess process; + + QString command = "lconvert"; + QStringList arguments; + arguments << qmFilePath; + arguments << "-o"; + arguments << tsFilePath; + + process.start(command, arguments); + + process.waitForFinished(); + + if (process.exitCode()== 0) + return true; + else + return false; +} + +bool tsToqm(const QString &tsFilePath, const QString &qmFilePath){ + QProcess process; + + QString command = "lrelease"; + QStringList arguments; + arguments << tsFilePath; + arguments << "-qm"; + arguments << qmFilePath; + + process.start(command, arguments); + + process.waitForFinished(); + + if (process.exitCode()== 0) + return true; + else + { + qDebug()<< QString("Can not compile %1 to %2").arg(tsFilePath,qmFilePath); + return false; + } +} + +bool LightApp_Msg_patching(const QString& app_name,const QString& qmFilePath) +{ + // Get GUI_ROOT_DIR + QByteArray gui_root_dir_env = qgetenv("GUI_ROOT_DIR"); + if (gui_root_dir_env.isEmpty()) + { + qCritical()<< "GUI_ROOT_DIR must be defined"; + return false; + } + + QString gui_root_dir = QString::fromUtf8(gui_root_dir_env); + QDir gui_root_qdir(gui_root_dir); + QString qmFilePathAbs(gui_root_qdir.filePath(qmFilePath)); + QStringList qmFilePathAbs_splited = qmFilePathAbs.split("/"); + QString tsFilePath(qmFilePathAbs_splited[qmFilePathAbs_splited.size() - 1]); + tsFilePath.replace(".qm",".ts"); + + if (qmTots(qmFilePathAbs,tsFilePath)) + qInfo() <<"qmTots OK"; + else + { + qDebug() <<"qmTots KO"; + return false; + } + + try + { + QFile input_tsFile(tsFilePath); + if(!input_tsFile.open(QFile::ReadOnly | QFile::Text)) { + qDebug() << "Could not open tsfile"; + throw; + } + std::unique_ptr> Context_list(new QList); + + LightApp_Msg_handling LAMsg_handling(std::move(Context_list)); + + LAMsg_handling.read(&input_tsFile); + + LAMsg_handling.GetContextbyName("@default")->GetMessagebySrc("APP_NAME")->Set_Translation(app_name); + + input_tsFile.close(); + + // Write in tsfile + QFile output_tsFile(tsFilePath); + if (!output_tsFile.open(QIODevice::WriteOnly | QIODevice::Text)) { + qDebug() << "Cannot open file for writing."; + throw; + } + QTextStream out(&output_tsFile); + out << LAMsg_handling.write(); + output_tsFile.close(); + } + catch(...) + { + qCritical() << "Something wrong with " << qmFilePathAbs; + return false; + } + + // write to qm file + if (tsToqm(tsFilePath,qmFilePathAbs)) + qInfo() <<"tsToqm OK"; + else + { + qDebug() <<"tsToqm KO"; + return false; + } + + return true; + +} +int main(int argc, char *argv[]) { + + if (argc < 2) { + qDebug() << "Usage: app_name must be declared"; + return 1; + } + + QString app_name = QString::fromUtf8(argv[1]); + QStringList qmFilePath_list = {"share/salome/resources/gui/LightApp_msg_fr.qm", + "share/salome/resources/gui/LightApp_msg_en.qm", + "share/salome/resources/gui/LightApp_msg_ja.qm"}; + + for (const QString& qmFilePath:qmFilePath_list) + { + if(!LightApp_Msg_patching(app_name,qmFilePath)) + { + qDebug() << "LightApp_Msg_patching failed!!!!"; + return 1; + } + } + return 0; + +} diff --git a/src/LightApp/CMakeLists.txt b/src/LightApp/CMakeLists.txt index 55172bee0..677cde862 100644 --- a/src/LightApp/CMakeLists.txt +++ b/src/LightApp/CMakeLists.txt @@ -294,6 +294,20 @@ SET(_other_SOURCES LightApp_WgViewModel.cxx LightApp_ExtInfoDlg.cxx ) + +SET(APPNAMECUSTOMIZE_SRC + AppNameCustomize.cxx + LightApp_Msg.cxx +) + +SET(APPNAMECUSTOMIZE_header + LightApp_Msg.h +) + +QT_WRAP_MOC(moc_APPNAMECUSTOMIZE_header ${APPNAMECUSTOMIZE_header}) + +SET(APPNAMECUSTOMIZE_SOURCES ${moc_APPNAMECUSTOMIZE_header} ${APPNAMECUSTOMIZE_SRC}) + IF(SALOME_USE_GLVIEWER) LIST(APPEND _other_SOURCES LightApp_GLSelector.cxx) ENDIF() @@ -338,3 +352,7 @@ INSTALL(FILES ${LightApp_HEADERS} DESTINATION ${SALOME_INSTALL_HEADERS}) QT_INSTALL_TS_RESOURCES("${_ts_RESOURCES}" "${SALOME_GUI_INSTALL_RES_DATA}") INSTALL(FILES ${_other_RESOURCES} DESTINATION ${SALOME_GUI_INSTALL_RES_DATA}) + +ADD_EXECUTABLE(AppNameCustomize ${APPNAMECUSTOMIZE_SOURCES} ) +TARGET_LINK_LIBRARIES(AppNameCustomize ${QT_LIBRARIES}) +INSTALL(TARGETS AppNameCustomize EXPORT ${PROJECT_NAME}TargetGroup DESTINATION ${SALOME_INSTALL_BINS}) diff --git a/src/LightApp/LightApp_Msg.cxx b/src/LightApp/LightApp_Msg.cxx new file mode 100644 index 000000000..4d1247bd4 --- /dev/null +++ b/src/LightApp/LightApp_Msg.cxx @@ -0,0 +1,152 @@ +#include "LightApp_Msg.h" + +using namespace LightApp_Msg; + +QString Context::ExportContext() +{ + QString context_string; + context_string.append(QString("\n")); + context_string.append(QString("%1\n").arg(Context_name)); + foreach (const Message& msg, Message_list) + { + context_string.append(QString("\n%1\n%2\n\n").arg(msg.GetSource(),msg.GetTranslation())); + } + context_string.append(QString("\n")); + + return context_string; +} + +Message* Context::GetMessagebySrc(const QString& src) +{ + uint i(0); + while(i < Message_list.size() && Message_list[i].GetSource() != src) + i++; + if (i < Message_list.size()) + return &Message_list[i]; +} + + + +Context* LightApp_Msg_handling::GetContextbyName(const QString& name) +{ + int i(0); + while(i < Context_list->size() && (*Context_list)[i].GetContext_name() != name) + i++; + if (i < Context_list->size()) + return &(*Context_list)[i]; + else + { + qDebug()<<"Context with name " << name << " doesn't exist in LightApp_Msg_handling"; + return nullptr; + } +} + +bool LightApp_Msg_handling::HaveContext(const QString& name) +{ + int i(0); + while(i < Context_list->size() && (*Context_list)[i].GetContext_name() != name) + i++; + if (i < Context_list->size()) + return true; + else + return false; +} + +QString LightApp_Msg_handling::write() +{ + QString updateXml; + updateXml.append(header); + for(auto ct:*Context_list) + updateXml.append(ct.ExportContext()); + updateXml.append(""); + + return updateXml; + +} +void LightApp_Msg_handling::read(QIODevice *device) +{ + + //header + QTextStream textStream(device); + // Read 3 first header line of ts file + for (int i = 0; i < 3 && !textStream.atEnd(); ++i) { + header.append(textStream.readLine() + "\n"); + } + device->seek(0); + + //context + xmlReader.setDevice(device); + //CustomEntityResolver resolver; + //xmlReader.setEntityResolver(&resolver); + + while(xmlReader.readNextStartElement()){ + if(xmlReader.name().toString() == "context") + readContext(); + } + +} + +void LightApp_Msg_handling::readContext() +{ + Q_ASSERT(xmlReader.isStartElement() && + xmlReader.name() == "context"); + + while(xmlReader.readNextStartElement()){ + if(xmlReader.name() == "name") + { + QString name = xmlReader.readElementText(); + if (!HaveContext(name)) + Context_list->append(Context(name)); + else + qWarning()<<"Context with name " << name << " is already appended in LightApp_Msg_handling"; + } + else if (xmlReader.name() == "message") + { + readMessage(); + } + else + xmlReader.skipCurrentElement(); + } +} + +void LightApp_Msg_handling::readMessage() +{ + Q_ASSERT(xmlReader.isStartElement() && + xmlReader.name() == "message"); + Message msg = Message("",""); + Message& msg_ref = msg; + while(xmlReader.readNextStartElement()){ + if(xmlReader.name() == "source") + readSrc(msg_ref); + else if(xmlReader.name() == "translation") + readTranslation(msg_ref); + else + xmlReader.skipCurrentElement(); + + } + + (*Context_list)[Context_list->size() - 1].AddMsg(msg); +} + +void LightApp_Msg_handling::readSrc(Message& msg) +{ + Q_ASSERT(xmlReader.isStartElement() && + xmlReader.name() == "source"); + QString src = xmlReader.readElementText(); + + msg.Set_Source(src); +} + +void LightApp_Msg_handling::readTranslation(Message& msg) +{ + Q_ASSERT(xmlReader.isStartElement() && + xmlReader.name() == "translation"); + + QString trl = xmlReader.readElementText(); + trl.replace("&","&"); + trl.replace("\"","""); + trl.replace("\'","'"); + trl.replace("<","<"); + trl.replace(">",">"); + msg.Set_Translation(trl); +} diff --git a/src/LightApp/LightApp_Msg.h b/src/LightApp/LightApp_Msg.h new file mode 100644 index 000000000..5fb5bdf05 --- /dev/null +++ b/src/LightApp/LightApp_Msg.h @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace LightApp_Msg +{ + +class Message +{ + public: + Message(const QString& src, const QString& trl):source(src),translation(trl){}; + QString GetSource() const { return source ;} + QString GetTranslation() const { return translation ;} + void Set_Source(const QString& str) { source = str; } + void Set_Translation(const QString& str) { translation = str; } + private: + QString source; + QString translation; +}; + +class Context +{ + public: + Context(const QString& name):Context_name(name){}; + QString GetContext_name(){return Context_name;} + void AddMsg(Message& msg) {Message_list.append(msg);} + void AddMsg(const QString& src, const QString& trl) {Message_list.append(Message(src,trl));} + Message* GetMessagebySrc(const QString&); + QList& GetMessageList(){return Message_list;} + QString ExportContext(); + private: + QString Context_name; + QList Message_list; +}; + +class LightApp_Msg_handling +{ +public: + LightApp_Msg_handling(std::unique_ptr> Context_list): + Context_list(std::move(Context_list)){}; + void read(QIODevice *device); + QString write(); + Context* GetContextbyName(const QString &name); + bool HaveContext(const QString &name); +private: + QXmlStreamReader xmlReader; + std::unique_ptr> Context_list; + QString header; + + void readContext(); + void readMessage(); + void readSrc(Message& msg); + void readTranslation(Message& msg); +}; + +} diff --git a/src/LightAppMsg_Customize/AppNameCustomize.cpp b/src/LightAppMsg_Customize/AppNameCustomize.cpp deleted file mode 100644 index 0defc2e1b..000000000 --- a/src/LightAppMsg_Customize/AppNameCustomize.cpp +++ /dev/null @@ -1,138 +0,0 @@ -#include "LightApp_msg.h" - -using namespace LightApp_msg; - -bool qmTots(const QString &qmFilePath, const QString &tsFilePath) { - QProcess process; - - QString command = "lconvert"; - QStringList arguments; - arguments << qmFilePath; - arguments << "-o"; - arguments << tsFilePath; - - process.start(command, arguments); - - process.waitForFinished(); - - if (process.exitCode()== 0) - return true; - else - return false; -} - -bool tsToqm(const QString &tsFilePath, const QString &qmFilePath){ - QProcess process; - - QString command = "lrelease"; - QStringList arguments; - arguments << tsFilePath; - arguments << "-qm"; - arguments << qmFilePath; - - process.start(command, arguments); - - process.waitForFinished(); - - if (process.exitCode()== 0) - return true; - else - { - qDebug()<< QString("Can not compile %1 to %2").arg(tsFilePath,qmFilePath); - return false; - } -} - -bool LightApp_msg_patching(const QString& app_name,const QString& qmFilePath) -{ - // Get GUI_ROOT_DIR - QByteArray gui_root_dir_env = qgetenv("GUI_ROOT_DIR"); - if (gui_root_dir_env.isEmpty()) - { - qCritical()<< "GUI_ROOT_DIR must be defined"; - return false; - } - - QString gui_root_dir = QString::fromUtf8(gui_root_dir_env); - QDir gui_root_qdir(gui_root_dir); - QString qmFilePathAbs(gui_root_qdir.filePath(qmFilePath)); - QStringList qmFilePathAbs_splited = qmFilePathAbs.split("/"); - QString tsFilePath(qmFilePathAbs_splited[qmFilePathAbs_splited.size() - 1]); - tsFilePath.replace(".qm",".ts"); - - if (qmTots(qmFilePathAbs,tsFilePath)) - qInfo() <<"qmTots OK"; - else - { - qDebug() <<"qmTots KO"; - return false; - } - - try - { - QFile input_tsFile(tsFilePath); - if(!input_tsFile.open(QFile::ReadOnly | QFile::Text)) { - qDebug() << "Could not open tsfile"; - throw; - } - std::unique_ptr> Context_list(new QList); - - LightApp_msg_handling LAMsg_handling(std::move(Context_list)); - - LAMsg_handling.read(&input_tsFile); - - LAMsg_handling.GetContextbyName("@default")->GetMessagebySrc("APP_NAME")->Set_Translation(app_name); - - input_tsFile.close(); - - // Write in tsfile - QFile output_tsFile(tsFilePath); - if (!output_tsFile.open(QIODevice::WriteOnly | QIODevice::Text)) { - qDebug() << "Cannot open file for writing."; - throw; - } - QTextStream out(&output_tsFile); - out << LAMsg_handling.write(); - output_tsFile.close(); - } - catch(...) - { - qCritical() << "Something wrong with " << qmFilePathAbs; - return false; - } - - // write to qm file - if (tsToqm(tsFilePath,qmFilePathAbs)) - qInfo() <<"tsToqm OK"; - else - { - qDebug() <<"tsToqm KO"; - return false; - } - - return true; - -} -int main(int argc, char *argv[]) { - - if (argc < 2) { - qDebug() << "Usage: app_name must be declared"; - return 1; - } - - QString app_name = QString::fromUtf8(argv[1]); - QStringList qmFilePath_list = {"share/salome/resources/gui/LightApp_msg_fr.qm", - "share/salome/resources/gui/LightApp_msg_en.qm", - "share/salome/resources/gui/LightApp_msg_ja.qm"}; - - for (const QString& qmFilePath:qmFilePath_list) - { - if(!LightApp_msg_patching(app_name,qmFilePath)) - { - qDebug() << "LightApp_msg_patching failed!!!!"; - return 1; - } - } - return 0; - -} diff --git a/src/LightAppMsg_Customize/AppNameCustomize.pro b/src/LightAppMsg_Customize/AppNameCustomize.pro deleted file mode 100644 index 5fa86fcc1..000000000 --- a/src/LightAppMsg_Customize/AppNameCustomize.pro +++ /dev/null @@ -1,7 +0,0 @@ -TEMPLATE = app -QT += core gui -CONFIG += c++11 -SOURCES += \ - AppNameCustomize.cpp \ - LightApp_msg.cpp -HEADERS += LightApp_msg.h \ No newline at end of file diff --git a/src/LightAppMsg_Customize/LightApp_msg.cpp b/src/LightAppMsg_Customize/LightApp_msg.cpp deleted file mode 100644 index 716801495..000000000 --- a/src/LightAppMsg_Customize/LightApp_msg.cpp +++ /dev/null @@ -1,163 +0,0 @@ -#include "LightApp_msg.h" - -using namespace LightApp_msg; - -QString CustomEntityResolver::resolveUndeclaredEntity(const QString &name) { - if (name == "&") - return "&"; - else if (name == """) - return """; - else if (name == "'") - return "'"; - return QString(); -} - - -QString Context::ExportContext() -{ - QString context_string; - context_string.append(QString("\n")); - context_string.append(QString("%1\n").arg(Context_name)); - foreach (const Message& msg, Message_list) - { - context_string.append(QString("\n%1\n%2\n\n").arg(msg.GetSource(),msg.GetTranslation())); - } - context_string.append(QString("\n")); - - return context_string; -} - -Message* Context::GetMessagebySrc(const QString& src) -{ - uint i(0); - while(i < Message_list.size() && Message_list[i].GetSource() != src) - i++; - if (i < Message_list.size()) - return &Message_list[i]; -} - - - -Context* LightApp_msg_handling::GetContextbyName(const QString& name) -{ - int i(0); - while(i < Context_list->size() && (*Context_list)[i].GetContext_name() != name) - i++; - if (i < Context_list->size()) - return &(*Context_list)[i]; - else - { - qDebug()<<"Context with name " << name << " doesn't exist in LightApp_msg_handling"; - return nullptr; - } -} - -bool LightApp_msg_handling::HaveContext(const QString& name) -{ - int i(0); - while(i < Context_list->size() && (*Context_list)[i].GetContext_name() != name) - i++; - if (i < Context_list->size()) - return true; - else - return false; -} - -QString LightApp_msg_handling::write() -{ - QString updateXml; - updateXml.append(header); - for(auto ct:*Context_list) - updateXml.append(ct.ExportContext()); - updateXml.append(""); - - return updateXml; - -} -void LightApp_msg_handling::read(QIODevice *device) -{ - - //header - QTextStream textStream(device); - // Read 3 first header line of ts file - for (int i = 0; i < 3 && !textStream.atEnd(); ++i) { - header.append(textStream.readLine() + "\n"); - } - device->seek(0); - - //context - xmlReader.setDevice(device); - //CustomEntityResolver resolver; - //xmlReader.setEntityResolver(&resolver); - - while(xmlReader.readNextStartElement()){ - if(xmlReader.name().toString() == "context") - readContext(); - } - -} - -void LightApp_msg_handling::readContext() -{ - Q_ASSERT(xmlReader.isStartElement() && - xmlReader.name() == "context"); - - while(xmlReader.readNextStartElement()){ - if(xmlReader.name() == "name") - { - QString name = xmlReader.readElementText(); - if (!HaveContext(name)) - Context_list->append(Context(name)); - else - qWarning()<<"Context with name " << name << " is already appended in LightApp_msg_handling"; - } - else if (xmlReader.name() == "message") - { - readMessage(); - } - else - xmlReader.skipCurrentElement(); - } -} - -void LightApp_msg_handling::readMessage() -{ - Q_ASSERT(xmlReader.isStartElement() && - xmlReader.name() == "message"); - Message msg = Message("",""); - Message& msg_ref = msg; - while(xmlReader.readNextStartElement()){ - if(xmlReader.name() == "source") - readSrc(msg_ref); - else if(xmlReader.name() == "translation") - readTranslation(msg_ref); - else - xmlReader.skipCurrentElement(); - - } - - (*Context_list)[Context_list->size() - 1].AddMsg(msg); -} - -void LightApp_msg_handling::readSrc(Message& msg) -{ - Q_ASSERT(xmlReader.isStartElement() && - xmlReader.name() == "source"); - QString src = xmlReader.readElementText(); - - msg.Set_Source(src); -} - -void LightApp_msg_handling::readTranslation(Message& msg) -{ - Q_ASSERT(xmlReader.isStartElement() && - xmlReader.name() == "translation"); - - QString trl = xmlReader.readElementText(); - trl.replace("&","&"); - trl.replace("\"","""); - trl.replace("\'","'"); - trl.replace("<","<"); - trl.replace(">",">"); - msg.Set_Translation(trl); -} diff --git a/src/LightAppMsg_Customize/LightApp_msg.h b/src/LightAppMsg_Customize/LightApp_msg.h deleted file mode 100644 index c4b322f60..000000000 --- a/src/LightAppMsg_Customize/LightApp_msg.h +++ /dev/null @@ -1,69 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace LightApp_msg -{ - -class Message -{ - public: - Message(const QString& src, const QString& trl):source(src),translation(trl){}; - QString GetSource() const { return source ;} - QString GetTranslation() const { return translation ;} - void Set_Source(const QString& str) { source = str; } - void Set_Translation(const QString& str) { translation = str; } - private: - QString source; - QString translation; -}; - -class Context -{ - public: - Context(const QString& name):Context_name(name){}; - QString GetContext_name(){return Context_name;} - void AddMsg(Message& msg) {Message_list.append(msg);} - void AddMsg(const QString& src, const QString& trl) {Message_list.append(Message(src,trl));} - Message* GetMessagebySrc(const QString&); - QList& GetMessageList(){return Message_list;} - QString ExportContext(); - private: - QString Context_name; - QList Message_list; -}; - -class LightApp_msg_handling -{ -public: - LightApp_msg_handling(std::unique_ptr> Context_list): - Context_list(std::move(Context_list)){}; - void read(QIODevice *device); - QString write(); - Context* GetContextbyName(const QString &name); - bool HaveContext(const QString &name); -private: - QXmlStreamReader xmlReader; - std::unique_ptr> Context_list; - QString header; - - void readContext(); - void readMessage(); - void readSrc(Message& msg); - void readTranslation(Message& msg); -}; - -} - -class CustomEntityResolver : public QXmlStreamEntityResolver { -public: - QString resolveUndeclaredEntity(const QString &name); -}; \ No newline at end of file