~michael-sheldon/ubuntu-keyboard/fix-oxide-dismiss-test

« back to all changes in this revision

Viewing changes to src/lib/models/layout.cpp

  • Committer: Thomas Moenicke
  • Date: 2013-07-19 12:05:07 UTC
  • Revision ID: thomas.moenicke@canonical.com-20130719120507-lzw5oq50xm567x0j
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of Maliit Plugins
 
3
 *
 
4
 * Copyright (C) 2012 Openismus GmbH. All rights reserved.
 
5
 *
 
6
 * Contact: maliit-discuss@lists.maliit.org
 
7
 *
 
8
 * Redistribution and use in source and binary forms, with or without modification,
 
9
 * are permitted provided that the following conditions are met:
 
10
 *
 
11
 * Redistributions of source code must retain the above copyright notice, this list
 
12
 * of conditions and the following disclaimer.
 
13
 * Redistributions in binary form must reproduce the above copyright notice, this list
 
14
 * of conditions and the following disclaimer in the documentation and/or other materials
 
15
 * provided with the distribution.
 
16
 * Neither the name of Nokia Corporation nor the names of its contributors may be
 
17
 * used to endorse or promote products derived from this software without specific
 
18
 * prior written permission.
 
19
 *
 
20
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 
21
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 
22
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 
23
 * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
25
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 
26
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 
27
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
28
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
29
 *
 
30
 */
 
31
 
 
32
#include "layout.h"
 
33
#include "keyarea.h"
 
34
#include "key.h"
 
35
#include "keydescription.h"
 
36
 
 
37
#include "logic/keyareaconverter.h"
 
38
#include "logic/dynamiclayout.h"
 
39
 
 
40
#include <QtGui/QGuiApplication>
 
41
#include <QScreen>
 
