2
* This file is part of Maliit Plugins
4
* Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
6
* Contact: Mohammad Anwari <Mohammad.Anwari@nokia.com>
8
* Redistribution and use in source and binary forms, with or without modification,
9
* are permitted provided that the following conditions are met:
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.
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.
32
#include "keyareaconverter.h"
34
#include "models/styleattributes.h"
35
#include "models/area.h"
36
#include "models/keyarea.h"
37
#include "models/key.h"
38
#include "logic/keyboardloader.h"
39
#include "logic/dynamiclayout.h"
41
namespace MaliitKeyboard {
44
//! \class KeyAreaConverter
45
//! Reads keyboard layouts and converts them into key areas. It can apply
46
//! styling attributes, making the key areas ready for rendering.
47
//! Explicit functions are used for the different key areas, instead of a
48
//! generic lookup. This makes the class easier to use and better documents
49
//! the valid combinations.
52
//! \brief Creates a key area from a keyboard.
53
//! \param attributes The styling attributes that get applied to the key area.
54
//! \param source The keyboard layout used for the key area.
55
//! \param orientation The layout orientation.
56
//! \param is_extended_keyarea Whether the resulting key area is used for
57
//! extended keys (optional).
58
KeyArea createFromKeyboard(StyleAttributes *attributes,
59
const Keyboard &source,
60
LayoutHelper::Orientation orientation,
61
bool is_extended_keyarea = false)
63
DynamicLayout::instance()->initDynamicLayout();
65
// An ad-hoc geometry updater that also uses styling information.
69
uiConst->calculateNumberOfRows(kb);
72
qCritical() << __PRETTY_FUNCTION__
73
<< "No style given, aborting.";
77
attributes->setStyleName(kb.style_name);
80
font.setName(uiConst->fontFamily());
81
font.setSize(uiConst->fontSize(orientation));
82
font.setColor(uiConst->fontColor());
84
Font small_font(font);
85
small_font.setSize(uiConst->fontSizeSmall(orientation));
87
static const QMargins bg_margins( uiConst->keyAreaBorders() );
89
const qreal max_width( uiConst->windowGeometryRect(qGuiApp->primaryScreen()->orientation()).width() );
90
const qreal key_height(uiConst->keyHeight(orientation));
91
const qreal keypadHeight = uiConst->keypadHeight(orientation);
93
const qreal padding = attributes->keyAreaPadding(orientation);
98
QVector<int> row_indices;
100
qreal consumed_width = 0;
102
QVector<int> margins = uiConst->calculateMargins(orientation, kb);
103
qreal margin = margins[0];
106
for (int index = 0; index < kb.keys.count(); ++index) {
107
row_indices.append(index);
108
Key &key(kb.keys[index]);
109
const KeyDescription &desc(kb.key_descriptions.at(index));
112
pos.setY((key_height + uiConst->spaceBetweenRows(orientation)) * desc.row + uiConst->keypadTopMargin(orientation));
113
bool at_row_start((index == 0)
114
|| (kb.key_descriptions.at(index - 1).row < desc.row));
115
bool at_row_end((index + 1 == kb.keys.count())
116
|| (index + 1 < kb.keys.count()
117
&& kb.key_descriptions.at(index + 1).row > desc.row));
122
if (desc.left_spacer || desc.right_spacer) {
126
width = uiConst->keyWidth(orientation, desc.width);
128
const qreal key_margin((at_row_start || at_row_end) ? margin + padding : margin * 2);
131
area.setBackground(uiConst->keyBackground(key.style(), KeyDescription::NormalState));
132
area.setBackgroundBorders(bg_margins);
133
area.setSize(QSize(width + key_margin, key_height));
137
key.setMargins(QMargins(at_row_start ? padding : margin, margin,
138
at_row_end ? padding : margin, margin));
140
const QString &text(key.label().text());
141
key.rLabel().setFont(text.count() > 1 ? small_font : font);
143
if (key.icon().isEmpty()) {
144
key.setIcon(attributes->icon(desc.icon,
145
KeyDescription::NormalState));
147
key.setIcon(attributes->customIcon(key.icon()));
150
pos.rx() += key.rect().width();
153
if (not is_extended_keyarea
154
&& spacer_count > 0 && pos.x() < max_width + 1) {
155
const int spacer_width = qMax<int>(0, max_width - pos.x()) / spacer_count;
159
Q_FOREACH (int row_index, row_indices) {
160
Key &k(kb.keys[row_index]);
161
const KeyDescription &d(kb.key_descriptions.at(row_index));
164
QMargins m(k.margins());
168
m.setLeft(m.left() + spacer_width);
169
extra_width += spacer_width;
172
if (d.right_spacer) {
173
m.setRight(m.right() + spacer_width);
174
extra_width += spacer_width;
179
r.translate(right_x - r.left(), 0);
180
r.setWidth(r.width() + extra_width);
181
k.setOrigin(r.topLeft());
182
k.rArea().setSize(r.size());
184
right_x = r.x() + r.width();
188
consumed_width = qMax<qreal>(consumed_width, key.rect().x()
199
area.setBackground(attributes->keyAreaBackground());
200
area.setBackgroundBorders( uiConst->keyAreaBorders() );
202
area.setSize(QSize((is_extended_keyarea ? consumed_width : max_width),
207
ka.setOrigin(is_extended_keyarea ? QPoint(0, -attributes->verticalOffset(orientation))
208
: QPoint(0, uiConst->wordRibbonHeight(orientation)));
216
//! \param attributes The styling attributes that should be applied to the
217
//! created key areas.
218
//! \param loader The keyboard layout loader.
219
KeyAreaConverter::KeyAreaConverter(StyleAttributes *attributes,
220
KeyboardLoader *loader)
221
: m_attributes(attributes)
223
, m_orientation(LayoutHelper::Landscape)
225
if (not attributes || not loader) {
226
qFatal("Neither attributes nor loader can be null.");
231
KeyAreaConverter::~KeyAreaConverter()
235
//! \brief Sets the layout orientation used for creating key areas.
236
//! \param orientation The layout orientation. Default: landscape.
237
void KeyAreaConverter::setLayoutOrientation(LayoutHelper::Orientation orientation)
239
m_orientation = orientation;
242
//! \brief Returns the main key area.
243
KeyArea KeyAreaConverter::keyArea() const
245
return createFromKeyboard(m_attributes, m_loader->keyboard(), m_orientation);
249
//! \brief Returns the next key area (right of main key area).
250
KeyArea KeyAreaConverter::nextKeyArea() const
252
return createFromKeyboard(m_attributes, m_loader->nextKeyboard(), m_orientation);
256
//! \brief Returns the previous key area (left of main key area).
257
KeyArea KeyAreaConverter::previousKeyArea() const
259
return createFromKeyboard(m_attributes, m_loader->previousKeyboard(), m_orientation);
263
//! \brief Returns the main key area with shift bindings activated.
264
KeyArea KeyAreaConverter::shiftedKeyArea() const
266
return createFromKeyboard(m_attributes, m_loader->shiftedKeyboard(), m_orientation);
270
//! \brief Returns the symbols key area.
271
//! \param page The symbols page to return (optional).
272
KeyArea KeyAreaConverter::symbolsKeyArea(int page) const
274
return createFromKeyboard(m_attributes, m_loader->symbolsKeyboard(page), m_orientation);
278
//! \brief Returns the main key area with dead key bindings activated.
279
//! \param dead The key used to look up the dead keys.
280
KeyArea KeyAreaConverter::deadKeyArea(const Key &dead) const
282
return createFromKeyboard(m_attributes, m_loader->deadKeyboard(dead), m_orientation);
286
//! \brief Returns the main key area with dead key bindings activated.
287
//! \param dead The key used to look up the dead keys.
288
KeyArea KeyAreaConverter::shiftedDeadKeyArea(const Key &dead) const
290
return createFromKeyboard(m_attributes, m_loader->shiftedDeadKeyboard(dead), m_orientation);
294
//! Returns an extended key area.
295
//! \param key The key used to look up the extended key binding.
296
KeyArea KeyAreaConverter::extendedKeyArea(const Key &key) const
298
return createFromKeyboard(m_attributes, m_loader->extendedKeyboard(key), m_orientation, true);
302
//! Returns the number key area.
303
KeyArea KeyAreaConverter::numberKeyArea() const
305
return createFromKeyboard(m_attributes, m_loader->numberKeyboard(), m_orientation);
309
//! Returns the phone number key area.
310
KeyArea KeyAreaConverter::phoneNumberKeyArea() const
312
return createFromKeyboard(m_attributes, m_loader->phoneNumberKeyboard(), m_orientation);
315
}} // namespace Logic, MaliitKeyboard