46 HsPage::HsPage(QGraphicsItem* parent) |
46 HsPage::HsPage(QGraphicsItem* parent) |
47 : HbWidget(parent), |
47 : HbWidget(parent), |
48 mDatabaseId(-1), |
48 mDatabaseId(-1), |
49 mWallpaper(0), |
49 mWallpaper(0), |
50 mRemovable(true), |
50 mRemovable(true), |
51 mTouchArea(0) |
51 mTouchArea(0), |
|
52 mPageMargin(0.0) |
52 { |
53 { |
53 setFlag(QGraphicsItem::ItemHasNoContents); |
54 setFlag(QGraphicsItem::ItemHasNoContents); |
54 setSizePolicy(QSizePolicy(QSizePolicy::Ignored, |
55 setSizePolicy(QSizePolicy(QSizePolicy::Ignored, |
55 QSizePolicy::Ignored)); |
56 QSizePolicy::Ignored)); |
56 |
57 |
57 setupTouchArea(); |
58 setupTouchArea(); |
|
59 |
|
60 //Page margin |
|
61 mPageMargin = HSCONFIGURATION_GET(pageMargin); |
|
62 connect(HsConfiguration::instance(), SIGNAL(propertyChanged(QString)), SLOT(onPageMarginChanged(QString))); |
58 } |
63 } |
59 |
64 |
60 /*! |
65 /*! |
61 Destructor. |
66 Destructor. |
62 */ |
67 */ |
154 mWidgets << widgetHost; |
159 mWidgets << widgetHost; |
155 widgetHost->setParentItem(this); |
160 widgetHost->setParentItem(this); |
156 |
161 |
157 return true; |
162 return true; |
158 } |
163 } |
|
164 |
159 /*! |
165 /*! |
160 Remove given \a widgetHost from a page. Widget is not deleted. |
166 Remove given \a widgetHost from a page. Widget is not deleted. |
161 Returns true if successful |
167 Returns true if successful |
162 */ |
168 */ |
163 bool HsPage::removeWidget(HsWidgetHost *widgetHost) |
169 bool HsPage::removeWidget(HsWidgetHost *widgetHost) |
170 mWidgets.removeOne(widgetHost); |
176 mWidgets.removeOne(widgetHost); |
171 widgetHost->setParentItem(0); |
177 widgetHost->setParentItem(0); |
172 |
178 |
173 return true; |
179 return true; |
174 } |
180 } |
|
181 |
175 /*! |
182 /*! |
176 Returns list of new widgets belonging to a page. Widgets which are |
183 Returns list of new widgets belonging to a page. Widgets which are |
177 not yet layouted are considered as new widgets. |
184 not yet layouted are considered as new widgets. |
178 */ |
185 */ |
179 QList<HsWidgetHost *> HsPage::newWidgets() |
186 QList<HsWidgetHost *> HsPage::newWidgets() |
195 if (mNewWidgets.contains(widgetHost)) { |
202 if (mNewWidgets.contains(widgetHost)) { |
196 return true; |
203 return true; |
197 } |
204 } |
198 |
205 |
199 HsWidgetPresentationData presentation; |
206 HsWidgetPresentationData presentation; |
|
207 QPointF adjustedWidgetPos = adjustedWidgetPosition(widgetHost->geometry()); |
200 presentation.orientation = HsScene::orientation(); |
208 presentation.orientation = HsScene::orientation(); |
201 if (!widgetHost->getPresentation(presentation)) { |
209 if (!widgetHost->getPresentation(presentation)) { |
202 presentation.orientation = HsScene::orientation(); |
210 presentation.orientation = HsScene::orientation(); |
203 presentation.setPos(mTouchPoint); |
211 presentation.setPos(adjustedWidgetPos); |
204 presentation.zValue = 0; |
212 presentation.zValue = 0; |
205 widgetHost->savePresentation(presentation); |
213 widgetHost->savePresentation(presentation); |
206 } |
214 } |
207 |
215 |
208 widgetHost->hide(); |
216 widgetHost->hide(); |
209 widgetHost->setPos(presentation.x, presentation.y); |
217 widgetHost->setPos(adjustedWidgetPos); |
210 widgetHost->setZValue(presentation.zValue); |
218 widgetHost->setZValue(presentation.zValue); |
211 |
219 |
212 connectWidget(widgetHost); |
220 connectWidget(widgetHost); |
213 mNewWidgets << widgetHost; |
221 mNewWidgets << widgetHost; |
214 |
222 |
242 mWidgets << mNewWidgets; |
250 mWidgets << mNewWidgets; |
243 mNewWidgets.clear(); |
251 mNewWidgets.clear(); |
244 } |
252 } |
245 |
253 |
246 /*! |
254 /*! |
|
255 Clears new widgets list and resets layout. |
|
256 */ |
|
257 void HsPage::resetNewWidgets() |
|
258 { |
|
259 mNewWidgets.clear(); |
|
260 setLayout(0); |
|
261 } |
|
262 |
|
263 /*! |
247 Remove page and all it's contained widgets from database |
264 Remove page and all it's contained widgets from database |
248 */ |
265 */ |
249 bool HsPage::deleteFromDatabase() |
266 bool HsPage::deleteFromDatabase() |
250 { |
267 { |
251 foreach (HsWidgetHost *widget, mWidgets) { |
268 foreach (HsWidgetHost *widget, mWidgets) { |
260 |
277 |
261 foreach (HsWidgetHost *widget, mUnavailableWidgets) { |
278 foreach (HsWidgetHost *widget, mUnavailableWidgets) { |
262 widget->remove(); |
279 widget->remove(); |
263 } |
280 } |
264 mUnavailableWidgets.clear(); |
281 mUnavailableWidgets.clear(); |
265 |
282 |
|
283 if (mWallpaper) { |
|
284 mWallpaper->remove(); |
|
285 } |
266 return HsDatabase::instance()->deletePage(mDatabaseId); |
286 return HsDatabase::instance()->deletePage(mDatabaseId); |
267 } |
287 } |
|
288 |
268 /*! |
289 /*! |
269 Return list of widgets belonging to a page |
290 Return list of widgets belonging to a page |
270 */ |
291 */ |
271 QList<HsWidgetHost *> HsPage::widgets() const |
292 QList<HsWidgetHost *> HsPage::widgets() const |
272 { |
293 { |
287 */ |
308 */ |
288 void HsPage::setRemovable(bool removable) |
309 void HsPage::setRemovable(bool removable) |
289 { |
310 { |
290 mRemovable = removable; |
311 mRemovable = removable; |
291 } |
312 } |
|
313 |
292 /*! |
314 /*! |
293 Return true if page is default page. |
315 Return true if page is default page. |
294 */ |
316 */ |
295 bool HsPage::isDefaultPage() const |
317 bool HsPage::isDefaultPage() const |
296 { |
318 { |
297 return mDatabaseId == HSCONFIGURATION_GET(defaultPageId); |
319 return mDatabaseId == HSCONFIGURATION_GET(defaultPageId); |
298 } |
320 } |
|
321 |
299 /*! |
322 /*! |
300 Return true if page is active page. |
323 Return true if page is active page. |
301 */ |
324 */ |
302 bool HsPage::isActivePage() const |
325 bool HsPage::isActivePage() const |
303 { |
326 { |
304 return this == HsScene::instance()->activePage(); |
327 return this == HsScene::instance()->activePage(); |
305 } |
328 } |
|
329 |
306 /*! |
330 /*! |
307 Create page into database and return instance of a new page. |
331 Create page into database and return instance of a new page. |
308 */ |
332 */ |
309 HsPage *HsPage::createInstance(const HsPageData &pageData) |
333 HsPage *HsPage::createInstance(const HsPageData &pageData) |
310 { |
334 { |
320 |
344 |
321 return 0; |
345 return 0; |
322 } |
346 } |
323 |
347 |
324 /*! |
348 /*! |
|
349 The widget is bounded in the rectangle which is smaller by pageMargin on all sides of page. |
|
350 */ |
|
351 QPointF HsPage::adjustedWidgetPosition(const QRectF &origWidgetRect) |
|
352 { |
|
353 QRectF widgetAreaRect = contentGeometry(); |
|
354 qreal widgetX = qBound(widgetAreaRect.left(), origWidgetRect.x(), widgetAreaRect.right() - origWidgetRect.width()); |
|
355 qreal widgetY = qBound(widgetAreaRect.top(), origWidgetRect.y(), widgetAreaRect.bottom() - origWidgetRect.height()); |
|
356 |
|
357 return QPointF(widgetX, widgetY); |
|
358 } |
|
359 |
|
360 /*! |
|
361 Returns rect of rectangular where widgets are allowed to be placed in the page. |
|
362 */ |
|
363 QRectF HsPage::contentGeometry() |
|
364 { |
|
365 return contentGeometry(HsScene::mainWindow()->orientation()); |
|
366 } |
|
367 |
|
368 /*! |
|
369 Returns rect of rectangular where widgets are allowed to be placed in the page. |
|
370 */ |
|
371 QRectF HsPage::contentGeometry(Qt::Orientation orientation) |
|
372 { |
|
373 QRectF pageRect = HsScene::mainWindow()->layoutRect(); |
|
374 |
|
375 if (orientation != HsScene::orientation()) { |
|
376 qreal width = pageRect.width(); |
|
377 qreal height = pageRect.height(); |
|
378 pageRect.setWidth(height); |
|
379 pageRect.setHeight(width); |
|
380 } |
|
381 |
|
382 //Take care of chrome in both orientation |
|
383 pageRect.setTop(64); |
|
384 |
|
385 //Shrink by page margins at each side |
|
386 return pageRect.adjusted(mPageMargin, mPageMargin, -mPageMargin, -mPageMargin); |
|
387 } |
|
388 |
|
389 /*! |
|
390 Returns rect of rectangular where widgets are allowed to be placed in the page. |
|
391 */ |
|
392 QRectF HsPage::contentRect() |
|
393 { |
|
394 return contentRect(HsScene::mainWindow()->orientation()); |
|
395 } |
|
396 |
|
397 /*! |
|
398 Returns rect of rectangular where widgets are allowed to be placed in the page. |
|
399 */ |
|
400 QRectF HsPage::contentRect(Qt::Orientation orientation) |
|
401 { |
|
402 QRectF rect = contentGeometry(orientation); |
|
403 rect.moveTopLeft(QPointF(0,0)); |
|
404 return rect; |
|
405 } |
|
406 |
|
407 /*! |
325 Calls onShow() for contained widgets. |
408 Calls onShow() for contained widgets. |
326 */ |
409 */ |
327 void HsPage::showWidgets() |
410 void HsPage::showWidgets() |
328 { |
411 { |
329 foreach (HsWidgetHost *widget, mWidgets) { |
412 foreach (HsWidgetHost *widget, mWidgets) { |
344 } |
427 } |
345 } |
428 } |
346 } |
429 } |
347 |
430 |
348 /*! |
431 /*! |
349 Propogate online state to widgets. |
432 Propagate online state to widgets. |
350 */ |
433 */ |
351 void HsPage::setOnline(bool online) |
434 void HsPage::setOnline(bool online) |
352 { |
435 { |
353 foreach (HsWidgetHost *widget, mNewWidgets) { |
436 foreach (HsWidgetHost *widget, mNewWidgets) { |
354 widget->setOnline(online); |
437 widget->setOnline(online); |
355 } |
438 } |
356 foreach (HsWidgetHost *widget, mWidgets) { |
439 foreach (HsWidgetHost *widget, mWidgets) { |
357 widget->setOnline(online); |
440 widget->setOnline(online); |
358 } |
441 } |
359 } |
442 } |
|
443 |
360 /*! |
444 /*! |
361 Update widgets z-values and persist those. Active widget has top most |
445 Update widgets z-values and persist those. Active widget has top most |
362 z-value. |
446 z-value. |
363 */ |
447 */ |
364 void HsPage::updateZValues() |
448 void HsPage::updateZValues() |
455 */ |
539 */ |
456 void HsPage::onWidgetResized() |
540 void HsPage::onWidgetResized() |
457 { |
541 { |
458 if ( !layout() ) { |
542 if ( !layout() ) { |
459 HsWidgetHost *widget = qobject_cast<HsWidgetHost *>(sender()); |
543 HsWidgetHost *widget = qobject_cast<HsWidgetHost *>(sender()); |
460 QRectF widgetRect = widget->geometry(); |
544 widget->setPos(adjustedWidgetPosition(widget->geometry())); |
461 QRectF pageRect = HsScene::mainWindow()->layoutRect(); |
|
462 qreal widgetX = qBound(qreal(0), widgetRect.x(), pageRect.width() - widgetRect.width()); |
|
463 qreal widgetY = qBound(qreal(64), widgetRect.y(), pageRect.height() - widgetRect.height()); |
|
464 widget->setPos(widgetX, widgetY); |
|
465 } else { |
545 } else { |
466 layout()->invalidate(); |
546 layout()->invalidate(); |
467 } |
547 } |
468 } |
548 } |
469 /*! |
549 /*! |
498 mUnavailableWidgets.append(widget); |
578 mUnavailableWidgets.append(widget); |
499 |
579 |
500 widget->hide(); |
580 widget->hide(); |
501 widget->setParentItem(0); |
581 widget->setParentItem(0); |
502 } |
582 } |
|
583 |
503 /*! |
584 /*! |
504 Run positioning algorithm for widgets which don't have position on |
585 Run positioning algorithm for widgets which don't have position on |
505 target orientation. Otherwise set orientation positions for widgets. |
586 target orientation. Otherwise set orientation positions for widgets. |
506 */ |
587 */ |
507 void HsPage::onOrientationChanged(Qt::Orientation orientation) |
588 void HsPage::onOrientationChanged(Qt::Orientation orientation) |
508 { |
589 { |
509 QRectF rect = HsScene::mainWindow()->layoutRect(); |
|
510 |
|
511 HsWidgetPositioningOnOrientationChange *converter = |
590 HsWidgetPositioningOnOrientationChange *converter = |
512 HsWidgetPositioningOnOrientationChange::instance(); |
591 HsWidgetPositioningOnOrientationChange::instance(); |
513 |
592 |
514 qreal chrome = 64; |
593 Qt::Orientation orientationFrom = orientation == Qt::Vertical ? Qt::Horizontal : Qt::Vertical; |
515 QRectF from(0, chrome, rect.height(), rect.width() - chrome); |
594 QRectF from = contentGeometry(orientationFrom); |
516 QRectF to(0, chrome, rect.width(), rect.height() - chrome); |
595 QRectF to = contentGeometry(orientation); |
517 |
596 |
518 HsWidgetPresentationData presentation; |
597 HsWidgetPresentationData presentation; |
519 presentation.orientation = orientation; |
598 presentation.orientation = orientation; |
520 |
599 |
521 foreach (HsWidgetHost *widget, mWidgets) { |
600 foreach (HsWidgetHost *widget, mWidgets) { |
523 QList<QRectF> geometries = converter->convert( |
602 QList<QRectF> geometries = converter->convert( |
524 from, QList<QRectF>() << widget->geometry(), to); |
603 from, QList<QRectF>() << widget->geometry(), to); |
525 widget->setGeometry(geometries.first()); |
604 widget->setGeometry(geometries.first()); |
526 widget->savePresentation(); |
605 widget->savePresentation(); |
527 } else { |
606 } else { |
528 widget->setPos(presentation.pos()); |
607 QRectF adjustWidgetPosition; |
|
608 adjustWidgetPosition = widget->geometry(); |
|
609 adjustWidgetPosition.moveTopLeft(presentation.pos()); |
|
610 widget->setPos(adjustedWidgetPosition(adjustWidgetPosition)); |
529 widget->setZValue(presentation.zValue); |
611 widget->setZValue(presentation.zValue); |
530 } |
612 widget->savePresentation(); //Needed to follow pageMargin dynamic change |
531 } |
613 } |
532 } |
614 } |
533 |
615 } |
534 /*! |
616 |
535 Clears new widgets list and resets layout. |
617 void HsPage::onPageMarginChanged(const QString &value) |
536 */ |
618 { |
537 void HsPage::resetNewWidgets() |
619 if (value == "pageMargin") { |
538 { |
620 mPageMargin = HSCONFIGURATION_GET(pageMargin); |
539 mNewWidgets.clear(); |
621 |
540 setLayout(0); |
622 if (!mWidgets.isEmpty()) { |
541 } |
623 foreach (HsWidgetHost *widget, mWidgets) { |
542 |
624 widget->setPos(adjustedWidgetPosition(widget->geometry())); |
|
625 widget->savePresentation(); |
|
626 } |
|
627 } |
|
628 |
|
629 if (!mNewWidgets.isEmpty()) { |
|
630 foreach (HsWidgetHost *widget, mNewWidgets) { |
|
631 widget->setPos(adjustedWidgetPosition(widget->geometry())); |
|
632 widget->savePresentation(); |
|
633 } |
|
634 } |
|
635 } |
|
636 } |