homescreenapp/hsapplication/tsrc/t_hsapplication/t_hsapplicationexe/src/hspage_mock.cpp
changeset 90 3ac3aaebaee5
child 101 ba1b7e218624
equal deleted inserted replaced
86:e4f038c420f7 90:3ac3aaebaee5
       
     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 <HbInstance>
       
    19 
       
    20 #include "hsdomainmodeldatastructures.h"
       
    21 #include "hsgui.h"
       
    22 #include "hspage.h"
       
    23 #include "hspagevisual.h"
       
    24 #include "hspagetoucharea.h"
       
    25 #include "hspagenewwidgetlayout.h"
       
    26 #include "hsscene.h"
       
    27 #include "hswidgethost.h"
       
    28 #include "hswidgethostvisual.h"
       
    29 #include "hswallpaper.h"
       
    30 #include "hswidgetpositioningonwidgetadd.h"
       
    31 #include "hswidgetpositioningonorientationchange.h"
       
    32 #include "hsconfiguration.h"
       
    33 
       
    34 
       
    35 /*!
       
    36     \class HsPage
       
    37     \ingroup group_hsdomainmodel
       
    38     \brief Represents a page in the framework.
       
    39     HsPage contains group of widgets. HsPage can have a wallpaper.
       
    40 */
       
    41 
       
    42 /*!
       
    43     Constructor.
       
    44 
       
    45     \a parent Owner.
       
    46     \a aFlags Window flags.
       
    47 */
       
    48 HsPage::HsPage(QObject* parent)
       
    49     : QObject(parent),
       
    50       mDatabaseId(-1),
       
    51       mPageVisual(new HsPageVisual), 
       
    52       mWallpaper(0),
       
    53       mRemovable(true),
       
    54       mPageMargin(0.0)
       
    55 {
       
    56     //Page margin
       
    57     mPageMargin = HSCONFIGURATION_GET(pageMargin);
       
    58     connect(HsConfiguration::instance(), SIGNAL(propertyChanged(QString)), SLOT(onPageMarginChanged(QString)));
       
    59 }
       
    60 
       
    61 /*!
       
    62     Destructor.
       
    63 */
       
    64 HsPage::~HsPage()
       
    65 {
       
    66     delete mWallpaper;
       
    67 }
       
    68 
       
    69 /*!
       
    70     Returns the database id.
       
    71 */
       
    72 int HsPage::databaseId() const
       
    73 {
       
    74     return mDatabaseId;
       
    75 }
       
    76 
       
    77 /*!
       
    78     Sets the database id to \a id.
       
    79 */
       
    80 void HsPage::setDatabaseId(int id)
       
    81 {
       
    82     mDatabaseId = id;
       
    83 }
       
    84 
       
    85 /*!
       
    86     Loads widgets.
       
    87 */
       
    88 bool HsPage::load()
       
    89 {
       
    90     if (HSCONFIGURATION_GET(sceneType) == HsConfiguration::PageWallpapers) {
       
    91         mWallpaper = new HsPageWallpaper(this);
       
    92     }
       
    93 
       
    94     HsWidgetHost *widget = new HsWidgetHost(-1);
       
    95     mWidgets.append(widget);
       
    96     connectWidget(widget);
       
    97     widget->setPage(this);
       
    98     widget->visual()->setParentItem(this->visual());
       
    99     widget->startWidget();
       
   100     return true;
       
   101 }
       
   102 
       
   103 /*!
       
   104     Return wallpaper.
       
   105 */
       
   106 HsWallpaper *HsPage::wallpaper() const
       
   107 {
       
   108     return mWallpaper;
       
   109 }
       
   110 /*!
       
   111     Add given existing \a widgetHost to a page. Returns true if successful
       
   112 */
       
   113 bool HsPage::addExistingWidget(HsWidgetHost *widgetHost)
       
   114 {
       
   115     if (!widgetHost) {
       
   116         return false;
       
   117     }
       
   118 
       
   119     if (mWidgets.contains(widgetHost)) {
       
   120         return true;
       
   121     }
       
   122 
       
   123     if (!widgetHost->setPage(this)) {
       
   124         return false;
       
   125     }
       
   126 
       
   127     connectWidget(widgetHost);
       
   128     mWidgets << widgetHost;
       
   129     widgetHost->visual()->setParentItem(this->visual());
       
   130 
       
   131     return true;
       
   132  }
       
   133 
       
   134 /*!
       
   135     Remove given \a widgetHost from a page. Widget is not deleted.
       
   136     Returns true if successful
       
   137 */
       
   138 bool HsPage::removeWidget(HsWidgetHost *widgetHost)
       
   139 {
       
   140     if (!widgetHost || !widgetHost->setPage(0)) {
       
   141         return false;
       
   142     }
       
   143 
       
   144     disconnectWidget(widgetHost);
       
   145     mWidgets.removeOne(widgetHost);
       
   146     widgetHost->visual()->setParentItem(0);
       
   147 
       
   148     return true;
       
   149 }
       
   150 
       
   151 /*!
       
   152     Returns list of new widgets belonging to a page. Widgets which are
       
   153     not yet layouted are considered as new widgets.
       
   154 */
       
   155 QList<HsWidgetHost *> HsPage::newWidgets()
       
   156 {
       
   157     return mNewWidgets;
       
   158 }
       
   159 
       
   160 /*!
       
   161     Adds new widget into a page. Returns true if successfull.
       
   162 */
       
   163 bool HsPage::addNewWidget(HsWidgetHost* widgetHost, const QPointF &touchPoint)
       
   164 {
       
   165     Q_UNUSED(touchPoint)
       
   166     if (!widgetHost || mWidgets.contains(widgetHost)) {
       
   167         return false;
       
   168     }
       
   169 
       
   170     if (mNewWidgets.contains(widgetHost)) {
       
   171         return true;
       
   172     }
       
   173 
       
   174     HsWidgetPresentationData presentation;
       
   175     presentation.orientation = Qt::Vertical;
       
   176     if (!widgetHost->getPresentation(presentation)) {
       
   177         presentation.orientation = Qt::Vertical;
       
   178         presentation.setPos(QPointF());
       
   179         presentation.zValue = 0;
       
   180         widgetHost->savePresentation(presentation);
       
   181     }
       
   182 
       
   183     widgetHost->visual()->hide();
       
   184     widgetHost->visual()->setPos(presentation.x, presentation.y);
       
   185     widgetHost->visual()->setZValue(presentation.zValue);
       
   186 
       
   187     connectWidget(widgetHost);
       
   188     mNewWidgets << widgetHost;
       
   189 
       
   190     return true;
       
   191 }
       
   192 
       
   193 /*!
       
   194     Layouts all the new widgets
       
   195 */
       
   196 void HsPage::layoutNewWidgets()
       
   197 {
       
   198     if (mNewWidgets.isEmpty()) {
       
   199         return;
       
   200     }
       
   201 
       
   202     updateZValues();
       
   203     HsWidgetHost *widget = 0;
       
   204     for (int i = 0; i < mNewWidgets.count(); ++i) {
       
   205         widget = mNewWidgets.at(i);
       
   206 //Not used in mock        newWidgetLayout->addItem(widget);
       
   207         widget->setPage(this);
       
   208         widget->visual()->setParentItem(visual());
       
   209         widget->showWidget();
       
   210         widget->visual()->show();
       
   211     }
       
   212     mWidgets << mNewWidgets;
       
   213     mNewWidgets.clear();
       
   214 }
       
   215 
       
   216 /*!
       
   217     Clears new widgets list and resets layout.
       
   218 */
       
   219 void HsPage::resetNewWidgets()
       
   220 {
       
   221     mNewWidgets.clear();
       
   222 }
       
   223 
       
   224 /*!
       
   225     Remove page and all it's contained widgets from database
       
   226 */
       
   227 bool HsPage::deleteFromDatabase()
       
   228 {
       
   229 	//Not used in mock
       
   230     return true;
       
   231 }
       
   232 
       
   233 /*!
       
   234     Return list of widgets belonging to a page
       
   235 */
       
   236 QList<HsWidgetHost *> HsPage::widgets() const
       
   237 {
       
   238     return mWidgets;
       
   239 }
       
   240 
       
   241 /*!
       
   242     Returns true if the page can be removed. Otherwise,
       
   243     returns false.
       
   244 */
       
   245 bool HsPage::isRemovable() const
       
   246 {
       
   247     return mRemovable;
       
   248 }
       
   249 
       
   250 /*!
       
   251     Sets removable flag to \a removable.
       
   252 */
       
   253 void HsPage::setRemovable(bool removable)
       
   254 {
       
   255     mRemovable = removable;
       
   256 }
       
   257 
       
   258 /*!
       
   259     Return true if page is default page.
       
   260 */
       
   261 bool HsPage::isDefaultPage() const
       
   262 {
       
   263     return mDatabaseId == HSCONFIGURATION_GET(defaultPageId);
       
   264 }
       
   265 
       
   266 /*!
       
   267     Return true if page is active page.
       
   268 */
       
   269 bool HsPage::isActivePage() const
       
   270 {
       
   271     return this == HsScene::instance()->activePage();
       
   272 }
       
   273 
       
   274 /*!
       
   275     Create page into database and return instance of a new page.
       
   276 */
       
   277 HsPage *HsPage::createInstance(const HsPageData &pageData)
       
   278 {
       
   279     Q_UNUSED(pageData);
       
   280     HsPage *page = new HsPage;
       
   281     return page;
       
   282 }
       
   283 
       
   284 /*!
       
   285     The widget is bounded in the rectangle which is smaller by PageMargin on all sides of page.
       
   286 */
       
   287 QPointF HsPage::adjustedWidgetPosition(const QRectF &origWidgetRect)
       
   288 {
       
   289     QRectF widgetAreaRect = contentGeometry();
       
   290     qreal widgetX = qBound(widgetAreaRect.left(), origWidgetRect.x(), widgetAreaRect.right() - origWidgetRect.width());
       
   291     qreal widgetY = qBound(widgetAreaRect.top(), origWidgetRect.y(), widgetAreaRect.bottom() - origWidgetRect.height());
       
   292 
       
   293     return QPointF(widgetX, widgetY);
       
   294 }
       
   295 
       
   296 /*!
       
   297     Returns rect of rectangular where widgets are allowed to be placed in the page.
       
   298 */
       
   299 QRectF HsPage::contentGeometry()
       
   300 {
       
   301     return contentGeometry(Qt::Vertical);
       
   302 }
       
   303 
       
   304 /*!
       
   305     Returns rect of rectangular where widgets are allowed to be placed in the page.
       
   306 */
       
   307 QRectF HsPage::contentGeometry(Qt::Orientation orientation)
       
   308 {
       
   309     QRectF pageRect = QRectF(0, 0, 360.0, 640.0);
       
   310 
       
   311     //Take care of chrome in both orientation
       
   312     pageRect.setTop(64);
       
   313 
       
   314     //Shrink by page margins at each side
       
   315     return pageRect.adjusted(mPageMargin, mPageMargin, -mPageMargin, -mPageMargin);
       
   316 }
       
   317 
       
   318 /*!
       
   319     Returns rect of rectangular where widgets are allowed to be placed in the page.
       
   320 */
       
   321 QRectF HsPage::contentRect()
       
   322 {
       
   323     return contentRect(Qt::Vertical);
       
   324 }
       
   325 
       
   326 /*!
       
   327     Returns rect of rectangular where widgets are allowed to be placed in the page.
       
   328 */
       
   329 QRectF HsPage::contentRect(Qt::Orientation orientation)
       
   330 {
       
   331     QRectF rect = contentGeometry(orientation);
       
   332     rect.moveTopLeft(QPointF(0,0));
       
   333     return rect;
       
   334 }
       
   335 
       
   336 /*!
       
   337     Calls onShow() for contained widgets.
       
   338 */
       
   339 void HsPage::showWidgets()
       
   340 {
       
   341 }
       
   342 
       
   343 /*!
       
   344     Calls onHide() for contained widgets.
       
   345 */
       
   346 void HsPage::hideWidgets()
       
   347 {
       
   348 }
       
   349 
       
   350 /*!
       
   351     Propagate online state to widgets.
       
   352 */
       
   353 void HsPage::setOnline(bool online)
       
   354 {
       
   355     foreach (HsWidgetHost *widget, mNewWidgets) {
       
   356         widget->setOnline(online);
       
   357     }
       
   358     foreach (HsWidgetHost *widget, mWidgets) {
       
   359         widget->setOnline(online);
       
   360     }
       
   361 }
       
   362 
       
   363 /*!
       
   364     Update widgets z-values and persist those. Active widget has top most
       
   365     z-value.
       
   366 */
       
   367 void HsPage::updateZValues()
       
   368 {
       
   369 }
       
   370 
       
   371 /*!
       
   372     Return this page's index.
       
   373 */
       
   374 int HsPage::pageIndex()
       
   375 {
       
   376     return HsScene::instance()->pages().indexOf(this);
       
   377 }
       
   378 /*!
       
   379     Utility to connect widget signals to page.
       
   380 */
       
   381 void HsPage::connectWidget(HsWidgetHost *widget)
       
   382 {
       
   383     connect(widget, SIGNAL(finished()), SLOT(onWidgetFinished()));
       
   384     connect(widget, SIGNAL(faulted()), SLOT(onWidgetFaulted()));
       
   385     connect(widget, SIGNAL(resized()), SLOT(onWidgetResized()));
       
   386     connect(widget, SIGNAL(available()), SLOT(onWidgetAvailable()));
       
   387     connect(widget, SIGNAL(unavailable()), SLOT(onWidgetUnavailable()));
       
   388 }
       
   389 /*!
       
   390     Disconnect widget signals from page
       
   391 */
       
   392 void HsPage::disconnectWidget(HsWidgetHost *widget)
       
   393 {
       
   394     widget->disconnect(this);
       
   395 }
       
   396 /*!
       
   397     Disconnect and remove widget
       
   398 */
       
   399 void HsPage::onWidgetFinished()
       
   400 {
       
   401     HsWidgetHost *widget = qobject_cast<HsWidgetHost *>(sender());
       
   402 
       
   403     // It can be in new widget list if we haven't layouted it yet
       
   404     // or layouted new widget and widget list
       
   405     if (!mNewWidgets.removeOne(widget)) {
       
   406         mWidgets.removeOne(widget);
       
   407     }
       
   408 
       
   409     disconnectWidget(widget);
       
   410     widget->remove();
       
   411 }
       
   412 /*!
       
   413     Remove widget if it faulted
       
   414 */
       
   415 void HsPage::onWidgetFaulted()
       
   416 {
       
   417     onWidgetFinished();
       
   418 }
       
   419 
       
   420 /*!
       
   421     Calculates new widget position on page when widget size changes. If page has layout then there are new widgets
       
   422     and we use layout to calculate new widget positions.
       
   423 */
       
   424 void HsPage::onWidgetResized()
       
   425 {
       
   426 }
       
   427 /*!
       
   428     Show widget if it came available
       
   429 */
       
   430 void HsPage::onWidgetAvailable()
       
   431 {
       
   432 }
       
   433 /*!
       
   434     Update internal bookkeeping and hide widget
       
   435 */
       
   436 void HsPage::onWidgetUnavailable()
       
   437 {
       
   438 }
       
   439 
       
   440 /*!
       
   441     Run positioning algorithm for widgets which don't have position on
       
   442     target orientation. Otherwise set orientation positions for widgets.
       
   443 */
       
   444 void HsPage::onOrientationChanged(Qt::Orientation orientation)
       
   445 {
       
   446     Q_UNUSED(orientation)
       
   447 }
       
   448 
       
   449 void HsPage::onPageMarginChanged(const QString &value)
       
   450 {
       
   451     Q_UNUSED(value)
       
   452 }
       
   453 
       
   454 HsPageVisual *HsPage::visual() const
       
   455 {
       
   456     return mPageVisual;
       
   457 }