1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include <QtVirtualKeyboard/qvirtualkeyboarddictionarymanager.h>
5#include <QtVirtualKeyboard/qvirtualkeyboarddictionary.h>
6#include <QtGlobal>
7#include <QSet>
8
9#include <QtCore/private/qobject_p.h>
10
11QT_BEGIN_NAMESPACE
12
13class QVirtualKeyboardDictionaryManagerPrivate : public QObjectPrivate
14{
15 Q_DECLARE_PUBLIC(QVirtualKeyboardDictionaryManager)
16public:
17 QVirtualKeyboardDictionaryManagerPrivate(QVirtualKeyboardDictionaryManager *q)
18 {
19 q_ptr = q;
20 }
21
22 void updateActiveDictionaries()
23 {
24 Q_Q(QVirtualKeyboardDictionaryManager);
25 QSet<QString> newActiveDictionaries(baseDictionaries);
26 newActiveDictionaries.unite(other: extraDictionaries);
27 if (activeDictionaries != newActiveDictionaries) {
28 activeDictionaries = newActiveDictionaries;
29 emit q->activeDictionariesChanged();
30 }
31 }
32
33 QSet<QString> baseDictionaries;
34 QSet<QString> extraDictionaries;
35 QSet<QString> activeDictionaries;
36 QMap<QString, QVirtualKeyboardDictionary *> dictionaries;
37};
38
39struct QVirtualKeyboardDictionaryManagerSingleton {
40 QVirtualKeyboardDictionaryManager instance;
41};
42
43Q_GLOBAL_STATIC(QVirtualKeyboardDictionaryManagerSingleton, singleton)
44
45/*!
46 \class QVirtualKeyboardDictionaryManager
47
48 \inmodule QtVirtualKeyboard
49 \ingroup qtvirtualkeyboard-cpp-for-apps
50
51 \brief Dictionary management for application-defined dictionaries.
52
53 This class allows you to manage dictionaries for virtual keyboard input
54 methods. Dictionaries are application-defined and can be activated in the
55 desired context.
56
57 \note Dictionaries do not refer to the built-in dictionaries of the input
58 method, but to the application's own dictionaries (for example, a phonebook
59 application).
60
61 The following operations are supported by the dictionary manager:
62 \list
63 \li Create dictionaries using the \c createDictionary() function.
64 \li Add or modify content of the dictionary instance.
65 \li Activate dictionaries by adding them to the list of
66 \l {baseDictionaries}{base dictionaries} or
67 \l {extraDictionaries}{extra dictionaries}.
68 \endlist
69
70 To activate dictionaries from QML, use the \l VirtualKeyboard attached
71 type.
72*/
73
74QVirtualKeyboardDictionaryManager::QVirtualKeyboardDictionaryManager(QObject *parent) :
75 QObject(*new QVirtualKeyboardDictionaryManagerPrivate(this), parent)
76{
77 Q_D(QVirtualKeyboardDictionaryManager);
78 connect(sender: this, signal: &QVirtualKeyboardDictionaryManager::baseDictionariesChanged,
79 slot: [=](){ d->updateActiveDictionaries(); });
80 connect(sender: this, signal: &QVirtualKeyboardDictionaryManager::extraDictionariesChanged,
81 slot: [=](){ d->updateActiveDictionaries(); });
82}
83
84/*!
85 Returns the singleton instance of the QVirtualKeyboardDictionaryManager.
86
87 All the functionalities must be called from the main thread.
88 */
89
90QVirtualKeyboardDictionaryManager *QVirtualKeyboardDictionaryManager::instance()
91{
92 return &singleton->instance;
93}
94
95QStringList QVirtualKeyboardDictionaryManager::availableDictionaries() const
96{
97 Q_D(const QVirtualKeyboardDictionaryManager);
98 return d->dictionaries.keys();
99}
100
101QStringList QVirtualKeyboardDictionaryManager::baseDictionaries() const
102{
103 Q_D(const QVirtualKeyboardDictionaryManager);
104 return d->baseDictionaries.values();
105}
106
107void QVirtualKeyboardDictionaryManager::setBaseDictionaries(const QStringList &baseDictionaries)
108{
109 Q_D(QVirtualKeyboardDictionaryManager);
110 QSet<QString> newBaseDictionaries(baseDictionaries.begin(), baseDictionaries.end());
111 const QStringList availableDict = availableDictionaries();
112 newBaseDictionaries.intersect(other: QSet<QString>(availableDict.begin(), availableDict.end()));
113 if (d->baseDictionaries != newBaseDictionaries) {
114 d->baseDictionaries = newBaseDictionaries;
115 emit baseDictionariesChanged();
116 }
117}
118
119QStringList QVirtualKeyboardDictionaryManager::extraDictionaries() const
120{
121 Q_D(const QVirtualKeyboardDictionaryManager);
122 return d->extraDictionaries.values();
123}
124
125void QVirtualKeyboardDictionaryManager::setExtraDictionaries(const QStringList &extraDictionaries)
126{
127 Q_D(QVirtualKeyboardDictionaryManager);
128 QSet<QString> newExtraDictionaries(extraDictionaries.begin(), extraDictionaries.end());
129 const QStringList availableDict = availableDictionaries();
130 newExtraDictionaries.intersect(other: QSet<QString>(availableDict.begin(), availableDict.end()));
131 if (d->extraDictionaries != newExtraDictionaries) {
132 d->extraDictionaries = newExtraDictionaries;
133 emit extraDictionariesChanged();
134 }
135}
136
137QStringList QVirtualKeyboardDictionaryManager::activeDictionaries() const
138{
139 Q_D(const QVirtualKeyboardDictionaryManager);
140 return d->activeDictionaries.values();
141}
142
143/*!
144 Create a dictionary with the given \a name or return the dictionary if it
145 already exists.
146
147 The dictionary instance is owned by the dictionary manager and must not be
148 destroyed manually. To free memory allocated for the dictionary, the
149 application may reset the \l{QVirtualKeyboardDictionary::contents}{contents}
150 of the dictionary.
151
152 \sa dictionary()
153 */
154
155QVirtualKeyboardDictionary *QVirtualKeyboardDictionaryManager::createDictionary(const QString &name)
156{
157 Q_D(QVirtualKeyboardDictionaryManager);
158
159 QVirtualKeyboardDictionary *dictionary = d->dictionaries.value(key: name);
160 if (!dictionary) {
161 dictionary = new QVirtualKeyboardDictionary(name, this);
162 d->dictionaries[name] = dictionary;
163 emit availableDictionariesChanged();
164 }
165
166 return dictionary;
167}
168
169/*!
170 Returns an existing dictionary with the given \a name if it exists, otherwise returns
171 \nullptr.
172
173 \sa createDictionary()
174 */
175
176QVirtualKeyboardDictionary *QVirtualKeyboardDictionaryManager::dictionary(const QString &name) const
177{
178 Q_D(const QVirtualKeyboardDictionaryManager);
179
180 return d->dictionaries.value(key: name);
181}
182
183/*!
184 \property QVirtualKeyboardDictionaryManager::availableDictionaries
185 \brief the list of all dictionaries.
186
187 This property holds the names of all existing dictionaries.
188*/
189
190/*!
191 \property QVirtualKeyboardDictionaryManager::baseDictionaries
192 \brief the list of currently active base dictionaries.
193
194 This property holds the names of base dictionaries. The application manages
195 this property.
196*/
197
198/*!
199 \property QVirtualKeyboardDictionaryManager::extraDictionaries
200 \brief the list of currently active extra dictionaries.
201
202 This property holds the names of extra dictionaries. The application manages
203 this property.
204*/
205
206/*!
207 \property QVirtualKeyboardDictionaryManager::activeDictionaries
208 \brief the list of currently active dictionaries.
209
210 This property holds the list of active dictionaries, which is a combination
211 of unique names included in baseDictionaries and extraDictionaries.
212*/
213
214QT_END_NAMESPACE
215

source code of qtvirtualkeyboard/src/virtualkeyboard/qvirtualkeyboarddictionarymanager.cpp