42
 
 
43
namespace MaliitKeyboard {
 
44
namespace Model {
 
45
namespace {
 
46
QUrl toUrl(const QString &directory,
 
47
            const QString &base_name)
 
48
{
 
49
    if (not (directory.isEmpty() || base_name.isEmpty())) {
 
50
        return QUrl(directory + "/" + base_name);
 
51
    }
 
52
 
 
53
    return QUrl();
 
54
 
 
55
}
 
56
}
 
57
 
 
58
 
 
59
class LayoutPrivate
 
60
{
 
61
public:
 
62
    QString title;
 
63
    KeyArea key_area;
 
64
    QString image_directory;
 
65
    QHash<int, QByteArray> roles;
 
66
 
 
67
    explicit LayoutPrivate();
 
68
};
 
69
 
 
70
 
 
71
LayoutPrivate::LayoutPrivate()
 
72
    : title()
 
73
    , key_area()
 
74
    , image_directory()
 
75
    , roles()
 
76
{
 
77
    // Model roles are used as variables in QML, hence the under_score naming
 
78
    // convention:
 
79
    roles[Layout::RoleKeyRectangle] = "key_rectangle";
 
80
    roles[Layout::RoleKeyReactiveArea] = "key_reactive_area";
 
81
    roles[Layout::RoleKeyBackground] = "key_background";
 
82
    roles[Layout::RoleKeyBackgroundBorders] = "key_background_borders";
 
83
    roles[Layout::RoleKeyText] = "key_text";
 
84
    roles[Layout::RoleKeyFont] = "key_font";
 
85
    roles[Layout::RoleKeyFontColor] = "key_font_color";
 
86
    roles[Layout::RoleKeyFontSize] = "key_font_size";
 
87
    roles[Layout::RoleKeyFontStretch] = "key_font_stretch";
 
88
    roles[Layout::RoleKeyIcon] = "key_icon";
 
89
    roles[Layout::RoleKeyActionInsert] = "key_action_insert";
 
90
}
 
91
 
 
92
 
 
93
Layout::Layout(QObject *parent)
 
94
    : QAbstractListModel(parent)
 
95
    , d_ptr(new LayoutPrivate)
 
96
{}
 
97
 
 
98
 
 
99
Layout::~Layout()
 
100
{}
 
101
 
 
102
 
 
103
void Layout::setTitle(const QString &title)
 
104
{
 
105
    Q_D(Layout);
 
106
 
 
107
    if (d->title != title) {
 
108
        d->title = title;
 
109
        Q_EMIT titleChanged(d->title);
 
110
    }
 
111
}
 
112
 
 
113
 
 
114
QString Layout::title() const
 
115
{
 
116
    Q_D(const Layout);
 
117
    return d->title;
 
118
}
 
119
 
 
120
 
 
121
void Layout::setKeyArea(const KeyArea &area)
 
122
{
 
123
    beginResetModel();
 
124
 
 
125
    Q_D(Layout);
 
126
    const bool geometry_changed(d->key_area.rect() != area.rect());
 
127
    const bool background_changed(d->key_area.area().background() != area.area().background());
 
128
    const bool background_borders_changed(d->key_area.area().backgroundBorders() != area.area().backgroundBorders());
 
129
    const bool visible_changed((d->key_area.keys().isEmpty() && not area.keys().isEmpty())
 
130
                               || (not d->key_area.keys().isEmpty() && area.keys().isEmpty()));
 
131
    const bool origin_changed(d->key_area.origin() != area.origin());
 
132
 
 
133
    d->key_area = area;
 
134
 
 
135
    if (origin_changed) {
 
136
        Q_EMIT originChanged(d->key_area.origin());
 
137
    }
 
138
 
 
139
    if (geometry_changed) {
 
140
        Q_EMIT widthChanged(width());
 
141
        Q_EMIT heightChanged(height());
 
142
    }
 
143
 
 
144
    if (background_changed) {
 
145
        Q_EMIT backgroundChanged(background());
 
146
    }
 
147
 
 
148
    if (background_borders_changed) {
 
149
        Q_EMIT backgroundBordersChanged(backgroundBorders());
 
150
    }
 
151
 
 
152
    if (visible_changed) {
 
153
        Q_EMIT visibleChanged(not d->key_area.keys().isEmpty());
 
154
    }
 
155
 
 
156
    endResetModel();
 
157
}
 
158
 
 
159
 
 
160
KeyArea Layout::keyArea() const
 
161
{
 
162
    Q_D(const Layout);
 
163
    return d->key_area;
 
164
}
 
165
 
 
166
 
 
167
void Layout::replaceKey(int index,
 
168
                        const Key &key)
 
169
{
 
170
    Q_D(Layout);
 
171
    d->key_area.rKeys().replace(index, key);
 
172
    Q_EMIT dataChanged(this->index(index, 0), this->index(index, 0));
 
173
}
 
174
 
 
175
 
 
176
bool Layout::isVisible() const
 
177
{
 
178
    Q_D(const Layout);
 
179
    return (not d->key_area.keys().isEmpty());
 
180
}
 
181
 
 
182
 
 
183
int Layout::width() const
 
184
{
 
185
    Q_D(const Layout);
 
186
    return d->key_area.rect().width();
 
187
}
 
188
 
 
189
 
 
190
int Layout::height() const
 
191
{
 
192
    Q_D(const Layout);
 
193
    return d->key_area.rect().height();
 
194
}
 
195
 
 
196
int Layout::wordRibbonHeight() const
 
197
{
 
198
    return uiConst->wordRibbonHeight(
 
199
                uiConst->screenToMaliitOrientation(
 
200
                    qGuiApp->primaryScreen()->orientation()) );
 
201
}
 
202
 
 
203
QPoint Layout::origin() const
 
204
{
 
205
    Q_D(const Layout);
 
206
    return d->key_area.origin();
 
207
}
 
208
 
 
209
 
 
210
QUrl Layout::background() const
 
211
{
 
212
    Q_D(const Layout);
 
213
    return toUrl(d->image_directory, d->key_area.area().background());
 
214
}
 
215
 
 
216
 
 
217
QRectF Layout::backgroundBorders() const
 
218
{
 
219
    Q_D(const Layout);
 
220
 
 
221
    const QMargins &m(d->key_area.area().backgroundBorders());
 
222
    return QRectF(m.left(), m.top(), m.right(), m.bottom());
 
223
}
 
224
 
 
225
int Layout::invisibleTouchAreaHeight() const
 
226
{
 
227
    return uiConst->invisibleTouchAreaHeight(
 
228
                uiConst->screenToMaliitOrientation(
 
229
                    qGuiApp->primaryScreen()->orientation()) );
 
230
}
 
231
 
 
232
void Layout::setImageDirectory(const QString &directory)
 
233
{
 
234
    Q_D(Layout);
 
235
 
 
236
    if (d->image_directory != directory) {
 
237
        d->image_directory = directory;
 
238
        // TODO: Make sure we don't accidentially invalidate the whole model twice
 
239
        beginResetModel();
 
240
        backgroundChanged(background());
 
241
        endResetModel();
 
242
    }
 
243
}
 
244
 
 
245
 
 
246
int Layout::rowCount(const QModelIndex &parent) const
 
247
{
 
248
    Q_UNUSED(parent)
 
249
    Q_D(const Layout);
 
250
    return d->key_area.keys().count();
 
251
}
 
252
 
 
253
 
 
254
QVariant Layout::data(const QModelIndex &index,
 
255
                      int role) const
 
256
{
 
257
    Q_D(const Layout);
 
258
 
 
259
    const QVector<Key> &keys(d->key_area.keys());
 
260
    const Key &key(index.row() < keys.count()
 
261
                   ? keys.at(index.row())
 
262
                   : Key());
 
263
 
 
264
    switch(role) {
 
265
    case RoleKeyReactiveArea:
 
266
        return QVariant(key.rect());
 
267
 
 
268
    case RoleKeyRectangle: {
 
269
        const QRect &r(key.rect());
 
270
        const QMargins &m(key.margins());
 
271
 
 
272
        return QVariant(QRectF(m.left(), m.top(),
 
273
                               r.width() - (m.left() + m.right()),
 
274
                               r.height() - (m.top() + m.bottom())));
 
275
    }
 
276
 
 
277
    case RoleKeyBackground:
 
278
        return QVariant(toUrl(d->image_directory, key.area().background()));
 
279
 
 
280
    case RoleKeyBackgroundBorders: {
 
281
        // Neither QML nor QVariant support QMargins type.
 
282
        // We need to transform QMargins into a QRectF so that we can abuse
 
283
        // left, top, right, bottom (of the QRectF) *as if* it was a QMargins.
 
284
        const QMargins &m(key.area().backgroundBorders());
 
285
        return QVariant(QRectF(m.left(), m.top(), m.right(), m.bottom()));
 
286
    }
 
287
 
 
288
    case RoleKeyText:
 
289
        return QVariant(key.label().text());
 
290
 
 
291
    case RoleKeyFont:
 
292
        return QVariant(QString(key.label().font().name()));
 
293
 
 
294
    case RoleKeyFontColor:
 
295
        // FIXME: QML expects QVariant(QColor(...)) here, but then we'd have a QtGui dependency, no?
 
296
        return QVariant(QString(key.label().font().color()));
 
297
 
 
298
    case RoleKeyFontSize:
 
299
        // FIXME: Using qMax to suppress warning about "invalid" 0.0 font sizes in QFont::setPointSizeF.
 
300
        return QVariant(qMax<int>(1, key.label().font().size()));
 
301
 
 
302
    case RoleKeyFontStretch:
 
303
        return QVariant(key.label().font().stretch());
 
304
 
 
305
    case RoleKeyIcon:
 
306
        return QVariant(toUrl(d->image_directory, key.icon()));
 
307
 
 
308
    case RoleKeyActionInsert:
 
309
        return QVariant(key.action() == Key::ActionInsert);
 
310
    }
 
311
 
 
312
    qWarning() << __PRETTY_FUNCTION__
 
313
               << "Invalid index or role (" << index.row() << role << ").";
 
314
 
 
315
    return QVariant();
 
316
}
 
317
 
 
318
 
 
319
QHash<int, QByteArray> Layout::roleNames() const
 
320
{
 
321
    Q_D(const Layout);
 
322
    return d->roles;
 
323
}
 
324
 
 
325
 
 
326
QVariant Layout::data(int index,
 
327
                      const QString &role) const
 
328
{
 
329
 
 
330
    const QModelIndex idx(this->index(index, 0));
 
331
    return data(idx, roleNames().key(role.toLatin1()));
 
332
}
 
333
 
 
334
 
 
335
}} // namespace Model, MaliitKeyboard