|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <QPainter> |
|
19 #include <QGraphicsLinearLayout> |
|
20 #include <HbInstance> |
|
21 |
|
22 #include "hspage.h" |
|
23 #include "hsscene.h" |
|
24 #include "hsdatabase.h" |
|
25 #include "hswidgethost.h" |
|
26 #include "hswidgetdata.h" |
|
27 #include "hswidgetpresentationdata.h" |
|
28 #include "hspagedata.h" |
|
29 #include "hswallpaper.h" |
|
30 #include "hswidgetpositioningonwidgetadd.h" |
|
31 |
|
32 |
|
33 /*! |
|
34 \class HsPage |
|
35 \ingroup group_hsutils |
|
36 \brief Represents a page in the framework. |
|
37 HsPage is a parent for a group of widgets. HsPage can have a wallpaper. |
|
38 */ |
|
39 |
|
40 /*! |
|
41 Constructor. |
|
42 |
|
43 \a parent Owner. |
|
44 \a aFlags Window flags. |
|
45 */ |
|
46 HsPage::HsPage(QGraphicsItem* parent) |
|
47 : HbWidget(parent), |
|
48 mDatabaseId(-1), |
|
49 mRemovable(true) |
|
50 { |
|
51 setFlag(QGraphicsItem::ItemHasNoContents); |
|
52 setSizePolicy(QSizePolicy(QSizePolicy::Ignored, |
|
53 QSizePolicy::Ignored)); |
|
54 } |
|
55 |
|
56 /*! |
|
57 Destructor. |
|
58 */ |
|
59 HsPage::~HsPage() |
|
60 { |
|
61 } |
|
62 |
|
63 /*! |
|
64 Returns the database id. |
|
65 */ |
|
66 int HsPage::databaseId() const |
|
67 { |
|
68 return mDatabaseId; |
|
69 } |
|
70 |
|
71 /*! |
|
72 Sets the database id to \a id. |
|
73 */ |
|
74 void HsPage::setDatabaseId(int id) |
|
75 { |
|
76 mDatabaseId = id; |
|
77 } |
|
78 |
|
79 /*! |
|
80 Loads widgets. |
|
81 */ |
|
82 bool HsPage::load() |
|
83 { |
|
84 HsDatabase *db = HsDatabase::instance(); |
|
85 Q_ASSERT(db); |
|
86 |
|
87 HsPageData pageData; |
|
88 if (!db->page(mDatabaseId, pageData, true)) { |
|
89 return false; |
|
90 } |
|
91 |
|
92 QList<HsWidgetData> widgetDatas = pageData.widgets(); |
|
93 foreach (HsWidgetData widgetData, widgetDatas) { |
|
94 HsWidgetHost *widget = new HsWidgetHost(widgetData.id()); |
|
95 connectWidget(widget); |
|
96 widget->load(); |
|
97 |
|
98 if (!widget->isValid()) { |
|
99 qDebug() << "HsPage: Widget loading failed."; |
|
100 continue; |
|
101 } |
|
102 |
|
103 widget->setPage(this); |
|
104 mWidgets << widget; |
|
105 } |
|
106 |
|
107 foreach (HsWidgetHost *widget, mWidgets) { |
|
108 widget->initializeWidget(); |
|
109 widget->showWidget(); |
|
110 widget->setParentItem(this); |
|
111 } |
|
112 |
|
113 return true; |
|
114 } |
|
115 |
|
116 bool HsPage::addExistingWidget(HsWidgetHost *widgetHost) |
|
117 { |
|
118 if (!widgetHost) { |
|
119 return false; |
|
120 } |
|
121 |
|
122 if (mWidgets.contains(widgetHost)) { |
|
123 return true; |
|
124 } |
|
125 |
|
126 if (!widgetHost->setPage(this)) { |
|
127 return false; |
|
128 } |
|
129 |
|
130 connectWidget(widgetHost); |
|
131 mWidgets << widgetHost; |
|
132 widgetHost->setParentItem(this); |
|
133 |
|
134 return true; |
|
135 } |
|
136 |
|
137 bool HsPage::removeWidget(HsWidgetHost *widgetHost) |
|
138 { |
|
139 if (!widgetHost || !widgetHost->setPage(0)) { |
|
140 return false; |
|
141 } |
|
142 |
|
143 disconnectWidget(widgetHost); |
|
144 mWidgets.removeOne(widgetHost); |
|
145 widgetHost->setParentItem(0); |
|
146 |
|
147 return true; |
|
148 } |
|
149 |
|
150 QList<HsWidgetHost *> HsPage::newWidgets() |
|
151 { |
|
152 return mNewWidgets; |
|
153 } |
|
154 |
|
155 /*! |
|
156 Adds new widget into a page. Returns true if successfull. |
|
157 */ |
|
158 bool HsPage::addNewWidget(HsWidgetHost* widgetHost) |
|
159 { |
|
160 if (!widgetHost || mWidgets.contains(widgetHost)) { |
|
161 return false; |
|
162 } |
|
163 |
|
164 if (mNewWidgets.contains(widgetHost)) { |
|
165 return true; |
|
166 } |
|
167 |
|
168 // Set presentation. |
|
169 QString key = HsScene::orientation() == Qt::Horizontal ? |
|
170 "landscape" : "portrait"; |
|
171 |
|
172 HsWidgetPresentationData presentation; |
|
173 if (!widgetHost->widgetPresentationData(key, presentation)) { |
|
174 presentation.setKey(key); |
|
175 presentation.setPosition(QPointF()); |
|
176 presentation.setSize(widgetHost->preferredSize()); |
|
177 presentation.setZValue(0); |
|
178 widgetHost->setWidgetPresentationData(presentation); |
|
179 } |
|
180 |
|
181 widgetHost->hide(); |
|
182 widgetHost->setGeometry(QRectF(presentation.position(), |
|
183 presentation.size())); |
|
184 widgetHost->setZValue(presentation.zValue()); |
|
185 |
|
186 connectWidget(widgetHost); |
|
187 mNewWidgets << widgetHost; |
|
188 |
|
189 return true; |
|
190 } |
|
191 |
|
192 /*! |
|
193 Layouts all the new widgets |
|
194 */ |
|
195 void HsPage::layoutNewWidgets() |
|
196 { |
|
197 QList<QRectF> oldWidgetGeometries; |
|
198 |
|
199 foreach(HsWidgetHost *widget, mNewWidgets) { |
|
200 oldWidgetGeometries << widget->rect(); |
|
201 } |
|
202 |
|
203 QList<QRectF> newWidgetGeometries = |
|
204 HsWidgetPositioningOnWidgetAdd::instance()->convert( |
|
205 rect().adjusted(10, 10, -10, -10), |
|
206 oldWidgetGeometries); |
|
207 |
|
208 updateZValues(); |
|
209 |
|
210 HsWidgetHost *widget = NULL; |
|
211 for(int i = 0; i < mNewWidgets.count(); ++i) { |
|
212 widget = mNewWidgets[i]; |
|
213 widget->setGeometry(newWidgetGeometries[i]); |
|
214 widget->setWidgetPresentation(); |
|
215 widget->setPage(this); |
|
216 widget->setParentItem(this); |
|
217 widget->show(); |
|
218 mWidgets << widget; |
|
219 } |
|
220 } |
|
221 |
|
222 /*! |
|
223 Resets the new widgets list |
|
224 */ |
|
225 void HsPage::resetNewWidgets() |
|
226 { |
|
227 mNewWidgets.clear(); |
|
228 } |
|
229 |
|
230 bool HsPage::deleteFromDatabase() |
|
231 { |
|
232 HsDatabase *db = HsDatabase::instance(); |
|
233 Q_ASSERT(db); |
|
234 |
|
235 db->transaction(); |
|
236 |
|
237 foreach (HsWidgetHost *widget, mWidgets) { |
|
238 if (!widget->deleteFromDatabase()) { |
|
239 db->rollback(); |
|
240 return false; |
|
241 } |
|
242 } |
|
243 |
|
244 if (!db->deletePage(mDatabaseId)) { |
|
245 db->rollback(); |
|
246 return false; |
|
247 } |
|
248 |
|
249 db->commit(); |
|
250 return true; |
|
251 } |
|
252 |
|
253 QList<HsWidgetHost *> HsPage::widgets() const |
|
254 { |
|
255 return mWidgets; |
|
256 } |
|
257 |
|
258 /*! |
|
259 Returns true if the page can be removed. Otherwise, |
|
260 returns false. |
|
261 */ |
|
262 bool HsPage::isRemovable() const |
|
263 { |
|
264 return mRemovable; |
|
265 } |
|
266 |
|
267 /*! |
|
268 Sets removable flag to \a removable. |
|
269 */ |
|
270 void HsPage::setRemovable(bool removable) |
|
271 { |
|
272 mRemovable = removable; |
|
273 } |
|
274 |
|
275 bool HsPage::updateZValues() |
|
276 { |
|
277 if (mWidgets.isEmpty()) { |
|
278 return true; |
|
279 } |
|
280 |
|
281 QMultiMap<qreal, HsWidgetHost *> map; |
|
282 foreach (HsWidgetHost *widget, mWidgets) { |
|
283 map.insert(widget->zValue(), widget); |
|
284 } |
|
285 |
|
286 QList<HsWidgetHost *> sortedWidgets = map.values(); |
|
287 |
|
288 HsWidgetHost *activeWidget = HsScene::instance()->activeWidget(); |
|
289 if (sortedWidgets.contains(activeWidget)) { |
|
290 sortedWidgets.removeOne(activeWidget); |
|
291 sortedWidgets.append(activeWidget); |
|
292 } |
|
293 |
|
294 int z = 0; |
|
295 foreach (HsWidgetHost *widget, sortedWidgets) { |
|
296 widget->setZValue(z++); |
|
297 widget->setWidgetPresentation(); |
|
298 } |
|
299 |
|
300 foreach (HsWidgetHost *widget, mNewWidgets) { |
|
301 widget->setZValue(z++); |
|
302 widget->setWidgetPresentation(); |
|
303 } |
|
304 |
|
305 return true; |
|
306 } |
|
307 /*! |
|
308 Propogate online state to widgets. |
|
309 |
|
310 */ |
|
311 void HsPage::setOnline(bool online) |
|
312 { |
|
313 foreach (HsWidgetHost *widget, mNewWidgets) { |
|
314 widget->setOnline(online); |
|
315 } |
|
316 foreach (HsWidgetHost *widget, mWidgets) { |
|
317 widget->setOnline(online); |
|
318 } |
|
319 } |
|
320 |
|
321 HsPage *HsPage::createInstance(const HsPageData &pageData) |
|
322 { |
|
323 HsDatabase *db = HsDatabase::instance(); |
|
324 Q_ASSERT(db); |
|
325 |
|
326 HsPageData data(pageData); |
|
327 if (!db->insertPage(data)) { |
|
328 return 0; |
|
329 } |
|
330 |
|
331 HsPage *page = new HsPage; |
|
332 page->setDatabaseId(data.id()); |
|
333 return page; |
|
334 } |
|
335 |
|
336 void HsPage::connectWidget(HsWidgetHost *widget) |
|
337 { |
|
338 connect(widget, SIGNAL(widgetFinished(HsWidgetHost*)), |
|
339 SLOT(onWidgetFinished(HsWidgetHost*))); |
|
340 } |
|
341 |
|
342 void HsPage::disconnectWidget(HsWidgetHost *widget) |
|
343 { |
|
344 widget->disconnect(this); |
|
345 } |
|
346 |
|
347 void HsPage::onWidgetFinished(HsWidgetHost *widget) |
|
348 { |
|
349 if (mNewWidgets.contains(widget)) { |
|
350 mNewWidgets.removeOne(widget); |
|
351 } else if (mWidgets.contains(widget)) { |
|
352 mWidgets.removeOne(widget); |
|
353 } else { |
|
354 return; |
|
355 } |
|
356 |
|
357 disconnectWidget(widget); |
|
358 widget->uninitializeWidget(); |
|
359 widget->deleteFromDatabase(); |
|
360 widget->deleteLater(); |
|
361 } |