From: DUC ANH HOANG Date: Tue, 23 Apr 2024 13:49:12 +0000 (+0200) Subject: AppName_customize: initialize X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=2bb9253f964ac83389f81dd2ed240f5f55f77c8e;p=modules%2Fgui.git AppName_customize: initialize cmake for compilation of AppNameCustomize --- diff --git a/src/LightApp/AppNameCustomize.cxx b/src/LightApp/AppNameCustomize.cxx new file mode 100644 index 000000000..a73c2b496 --- /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 SALOME_APPLICATION_DIR + QByteArray gui_root_dir_env = qgetenv("SALOME_APPLICATION_DIR"); + if (gui_root_dir_env.isEmpty()) + { + qCritical()<< "SALOME_APPLICATION_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 7ea7c55ac..f37dfae1a 100644 --- a/src/LightApp/CMakeLists.txt +++ b/src/LightApp/CMakeLists.txt @@ -303,6 +303,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() @@ -350,3 +364,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); +}; + +}