Skip to content

Commit

Permalink
feat: allow to return ribbons for language
Browse files Browse the repository at this point in the history
  • Loading branch information
Montel committed Oct 22, 2024
1 parent f79960f commit 244ff73
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/API/knut/rcdocument.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)|
Expand Down Expand Up @@ -191,6 +192,10 @@ Previews the result of the conversion RC->UI

Returns the ribbon for the given `id`.

#### <a name="ribbon"></a>[Menu](../knut/menu.md) **ribbon**(string id, string language)

Returns the ribbon for the given `id` for specific `language`.

#### <a name="string"></a>string **string**(string id)

Returns the string for the given `id`.
Expand Down
18 changes: 18 additions & 0 deletions src/core/rcdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<RcCore::RcFile *>(&m_rcFile)->data[language];
if (auto ribbon = data.ribbon(id)) {
const_cast<RcCore::Ribbon *>(ribbon)->load();
return *ribbon;
}
}
return {};
}

QStringList RcDocument::dialogIds() const
{
LOG();
Expand Down
1 change: 1 addition & 0 deletions src/core/rcdocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 25 additions & 0 deletions src/rccore/ribbon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "ribbon.h"
#include "utils/log.h"
#include <kdalgorithms.h>

#include <pugixml.hpp>

Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/rccore/ribbon.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ struct Ribbon
QString fileName;

bool load();
Q_INVOKABLE RcCore::RibbonElement elementFromId(const QString &id) const;
};

} // namespace RcCore
Expand Down

0 comments on commit 244ff73

Please sign in to comment.