| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <QtVirtualKeyboard/private/settings_p.h> |
| 5 | #include <QtCore/private/qobject_p.h> |
| 6 | #include <QStandardPaths> |
| 7 | #include <QFileInfo> |
| 8 | #include <QDir> |
| 9 | #include "virtualkeyboarddebug_p.h" |
| 10 | #ifdef QT_VIRTUALKEYBOARD_SOUNDS_ENABLED |
| 11 | #include <QtMultimedia/qaudio.h> |
| 12 | #endif |
| 13 | |
| 14 | QT_BEGIN_NAMESPACE |
| 15 | namespace QtVirtualKeyboard { |
| 16 | |
| 17 | class SettingsPrivate : public QObjectPrivate |
| 18 | { |
| 19 | public: |
| 20 | SettingsPrivate() : |
| 21 | QObjectPrivate(), |
| 22 | style(), |
| 23 | styleName(), |
| 24 | locale(), |
| 25 | availableLocales(), |
| 26 | activeLocales(), |
| 27 | layoutPath(), |
| 28 | wclAutoHideDelay(5000), |
| 29 | wclAlwaysVisible(false), |
| 30 | wclAutoCommitWord(false), |
| 31 | fullScreenMode(false), |
| 32 | userDataPath(QStringLiteral("%1/qtvirtualkeyboard" ) |
| 33 | .arg(a: QStandardPaths::writableLocation( |
| 34 | type: QStandardPaths::GenericConfigLocation))), |
| 35 | hwrTimeoutForAlphabetic(500), |
| 36 | hwrTimeoutForCjk(500), |
| 37 | handwritingModeDisabled(false), |
| 38 | defaultInputMethodDisabled(false), |
| 39 | defaultDictionaryDisabled(false), |
| 40 | visibleFunctionKeys(QtVirtualKeyboard::KeyboardFunctionKey::All), |
| 41 | closeOnReturn(false), |
| 42 | keySoundVolume(1.0) |
| 43 | { |
| 44 | ensureUserDataPathExists(); |
| 45 | } |
| 46 | |
| 47 | void ensureUserDataPathExists() const |
| 48 | { |
| 49 | if (!userDataPath.isEmpty() && !QFileInfo::exists(file: userDataPath)) { |
| 50 | if (!QDir::root().mkpath(dirPath: userDataPath)) { |
| 51 | VIRTUALKEYBOARD_WARN() << "Cannot create directory for user data" << userDataPath; |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | QString style; |
| 57 | QString styleName; |
| 58 | QString locale; |
| 59 | QStringList availableLocales; |
| 60 | QStringList activeLocales; |
| 61 | QUrl layoutPath; |
| 62 | int wclAutoHideDelay; |
| 63 | bool wclAlwaysVisible; |
| 64 | bool wclAutoCommitWord; |
| 65 | bool fullScreenMode; |
| 66 | QString userDataPath; |
| 67 | int hwrTimeoutForAlphabetic; |
| 68 | int hwrTimeoutForCjk; |
| 69 | Qt::InputMethodHints inputMethodHints; |
| 70 | bool handwritingModeDisabled; |
| 71 | bool defaultInputMethodDisabled; |
| 72 | bool defaultDictionaryDisabled; |
| 73 | QtVirtualKeyboard::KeyboardFunctionKeys visibleFunctionKeys; |
| 74 | bool closeOnReturn; |
| 75 | qreal keySoundVolume; |
| 76 | }; |
| 77 | |
| 78 | static QScopedPointer<Settings> s_settingsInstance; |
| 79 | |
| 80 | /*! |
| 81 | \class QtVirtualKeyboard::Settings |
| 82 | \internal |
| 83 | */ |
| 84 | |
| 85 | Settings::Settings(QObject *parent) : |
| 86 | QObject(*new SettingsPrivate(), parent) |
| 87 | { |
| 88 | } |
| 89 | |
| 90 | Settings *Settings::instance() |
| 91 | { |
| 92 | if (!s_settingsInstance) |
| 93 | s_settingsInstance.reset(other: new Settings()); |
| 94 | return s_settingsInstance.data(); |
| 95 | } |
| 96 | |
| 97 | QString Settings::style() const |
| 98 | { |
| 99 | Q_D(const Settings); |
| 100 | return d->style; |
| 101 | } |
| 102 | |
| 103 | void Settings::setStyle(const QString &style) |
| 104 | { |
| 105 | Q_D(Settings); |
| 106 | if (d->style != style) { |
| 107 | d->style = style; |
| 108 | emit styleChanged(); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | QString Settings::styleName() const |
| 113 | { |
| 114 | Q_D(const Settings); |
| 115 | return d->styleName; |
| 116 | } |
| 117 | |
| 118 | void Settings::setStyleName(const QString &styleName) |
| 119 | { |
| 120 | Q_D(Settings); |
| 121 | if (d->styleName != styleName) { |
| 122 | d->styleName = styleName; |
| 123 | emit styleNameChanged(); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | QString Settings::locale() const |
| 128 | { |
| 129 | Q_D(const Settings); |
| 130 | return d->locale; |
| 131 | } |
| 132 | |
| 133 | void Settings::setLocale(const QString &locale) |
| 134 | { |
| 135 | Q_D(Settings); |
| 136 | if (d->locale != locale) { |
| 137 | d->locale = locale; |
| 138 | emit localeChanged(); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | QStringList Settings::availableLocales() const |
| 143 | { |
| 144 | Q_D(const Settings); |
| 145 | return d->availableLocales; |
| 146 | } |
| 147 | |
| 148 | void Settings::setAvailableLocales(const QStringList &availableLocales) |
| 149 | { |
| 150 | Q_D(Settings); |
| 151 | if (d->availableLocales != availableLocales) { |
| 152 | d->availableLocales = availableLocales; |
| 153 | emit availableLocalesChanged(); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | QStringList Settings::activeLocales() const |
| 158 | { |
| 159 | Q_D(const Settings); |
| 160 | return d->activeLocales; |
| 161 | } |
| 162 | |
| 163 | void Settings::setActiveLocales(const QStringList &activeLocales) |
| 164 | { |
| 165 | Q_D(Settings); |
| 166 | if (d->activeLocales != activeLocales) { |
| 167 | d->activeLocales = activeLocales; |
| 168 | emit activeLocalesChanged(); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | QUrl Settings::layoutPath() const |
| 173 | { |
| 174 | Q_D(const Settings); |
| 175 | return d->layoutPath; |
| 176 | } |
| 177 | |
| 178 | void Settings::setLayoutPath(const QUrl &layoutPath) |
| 179 | { |
| 180 | Q_D(Settings); |
| 181 | if (d->layoutPath != layoutPath) { |
| 182 | d->layoutPath = layoutPath; |
| 183 | emit layoutPathChanged(); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | int Settings::wclAutoHideDelay() const |
| 188 | { |
| 189 | Q_D(const Settings); |
| 190 | return d->wclAutoHideDelay; |
| 191 | } |
| 192 | |
| 193 | void Settings::setWclAutoHideDelay(int wclAutoHideDelay) |
| 194 | { |
| 195 | Q_D(Settings); |
| 196 | if (d->wclAutoHideDelay != wclAutoHideDelay) { |
| 197 | d->wclAutoHideDelay = wclAutoHideDelay; |
| 198 | emit wclAutoHideDelayChanged(); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | bool Settings::wclAlwaysVisible() const |
| 203 | { |
| 204 | Q_D(const Settings); |
| 205 | return d->wclAlwaysVisible; |
| 206 | } |
| 207 | |
| 208 | void Settings::setWclAlwaysVisible(bool wclAlwaysVisible) |
| 209 | { |
| 210 | Q_D(Settings); |
| 211 | if (d->wclAlwaysVisible != wclAlwaysVisible) { |
| 212 | d->wclAlwaysVisible = wclAlwaysVisible; |
| 213 | emit wclAlwaysVisibleChanged(); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | bool Settings::wclAutoCommitWord() const |
| 218 | { |
| 219 | Q_D(const Settings); |
| 220 | return d->wclAutoCommitWord; |
| 221 | } |
| 222 | |
| 223 | void Settings::setWclAutoCommitWord(bool wclAutoCommitWord) |
| 224 | { |
| 225 | Q_D(Settings); |
| 226 | if (d->wclAutoCommitWord != wclAutoCommitWord) { |
| 227 | d->wclAutoCommitWord = wclAutoCommitWord; |
| 228 | emit wclAutoCommitWordChanged(); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | bool Settings::fullScreenMode() const |
| 233 | { |
| 234 | Q_D(const Settings); |
| 235 | return d->fullScreenMode; |
| 236 | } |
| 237 | |
| 238 | void Settings::setFullScreenMode(bool fullScreenMode) |
| 239 | { |
| 240 | Q_D(Settings); |
| 241 | if (d->fullScreenMode != fullScreenMode) { |
| 242 | d->fullScreenMode = fullScreenMode; |
| 243 | emit fullScreenModeChanged(); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | QString Settings::userDataPath() const |
| 248 | { |
| 249 | Q_D(const Settings); |
| 250 | return d->userDataPath; |
| 251 | } |
| 252 | |
| 253 | void Settings::setUserDataPath(const QString &userDataPath) |
| 254 | { |
| 255 | Q_D(Settings); |
| 256 | if (d->userDataPath != userDataPath) { |
| 257 | d->userDataPath = userDataPath; |
| 258 | d->ensureUserDataPathExists(); |
| 259 | emit userDataPathChanged(); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | int Settings::hwrTimeoutForAlphabetic() const |
| 264 | { |
| 265 | Q_D(const Settings); |
| 266 | return d->hwrTimeoutForAlphabetic; |
| 267 | } |
| 268 | |
| 269 | void Settings::setHwrTimeoutForAlphabetic(int hwrTimeoutForAlphabetic) |
| 270 | { |
| 271 | Q_D(Settings); |
| 272 | if (d->hwrTimeoutForAlphabetic != hwrTimeoutForAlphabetic) { |
| 273 | d->hwrTimeoutForAlphabetic = hwrTimeoutForAlphabetic; |
| 274 | emit hwrTimeoutForAlphabeticChanged(); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | int Settings::hwrTimeoutForCjk() const |
| 279 | { |
| 280 | Q_D(const Settings); |
| 281 | return d->hwrTimeoutForCjk; |
| 282 | } |
| 283 | |
| 284 | void Settings::setHwrTimeoutForCjk(int hwrTimeoutForCjk) |
| 285 | { |
| 286 | Q_D(Settings); |
| 287 | if (d->hwrTimeoutForCjk != hwrTimeoutForCjk) { |
| 288 | d->hwrTimeoutForCjk = hwrTimeoutForCjk; |
| 289 | emit hwrTimeoutForCjkChanged(); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | Qt::InputMethodHints Settings::inputMethodHints() const |
| 294 | { |
| 295 | Q_D(const Settings); |
| 296 | return d->inputMethodHints; |
| 297 | } |
| 298 | |
| 299 | void Settings::setInputMethodHints(const Qt::InputMethodHints &inputMethodHints) |
| 300 | { |
| 301 | Q_D(Settings); |
| 302 | if (d->inputMethodHints != inputMethodHints) { |
| 303 | d->inputMethodHints = inputMethodHints; |
| 304 | emit inputMethodHintsChanged(); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | bool Settings::isHandwritingModeDisabled() const |
| 309 | { |
| 310 | Q_D(const Settings); |
| 311 | return d->handwritingModeDisabled; |
| 312 | } |
| 313 | |
| 314 | void Settings::setHandwritingModeDisabled(bool handwritingModeDisabled) |
| 315 | { |
| 316 | Q_D(Settings); |
| 317 | if (d->handwritingModeDisabled != handwritingModeDisabled) { |
| 318 | d->handwritingModeDisabled = handwritingModeDisabled; |
| 319 | emit handwritingModeDisabledChanged(); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | bool Settings::isDefaultInputMethodDisabled() const |
| 324 | { |
| 325 | Q_D(const Settings); |
| 326 | return d->defaultInputMethodDisabled; |
| 327 | } |
| 328 | |
| 329 | void Settings::setDefaultInputMethodDisabled(bool defaultInputMethodDisabled) |
| 330 | { |
| 331 | Q_D(Settings); |
| 332 | if (d->defaultInputMethodDisabled != defaultInputMethodDisabled) { |
| 333 | d->defaultInputMethodDisabled = defaultInputMethodDisabled; |
| 334 | emit defaultInputMethodDisabledChanged(); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | bool QtVirtualKeyboard::Settings::isDefaultDictionaryDisabled() const |
| 339 | { |
| 340 | Q_D(const Settings); |
| 341 | return d->defaultDictionaryDisabled; |
| 342 | } |
| 343 | |
| 344 | void QtVirtualKeyboard::Settings::setDefaultDictionaryDisabled(bool defaultDictionaryDisabled) |
| 345 | { |
| 346 | Q_D(Settings); |
| 347 | if (d->defaultDictionaryDisabled != defaultDictionaryDisabled) { |
| 348 | d->defaultDictionaryDisabled = defaultDictionaryDisabled; |
| 349 | emit defaultDictionaryDisabledChanged(); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | QtVirtualKeyboard::KeyboardFunctionKeys Settings::visibleFunctionKeys() const |
| 354 | { |
| 355 | Q_D(const Settings); |
| 356 | return d->visibleFunctionKeys; |
| 357 | } |
| 358 | |
| 359 | void Settings::setVisibleFunctionKeys(QtVirtualKeyboard::KeyboardFunctionKeys newVisibleFunctionKeys) |
| 360 | { |
| 361 | Q_D(Settings); |
| 362 | if (d->visibleFunctionKeys != newVisibleFunctionKeys) { |
| 363 | d->visibleFunctionKeys = newVisibleFunctionKeys; |
| 364 | emit visibleFunctionKeysChanged(); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | bool Settings::closeOnReturn() const |
| 369 | { |
| 370 | Q_D(const Settings); |
| 371 | return d->closeOnReturn; |
| 372 | } |
| 373 | |
| 374 | void Settings::setCloseOnReturn(bool enabled) |
| 375 | { |
| 376 | Q_D(Settings); |
| 377 | if (d->closeOnReturn != enabled) { |
| 378 | d->closeOnReturn = enabled; |
| 379 | emit closeOnReturnChanged(); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | qreal Settings::keySoundVolume() const |
| 384 | { |
| 385 | Q_D(const Settings); |
| 386 | return d->keySoundVolume; |
| 387 | } |
| 388 | |
| 389 | void Settings::setKeySoundVolume(qreal volume) |
| 390 | { |
| 391 | Q_D(Settings); |
| 392 | qreal volumeBounded = qBound(min: 0.0, val: volume, max: 1.0); |
| 393 | if (d->keySoundVolume != volumeBounded) { |
| 394 | d->keySoundVolume = volumeBounded; |
| 395 | emit keySoundVolumeChanged(); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | } // namespace QtVirtualKeyboard |
| 400 | QT_END_NAMESPACE |
| 401 | |