|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (developer.feedback@nokia.com) |
|
6 ** |
|
7 ** This file is part of the HbInput module of the UI Extensions for Mobile. |
|
8 ** |
|
9 ** GNU Lesser General Public License Usage |
|
10 ** This file may be used under the terms of the GNU Lesser General Public |
|
11 ** License version 2.1 as published by the Free Software Foundation and |
|
12 ** appearing in the file LICENSE.LGPL included in the packaging of this file. |
|
13 ** Please review the following information to ensure the GNU Lesser General |
|
14 ** Public License version 2.1 requirements will be met: |
|
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
16 ** |
|
17 ** In addition, as a special exception, Nokia gives you certain additional |
|
18 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
20 ** |
|
21 ** If you have questions regarding the use of this file, please contact |
|
22 ** Nokia at developer.feedback@nokia.com. |
|
23 ** |
|
24 ****************************************************************************/ |
|
25 |
|
26 #include "hbinputmethodselectionlist.h" |
|
27 |
|
28 #include <QGraphicsLinearLayout> |
|
29 |
|
30 #include <hbmainwindow.h> |
|
31 #include <hblistwidget.h> |
|
32 #include <hblistwidgetitem.h> |
|
33 #include <hbabstractviewitem.h> |
|
34 #include <hbinputmethod.h> |
|
35 #include <hbinputsettingproxy.h> |
|
36 #include <hbinputregioncollector_p.h> |
|
37 |
|
38 #include "hbdialog_p.h" |
|
39 |
|
40 /// @cond |
|
41 |
|
42 class HbInputMethodSelectionListPrivate : public HbDialogPrivate |
|
43 { |
|
44 Q_DECLARE_PUBLIC(HbInputMethodSelectionList) |
|
45 |
|
46 public: |
|
47 HbInputMethodSelectionListPrivate(); |
|
48 |
|
49 void createSelectionList(); |
|
50 |
|
51 public: |
|
52 HbListWidget *mList; |
|
53 QList<int> mMethodIndices; |
|
54 |
|
55 }; |
|
56 |
|
57 HbInputMethodSelectionListPrivate::HbInputMethodSelectionListPrivate() |
|
58 : mList(0) |
|
59 { |
|
60 } |
|
61 |
|
62 void HbInputMethodSelectionListPrivate::createSelectionList() |
|
63 { |
|
64 Q_Q(HbInputMethodSelectionList); |
|
65 |
|
66 mList = new HbListWidget(); |
|
67 mList->setSelectionMode(HbAbstractItemView::SingleSelection); |
|
68 mList->setItemRecycling(false); |
|
69 |
|
70 HbInputSettingProxy *proxy = HbInputSettingProxy::instance(); |
|
71 HbInputMethodDescriptor descriptor = proxy->preferredInputMethod(); |
|
72 QByteArray customData = proxy->preferredInputMethodCustomData(q->mainWindow()->orientation()); |
|
73 |
|
74 QList<HbInputMethodDescriptor> methodList = HbInputMethod::listCustomInputMethods(q->mainWindow()->orientation(), proxy->globalInputLanguage()); |
|
75 methodList.insert(0, HbInputMethod::defaultInputMethod(q->mainWindow()->orientation())); |
|
76 |
|
77 int selectedIndex = -1; |
|
78 int index = -1; |
|
79 for (int i = 0; i < methodList.count(); ++i) { |
|
80 // If descriptor contains multiple display names, it supports multiple input methods |
|
81 // which should be added one by one to the list |
|
82 QStringList displayNames = methodList.at(i).displayNames(); |
|
83 if (!displayNames.isEmpty()) { |
|
84 QList<HbIcon> icons = methodList.at(i).icons(); |
|
85 for (int j = 0; j < displayNames.count(); ++j) { |
|
86 HbListWidgetItem* item = new HbListWidgetItem(); |
|
87 QString displayName = displayNames.at(j); |
|
88 item->setText(displayName); |
|
89 if (j < icons.count()) { |
|
90 item->setIcon(icons.at(j)); |
|
91 } else { |
|
92 item->setIcon(methodList.at(i).icon()); |
|
93 } |
|
94 mList->addItem(item); |
|
95 mMethodIndices.append(i); |
|
96 ++index; |
|
97 |
|
98 if (descriptor.pluginNameAndPath() == methodList.at(i).pluginNameAndPath() && |
|
99 QString::fromUtf8(customData) == displayName) { |
|
100 selectedIndex = index; |
|
101 } |
|
102 } |
|
103 } else { |
|
104 // Descriptor contains information about one input method |
|
105 HbListWidgetItem* item = new HbListWidgetItem(); |
|
106 QString displayName = methodList.at(i).displayName(); |
|
107 item->setText(displayName); |
|
108 item->setIcon(methodList.at(i).icon()); |
|
109 mList->addItem(item); |
|
110 mMethodIndices.append(i); |
|
111 ++index; |
|
112 |
|
113 if (descriptor.pluginNameAndPath() == methodList.at(i).pluginNameAndPath() && |
|
114 descriptor.displayName() == displayName ) { |
|
115 selectedIndex = index; |
|
116 } |
|
117 } |
|
118 } |
|
119 |
|
120 // Default input method doesn't have valid plugin name, so set the index directly |
|
121 if (descriptor.isDefault() || descriptor.pluginNameAndPath().isEmpty()) { |
|
122 selectedIndex = 0; |
|
123 } |
|
124 |
|
125 HbAbstractViewItem *viewItem = mList->viewItem(selectedIndex); |
|
126 if (viewItem) { |
|
127 mList->setCurrentIndex(viewItem->modelIndex(), QItemSelectionModel::Select); |
|
128 } |
|
129 } |
|
130 |
|
131 /// @endcond |
|
132 |
|
133 /*! |
|
134 Constructs input method selection list |
|
135 */ |
|
136 HbInputMethodSelectionList::HbInputMethodSelectionList(QGraphicsWidget* parent) |
|
137 : HbDialog(*new HbInputMethodSelectionListPrivate(), parent) |
|
138 { |
|
139 Q_D(HbInputMethodSelectionList); |
|
140 HbInputRegionCollector::instance()->attach(this); |
|
141 |
|
142 HbStyle style; |
|
143 qreal listWidth(300); |
|
144 style.parameter(QString("expr(var(hb-param-screen-short-edge)-(2*var(hb-param-margin-gene-screen)))"), listWidth); |
|
145 qreal margin(5); |
|
146 style.parameter(QString("hb-param-margin-gene-popup"), margin); |
|
147 |
|
148 QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(); |
|
149 layout->setContentsMargins(margin, margin, margin, margin); |
|
150 |
|
151 // set default values for popup |
|
152 setTimeout(HbDialog::NoTimeout); |
|
153 setBackgroundFaded(false); |
|
154 setDismissPolicy(TapOutside); |
|
155 setPreferredWidth(listWidth); |
|
156 setModal(true); |
|
157 |
|
158 // Make sure input method selection list never steals focus. |
|
159 setFlag(QGraphicsItem::ItemIsPanel, true); |
|
160 setActive(false); |
|
161 |
|
162 d->createSelectionList(); |
|
163 |
|
164 layout->addItem(d->mList); |
|
165 QGraphicsWidget *content = new QGraphicsWidget(this); |
|
166 content->setLayout(layout); |
|
167 setContentWidget(content); |
|
168 |
|
169 connect(d->mList, SIGNAL(activated(HbListWidgetItem*)), this, SLOT(activateSelectedMethod(HbListWidgetItem*))); |
|
170 connect(d->mList, SIGNAL(longPressed(HbListWidgetItem*, const QPointF&)), this, SLOT(activateSelectedMethod(HbListWidgetItem*))); |
|
171 } |
|
172 |
|
173 /*! |
|
174 Destructs the object. |
|
175 */ |
|
176 HbInputMethodSelectionList::~HbInputMethodSelectionList() |
|
177 { |
|
178 } |
|
179 |
|
180 /*! |
|
181 Called when input method is selected from the list. Signal inputMethodSelected |
|
182 is emitted with selected input method as parameter. |
|
183 */ |
|
184 void HbInputMethodSelectionList::activateSelectedMethod(HbListWidgetItem *item) |
|
185 { |
|
186 Q_D(HbInputMethodSelectionList); |
|
187 |
|
188 int index = d->mMethodIndices.at(d->mList->row(item)); |
|
189 QByteArray customData; |
|
190 |
|
191 HbInputMethodDescriptor selectedMethod; |
|
192 |
|
193 if (index == 0) { |
|
194 selectedMethod = HbInputMethod::defaultInputMethod(mainWindow()->orientation()); |
|
195 |
|
196 if (!selectedMethod.displayNames().isEmpty()) { |
|
197 customData = selectedMethod.displayNames().at(d->mList->row(item)).toUtf8(); |
|
198 } |
|
199 } else { |
|
200 HbInputSettingProxy *proxy = HbInputSettingProxy::instance(); |
|
201 QList<HbInputMethodDescriptor> customList = HbInputMethod::listCustomInputMethods(mainWindow()->orientation(), proxy->globalInputLanguage()); |
|
202 selectedMethod = customList.at(index - 1); |
|
203 |
|
204 int firstItemIndex = d->mMethodIndices.indexOf(index); |
|
205 |
|
206 if (!selectedMethod.displayNames().isEmpty()) { |
|
207 customData = selectedMethod.displayNames().at(d->mList->row(item) - firstItemIndex).toUtf8(); |
|
208 } |
|
209 } |
|
210 |
|
211 close(); |
|
212 |
|
213 emit inputMethodSelected(selectedMethod, customData); |
|
214 } |
|
215 |
|
216 // End of file |