aboutsummaryrefslogtreecommitdiffstats
path: root/src/virtualkeyboard/declarativeselectionlistmodel.cpp
blob: a124d1c1c8d3d97d201f5cd1da21339bd58c89f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc
** All rights reserved.
** For any questions to Digia, please use contact form at http://www.qt.io
**
** This file is part of the Qt Virtual Keyboard add-on for Qt Enterprise.
**
** Licensees holding valid Qt Enterprise licenses may use this file in
** accordance with the Qt Enterprise License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.
**
** If you have questions regarding the use of this file, please use
** contact form at http://www.qt.io
**
****************************************************************************/

#include "declarativeselectionlistmodel.h"
#include "abstractinputmethod.h"
#include <QtCore/private/qabstractitemmodel_p.h>

class DeclarativeSelectionListModelPrivate : public QAbstractItemModelPrivate
{
public:
    DeclarativeSelectionListModelPrivate() :
        QAbstractItemModelPrivate(),
        dataSource(0),
        type(DeclarativeSelectionListModel::WordCandidateList),
        rowCount(0)
    {
    }

    QHash<int, QByteArray> roles;
    AbstractInputMethod *dataSource;
    DeclarativeSelectionListModel::Type type;
    int rowCount;
};

/*!
    \qmltype SelectionListModel
    \instantiates DeclarativeSelectionListModel
    \inqmlmodule QtQuick.Enterprise.VirtualKeyboard
    \ingroup qtvirtualkeyboard-qml
    \brief Provides a data model for the selection lists.

    The SelectionListModel is a data model for word candidates
    provided by the input method.

    An instance of SelectionListModel cannot be created directly.
    Instead, the InputEngine manages the instances and provides
    access to the model by InputEngine::wordCandidateListModel
    property.

    The model exposes the following data roles for the list delegate:
    \list
        \li \c display Display text for item
        \li \c wordCompletionLength Word completion length for item
    \endlist

    The activeItemChanged signal indicates which item is currently
    highlighted by the input method. The view should respond to this
    signal by highlighting the corresponding item in the list.

    The user selection is handled by the selectItem() method. The view
    should be invoke this method when the user selects an item from the
    list.
*/

/*!
    \class DeclarativeSelectionListModel

    \inmodule InputFramework

    \brief List model for selection lists.

    This class acts as a bridge between the UI and the
    input method that provides the data for selection
    lists.
*/

/*!
    \enum DeclarativeSelectionListModel::Type

    This enum specifies the type of selection list.

    \value WordCandidateList
           Shows list of word candidates
*/

/*!
    \enum DeclarativeSelectionListModel::Role

    This enum specifies a role of the data requested.

    \value DisplayRole
           The data to be rendered in form of text.
    \value WordCompletionLengthRole
           An integer specifying the length of the word
           the completion part expressed as the
           number of characters counted from the
           end of the string.
*/

DeclarativeSelectionListModel::DeclarativeSelectionListModel(QObject *parent) :
    QAbstractListModel(*new DeclarativeSelectionListModelPrivate(), parent)
{
    Q_D(DeclarativeSelectionListModel);
    d->roles[DisplayRole] = "display";
    d->roles[WordCompletionLengthRole] = "wordCompletionLength";
}

/*!
    \internal
*/
DeclarativeSelectionListModel::~DeclarativeSelectionListModel()
{
}

/*!
    \internal
*/
void DeclarativeSelectionListModel::setDataSource(AbstractInputMethod *dataSource, Type type)
{
    Q_D(DeclarativeSelectionListModel);
    if (d->dataSource) {
        disconnect(this, SLOT(selectionListChanged(int)));
        disconnect(this, SLOT(selectionListActiveItemChanged(int, int)));
    }
    d->type = type;
    if (d->dataSource) {
        d->dataSource = 0;
        selectionListChanged(type);
        selectionListActiveItemChanged(type, -1);
    }
    d->dataSource = dataSource;
    if (d->dataSource) {
        connect(d->dataSource, SIGNAL(selectionListChanged(int)), SLOT(selectionListChanged(int)));
        connect(d->dataSource, SIGNAL(selectionListActiveItemChanged(int, int)), SLOT(selectionListActiveItemChanged(int, int)));
    }
}

