Changeset View
Changeset View
Standalone View
Standalone View
kcms/keyboard/kcmui/advanced_model.cpp
- This file was added.
1 | #include "advanced_model.h" | ||||
---|---|---|---|---|---|
2 | #include "../xkb_rules.h" | ||||
3 | #include <QDebug> | ||||
4 | | ||||
5 | AdvancedModel::AdvancedModel(QObject *parent) | ||||
6 | : QAbstractItemModel(parent) | ||||
7 | { | ||||
8 | } | ||||
9 | | ||||
10 | int AdvancedModel::rowCount(const QModelIndex &parent) const | ||||
11 | { | ||||
12 | if (!parent.isValid()) { // root | ||||
13 | return XkbRules::self()->optionGroupInfos.size(); | ||||
14 | } | ||||
15 | | ||||
16 | if (!parent.parent().isValid()) { // 2nd level | ||||
17 | return XkbRules::self()->optionGroupInfos[parent.row()]->optionInfos.size(); | ||||
18 | } | ||||
19 | | ||||
20 | return 0; | ||||
21 | } | ||||
22 | | ||||
23 | int AdvancedModel::columnCount(const QModelIndex &parent) const | ||||
24 | { | ||||
25 | Q_UNUSED(parent); | ||||
26 | return 1; | ||||
27 | } | ||||
28 | | ||||
29 | Qt::ItemFlags AdvancedModel::flags(const QModelIndex &index) const | ||||
30 | { | ||||
31 | if (!index.isValid()) { | ||||
32 | return Qt::NoItemFlags; | ||||
33 | } | ||||
34 | | ||||
35 | return QAbstractItemModel::flags(index); | ||||
36 | } | ||||
37 | | ||||
38 | QModelIndex AdvancedModel::index(int row, int column, const QModelIndex &parent) const | ||||
39 | { | ||||
40 | if (column > 0) return QModelIndex(); | ||||
41 | if (!parent.isValid()) { | ||||
42 | return createIndex(row, column, quintptr(-1)); | ||||
43 | } | ||||
44 | else if (!parent.parent().isValid()){ | ||||
45 | return createIndex(row, column, quintptr(parent.row())); | ||||
46 | } | ||||
47 | return QModelIndex(); | ||||
48 | } | ||||
49 | | ||||
50 | QModelIndex AdvancedModel::parent(const QModelIndex &index) const | ||||
51 | { | ||||
52 | if (!index.isValid()) { | ||||
53 | return QModelIndex(); | ||||
54 | } | ||||
55 | | ||||
56 | if (index.internalId() == quintptr(-1)) { // root | ||||
57 | return QModelIndex(); | ||||
58 | } | ||||
59 | | ||||
60 | return createIndex(static_cast<int>(index.internalId()), 0, quintptr(-1)); | ||||
61 | } | ||||
62 | | ||||
63 | QVariant AdvancedModel::data(const QModelIndex &index, int role) const | ||||
64 | { | ||||
65 | if (!index.isValid()) { | ||||
66 | return QVariant(); | ||||
67 | } | ||||
68 | | ||||
69 | if (!index.parent().isValid()) { | ||||
70 | auto const& infos = XkbRules::self()->optionGroupInfos[index.row()]; | ||||
71 | | ||||
72 | switch (role) { | ||||
73 | case Roles::DescriptionRole: | ||||
74 | return infos->description; | ||||
75 | case Roles::NameRole: | ||||
76 | case Roles::SectionNameRole: | ||||
77 | return infos->name; | ||||
78 | case Roles::SelectedRole: | ||||
79 | return false; | ||||
80 | case Roles::ExclusiveRole: | ||||
81 | return infos->exclusive; | ||||
82 | case Roles::IsGroupRole: | ||||
83 | return true; | ||||
84 | case Roles::SectionNamePlusIsGroupRole: | ||||
85 | return infos->name + "+true"; | ||||
86 | } | ||||
87 | } | ||||
88 | else if (!index.parent().parent().isValid()){ | ||||
89 | auto const& info = XkbRules::self()->optionGroupInfos[index.parent().row()]->optionInfos[index.row()]; | ||||
90 | | ||||
91 | switch (role) { | ||||
92 | case Roles::DescriptionRole: | ||||
93 | return info->description; | ||||
94 | case Roles::NameRole: | ||||
95 | return info->name; | ||||
96 | case Roles::SectionNameRole: | ||||
97 | return data(index.parent(), Roles::SectionNameRole); | ||||
98 | case Roles::SelectedRole: | ||||
99 | return m_enabledOptions.contains(info->name); | ||||
100 | case Roles::ExclusiveRole: | ||||
101 | return data(index.parent(), Roles::ExclusiveRole); | ||||
102 | case Roles::IsGroupRole: | ||||
103 | return false; | ||||
104 | case Roles::SectionNamePlusIsGroupRole: | ||||
105 | return data(index.parent(), Roles::SectionNameRole).toString() + "+false"; | ||||
106 | } | ||||
107 | } | ||||
108 | | ||||
109 | return QVariant(); | ||||
110 | } | ||||
111 | | ||||
112 | bool AdvancedModel::setData(const QModelIndex &idx, const QVariant &value, int role) | ||||
113 | { | ||||
114 | if (role == Roles::SelectedRole) { | ||||
115 | QString optionName = data(idx, Roles::NameRole).toString(); | ||||
116 | bool exclusive = data(idx, Roles::ExclusiveRole).toBool(); | ||||
117 | | ||||
118 | m_enabledOptions.removeAll(optionName); | ||||
119 | if (exclusive) { | ||||
120 | for (int i = 0; i < m_enabledOptions.size(); ++i) { | ||||
121 | QString name = m_enabledOptions[i]; | ||||
122 | if (name.split(":")[0] == optionName.split(":")[0]) { | ||||
123 | m_enabledOptions.removeAt(i); | ||||
124 | | ||||
125 | for (int j = 0; j < rowCount(idx.parent()); ++j) { | ||||
126 | QModelIndex otherIdx = index(j, 0, idx.parent()); | ||||
127 | if (data(otherIdx, Roles::NameRole).toString() == name) { | ||||
128 | emit dataChanged(otherIdx, otherIdx, { Roles::SelectedRole }); | ||||
129 | } | ||||
130 | } | ||||
131 | | ||||
132 | } | ||||
133 | } | ||||
134 | } | ||||
135 | | ||||
136 | if (value.toBool()) { | ||||
137 | m_enabledOptions << optionName; | ||||
138 | } | ||||
139 | | ||||
140 | emit dataChanged(idx, idx, { Roles::SelectedRole }); | ||||
141 | qDebug() << m_enabledOptions; | ||||
142 | return true; | ||||
143 | } | ||||
144 | return false; | ||||
145 | } | ||||
146 | | ||||
147 | QStringList AdvancedModel::enabledOptions() const | ||||
148 | { | ||||
149 | return m_enabledOptions; | ||||
150 | } | ||||
151 | | ||||
152 | void AdvancedModel::setEnabledOptions(const QStringList &enabledOptions) | ||||
153 | { | ||||
154 | m_enabledOptions = enabledOptions; | ||||
155 | } |