Changeset View
Changeset View
Standalone View
Standalone View
kcms/keyboard/kcmui/keyboard_model_model.cpp
- This file was added.
1 | #include "keyboard_model_model.h" | ||||
---|---|---|---|---|---|
2 | | ||||
3 | #include <KLocalizedString> | ||||
4 | #include <QErrorMessage> | ||||
5 | #include <QDebug> | ||||
6 | | ||||
7 | #include <algorithm> | ||||
8 | | ||||
9 | KeyboardModelModel::KeyboardModelModel(XkbRules const& rules, QObject* parent) | ||||
10 | : QAbstractListModel(parent) | ||||
11 | { | ||||
12 | m_list.reserve(rules.modelInfos.count()); | ||||
13 | for (auto model_info : rules.modelInfos) { | ||||
14 | m_list.push_back({model_info->vendor, model_info->name, model_info->description}); | ||||
15 | } | ||||
16 | std::sort(m_list.begin(), m_list.end(), [](KeyboardModel const& a, KeyboardModel const& b) { | ||||
17 | return a.display_text() < b.display_text(); | ||||
18 | }); | ||||
19 | } | ||||
20 | | ||||
21 | int KeyboardModelModel::rowCount(const QModelIndex& /* parent */) const | ||||
22 | { | ||||
23 | return m_list.size(); | ||||
24 | } | ||||
25 | | ||||
26 | QVariant KeyboardModelModel::data(const QModelIndex& index, int role) const | ||||
27 | { | ||||
28 | auto model = m_list.at(index.row()); | ||||
29 | if (role == Roles::DescriptionRole) { | ||||
30 | return model.display_text(); | ||||
31 | } | ||||
32 | if (role == Roles::NameRole) { | ||||
33 | return model.name; | ||||
34 | } | ||||
35 | return QVariant(); | ||||
36 | } | ||||
37 | | ||||
38 | Qt::ItemFlags KeyboardModelModel::flags(const QModelIndex& /* index */) const | ||||
39 | { | ||||
40 | return Qt::ItemIsEnabled | Qt::ItemIsSelectable; | ||||
41 | } | ||||
42 | | ||||
43 | QHash<int, QByteArray> KeyboardModelModel::roleNames() const | ||||
44 | { | ||||
45 | return { { Roles::DescriptionRole, "description" }, { Roles::NameRole, "name" } }; | ||||
46 | } | ||||
47 | | ||||
48 | int KeyboardModelModel::findIndexByName(const QString &name) const | ||||
49 | { | ||||
50 | int index; | ||||
51 | for (index = 0; index < m_list.size(); ++index) { | ||||
52 | if (m_list[index].name == name) | ||||
53 | break; | ||||
54 | } | ||||
55 | if (index == m_list.size()) { | ||||
56 | qWarning() << i18nc("keyboard model", "Keyboard model \"%1\" not detected", name); | ||||
57 | return -1; | ||||
58 | } | ||||
59 | return index; | ||||
60 | } | ||||
61 | | ||||
62 | QString KeyboardModelModel::KeyboardModel::display_text() const | ||||
63 | { | ||||
64 | return i18nc("vendor | keyboard model", "%1 | %2", vendor, description); | ||||
65 | } |