Commit d6438a01 authored by Andrey Butirsky's avatar Andrey Butirsky
Browse files

refactor: keyboard layout applet: return to Properties-based QML API

Return "layout" and "layoutsList" Properties where all layouts names are
now stored.
getLayoutLongName() DBus method is not used any more so it was also
deleted from DBus API in KWin and P-D, see corresponding commits there.

The Properties interface allows QML code be more declarative.
There is a trade-off though as now we have to:
- store all layout names on client side, even if it's not needed for
base QML layout component here
- duplicate layouts in layoutsList Property as they are also stored in
Action objects of the plasmoid in P-D.
parent 9c885b97
...@@ -12,20 +12,11 @@ ...@@ -12,20 +12,11 @@
template<> template<>
inline void KeyboardLayout::requestDBusData<KeyboardLayout::Layout>() inline void KeyboardLayout::requestDBusData<KeyboardLayout::Layout>()
{ if (mIface) requestDBusData(mIface->getLayout(), &KeyboardLayout::layoutChanged); } { if (mIface) requestDBusData(mIface->getLayout(), mLayout, &KeyboardLayout::layoutChanged); }
template<> template<>
inline void KeyboardLayout::requestDBusData<KeyboardLayout::Layouts>() inline void KeyboardLayout::requestDBusData<KeyboardLayout::LayoutsList>()
{ if (mIface) requestDBusData(mIface->getLayoutsList(), &KeyboardLayout::layoutsChanged); } { if (mIface) requestDBusData(mIface->getLayoutsList(), mLayoutsList, &KeyboardLayout::layoutsListChanged); }
template<>
inline void KeyboardLayout::requestDBusData<KeyboardLayout::LayoutLongName>()
{ if (mIface) requestDBusData(mIface->getLayoutLongName(), &KeyboardLayout::layoutLongNameChanged); }
void KeyboardLayout::requestLayoutLongName()
{
requestDBusData<LayoutLongName>();
}
KeyboardLayout::KeyboardLayout(QObject* parent) KeyboardLayout::KeyboardLayout(QObject* parent)
...@@ -45,12 +36,16 @@ KeyboardLayout::KeyboardLayout(QObject* parent) ...@@ -45,12 +36,16 @@ KeyboardLayout::KeyboardLayout(QObject* parent)
} }
connect(mIface, &OrgKdeKeyboardLayoutsInterface::layoutChanged, connect(mIface, &OrgKdeKeyboardLayoutsInterface::layoutChanged,
this, &KeyboardLayout::layoutChanged); this, [this](uint index)
{
mLayout = index;
emit layoutChanged();
});
connect(mIface, &OrgKdeKeyboardLayoutsInterface::layoutListChanged, connect(mIface, &OrgKdeKeyboardLayoutsInterface::layoutListChanged,
this, [this]() this, [this]()
{ {
requestDBusData<Layouts>(); requestDBusData<LayoutsList>();
requestDBusData<Layout>(); requestDBusData<Layout>();
}); });
...@@ -77,18 +72,18 @@ void KeyboardLayout::setLayout(uint index) ...@@ -77,18 +72,18 @@ void KeyboardLayout::setLayout(uint index)
} }
template<class T> template<class T>
void KeyboardLayout::requestDBusData(QDBusPendingReply<T> pendingReply, void (KeyboardLayout::*notify)(T)) void KeyboardLayout::requestDBusData(QDBusPendingReply<T> pendingReply, T &out, void (KeyboardLayout::*notify)())
{ {
const QDBusPendingCallWatcher * const watcher = new QDBusPendingCallWatcher(pendingReply, this); connect(new QDBusPendingCallWatcher(pendingReply, this), &QDBusPendingCallWatcher::finished, this,
connect(watcher, &QDBusPendingCallWatcher::finished, this, [this, &out, notify](QDBusPendingCallWatcher *watcher)
[this, notify](QDBusPendingCallWatcher *watcher)
{ {
QDBusPendingReply<T> reply = *watcher; QDBusPendingReply<T> reply = *watcher;
if (reply.isError()) { if (reply.isError()) {
qCWarning(KEYBOARD_LAYOUT) << reply.error().message(); qCWarning(KEYBOARD_LAYOUT) << reply.error().message();
} else {
emit (this->*notify)(reply.value());
} }
out = reply.value();
emit (this->*notify)();
watcher->deleteLater(); watcher->deleteLater();
} }
); );
......
...@@ -18,31 +18,40 @@ class KeyboardLayout : public QObject ...@@ -18,31 +18,40 @@ class KeyboardLayout : public QObject
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(uint layout
MEMBER mLayout
WRITE setLayout
NOTIFY layoutChanged)
Q_PROPERTY(const QVector<LayoutNames>& layoutsList
READ getLayoutsList
NOTIFY layoutsListChanged)
public: public:
explicit KeyboardLayout(QObject *parent = nullptr); explicit KeyboardLayout(QObject *parent = nullptr);
~KeyboardLayout() override; ~KeyboardLayout() override;
Q_SIGNALS: Q_SIGNALS:
void layoutChanged(uint index); void layoutChanged();
void layoutsChanged(QVector<LayoutNames> layouts); void layoutsListChanged();
void layoutLongNameChanged(QString longName);
protected: protected:
Q_INVOKABLE void switchToNextLayout(); Q_INVOKABLE void switchToNextLayout();
Q_INVOKABLE void switchToPreviousLayout(); Q_INVOKABLE void switchToPreviousLayout();
Q_INVOKABLE void setLayout(uint index);
Q_INVOKABLE void requestLayoutLongName();
private: private:
enum DBusData {Layout, LayoutLongName, Layouts}; void setLayout(uint index);
const QVector<LayoutNames> &getLayoutsList() const { return mLayoutsList; }
enum DBusData {Layout, LayoutsList};
template<class T> template<class T>
void requestDBusData(QDBusPendingReply<T> pendingReply, void (KeyboardLayout::*notify)(T)); void requestDBusData(QDBusPendingReply<T> pendingReply, T &out, void (KeyboardLayout::*notify)());
template<DBusData> template<DBusData>
void requestDBusData(); void requestDBusData();
uint mLayout;
QVector<LayoutNames> mLayoutsList;
OrgKdeKeyboardLayoutsInterface *mIface; OrgKdeKeyboardLayoutsInterface *mIface;
}; };
......
...@@ -18,9 +18,6 @@ ...@@ -18,9 +18,6 @@
<method name="getLayout"> <method name="getLayout">
<arg type="u" direction="out"/> <arg type="u" direction="out"/>
</method> </method>
<method name="getLayoutLongName">
<arg type="s" direction="out"/>
</method>
<method name="getLayoutsList"> <method name="getLayoutsList">
<arg type="a(sss)" direction="out"/> <arg type="a(sss)" direction="out"/>
<annotation name="org.qtproject.QtDBus.QtTypeName.Out0" value="QVector&lt;LayoutNames&gt;" /> <annotation name="org.qtproject.QtDBus.QtTypeName.Out0" value="QVector&lt;LayoutNames&gt;" />
......
...@@ -10,9 +10,10 @@ import org.kde.plasma.workspace.keyboardlayout 1.0 ...@@ -10,9 +10,10 @@ import org.kde.plasma.workspace.keyboardlayout 1.0
PlasmaComponents3.ToolButton { PlasmaComponents3.ToolButton {
property alias keyboardLayout: keyboardLayout property alias keyboardLayout: keyboardLayout
property alias connections: connections readonly property bool hasMultipleKeyboardLayouts: keyboardLayout.layoutsList.length > 1
property bool hasMultipleKeyboardLayouts readonly property var layoutNames: keyboardLayout.layoutsList[keyboardLayout.layout]
text: layoutNames.longName
visible: hasMultipleKeyboardLayouts visible: hasMultipleKeyboardLayouts
Accessible.name: i18ndc("plasma_lookandfeel_org.kde.lookandfeel", "Button to change keyboard layout", "Switch layout") Accessible.name: i18ndc("plasma_lookandfeel_org.kde.lookandfeel", "Button to change keyboard layout", "Switch layout")
...@@ -34,20 +35,5 @@ PlasmaComponents3.ToolButton { ...@@ -34,20 +35,5 @@ PlasmaComponents3.ToolButton {
KeyboardLayout { KeyboardLayout {
id: keyboardLayout id: keyboardLayout
onLayoutsChanged: hasMultipleKeyboardLayouts = layouts.length > 1
}
Connections {
id: connections
target: keyboardLayout
function onLayoutChanged() {
keyboardLayout.requestLayoutLongName()
}
function onLayoutLongNameChanged(longName) {
text = longName
}
} }
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment