From 244ff73e40b042a4b471ba65030e8533bc86ddd3 Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Tue, 22 Oct 2024 15:18:38 +0200 Subject: [PATCH] feat: allow to return ribbons for language --- docs/API/knut/rcdocument.md | 5 +++++ src/core/rcdocument.cpp | 18 ++++++++++++++++++ src/core/rcdocument.h | 1 + src/rccore/ribbon.cpp | 25 +++++++++++++++++++++++++ src/rccore/ribbon.h | 1 + 5 files changed, 50 insertions(+) diff --git a/docs/API/knut/rcdocument.md b/docs/API/knut/rcdocument.md index 02747946..9a4ae170 100644 --- a/docs/API/knut/rcdocument.md +++ b/docs/API/knut/rcdocument.md @@ -41,6 +41,7 @@ import Knut |bool |**[mergeLanguages](#mergeLanguages)**()| |bool |**[previewDialog](#previewDialog)**([Widget](../knut/widget.md) dialog)| |[Menu](../knut/menu.md) |**[ribbon](#ribbon)**(string id)| +|[Menu](../knut/menu.md) |**[ribbon](#ribbon)**(string id, string language)| |string |**[string](#string)**(string id)| |string |**[stringForDialog](#stringForDialog)**(string dialogId, string id)| |string |**[stringForDialogAndLanguage](#stringForDialogAndLanguage)**(string language, string dialogId, string id)| @@ -191,6 +192,10 @@ Previews the result of the conversion RC->UI Returns the ribbon for the given `id`. +#### [Menu](../knut/menu.md) **ribbon**(string id, string language) + +Returns the ribbon for the given `id` for specific `language`. + #### string **string**(string id) Returns the string for the given `id`. diff --git a/src/core/rcdocument.cpp b/src/core/rcdocument.cpp index d7f31fe1..d72280c0 100644 --- a/src/core/rcdocument.cpp +++ b/src/core/rcdocument.cpp @@ -261,6 +261,24 @@ RcCore::Ribbon RcDocument::ribbon(const QString &id) const return {}; } +/*! + * \qmlmethod Menu RcDocument::ribbon(string id, string language) + * Returns the ribbon for the given `id` for specific `language`. + */ +RcCore::Ribbon RcDocument::ribbonForLanguage(const QString &id, const QString &language) const +{ + LOG(id); + + if (m_rcFile.isValid && m_rcFile.data.contains(language)) { + const RcCore::Data data = const_cast(&m_rcFile)->data[language]; + if (auto ribbon = data.ribbon(id)) { + const_cast(ribbon)->load(); + return *ribbon; + } + } + return {}; +} + QStringList RcDocument::dialogIds() const { LOG(); diff --git a/src/core/rcdocument.h b/src/core/rcdocument.h index 043e7171..b088477c 100644 --- a/src/core/rcdocument.h +++ b/src/core/rcdocument.h @@ -79,6 +79,7 @@ class RcDocument : public Document Q_INVOKABLE RcCore::Menu menu(const QString &id) const; Q_INVOKABLE RcCore::Ribbon ribbon(const QString &id) const; + Q_INVOKABLE RcCore::Ribbon ribbonForLanguage(const QString &id, const QString &language) const; QStringList dialogIds() const; QStringList menuIds() const; diff --git a/src/rccore/ribbon.cpp b/src/rccore/ribbon.cpp index ebea4469..281e7148 100644 --- a/src/rccore/ribbon.cpp +++ b/src/rccore/ribbon.cpp @@ -10,6 +10,7 @@ #include "ribbon.h" #include "utils/log.h" +#include #include @@ -264,6 +265,30 @@ bool Ribbon::load() return true; } +RibbonElement Ribbon::elementFromId(const QString &id) const +{ + const auto menuElements = menu.elements; + auto result = kdalgorithms::find_if(menuElements, [&id](const auto &item) { + return item.id == id; + }); + if (result) { + return *result; + } + + const auto categoriesElements = categories; + for (const auto &cat : categoriesElements) { + for (const auto &panel : cat.panels) { + auto result = kdalgorithms::find_if(panel.elements, [&id](const auto &item) { + return item.id == id; + }); + if (result) { + return *result; + } + } + } + return {}; +} + bool operator==(const RibbonElement &lhs, const RibbonElement &rhs) { return lhs.id == rhs.id; diff --git a/src/rccore/ribbon.h b/src/rccore/ribbon.h index e22849c0..a897da01 100644 --- a/src/rccore/ribbon.h +++ b/src/rccore/ribbon.h @@ -125,6 +125,7 @@ struct Ribbon QString fileName; bool load(); + Q_INVOKABLE RcCore::RibbonElement elementFromId(const QString &id) const; }; } // namespace RcCore