diff --git a/kcm/CMakeLists.txt b/kcm/CMakeLists.txt index 117017f..7cdeeda 100644 --- a/kcm/CMakeLists.txt +++ b/kcm/CMakeLists.txt @@ -10,6 +10,8 @@ kconfig_add_kcfg_files(kcm_plasmakeyboard GENERATE_MOC plasmakeyboardsettings.kc target_sources(kcm_plasmakeyboard PRIVATE ../virtualkeyboard/keyboardlayoutmetadata.cpp ../virtualkeyboard/keyboardlayoutmetadata.h + enabledkeyboardlayoutmodel.cpp + enabledkeyboardlayoutmodel.h keyboardlayoutgroupmodel.cpp keyboardlayoutgroupmodel.h keyboardlayoutmodel.cpp diff --git a/kcm/enabledkeyboardlayoutmodel.cpp b/kcm/enabledkeyboardlayoutmodel.cpp new file mode 100644 index 0000000..82df54d --- /dev/null +++ b/kcm/enabledkeyboardlayoutmodel.cpp @@ -0,0 +1,89 @@ +/* + SPDX-FileCopyrightText: 2026 Devin Lin + + SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL +*/ + +#include "enabledkeyboardlayoutmodel.h" + +#include "keyboardlayoutmetadata.h" + +EnabledKeyboardLayoutModel::EnabledKeyboardLayoutModel(QObject *parent) + : QAbstractListModel(parent) +{ + for (const auto &entry : KeyboardLayoutMetadata::keyboardLayouts()) { + m_installedLayouts.insert(entry.layoutId, + { + .layoutId = entry.layoutId, + .name = KeyboardLayoutModel::displayName(entry), + .description = KeyboardLayoutModel::descriptionForLocales(entry.locales), + .locales = entry.locales, + }); + } +} + +int EnabledKeyboardLayoutModel::rowCount(const QModelIndex &parent) const +{ + if (parent.isValid()) { + return 0; + } + return m_layoutIds.size(); +} + +QVariant EnabledKeyboardLayoutModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid() || index.row() < 0 || index.row() >= m_layoutIds.size()) { + return {}; + } + + QString layoutId = m_layoutIds.at(index.row()); + const auto it = m_installedLayouts.constFind(layoutId); + const bool installed = it != m_installedLayouts.constEnd(); + + switch (role) { + case LayoutIdRole: + return layoutId; + case NameRole: + return installed ? it->name : layoutId; + default: + return {}; + } +} + +QHash EnabledKeyboardLayoutModel::roleNames() const +{ + return { + {LayoutIdRole, "layoutId"}, + {NameRole, "name"}, + }; +} + +QStringList EnabledKeyboardLayoutModel::layoutIds() const +{ + return m_layoutIds; +} + +void EnabledKeyboardLayoutModel::setLayoutIds(const QStringList &layoutIds) +{ + if (m_layoutIds == layoutIds) { + return; + } + + beginResetModel(); + m_layoutIds = layoutIds; + endResetModel(); +} + +bool EnabledKeyboardLayoutModel::move(int fromRow, int destRow) +{ + if (fromRow == destRow || fromRow < 0 || destRow < 0 || fromRow >= m_layoutIds.size() || destRow >= m_layoutIds.size()) { + return false; + } + + if (!beginMoveRows({}, fromRow, fromRow, {}, destRow > fromRow ? destRow + 1 : destRow)) { + return false; + } + m_layoutIds.move(fromRow, destRow); + endMoveRows(); + return true; +} diff --git a/kcm/enabledkeyboardlayoutmodel.h b/kcm/enabledkeyboardlayoutmodel.h new file mode 100644 index 0000000..75362f2 --- /dev/null +++ b/kcm/enabledkeyboardlayoutmodel.h @@ -0,0 +1,42 @@ +/* + SPDX-FileCopyrightText: 2026 Devin Lin + + SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL +*/ + +#pragma once + +#include "keyboardlayoutmodel.h" + +#include +#include +#include + +/** + * Model to list the enabled keyboard layouts that are shown to the user. + */ +class EnabledKeyboardLayoutModel : public QAbstractListModel +{ + Q_OBJECT + +public: + enum Role { + LayoutIdRole = Qt::UserRole + 1, + NameRole, + }; + Q_ENUM(Role) + + explicit EnabledKeyboardLayoutModel(QObject *parent = nullptr); + + int rowCount(const QModelIndex &parent = {}) const override; + QVariant data(const QModelIndex &index, int role) const override; + QHash roleNames() const override; + + QStringList layoutIds() const; + void setLayoutIds(const QStringList &layoutIds); + bool move(int fromRow, int destRow); + +private: + QHash m_installedLayouts; + QStringList m_layoutIds; +}; diff --git a/kcm/keyboardlayoutgroupmodel.cpp b/kcm/keyboardlayoutgroupmodel.cpp index 3a0c3f4..88be015 100644 --- a/kcm/keyboardlayoutgroupmodel.cpp +++ b/kcm/keyboardlayoutgroupmodel.cpp @@ -143,8 +143,7 @@ void KeyboardLayoutGroupModel::loadInstalledGroups() } const QString layoutName = KeyboardLayoutMetadata::keyboardLayoutName(entry.layoutDefinition); - const QString keyboardName = KeyboardLayoutMetadata::keyboardDisplayName(entry); - const QString displayName = layoutName.isEmpty() ? keyboardName : keyboardName + QStringLiteral(" - ") + layoutName; + const QString displayName = KeyboardLayoutModel::displayName(entry); flatLayouts.append({ .layoutId = entry.layoutId, diff --git a/kcm/keyboardlayoutmodel.cpp b/kcm/keyboardlayoutmodel.cpp index 136506e..7d35749 100644 --- a/kcm/keyboardlayoutmodel.cpp +++ b/kcm/keyboardlayoutmodel.cpp @@ -140,6 +140,13 @@ bool KeyboardLayoutModel::matchesFilter(const Layout &layout, const QString &fil return filterText.isEmpty() || layout.name.toLower().contains(filterText) || layout.description.toLower().contains(filterText); } +QString KeyboardLayoutModel::displayName(const KeyboardLayoutMetadata::Layout &entry) +{ + const QString layoutName = KeyboardLayoutMetadata::keyboardLayoutName(entry.layoutDefinition); + const QString keyboardName = KeyboardLayoutMetadata::keyboardDisplayName(entry); + return layoutName.isEmpty() ? keyboardName : keyboardName + QStringLiteral(" - ") + layoutName; +} + QString KeyboardLayoutModel::descriptionForLocales(const QStringList &locales) { QStringList names; diff --git a/kcm/keyboardlayoutmodel.h b/kcm/keyboardlayoutmodel.h index 9f1e59f..269f0f3 100644 --- a/kcm/keyboardlayoutmodel.h +++ b/kcm/keyboardlayoutmodel.h @@ -6,6 +6,8 @@ #pragma once +#include "keyboardlayoutmetadata.h" + #include #include @@ -45,6 +47,7 @@ public: int enabledVisibleLayoutCount() const; QString layoutForLocale(const QString &locale) const; void setLayouts(QList layouts); + static QString displayName(const KeyboardLayoutMetadata::Layout &entry); static QString descriptionForLocales(const QStringList &locales); static bool matchesFilter(const Layout &layout, const QString &filterText); diff --git a/kcm/plasmakeyboardkcm.cpp b/kcm/plasmakeyboardkcm.cpp index c2fc0bd..bf35503 100644 --- a/kcm/plasmakeyboardkcm.cpp +++ b/kcm/plasmakeyboardkcm.cpp @@ -18,6 +18,7 @@ K_PLUGIN_CLASS_WITH_JSON(PlasmaKeyboardKcm, "kcm_plasmakeyboard.json") PlasmaKeyboardKcm::PlasmaKeyboardKcm(QObject *parent, const KPluginMetaData &metaData) : KQuickManagedConfigModule(parent, metaData) , m_settings(new PlasmaKeyboardSettings(this)) + , m_enabledKeyboardLayouts(new EnabledKeyboardLayoutModel(this)) , m_availableKeyboardLayoutGroups(new KeyboardLayoutGroupModel(this)) { qmlRegisterAnonymousType("org.kde.plasma.keyboard.settings", 0); @@ -45,9 +46,11 @@ PlasmaKeyboardKcm::PlasmaKeyboardKcm(QObject *parent, const KPluginMetaData &met m_settings->setEnabledLocales({}); } } + m_enabledKeyboardLayouts->setLayoutIds(enabledKeyboardLayoutIds); m_availableKeyboardLayoutGroups->setEnabledLayoutIds(enabledKeyboardLayoutIds); connect(m_settings, &PlasmaKeyboardSettings::enabledKeyboardLayoutIdsChanged, this, [this]() { + m_enabledKeyboardLayouts->setLayoutIds(m_settings->enabledKeyboardLayoutIds()); m_availableKeyboardLayoutGroups->setEnabledLayoutIds(m_settings->enabledKeyboardLayoutIds()); }); } @@ -57,6 +60,11 @@ PlasmaKeyboardSettings *PlasmaKeyboardKcm::plasmaKeyboardSettings() const return m_settings; } +QAbstractItemModel *PlasmaKeyboardKcm::enabledKeyboardLayouts() const +{ + return m_enabledKeyboardLayouts; +} + QAbstractItemModel *PlasmaKeyboardKcm::availableKeyboardLayouts() const { return m_availableKeyboardLayoutGroups->layouts(); @@ -109,6 +117,15 @@ void PlasmaKeyboardKcm::disableKeyboardLayout(const QString &layoutId) m_settings->setEnabledKeyboardLayoutIds(enabledKeyboardLayoutIds); } +void PlasmaKeyboardKcm::moveKeyboardLayout(int from, int to) +{ + if (!m_enabledKeyboardLayouts->move(from, to)) { + return; + } + + m_settings->setEnabledKeyboardLayoutIds(m_enabledKeyboardLayouts->layoutIds()); +} + void PlasmaKeyboardKcm::refreshAvailableKeyboardLayoutFilter() { m_availableKeyboardLayoutGroups->setFormFactorFilter(keyboardLayoutFormFactorFilterEnabled() ? m_keyboardLayoutFormFactorFilter : QStringList{}); diff --git a/kcm/plasmakeyboardkcm.h b/kcm/plasmakeyboardkcm.h index 8208a33..73efcd7 100644 --- a/kcm/plasmakeyboardkcm.h +++ b/kcm/plasmakeyboardkcm.h @@ -9,6 +9,7 @@ #include +#include "enabledkeyboardlayoutmodel.h" #include "keyboardlayoutgroupmodel.h" #include "plasmakeyboardsettings.h" @@ -18,6 +19,7 @@ class PlasmaKeyboardKcm : public KQuickManagedConfigModule { Q_OBJECT Q_PROPERTY(PlasmaKeyboardSettings *plasmaKeyboardSettings READ plasmaKeyboardSettings CONSTANT) + Q_PROPERTY(QAbstractItemModel *enabledKeyboardLayouts READ enabledKeyboardLayouts CONSTANT) Q_PROPERTY(QAbstractItemModel *availableKeyboardLayouts READ availableKeyboardLayouts CONSTANT) Q_PROPERTY(QAbstractItemModel *availableKeyboardLayoutGroups READ availableKeyboardLayoutGroups CONSTANT) Q_PROPERTY(bool hasHiddenKeyboardLayouts READ hasHiddenKeyboardLayouts CONSTANT) @@ -28,6 +30,7 @@ public: PlasmaKeyboardKcm(QObject *parent, const KPluginMetaData &metaData); PlasmaKeyboardSettings *plasmaKeyboardSettings() const; + QAbstractItemModel *enabledKeyboardLayouts() const; QAbstractItemModel *availableKeyboardLayouts() const; QAbstractItemModel *availableKeyboardLayoutGroups() const; bool hasHiddenKeyboardLayouts() const; @@ -37,6 +40,7 @@ public: Q_INVOKABLE void enableKeyboardLayout(const QString &layoutId); Q_INVOKABLE void disableKeyboardLayout(const QString &layoutId); + Q_INVOKABLE void moveKeyboardLayout(int from, int to); Q_SIGNALS: void keyboardLayoutFormFactorFilterEnabledChanged(); @@ -45,6 +49,7 @@ private: void refreshAvailableKeyboardLayoutFilter(); PlasmaKeyboardSettings *m_settings = nullptr; + EnabledKeyboardLayoutModel *m_enabledKeyboardLayouts = nullptr; KeyboardLayoutGroupModel *m_availableKeyboardLayoutGroups = nullptr; QStringList m_keyboardLayoutFormFactorFilter; bool m_hasHiddenKeyboardLayouts = false; diff --git a/kcm/ui/AddKeyboardLayoutPage.qml b/kcm/ui/AddKeyboardLayoutPage.qml new file mode 100644 index 0000000..fd3107f --- /dev/null +++ b/kcm/ui/AddKeyboardLayoutPage.qml @@ -0,0 +1,13 @@ +/* + SPDX-FileCopyrightText: 2026 Devin Lin + + SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL +*/ + +import org.kde.kirigami as Kirigami + +Kirigami.ScrollablePage { + title: i18n("Add Layout") + + KeyboardLayoutSelectorListView {} +} diff --git a/kcm/ui/KeyboardLayoutSelectorListView.qml b/kcm/ui/KeyboardLayoutSelectorListView.qml index 89610e3..4b23440 100644 --- a/kcm/ui/KeyboardLayoutSelectorListView.qml +++ b/kcm/ui/KeyboardLayoutSelectorListView.qml @@ -59,14 +59,6 @@ ListView { checked = Qt.binding(() => !kcm.keyboardLayoutFormFactorFilterEnabled); } } - - Kirigami.InlineMessage { - Layout.topMargin: Kirigami.Units.smallSpacing - Layout.fillWidth: true - text: i18n("No keyboard layouts selected. The default keyboard layout for the system will be used.") - type: Kirigami.MessageType.Information - visible: kcm.plasmaKeyboardSettings.enabledKeyboardLayoutIds.length === 0 - } } } diff --git a/kcm/ui/KeyboardLayoutsPage.qml b/kcm/ui/KeyboardLayoutsPage.qml new file mode 100644 index 0000000..fa32ae5 --- /dev/null +++ b/kcm/ui/KeyboardLayoutsPage.qml @@ -0,0 +1,102 @@ +/* + SPDX-FileCopyrightText: 2026 Devin Lin + + SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL +*/ + +import QtQuick +import QtQuick.Controls as QQC2 +import QtQuick.Layouts + +import org.kde.kirigami as Kirigami + +Kirigami.ScrollablePage { + id: root + + title: i18n("On-Screen Keyboard Layouts") + + actions: [ + Kirigami.Action { + id: addLayoutAction + text: i18n("Add Layout…") + icon.name: "list-add-symbolic" + onTriggered: kcm.push("AddKeyboardLayoutPage.qml") + } + ] + + ListView { + id: layoutList + + model: kcm.enabledKeyboardLayouts + + Kirigami.PlaceholderMessage { + anchors.centerIn: parent + width: parent.width - Kirigami.Units.gridUnit * 4 + visible: layoutList.count === 0 + icon.name: "input-keyboard-virtual" + text: i18n("No keyboard layouts selected") + explanation: i18n("The default keyboard layout for the system will be used.") + helpfulAction: addLayoutAction + } + + delegate: QQC2.ItemDelegate { + id: layoutDelegate + + required property int index + required property string layoutId + required property string name + + width: layoutList.width + + down: false + highlighted: false + hoverEnabled: false + + contentItem: RowLayout { + spacing: Kirigami.Units.smallSpacing + + Kirigami.TitleSubtitle { + Layout.fillWidth: true + title: layoutDelegate.name + } + + QQC2.ToolButton { + icon.name: "arrow-up-symbolic" + text: i18nc("Move the layout up a row", "Move Up") + display: QQC2.AbstractButton.IconOnly + visible: layoutList.count > 1 + enabled: layoutDelegate.index > 0 + onClicked: kcm.moveKeyboardLayout(layoutDelegate.index, layoutDelegate.index - 1) + + QQC2.ToolTip.text: text + QQC2.ToolTip.visible: hovered + QQC2.ToolTip.delay: Kirigami.Units.toolTipDelay + } + + QQC2.ToolButton { + icon.name: "arrow-down-symbolic" + text: i18nc("Move the layout down a row", "Move Down") + display: QQC2.AbstractButton.IconOnly + visible: layoutList.count > 1 + enabled: layoutDelegate.index < layoutList.count - 1 + onClicked: kcm.moveKeyboardLayout(layoutDelegate.index, layoutDelegate.index + 1) + + QQC2.ToolTip.text: text + QQC2.ToolTip.visible: hovered + QQC2.ToolTip.delay: Kirigami.Units.toolTipDelay + } + + QQC2.ToolButton { + icon.name: "list-remove-symbolic" + text: i18nc("Remove the layout", "Remove") + display: QQC2.AbstractButton.IconOnly + onClicked: kcm.disableKeyboardLayout(layoutDelegate.layoutId) + + QQC2.ToolTip.text: text + QQC2.ToolTip.visible: hovered + QQC2.ToolTip.delay: Kirigami.Units.toolTipDelay + } + } + } + } +} diff --git a/kcm/ui/main.qml b/kcm/ui/main.qml index d48724f..4995636 100644 --- a/kcm/ui/main.qml +++ b/kcm/ui/main.qml @@ -24,14 +24,7 @@ KCM.SimpleKCM { Kirigami.FormData.label: i18n("Keyboard layouts:") text: i18n("Configure…") icon.name: "languages" - onClicked: kcm.push(layoutPage); - - Kirigami.ScrollablePage { - id: layoutPage - title: i18n("Keyboard Layouts") - - KeyboardLayoutSelectorListView {} - } + onClicked: kcm.push("KeyboardLayoutsPage.qml") } QQC2.CheckBox { diff --git a/kcm/ui/main_handset.qml b/kcm/ui/main_handset.qml index 52d9999..dbb82c1 100644 --- a/kcm/ui/main_handset.qml +++ b/kcm/ui/main_handset.qml @@ -74,14 +74,7 @@ KCM.SimpleKCM { id: layoutList text: i18n("Keyboard Layouts") icon.name: 'languages' - onClicked: kcm.push(layoutPage) - - Kirigami.ScrollablePage { - id: layoutPage - title: i18n("Keyboard Layouts") - - KeyboardLayoutSelectorListView {} - } + onClicked: kcm.push("KeyboardLayoutsPage.qml") } }