phoneplugins/infowidgetplugin/infowidget/src/infowidgetlayoutmanager.cpp
branchRCL_3
changeset 24 41a7f70b3818
equal deleted inserted replaced
23:40a3f856b14d 24:41a7f70b3818
       
     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 <QtGlobal>
       
    19 #include <QObject>
       
    20 #include <QGraphicsWidget>
       
    21 #include <hbdocumentloader.h>
       
    22 #include <hblabel.h>
       
    23 #include <hbaction.h>
       
    24 #include <hbmarqueeitem.h>
       
    25 #include <hbiconitem.h> 
       
    26 #include <hbpushbutton.h>
       
    27 #include <hbinstance.h>
       
    28 #include "infowidgetlayoutmanager.h"
       
    29 #include "infowidgetlogging.h"
       
    30 
       
    31 /*!
       
    32   \class InfoWidgetDocumentLoader
       
    33   \brief Custom document loader for Operator info widget  
       
    34 
       
    35    Derived from HbDocumentLoader.
       
    36     
       
    37 */
       
    38 
       
    39 /*!
       
    40   \class InfoWidgetLayoutManager
       
    41   \brief Layout manager class for Operator info widget.   
       
    42 
       
    43    Handles layout document loading and accessing the loaded 
       
    44    widgets.   
       
    45     
       
    46 */
       
    47 
       
    48 // Local constants 
       
    49 const char INFOWIDGET_DOCML_FILE[] = ":/resource/infowidget.docml";
       
    50 const char SETTINGS_DIALOG_DOCML_FILE[] = ":/resource/settingsdialog.docml";
       
    51 
       
    52 const char LAYOUT_PREFIX_INFO_DISPLAY[] = "id:";
       
    53 const char LAYOUT_PREFIX_SETTINGS_DIALOG[] = "sd:";
       
    54 const char LAYOUT_NAME_CONTENT[] = "content";
       
    55 const char LAYOUT_NAME_MCNMARQUEEITEM[] = "mcnMarqueeItem";
       
    56 const char LAYOUT_NAME_SPNMARQUEEITEM[] = "spnMarqueeItem";
       
    57 const char LAYOUT_NAME_SATMARQUEEITEM[] = "satMarqueeItem";
       
    58 const char LAYOUT_NAME_SPNICON[] = "spnIcon";
       
    59 const char LAYOUT_NAME_MCNICON[] = "mcnIcon";
       
    60 const char LAYOUT_NAME_SATTEXTICON[] = "satTextIcon";
       
    61 const char LAYOUT_NAME_SPNCHECKBOX[] = "spnCheckBox";
       
    62 const char LAYOUT_NAME_MCNCHECKBOX[] = "mcnCheckBox";
       
    63 const char LAYOUT_NAME_SATTEXTCHECKBOX[] = "satTextCheckBox";
       
    64 const char LAYOUT_NAME_OKACTION[] = "okAction";
       
    65 const char LAYOUT_NAME_CANCELACTION[] = "cancelAction";
       
    66 const char LAYOUT_NAME_SETTINGSDIALOG[] = "settingsDialog"; 
       
    67 const char LAYOUT_NAME_SETTINGSCONTAINER[] = "settingsContainer";
       
    68 const char LAYOUT_NAME_CONTAINER[] = "container";
       
    69 
       
    70 /*!
       
    71   Create object from document. 
       
    72  */
       
    73 QObject *InfoWidgetDocumentLoader::createObject(
       
    74     const QString &type,
       
    75     const QString &name)
       
    76 {
       
    77     DPRINT;
       
    78     if ( type == HbMarqueeItem::staticMetaObject.className() ) {
       
    79         QObject *object = new HbMarqueeItem;
       
    80         object->setObjectName(name);
       
    81         return object;
       
    82     }
       
    83     return HbDocumentLoader::createObject(type, name);
       
    84 }
       
    85 
       
    86 /*!
       
    87    Constructor. 
       
    88 */
       
    89 InfoWidgetLayoutManager::InfoWidgetLayoutManager(QObject *parent) 
       
    90 : QObject(parent), 
       
    91   m_documentLoader(NULL), 
       
    92   m_displayRole(InfoDisplay),
       
    93   m_cachedLayoutRowHeight(0.0)
       
    94 {
       
    95     DPRINT; 
       
    96     m_documentLoader.reset(new InfoWidgetDocumentLoader);
       
    97 }
       
    98 
       
    99 /*!
       
   100    Destructor. 
       
   101 */
       
   102 InfoWidgetLayoutManager::~InfoWidgetLayoutManager()
       
   103 {
       
   104     DPRINT;
       
   105 }
       
   106 
       
   107 /*!
       
   108    Destroy all widgets. 
       
   109    Deletes parent widgets of each display 
       
   110    causing deletion of child items. 
       
   111 */
       
   112 void InfoWidgetLayoutManager::destroyWidgets()
       
   113 {
       
   114     DPRINT;
       
   115     // Destroy parent items 
       
   116     removeWidget(RoleContent); 
       
   117     removeWidget(RoleSettingsDialog); 
       
   118 }
       
   119 
       
   120 /*!
       
   121    InfoWidgetLayoutManager::currentDisplayRole()
       
   122 */
       
   123 InfoWidgetLayoutManager::DisplayRole InfoWidgetLayoutManager::currentDisplayRole() 
       
   124 {
       
   125     DPRINT; 
       
   126     return m_displayRole;
       
   127 } 
       
   128 
       
   129 /*!
       
   130    InfoWidgetLayoutManager::currentWidgetRoles()
       
   131 */
       
   132 QList<InfoWidgetLayoutManager::LayoutItemRole> InfoWidgetLayoutManager::currentWidgetRoles() 
       
   133 {
       
   134     DPRINT; 
       
   135     return m_widgets.keys(); 
       
   136 } 
       
   137 
       
   138 /*!
       
   139    Read row height from style. 
       
   140 */
       
   141 qreal InfoWidgetLayoutManager::layoutRowHeight()
       
   142 {
       
   143     DPRINT;
       
   144     // Read from style only if not already initialized
       
   145     if (m_cachedLayoutRowHeight == 0.0) { 
       
   146         bool ok = hbInstance->style()->parameter("hb-param-graphic-size-primary-small", 
       
   147                 m_cachedLayoutRowHeight);
       
   148             DPRINT << ": row height from style: " << m_cachedLayoutRowHeight;
       
   149         if (!ok) {
       
   150             DWARNING << ": Error, paremeters reading failed!";
       
   151         }
       
   152     }
       
   153     return m_cachedLayoutRowHeight;
       
   154 }
       
   155 
       
   156 /*!
       
   157    Check if text fits to given rect width.  
       
   158 */
       
   159 bool InfoWidgetLayoutManager::textFitsToRect(QString text, 
       
   160         QFont font, QRectF rect) const 
       
   161 {
       
   162     DPRINT; 
       
   163     bool fits(true);
       
   164     if (!rect.isEmpty()) { 
       
   165         QFontMetricsF metrics(font);
       
   166         qreal width = metrics.boundingRect(text).width();
       
   167         if (width > rect.width() ) {
       
   168             fits = false; 
       
   169         }
       
   170     }
       
   171     return fits; 
       
   172 }
       
   173 
       
   174 /*!
       
   175    Returns content widget.  
       
   176    The content widget is layout main widget and parent for 
       
   177    sub-widgets in current display.     
       
   178 */
       
   179 QGraphicsWidget* InfoWidgetLayoutManager::contentWidget()
       
   180 {
       
   181     DPRINT; 
       
   182     return getWidget(RoleContent); 
       
   183 }
       
   184 
       
   185 /*!
       
   186    Returns list of marquee items.  
       
   187 */
       
   188 QList<HbMarqueeItem *> InfoWidgetLayoutManager::marqueeItems() 
       
   189 {
       
   190     DPRINT;
       
   191     QList<HbMarqueeItem *> items;
       
   192     
       
   193     QList<LayoutItemRole> marqueeItemRoles; 
       
   194     marqueeItemRoles.append(RoleSpnMarqueeItem); 
       
   195     marqueeItemRoles.append(RoleMcnMarqueeItem); 
       
   196     marqueeItemRoles.append(RoleSatMarqueeItem); 
       
   197 
       
   198     foreach (LayoutItemRole role, marqueeItemRoles) {
       
   199         QGraphicsWidget *widget = getWidget(role); 
       
   200         if (widget) {
       
   201             HbMarqueeItem *item = 
       
   202                     qobject_cast<HbMarqueeItem*>(widget); 
       
   203             if (item) {
       
   204                 items.append(item); 
       
   205                 item = NULL; 
       
   206             }
       
   207         }
       
   208     }
       
   209     return items; 
       
   210 }
       
   211 
       
   212 /*!
       
   213    Get widget with given item role. 
       
   214 */
       
   215 QGraphicsWidget* InfoWidgetLayoutManager::getWidget(LayoutItemRole itemRole)
       
   216 {
       
   217     return m_widgets.value(itemRole);
       
   218 }
       
   219 
       
   220 /*!
       
   221    Get object with given item role. 
       
   222 */
       
   223 QObject* InfoWidgetLayoutManager::getObject(LayoutItemRole itemRole)
       
   224 {
       
   225     return m_objects.value(itemRole);
       
   226 }
       
   227 
       
   228 /*!
       
   229    Remove widget with given item role. 
       
   230 */
       
   231 void InfoWidgetLayoutManager::removeWidget(LayoutItemRole itemRole, 
       
   232         bool deleteLater)
       
   233 {
       
   234     DPRINT;
       
   235     QGraphicsWidget *widget = m_widgets.value(itemRole); 
       
   236     if (widget) {
       
   237         if (!deleteLater) {
       
   238             delete widget;
       
   239         } else {
       
   240             widget->deleteLater(); 
       
   241         }
       
   242     }
       
   243     m_widgets.remove(itemRole);
       
   244     m_infoDisplayWidgets.remove(itemRole);
       
   245     m_settingsDialogWidgets.remove(itemRole);
       
   246 }
       
   247 
       
   248 /*!
       
   249     Returns info display layout.  
       
   250 */
       
   251 QGraphicsWidget* InfoWidgetLayoutManager::layoutInfoDisplay()
       
   252 {   
       
   253     DPRINT;
       
   254     m_displayRole = InfoDisplay;
       
   255     
       
   256     loadWidgets(InfoWidgetLayoutManager::InfoDisplay);
       
   257     m_widgets = m_infoDisplayWidgets; 
       
   258     
       
   259     QGraphicsWidget *infoDisplay = getWidget(RoleContent);
       
   260     Q_ASSERT(infoDisplay); 
       
   261     return infoDisplay; 
       
   262 }
       
   263 
       
   264 /*!
       
   265     Returns settings dialog layout. 
       
   266 */
       
   267 QGraphicsWidget* InfoWidgetLayoutManager::layoutSettingsDialog()
       
   268 {   
       
   269     DPRINT;
       
   270     m_displayRole = SettingsDialog;
       
   271     loadWidgets(InfoWidgetLayoutManager::SettingsDialog); 
       
   272     m_widgets = m_settingsDialogWidgets; 
       
   273     
       
   274     QGraphicsWidget *dialog = getWidget(RoleSettingsDialog);
       
   275     Q_ASSERT(dialog);
       
   276     
       
   277     HbAction *okAction = qobject_cast<HbAction *>(
       
   278             getObject(RoleOkAction));
       
   279     HbAction *cancelAction = qobject_cast<HbAction *>(
       
   280             getObject(RoleCancelAction));
       
   281     if (okAction && cancelAction) {
       
   282         dialog->addAction(okAction);
       
   283         dialog->addAction(cancelAction);
       
   284     }
       
   285     return dialog;    
       
   286 }
       
   287 
       
   288 /*!
       
   289     Load widgets from document for given display role.
       
   290 */
       
   291 bool InfoWidgetLayoutManager::loadWidgets(const DisplayRole displayRole, 
       
   292         const QList<LayoutItemRole> &displayWidgets,
       
   293         QMap<LayoutItemRole, QGraphicsWidget *> &widgetMap)
       
   294 {
       
   295     DPRINT;
       
   296     bool loadResult(true); 
       
   297 
       
   298     // Cleanup previously loaded content in case of any data  
       
   299     widgetMap.clear(); 
       
   300     
       
   301     bool loaded = true;
       
   302     if (displayRole != SettingsDialog) {
       
   303         m_documentLoader->load(INFOWIDGET_DOCML_FILE, &loaded);
       
   304     } else {
       
   305         m_documentLoader->load(SETTINGS_DIALOG_DOCML_FILE, &loaded);
       
   306     }
       
   307     
       
   308     Q_ASSERT_X(loaded, 
       
   309             "InfoWidgetLayoutManager", 
       
   310             "Invalid docml file");    
       
   311     
       
   312     foreach (LayoutItemRole role, displayWidgets) {
       
   313         QGraphicsWidget *widget = 
       
   314                 loadWidget(*m_documentLoader, displayRole, role);
       
   315            if (widget) {
       
   316                widgetMap.insert(role, widget);
       
   317                widget = NULL;
       
   318            } 
       
   319     }
       
   320     
       
   321     if (widgetMap.count() == displayWidgets.count()) {
       
   322         loadResult = true;
       
   323         } else {
       
   324             DWARNING << ": all widgets were not loaded!";
       
   325             loadResult = false;
       
   326         }        
       
   327 
       
   328     m_objects.clear();
       
   329     if (displayRole == SettingsDialog) {
       
   330         QObject *okAction = 
       
   331                 loadObject(*m_documentLoader, 
       
   332                         displayRole, 
       
   333                         RoleOkAction);
       
   334                 m_objects.insert(RoleOkAction, okAction); 
       
   335         QObject *cancelAction = 
       
   336                 loadObject(*m_documentLoader, 
       
   337                         displayRole, 
       
   338                         RoleCancelAction); 
       
   339                 m_objects.insert(RoleCancelAction, cancelAction); 
       
   340     } 
       
   341 
       
   342     return loadResult; 
       
   343 }
       
   344 
       
   345 /*!
       
   346     Loads or restores widgets from layout document. 
       
   347     Called when layout items haven't been loaded yet, or 
       
   348     have been deleted and items should be shown again. 
       
   349 */
       
   350 bool InfoWidgetLayoutManager::loadWidgets(const DisplayRole displayRole)
       
   351 {
       
   352     QList<LayoutItemRole> displayWidgetRoles = widgetRoles(displayRole); 
       
   353     bool loadResult(false); 
       
   354     switch (displayRole) {
       
   355         case InfoDisplay:
       
   356             loadResult = loadWidgets(displayRole, 
       
   357                         displayWidgetRoles,
       
   358                         m_infoDisplayWidgets); 
       
   359             break; 
       
   360         case SettingsDialog:  
       
   361             loadResult = loadWidgets(displayRole, 
       
   362                         displayWidgetRoles,
       
   363                         m_settingsDialogWidgets); 
       
   364             break;
       
   365         default: 
       
   366             break; 
       
   367     }
       
   368     return loadResult; 
       
   369 }
       
   370 
       
   371 /*!
       
   372     Loads widget by given widget role id.
       
   373 */
       
   374 QGraphicsWidget* InfoWidgetLayoutManager::loadWidget(InfoWidgetDocumentLoader &loader, 
       
   375         DisplayRole displayRole, 
       
   376         LayoutItemRole widgetRole)
       
   377 {
       
   378     DPRINT;
       
   379     QString widgetPrefix; 
       
   380     if (displayRole == InfoDisplay) {
       
   381         widgetPrefix = LAYOUT_PREFIX_INFO_DISPLAY;
       
   382     } else if (displayRole == SettingsDialog) {
       
   383         widgetPrefix = LAYOUT_PREFIX_SETTINGS_DIALOG;
       
   384     }
       
   385         
       
   386     QString widgetName = widgetPrefix;
       
   387     switch (widgetRole) 
       
   388         {
       
   389         case RoleContent: 
       
   390             widgetName.append(LAYOUT_NAME_CONTENT);
       
   391         break;     
       
   392         case RoleMcnMarqueeItem: 
       
   393             widgetName.append(LAYOUT_NAME_MCNMARQUEEITEM);
       
   394         break; 
       
   395         case RoleSpnMarqueeItem: 
       
   396             widgetName.append(LAYOUT_NAME_SPNMARQUEEITEM);
       
   397         break;
       
   398         case RoleSatMarqueeItem: 
       
   399             widgetName.append(LAYOUT_NAME_SATMARQUEEITEM);
       
   400         break; 
       
   401         case RoleSpnIcon: 
       
   402             widgetName.append(LAYOUT_NAME_SPNICON);
       
   403         break; 
       
   404         case RoleMcnIcon: 
       
   405             widgetName.append(LAYOUT_NAME_MCNICON);
       
   406         break; 
       
   407         case RoleSatTextIcon: 
       
   408             widgetName.append(LAYOUT_NAME_SATTEXTICON);
       
   409         break;
       
   410         case RoleSpnCheckBox: 
       
   411             widgetName.append(LAYOUT_NAME_SPNCHECKBOX);
       
   412         break; 
       
   413         case RoleMcnCheckBox: 
       
   414             widgetName.append(LAYOUT_NAME_MCNCHECKBOX);
       
   415         break; 
       
   416         case RoleSatTextCheckBox: 
       
   417             widgetName.append(LAYOUT_NAME_SATTEXTCHECKBOX);
       
   418         break; 
       
   419         case RoleSettingsDialog: 
       
   420             widgetName.append(LAYOUT_NAME_SETTINGSDIALOG);
       
   421         break;
       
   422         case RoleContainer: 
       
   423             widgetName.append(LAYOUT_NAME_CONTAINER);
       
   424         break;        
       
   425         case RoleSettingsContainer: 
       
   426             widgetName.append(LAYOUT_NAME_SETTINGSCONTAINER);
       
   427         break; 
       
   428         
       
   429         case RoleUndefined: // Fall through 
       
   430         default: 
       
   431             break; 
       
   432         }
       
   433     
       
   434     QGraphicsWidget *widget = loader.findWidget(widgetName);
       
   435     return widget; 
       
   436 }
       
   437 
       
   438 /*!
       
   439     Loads object by given object role id. 
       
   440 */
       
   441 QObject* InfoWidgetLayoutManager::loadObject(InfoWidgetDocumentLoader &loader, 
       
   442         DisplayRole displayRole, 
       
   443         LayoutItemRole objectRole)
       
   444 {
       
   445     DPRINT;
       
   446     QString objectPrefix; 
       
   447     if (displayRole == InfoDisplay) {
       
   448         objectPrefix = LAYOUT_PREFIX_INFO_DISPLAY;
       
   449     } else if (displayRole == SettingsDialog) {
       
   450         objectPrefix = LAYOUT_PREFIX_SETTINGS_DIALOG;
       
   451     }
       
   452         
       
   453     QString objectName = objectPrefix;
       
   454     switch (objectRole) 
       
   455         {
       
   456         case RoleOkAction: 
       
   457             objectName.append(LAYOUT_NAME_OKACTION);
       
   458         break;
       
   459         case RoleCancelAction: 
       
   460             objectName.append(LAYOUT_NAME_CANCELACTION);
       
   461         break;
       
   462 
       
   463         case RoleUndefined: // Fall through 
       
   464         default: 
       
   465             break; 
       
   466         }
       
   467 
       
   468     QObject *object = loader.findObject(objectName);
       
   469     if (!object) {
       
   470         DWARNING << ": ERROR, object not found!";
       
   471     }
       
   472            
       
   473     return object; 
       
   474 }
       
   475 
       
   476 /*!
       
   477     Returns supported widget roles for specific display.
       
   478 */
       
   479 const QList<InfoWidgetLayoutManager::LayoutItemRole> InfoWidgetLayoutManager::widgetRoles(
       
   480         DisplayRole displayRole) const
       
   481 {
       
   482     QList<LayoutItemRole> widgetRoles; 
       
   483     switch (displayRole) {
       
   484         case InfoDisplay: 
       
   485             widgetRoles.append(RoleContent);
       
   486             widgetRoles.append(RoleSpnIcon);
       
   487             widgetRoles.append(RoleSpnMarqueeItem);
       
   488             widgetRoles.append(RoleMcnIcon);
       
   489             widgetRoles.append(RoleMcnMarqueeItem);
       
   490             widgetRoles.append(RoleSatTextIcon);
       
   491             widgetRoles.append(RoleSatMarqueeItem);
       
   492             break;
       
   493         case SettingsDialog: 
       
   494             widgetRoles.append(RoleSettingsDialog);
       
   495             widgetRoles.append(RoleSettingsContainer);
       
   496             widgetRoles.append(RoleSpnCheckBox);
       
   497             widgetRoles.append(RoleMcnCheckBox);
       
   498             widgetRoles.append(RoleSatTextCheckBox);
       
   499             break;
       
   500             
       
   501         default: 
       
   502             break; 
       
   503     }
       
   504     
       
   505     return widgetRoles; 
       
   506 }    
       
   507     
       
   508 // End of File. 
       
   509 
       
   510