Changeset View
Changeset View
Standalone View
Standalone View
kcms/keyboard/kcmui/kcm_keyboard.cpp
- This file was added.
1 | #include "kcm_keyboard.h" | ||||
---|---|---|---|---|---|
2 | | ||||
3 | #include <KAboutData> | ||||
4 | #include <KConfigGroup> | ||||
5 | #include <KGlobalAccel> | ||||
6 | #include <KLocalizedString> | ||||
7 | #include <KPluginFactory> | ||||
8 | #include <KSharedConfig> | ||||
9 | #include <QApplication> | ||||
10 | #include <QDBusConnection> | ||||
11 | #include <QDBusMessage> | ||||
12 | #include <QQuickItem> | ||||
13 | #include <QString> | ||||
14 | | ||||
15 | #include "hardware_config_model.h" | ||||
16 | #include "layout_config_model.h" | ||||
17 | #include "layout_list_xkb_expand_proxy_model.h" | ||||
18 | #include "advanced_model.h" | ||||
19 | #include "advanced_filter_proxy_model.h" | ||||
20 | #include "advanced_flatten_proxy_model.h" | ||||
21 | | ||||
22 | #include "../keyboard_dbus.h" | ||||
23 | | ||||
24 | K_PLUGIN_FACTORY_WITH_JSON(KeyboardModuleFactory, | ||||
25 | "kcm_keyboard.json", | ||||
26 | registerPlugin<KcmKeyboard>();) | ||||
27 | | ||||
28 | KcmKeyboard::KcmKeyboard(QObject* parent, const QVariantList& args) | ||||
29 | : KQuickAddons::ConfigModule(parent, args) | ||||
30 | , m_hardwareConfigModel(new HardwareConfigModel(this)) | ||||
31 | , m_layoutConfigModel(new LayoutConfigModel(this)) | ||||
32 | , m_advancedModel(new AdvancedFlattenProxyModel(this)) | ||||
33 | , m_underlyingAdvancedModel(new AdvancedModel(this)) | ||||
34 | { | ||||
35 | AdvancedFilterProxyModel *filter = new AdvancedFilterProxyModel(this); | ||||
36 | filter->setSourceModel(m_underlyingAdvancedModel); | ||||
37 | m_advancedModel->setSourceModel(filter); | ||||
38 | | ||||
39 | KAboutData* about = new KAboutData( | ||||
40 | "kcm_keyboard", i18n("..."), | ||||
41 | QStringLiteral("1.0"), QString(), KAboutLicense::GPL); | ||||
42 | | ||||
43 | about->addAuthor(i18n("Andriy Rysin")); | ||||
44 | about->addAuthor(i18n("Park Gun")); | ||||
45 | setAboutData(about); | ||||
46 | setButtons(Help | Apply | Default); | ||||
47 | | ||||
48 | m_actionCollection = new KActionCollection(this, QStringLiteral("KDE Keyboard Layout Switcher")); | ||||
49 | m_nextLayoutAction = m_actionCollection->addAction(QStringLiteral("Switch to Next Keyboard Layout")); | ||||
50 | m_nextLayoutAction->setText(i18n("Switch to Next Keyboard Layout")); | ||||
51 | m_nextLayoutAction->setProperty("isConfigurationAction", true); | ||||
52 | | ||||
53 | QObject::connect(mainUi(), SIGNAL(changed()), this, SLOT(changed())); | ||||
54 | } | ||||
55 | | ||||
56 | KcmKeyboard::~KcmKeyboard() | ||||
57 | { | ||||
58 | } | ||||
59 | | ||||
60 | void KcmKeyboard::load() | ||||
61 | { | ||||
62 | { | ||||
63 | KConfigGroup kxkbrc( | ||||
64 | KSharedConfig::openConfig(QStringLiteral("kxkbrc"), KConfig::NoGlobals), | ||||
65 | QStringLiteral("Layout")); | ||||
66 | | ||||
67 | m_hardwareConfigModel->setKeyboardModel(kxkbrc.readEntry<QString>("Model", "pc104")); | ||||
68 | | ||||
69 | m_layoutConfigModel->loadEnabledLayouts(); | ||||
70 | | ||||
71 | m_layoutConfigModel->setShowLayoutIndicator(kxkbrc.readEntry<bool>("ShowLayoutIndicator", true)); | ||||
72 | m_layoutConfigModel->setShowForSingleLayout(kxkbrc.readEntry<bool>("ShowSingle", false)); | ||||
73 | | ||||
74 | m_layoutConfigModel->setLayoutIndicatorShowFlag(kxkbrc.readEntry<bool>("ShowFlag", false)); | ||||
75 | m_layoutConfigModel->setLayoutIndicatorShowLabel(kxkbrc.readEntry<bool>("ShowLabel", true)); | ||||
76 | | ||||
77 | QString switching_policy = kxkbrc.readEntry<QString>("SwitchMode", "Global"); | ||||
78 | | ||||
79 | auto it = std::find( | ||||
80 | std::begin(LayoutConfigModel::switch_modes), | ||||
81 | std::end(LayoutConfigModel::switch_modes), | ||||
82 | switching_policy); | ||||
83 | if (it != std::end(LayoutConfigModel::switch_modes)) { | ||||
84 | m_layoutConfigModel->setSwitchingPolicyIndex( | ||||
85 | static_cast<int>(it - std::begin(LayoutConfigModel::switch_modes))); | ||||
86 | } else { | ||||
87 | qWarning() << "invalid switching policy " << switching_policy; | ||||
88 | } | ||||
89 | | ||||
90 | QStringList options = kxkbrc.readEntry<QString>("Options", "").split(","); | ||||
91 | QStringList miscOptions; | ||||
92 | for (QString const& option : options) { | ||||
93 | QString groupName = option.split(":")[0]; | ||||
94 | | ||||
95 | if (groupName == "grp") { | ||||
96 | m_layoutConfigModel->setMainShiftKey(option); | ||||
97 | } | ||||
98 | else if (groupName == "lv3") { | ||||
99 | m_layoutConfigModel->setThirdLevelShortcut(option); | ||||
100 | } | ||||
101 | else { | ||||
102 | miscOptions << option; | ||||
103 | } | ||||
104 | } | ||||
105 | | ||||
106 | m_underlyingAdvancedModel->setEnabledOptions(miscOptions); | ||||
107 | } | ||||
108 | | ||||
109 | // Alternative Shortcut | ||||
110 | KGlobalAccel::self()->setShortcut( | ||||
111 | m_nextLayoutAction, | ||||
112 | QList<QKeySequence>() << QKeySequence(Qt::ALT + Qt::CTRL + Qt::Key_K), | ||||
113 | KGlobalAccel::Autoloading); | ||||
114 | QList<QKeySequence> shortcuts = KGlobalAccel::self()->shortcut(m_nextLayoutAction); | ||||
115 | QKeySequence shortcut = shortcuts.isEmpty() ? QKeySequence() : shortcuts.first(); | ||||
116 | m_layoutConfigModel->setAlternativeShortcut(shortcut.toString()); | ||||
117 | | ||||
118 | { | ||||
119 | KConfigGroup kcminputrc( | ||||
120 | KSharedConfig::openConfig(QStringLiteral("kcminputrc")), | ||||
121 | QStringLiteral("Keyboard")); | ||||
122 | | ||||
123 | m_hardwareConfigModel->setNumlockOnStartup( | ||||
124 | kcminputrc.readEntry<int>("NumLock", static_cast<int>(TriState::UNCHANGED))); | ||||
125 | m_hardwareConfigModel->setKeyboardRepeat( | ||||
126 | kcminputrc.readEntry<int>("KeyboardRepeating", static_cast<int>(TriState::UNCHANGED))); | ||||
127 | m_hardwareConfigModel->setRepeatDelay( | ||||
128 | kcminputrc.readEntry<int>("RepeatDelay", 600)); | ||||
129 | m_hardwareConfigModel->setRepeatRate( | ||||
130 | kcminputrc.readEntry<double>("RepeatRate", 25.0)); | ||||
131 | } | ||||
132 | } | ||||
133 | | ||||
134 | void KcmKeyboard::save() | ||||
135 | { | ||||
136 | { | ||||
137 | KConfigGroup kxkbrc( | ||||
138 | KSharedConfig::openConfig(QStringLiteral("kxkbrc"), KConfig::NoGlobals), | ||||
139 | QStringLiteral("Layout")); | ||||
140 | | ||||
141 | kxkbrc.writeEntry<QString>("Model", m_hardwareConfigModel->keyboardModel()); | ||||
142 | | ||||
143 | kxkbrc.writeEntry<bool>("ShowLayoutIndicator", m_layoutConfigModel->showLayoutIndicator()); | ||||
144 | | ||||
145 | kxkbrc.writeEntry<bool>("ShowSingle", m_layoutConfigModel->showForSingleLayout()); | ||||
146 | | ||||
147 | kxkbrc.writeEntry<bool>("ShowFlag", m_layoutConfigModel->layoutIndicatorShowFlag()); | ||||
148 | kxkbrc.writeEntry<bool>("ShowLabel", m_layoutConfigModel->layoutIndicatorShowLabel()); | ||||
149 | | ||||
150 | if (m_layoutConfigModel->switchingPolicyIndex() >= 0 && m_layoutConfigModel->switchingPolicyIndex() < std::end(LayoutConfigModel::switch_modes) - std::begin(LayoutConfigModel::switch_modes)) { | ||||
151 | kxkbrc.writeEntry<QString>("SwitchMode", LayoutConfigModel::switch_modes[m_layoutConfigModel->switchingPolicyIndex()]); | ||||
152 | } else { | ||||
153 | qWarning() << "invalid switching policy index" << m_layoutConfigModel->switchingPolicyIndex(); | ||||
154 | } | ||||
155 | | ||||
156 | QStringList options = m_underlyingAdvancedModel->enabledOptions(); | ||||
157 | if (!m_layoutConfigModel->mainShiftKey().isEmpty()) { | ||||
158 | options << m_layoutConfigModel->mainShiftKey(); | ||||
159 | } | ||||
160 | if (!m_layoutConfigModel->thirdLevelShortcut().isEmpty()) { | ||||
161 | options << m_layoutConfigModel->thirdLevelShortcut(); | ||||
162 | } | ||||
163 | kxkbrc.writeEntry<QString>("Options", options.join(",")); | ||||
164 | | ||||
165 | m_layoutConfigModel->saveEnabledLayouts(); | ||||
166 | | ||||
167 | kxkbrc.sync(); | ||||
168 | } | ||||
169 | | ||||
170 | KGlobalAccel::self()->setShortcut( | ||||
171 | m_nextLayoutAction, | ||||
172 | QList<QKeySequence>() << QKeySequence(m_layoutConfigModel->alternativeShortcut()), | ||||
173 | KGlobalAccel::NoAutoloading); | ||||
174 | | ||||
175 | { | ||||
176 | KConfigGroup kcminputrc( | ||||
177 | KSharedConfig::openConfig(QStringLiteral("kcminputrc")), | ||||
178 | QStringLiteral("Keyboard")); | ||||
179 | | ||||
180 | kcminputrc.writeEntry<int>("NumLock", static_cast<int>(m_hardwareConfigModel->numlockOnStartup())); | ||||
181 | kcminputrc.writeEntry<int>("KeyboardRepeating", static_cast<int>(m_hardwareConfigModel->keyboardRepeat())); | ||||
182 | kcminputrc.writeEntry<int>("RepeatDelay", m_hardwareConfigModel->repeatDelay()); | ||||
183 | kcminputrc.writeEntry<double>("RepeatRate", m_hardwareConfigModel->repeatRate()); | ||||
184 | | ||||
185 | kcminputrc.sync(); | ||||
186 | } | ||||
187 | | ||||
188 | // dbus call to the kded (in X11) / kwin (in Wayland) to apply the config changes | ||||
189 | QDBusMessage message = QDBusMessage::createSignal( | ||||
190 | KEYBOARD_DBUS_OBJECT_PATH, | ||||
191 | KEYBOARD_DBUS_SERVICE_NAME, | ||||
192 | KEYBOARD_DBUS_CONFIG_RELOAD_MESSAGE); | ||||
193 | QDBusConnection::sessionBus().send(message); | ||||
194 | } | ||||
195 | | ||||
196 | void KcmKeyboard::defaults() | ||||
197 | { | ||||
198 | m_hardwareConfigModel->setKeyboardModel(QStringLiteral("pc104")); | ||||
199 | | ||||
200 | m_hardwareConfigModel->setNumlockOnStartup(static_cast<int>(TriState::UNCHANGED)); | ||||
201 | m_hardwareConfigModel->setKeyboardRepeat(static_cast<int>(TriState::UNCHANGED)); | ||||
202 | m_hardwareConfigModel->setRepeatDelay(600); | ||||
203 | m_hardwareConfigModel->setRepeatRate(25.0); | ||||
204 | | ||||
205 | m_layoutConfigModel->setShowLayoutIndicator(true); | ||||
206 | m_layoutConfigModel->setShowForSingleLayout(false); | ||||
207 | m_layoutConfigModel->setLayoutIndicatorShowFlag(false); | ||||
208 | m_layoutConfigModel->setLayoutIndicatorShowLabel(true); | ||||
209 | m_layoutConfigModel->setSwitchingPolicyIndex(0); | ||||
210 | m_layoutConfigModel->setAlternativeShortcut( | ||||
211 | QKeySequence(Qt::ALT + Qt::CTRL + Qt::Key_K).toString()); | ||||
212 | | ||||
213 | changed(); | ||||
214 | } | ||||
215 | | ||||
216 | void KcmKeyboard::changed() | ||||
217 | { | ||||
218 | setNeedsSave(true); | ||||
219 | emit needsSaveChanged(); | ||||
220 | } | ||||
221 | | ||||
222 | HardwareConfigModel* KcmKeyboard::hardwareModel() | ||||
223 | { | ||||
224 | return m_hardwareConfigModel; | ||||
225 | } | ||||
226 | | ||||
227 | LayoutConfigModel* KcmKeyboard::layoutModel() | ||||
228 | { | ||||
229 | return m_layoutConfigModel; | ||||
230 | } | ||||
231 | | ||||
232 | AdvancedFlattenProxyModel *KcmKeyboard::advancedModel() | ||||
233 | { | ||||
234 | return m_advancedModel; | ||||
235 | } | ||||
236 | | ||||
237 | #include <kcm_keyboard.moc> |