~penk/phablet-extras/maliit-plugins-qml-candidates

« back to all changes in this revision

Viewing changes to maliit-keyboard/lib/logic/keyareaconverter.cpp

branch to get the new maliit-keyboard.

Approved by Bill Filler, PS Jenkins bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
#include "logic/keyboardloader.h"
39
39
 
40
40
#include <QtCore>
 
41
#include <QtGui/QGuiApplication>
 
42
#include <QtGui/QScreen>
 
43
#include <QtQuick>
41
44
 
42
45
namespace MaliitKeyboard {
43
46
namespace Logic {
 
47
namespace Helper {
 
48
QVector<int> calculateMargins(LayoutHelper::Orientation orientation,
 
49
                              Keyboard& kb)
 
50
{
 
51
    const qreal screenWidth = QGuiApplication::primaryScreen()->size().width();
 
52
 
 
53
    QVector<int> margins;
 
54
 
 
55
    int spaceTakenByKeys = 0;
 
56
    int numberOfKeysInRow = 0;
 
57
    for (int index = 0; index < kb.keys.count(); ++index) {
 
58
        const KeyDescription &desc(kb.key_descriptions.at(index));
 
59
        int width = uiConst->keyWidth(orientation);
 
60
        spaceTakenByKeys += width;
 
61
        numberOfKeysInRow++;
 
62
 
 
63
        bool at_row_end((index + 1 == kb.keys.count())
 
64
                        || (index + 1 < kb.keys.count()
 
65
                            && kb.key_descriptions.at(index + 1).row > desc.row));
 
66
 
 
67
        if (at_row_end) {
 
68
            int marginThisRow = ( (screenWidth - spaceTakenByKeys) / (numberOfKeysInRow-1) ) / 2;
 
69
            margins.append(marginThisRow);
 
70
            spaceTakenByKeys = 0;
 
71
        }
 
72
    }
 
73
 
 
74
    return margins;
 
75
}
 
76
 
 
77
UiConstants* UiConstants::self = 0;
 
78
 
 
79
void UiConstants::initUiConstants()
 
80
{
 
81
    instance();
 
82
 
 
83
    if (!m_initialized) {
 
84
        const QString g_maliit_ui_constants_qml(MALIIT_KEYBOARD_DATA_DIR "/maliit-ui-constants.qml");
 
85
 
 
86
        QQuickView uiConstantsHelperView;
 
87
        uiConstantsHelperView.setSource(QUrl::fromLocalFile(g_maliit_ui_constants_qml));
 
88
        QQuickItem* portraitConstantsHelper = uiConstantsHelperView.rootObject();
 
89
 
 
90
        const int screenWidth = QGuiApplication::primaryScreen()->size().width();
 
91
        const int screenHeight = QGuiApplication::primaryScreen()->size().height();
 
92
 
 
93
        // generic
 
94
 
 
95
        m_genericStorage->keyAreaBorders = QMargins(portraitConstantsHelper->property("key_area_borders").toInt(),
 
96
                                                     portraitConstantsHelper->property("key_area_borders").toInt(),
 
97
                                                     portraitConstantsHelper->property("key_area_borders").toInt(),
 
98
                                                     portraitConstantsHelper->property("key_area_borders").toInt()
 
99
                                                     );
 
100
 
 
101
        m_genericStorage->fontColor = portraitConstantsHelper->property("font_color").toByteArray();
 
102
        m_genericStorage->fontFamily = portraitConstantsHelper->property("font_family").toByteArray();
 
103
 
 
104
        // portrait
 
105
 
 
106
        m_portraitStorage->keyboardTotalHeight = portraitConstantsHelper->property("portrait_keyboard_total_height").toInt();
 
107
        m_portraitStorage->keyboardVisibleHeight = portraitConstantsHelper->property("portrait_keyboard_visible_height").toInt();
 
108
        m_portraitStorage->keyboardScreenWidthRatio = portraitConstantsHelper->property("portrait_keyboard_screen_width_ratio").toReal();
 
109
 
 
110
        const int xp = (screenWidth - (screenWidth * m_portraitStorage->keyboardScreenWidthRatio)) / 2;
 
111
        const int yp = screenHeight - m_portraitStorage->keyboardVisibleHeight;
 
112
 
 
113
        m_portraitStorage->keyboardSurfaceRect = QRect(xp, yp, screenWidth,  m_portraitStorage->keyboardTotalHeight);
 
114
 
 
115
        m_portraitStorage->keyHeight = portraitConstantsHelper->property("portrait_key_height").toReal();
 
116
        m_portraitStorage->keyWidth = portraitConstantsHelper->property("portrait_key_width").toReal();
 
117
 
 
118
        m_portraitStorage->fontSize = portraitConstantsHelper->property("portrait_font_size").toReal();
 
119
 
 
120
 
 
121
        // landscape
 
122
 
 
123
        m_landscapeStorage->keyboardTotalHeight = portraitConstantsHelper->property("landscape_keyboard_total_height").toInt();
 
124
        m_landscapeStorage->keyboardVisibleHeight = portraitConstantsHelper->property("landscape_keyboard_visible_height").toInt();
 
125
        m_landscapeStorage->keyboardScreenWidthRatio = portraitConstantsHelper->property("landscape_keyboard_screen_width_ratio").toReal();
 
126
 
 
127
        const int xl = (screenWidth - (screenWidth * m_landscapeStorage->keyboardScreenWidthRatio)) / 2;
 
128
        const int yl = screenHeight - m_landscapeStorage->keyboardVisibleHeight;
 
129
 
 
130
        m_landscapeStorage->keyboardSurfaceRect = QRect(xl, yl, screenWidth,  m_landscapeStorage->keyboardTotalHeight);
 
131
 
 
132
        m_landscapeStorage->keyHeight = portraitConstantsHelper->property("landscape_key_height").toReal();
 
133
        m_landscapeStorage->keyWidth = portraitConstantsHelper->property("landscape_key_width").toReal();
 
134
 
 
135
        m_landscapeStorage->fontSize = portraitConstantsHelper->property("landscape_font_size").toReal();
 
136
 
 
137
 
 
138
        m_initialized = true;
 
139
    }
 
140
}
 
141
 
 
142
} // namespace Helper
44
143
 
45
144
//! \class KeyAreaConverter
46
145
//! Reads keyboard layouts and converts them into key areas. It can apply
61
160
                           LayoutHelper::Orientation orientation,
62
161
                           bool is_extended_keyarea = false)
63
162
{
 
163
    Helper::UiConstants::instance()->initUiConstants();
 
164
 
64
165
    // An ad-hoc geometry updater that also uses styling information.
65
166
    KeyArea ka;
66
167
    Keyboard kb(source);
74
175
    attributes->setStyleName(kb.style_name);
75
176
 
76
177
    Font font;
77
 
    font.setName(attributes->fontName(orientation));
78
 
    font.setSize(attributes->fontSize(orientation));
79
 
    font.setColor(attributes->fontColor(orientation));
 
178
    font.setName(uiConst->fontFamily());
 
179
    font.setSize(uiConst->fontSize(orientation));
 
180
    font.setColor(uiConst->fontColor());
80
181
 
81
182
    Font small_font(font);
82
183
    small_font.setSize(attributes->smallFontSize(orientation));
83
184
 
84
 
    static const QMargins bg_margins(attributes->keyBackgroundBorders());
85
 
 
86
 
    const qreal max_width(attributes->keyAreaWidth(orientation));
87
 
    const qreal key_height(attributes->keyHeight(orientation));
88
 
    const qreal margin = attributes->keyMargin(orientation);
 
185
    static const QMargins bg_margins( uiConst->keyAreaBorders() );
 
186
 
 
187
    const qreal max_width( uiConst->keyboardSurfaceRect(orientation).width() );
 
188
    const qreal key_height(uiConst->keyHeight(orientation));
 
189
    const qreal keyboard_visible_height = uiConst->keyboardVisibleHeight(orientation);
 
190
 
89
191
    const qreal padding = attributes->keyAreaPadding(orientation);
90
192
 
 
193
 
91
194
    QPoint pos(0, 0);
92
195
    QVector<int> row_indices;
93
196
    int spacer_count = 0;
94
197
    qreal consumed_width = 0;
95
198
 
 
199
    QVector<int> margins = Helper::calculateMargins(orientation, kb);
96
200
 
 
201
    int row = 0;
97
202
    for (int index = 0; index < kb.keys.count(); ++index) {
98
203
        row_indices.append(index);
99
204
        Key &key(kb.keys[index]);
100
205
        const KeyDescription &desc(kb.key_descriptions.at(index));
 
206
 
101
207
        int width = 0;
102
208
        pos.setY(key_height * desc.row);
103
209
 
107
213
                        || (index + 1 < kb.keys.count()
108
214
                            && kb.key_descriptions.at(index + 1).row > desc.row));
109
215
 
 
216
        qreal margin = margins[row];
 
217
 
 
218
        if (at_row_end)
 
219
            row++;
 
220
 
110
221
        if (desc.left_spacer || desc.right_spacer) {
111
222
            ++spacer_count;
112
223
        }
113
224
 
114
 
        width = attributes->keyWidth(orientation, desc.width);
 
225
        width = uiConst->keyWidth(orientation);
 
226
                //attributes->keyWidth(orientation, desc.width);
115
227
 
116
228
        const qreal key_margin((at_row_start || at_row_end) ? margin + padding : margin * 2);
117
229
 
185
297
 
186
298
    Area area;
187
299
    area.setBackground(attributes->keyAreaBackground());
188
 
    area.setBackgroundBorders(attributes->keyAreaBackgroundBorders());
 
300
    area.setBackgroundBorders( uiConst->keyAreaBorders() );
 
301
 
189
302
    area.setSize(QSize((is_extended_keyarea ? consumed_width : max_width),
190
 
                       pos.y() + key_height));
 
303
                       keyboard_visible_height
 
304
                       ));
191
305
 
192
306
    ka.setArea(area);
193
307
    ka.setOrigin(is_extended_keyarea ? QPoint(0, -attributes->verticalOffset(orientation))