Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to return ribbons for language #198

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -296,6 +296,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 @@ -80,6 +80,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
Loading