/*!
    \internal
*/
AbstractInputMethod *DeclarativeSelectionListModel::dataSource() const
{
    Q_D(const DeclarativeSelectionListModel);
    return d->dataSource;
}

/*!
    \internal
*/
int DeclarativeSelectionListModel::rowCount(const QModelIndex &parent) const
{
    Q_D(const DeclarativeSelectionListModel);
    Q_UNUSED(parent)
    return d->rowCount;
}

/*!
    \internal
*/
QVariant DeclarativeSelectionListModel::data(const QModelIndex &index, int role) const
{
    Q_D(const DeclarativeSelectionListModel);
    return d->dataSource ? d->dataSource->selectionListData(d->type, index.row(), role) : QVariant();
}

/*!
    \internal
*/
QHash<int,QByteArray> DeclarativeSelectionListModel::roleNames() const
{
    Q_D(const DeclarativeSelectionListModel);
    return d->roles;
}

/*! \qmlmethod void SelectionListModel::selectItem(int index)

    This method should be called when the user selects an item at position
    \a index from the list.
    The selection is forwarded to the input method for further processing.
*/
/*!
    This method should be called when the user selects an item at position
    \a index from the list.
    The selection is forwarded to the input method for further processing.
*/
void DeclarativeSelectionListModel::selectItem(int index)
{
    Q_D(DeclarativeSelectionListModel);
    if (d->dataSource) {
        emit itemSelected(index);
        d->dataSource->selectionListItemSelected(d->type, index);
    }
}

/*!
 * \internal
 */
QVariant DeclarativeSelectionListModel::itemData(int index, int role) const
{
    return data(this->index(index, 0), role);
}

/*!
    \internal
*/
void DeclarativeSelectionListModel::selectionListChanged(int type)
{
    Q_D(DeclarativeSelectionListModel);
    if (static_cast<Type>(type) == d->type) {
        int oldCount = d->rowCount;
        int newCount = d->dataSource ? d->dataSource->selectionListItemCount(d->type) : 0;
        if (newCount) {
            int changedCount = qMin(oldCount, newCount);
            if (changedCount)
                dataChanged(index(0), index(changedCount - 1));
            if (oldCount > newCount) {
                beginRemoveRows(QModelIndex(), newCount, oldCount - 1);
                d->rowCount = newCount;
                endRemoveRows();
            } else if (oldCount < newCount) {
                beginInsertRows(QModelIndex(), oldCount, newCount - 1);
                d->rowCount = newCount;
                endInsertRows();
            }
        } else {
            beginResetModel();
            d->rowCount = 0;
            endResetModel();
        }
    }
}

/*!
    \internal
*/
void DeclarativeSelectionListModel::selectionListActiveItemChanged(int type, int index)
{
    Q_D(DeclarativeSelectionListModel);
    if (static_cast<Type>(type) == d->type) {
        emit activeItemChanged(index);
    }
}

/*!
    \qmlsignal void SelectionListModel::activeItemChanged(int index)

    This signal is emitted when the active item in the list changes. The
    UI should react to this signal by highlighting the item at \a index in
    the list.
*/
/*!
    \fn void DeclarativeSelectionListModel::activeItemChanged(int index)

    This signal is emitted when the active item in the list changes. The
    UI should react to this signal by highlighting the item at \a index in
    the list.
*/

/*!
    \qmlsignal void SelectionListModel::itemSelected(int index)

    This signal is emitted when an item at \a index is selected by the user.
*/
/*!
    \fn void DeclarativeSelectionListModel::itemSelected(int index)

    This signal is emitted when an item at \a index is selected by the user.
*/