phoneplugins/infowidgetplugin/infowidget/src/infowidgetlayoutmanager.cpp
branchGCC_SURGE
changeset 51 f39ed5e045e0
parent 46 bc5a64e5bc3c
child 50 377c906a8701
equal deleted inserted replaced
40:bab96b7ed1a4 51:f39ed5e045e0
       
     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_layoutRows(0),
       
    94   m_cachedLayoutRowHeight(0.0)
       
    95 {
       
    96     DPRINT; 
       
    97     // Fill supported layout item roles for info display
       
    98     QList<LayoutItemRole> displayWidgetRoles = widgetRoles(InfoDisplay);
       
    99     
       
   100     // Try to load all widgets in list by widget role 
       
   101     bool loadResult = loadWidgets(InfoDisplay, 
       
   102             displayWidgetRoles,
       
   103             m_infoDisplayWidgets); 
       
   104     
       
   105     // Fill supported layout item roles for settings display
       
   106     displayWidgetRoles = widgetRoles(SettingsDialog);
       
   107 
       
   108     // Try to load all widgets in list by widget role 
       
   109     loadResult = loadWidgets(SettingsDialog, 
       
   110             displayWidgetRoles,
       
   111             m_settingsDialogWidgets); 
       
   112 }
       
   113 
       
   114 /*!
       
   115    Destructor. 
       
   116 */
       
   117 InfoWidgetLayoutManager::~InfoWidgetLayoutManager()
       
   118 {
       
   119     DPRINT;
       
   120     if (m_documentLoader) { 
       
   121         delete m_documentLoader;
       
   122     }
       
   123 }
       
   124 
       
   125 /*!
       
   126    Destroy all widgets. 
       
   127    Deletes parent widgets of each display 
       
   128    causing deletion of child items. 
       
   129 */
       
   130 void InfoWidgetLayoutManager::destroyWidgets()
       
   131 {
       
   132     DPRINT;
       
   133     // Destroy parent items 
       
   134     removeWidget(RoleContent); 
       
   135     removeWidget(RoleSettingsDialog); 
       
   136 }
       
   137 
       
   138 /*!
       
   139    InfoWidgetLayoutManager::currentDisplayRole()
       
   140 */
       
   141 InfoWidgetLayoutManager::DisplayRole InfoWidgetLayoutManager::currentDisplayRole() 
       
   142 {
       
   143     DPRINT; 
       
   144     return m_displayRole;
       
   145 } 
       
   146 
       
   147 /*!
       
   148    InfoWidgetLayoutManager::currentWidgetRoles()
       
   149 */
       
   150 QList<InfoWidgetLayoutManager::LayoutItemRole> InfoWidgetLayoutManager::currentWidgetRoles() 
       
   151 {
       
   152     DPRINT; 
       
   153     return m_widgets.keys(); 
       
   154 } 
       
   155 
       
   156 /*!
       
   157    Returns count of layout rows. 
       
   158 */
       
   159 int InfoWidgetLayoutManager::layoutRows() const 
       
   160 {
       
   161     DPRINT; 
       
   162     return m_layoutRows;
       
   163 } 
       
   164 
       
   165 /*!
       
   166    Set count of layout rows.  
       
   167 */
       
   168 void InfoWidgetLayoutManager::setLayoutRows(int rows) 
       
   169 {
       
   170     DPRINT; 
       
   171     m_layoutRows = rows; 
       
   172 } 
       
   173 
       
   174 /*!
       
   175    Read row height from style. 
       
   176 */
       
   177 qreal InfoWidgetLayoutManager::layoutRowHeight()
       
   178 {
       
   179     DPRINT;
       
   180     // Read from style only if not already initialized
       
   181     if (m_cachedLayoutRowHeight == 0.0) { 
       
   182         bool ok = hbInstance->style()->parameter("hb-param-graphic-size-primary-small", 
       
   183                 m_cachedLayoutRowHeight);
       
   184             DPRINT << ": row height from style: " << m_cachedLayoutRowHeight;
       
   185         if (!ok) {
       
   186             DWARNING << ": Error, paremeters reading failed!!";
       
   187         }
       
   188     }
       
   189     return m_cachedLayoutRowHeight;
       
   190 }
       
   191 
       
   192 /*!
       
   193    Check if text fits to given rect width.  
       
   194 */
       
   195 bool InfoWidgetLayoutManager::textFitsToRect(QString text, 
       
   196         QFont font, QRectF rect) const 
       
   197 {
       
   198     bool fits(true);
       
   199     if (!rect.isEmpty()) { 
       
   200         QFontMetricsF metrics(font);
       
   201         qreal width = metrics.boundingRect(text).width();
       
   202         if (width > rect.width() ) {
       
   203             fits = false; 
       
   204         }
       
   205     }
       
   206     return fits; 
       
   207 }
       
   208 
       
   209 /*!
       
   210    Returns content widget.  
       
   211    The content widget is layout main widget and parent for 
       
   212    sub-widgets in current display.     
       
   213 */
       
   214 QGraphicsWidget* InfoWidgetLayoutManager::contentWidget()
       
   215 {
       
   216     DPRINT; 
       
   217     return getWidget(RoleContent); 
       
   218 }
       
   219 
       
   220 /*!
       
   221    Returns list of marquee items.  
       
   222 */
       
   223 QList<HbMarqueeItem *> InfoWidgetLayoutManager::marqueeItems() 
       
   224 {
       
   225     DPRINT;
       
   226     QList<HbMarqueeItem *> items;
       
   227     
       
   228     QList<LayoutItemRole> marqueeItemRoles; 
       
   229     marqueeItemRoles.append(RoleSpnMarqueeItem);
       
   230     marqueeItemRoles.append(RoleMcnMarqueeItem);
       
   231     marqueeItemRoles.append(RoleSatMarqueeItem);
       
   232     
       
   233     foreach (LayoutItemRole role, marqueeItemRoles) {
       
   234         QGraphicsWidget *widget = getWidget(role); 
       
   235         if (widget) {
       
   236             HbMarqueeItem *item = 
       
   237                     qobject_cast<HbMarqueeItem*>(widget); 
       
   238             if (item) {
       
   239                 items.append(item); 
       
   240                 item = NULL; 
       
   241             }
       
   242         }
       
   243     }
       
   244     return items; 
       
   245 }
       
   246 
       
   247 /*!
       
   248    Get widget with given item role. 
       
   249 */
       
   250 QGraphicsWidget* InfoWidgetLayoutManager::getWidget(LayoutItemRole itemRole)
       
   251 {
       
   252     QGraphicsWidget *widget = m_widgets.value(itemRole); 
       
   253     return widget; 
       
   254 }
       
   255 
       
   256 /*!
       
   257    Get object with given item role. 
       
   258 */
       
   259 QObject* InfoWidgetLayoutManager::getObject(LayoutItemRole itemRole)
       
   260 {
       
   261     QObject *object = m_objects.value(itemRole); 
       
   262     return object; 
       
   263 }
       
   264 
       
   265 /*!
       
   266    Remove widget with given item role. 
       
   267 */
       
   268 void InfoWidgetLayoutManager::removeWidget(LayoutItemRole itemRole, 
       
   269         bool deleteLater)
       
   270 {
       
   271     DPRINT;
       
   272     QGraphicsWidget *widget = m_widgets.value(itemRole); 
       
   273     if (widget) {
       
   274         if (!deleteLater) {
       
   275             delete widget;
       
   276         } else {
       
   277             widget->deleteLater(); 
       
   278         }
       
   279     }
       
   280     
       
   281     m_widgets.remove(itemRole);
       
   282     m_infoDisplayWidgets.remove(itemRole);
       
   283     m_settingsDialogWidgets.remove(itemRole);
       
   284 }
       
   285 
       
   286 /*!
       
   287     Returns info display layout.  
       
   288 */
       
   289 QGraphicsLayout* InfoWidgetLayoutManager::layoutInfoDisplay()
       
   290 {   
       
   291     DPRINT;
       
   292     m_displayRole = InfoDisplay;
       
   293     m_widgets = m_infoDisplayWidgets; 
       
   294            
       
   295     QGraphicsLayout *activeLayout(NULL); 
       
   296     QGraphicsWidget *content = getWidget(RoleContent); 
       
   297     if (content) {
       
   298         DPRINT << ": content found, getting layout";
       
   299         activeLayout = content->layout(); 
       
   300     }
       
   301     return activeLayout; 
       
   302 }
       
   303 
       
   304 /*!
       
   305     Returns settings dialog layout. 
       
   306 */
       
   307 QGraphicsLayout* InfoWidgetLayoutManager::layoutSettingsDialog()
       
   308 {   
       
   309     DPRINT;
       
   310     m_displayRole = SettingsDialog;
       
   311     m_widgets = m_settingsDialogWidgets; 
       
   312     
       
   313     QGraphicsLayout *activeLayout(NULL); 
       
   314     QGraphicsWidget *dialog = getWidget(RoleSettingsDialog); 
       
   315     if (dialog) {
       
   316         activeLayout = dialog->layout(); 
       
   317 
       
   318         HbAction *okAction = qobject_cast<HbAction *>(
       
   319                 getObject(RoleOkAction));
       
   320         if (okAction) {
       
   321             dialog->addAction(okAction); 
       
   322         }
       
   323         
       
   324         HbAction *cancelAction = qobject_cast<HbAction *>(
       
   325                 getObject(RoleCancelAction));
       
   326         if (cancelAction) {
       
   327             dialog->addAction(cancelAction);
       
   328         }
       
   329     }
       
   330     return activeLayout;    
       
   331 }
       
   332 
       
   333 /*!
       
   334     Load widgets from document for given display role.
       
   335 */
       
   336 bool InfoWidgetLayoutManager::loadWidgets(const DisplayRole displayRole, 
       
   337         const QList<LayoutItemRole> &displayWidgets,
       
   338         QMap<LayoutItemRole, QGraphicsWidget *> &widgetMap)
       
   339 {
       
   340     DPRINT;
       
   341     bool loadResult(true); 
       
   342 
       
   343     // Cleanup previously loaded content in case of any data  
       
   344     widgetMap.clear(); 
       
   345     
       
   346     if (!m_documentLoader) {
       
   347         m_documentLoader = new InfoWidgetDocumentLoader;
       
   348     }
       
   349     Q_ASSERT(m_documentLoader); 
       
   350     
       
   351     bool loaded = true;
       
   352     if (displayRole != SettingsDialog) {
       
   353         m_documentLoader->load(INFOWIDGET_DOCML_FILE, &loaded);
       
   354     } else {
       
   355         m_documentLoader->load(SETTINGS_DIALOG_DOCML_FILE, &loaded);
       
   356     }
       
   357     
       
   358     Q_ASSERT_X(loaded, 
       
   359             "InfoWidgetLayoutManager", 
       
   360             "Invalid docml file");    
       
   361     
       
   362     
       
   363     foreach (LayoutItemRole role, displayWidgets) {
       
   364         QGraphicsWidget *widget = 
       
   365                 loadWidget(*m_documentLoader, displayRole, role);
       
   366            if (widget) {
       
   367                widgetMap.insert(role, widget);
       
   368                widget = NULL;
       
   369            } 
       
   370     }
       
   371     
       
   372     if (widgetMap.count() == displayWidgets.count()) {
       
   373         loadResult = true;
       
   374         } else {
       
   375             DWARNING << ": all widgets were not loaded!";
       
   376             loadResult = false;
       
   377         }        
       
   378 
       
   379     m_objects.clear();
       
   380     if (displayRole == SettingsDialog) {
       
   381         QObject *okAction = 
       
   382                 loadObject(*m_documentLoader, 
       
   383                         displayRole, 
       
   384                         RoleOkAction);
       
   385                 m_objects.insert(RoleOkAction, okAction); 
       
   386         QObject *cancelAction = 
       
   387                 loadObject(*m_documentLoader, 
       
   388                         displayRole, 
       
   389                         RoleCancelAction); 
       
   390                 m_objects.insert(RoleCancelAction, cancelAction); 
       
   391     } 
       
   392 
       
   393     return loadResult; 
       
   394 }
       
   395 
       
   396 /*!
       
   397     Restores widgets from layout document. 
       
   398     Called when layout items have been deleted  
       
   399     and items should be shown again. 
       
   400 */
       
   401 bool InfoWidgetLayoutManager::reloadWidgets(const DisplayRole displayRole)
       
   402 {
       
   403     QList<LayoutItemRole> displayWidgetRoles = widgetRoles(displayRole); 
       
   404     bool loadResult(false); 
       
   405     
       
   406     switch (displayRole) {
       
   407         case InfoDisplay:
       
   408             loadResult = loadWidgets(displayRole, 
       
   409                         displayWidgetRoles,
       
   410                         m_infoDisplayWidgets); 
       
   411             break; 
       
   412         case SettingsDialog:  
       
   413             loadResult = loadWidgets(displayRole, 
       
   414                         displayWidgetRoles,
       
   415                         m_settingsDialogWidgets); 
       
   416             break;
       
   417         default: 
       
   418             break; 
       
   419     }
       
   420     return loadResult; 
       
   421 }
       
   422 
       
   423 /*!
       
   424     Loads widget by given widget role id.
       
   425 */
       
   426 QGraphicsWidget* InfoWidgetLayoutManager::loadWidget(InfoWidgetDocumentLoader &loader, 
       
   427         DisplayRole displayRole, 
       
   428         LayoutItemRole widgetRole)
       
   429 {
       
   430     DPRINT;
       
   431     QString widgetPrefix; 
       
   432     if (displayRole == InfoDisplay) {
       
   433         widgetPrefix = LAYOUT_PREFIX_INFO_DISPLAY;
       
   434     } else if (displayRole == SettingsDialog) {
       
   435         widgetPrefix = LAYOUT_PREFIX_SETTINGS_DIALOG;
       
   436     }
       
   437         
       
   438     QString widgetName = widgetPrefix;
       
   439     switch (widgetRole) 
       
   440         {
       
   441         case RoleContent: 
       
   442             widgetName.append(LAYOUT_NAME_CONTENT);
       
   443         break;     
       
   444         case RoleMcnMarqueeItem: 
       
   445             widgetName.append(LAYOUT_NAME_MCNMARQUEEITEM);
       
   446         break; 
       
   447         case RoleSpnMarqueeItem: 
       
   448             widgetName.append(LAYOUT_NAME_SPNMARQUEEITEM);
       
   449         break;
       
   450         case RoleSatMarqueeItem: 
       
   451             widgetName.append(LAYOUT_NAME_SATMARQUEEITEM);
       
   452         break; 
       
   453         case RoleSpnIcon: 
       
   454             widgetName.append(LAYOUT_NAME_SPNICON);
       
   455         break; 
       
   456         case RoleMcnIcon: 
       
   457             widgetName.append(LAYOUT_NAME_MCNICON);
       
   458         break; 
       
   459         case RoleSatTextIcon: 
       
   460             widgetName.append(LAYOUT_NAME_SATTEXTICON);
       
   461         break;
       
   462         case RoleSpnCheckBox: 
       
   463             widgetName.append(LAYOUT_NAME_SPNCHECKBOX);
       
   464         break; 
       
   465         case RoleMcnCheckBox: 
       
   466             widgetName.append(LAYOUT_NAME_MCNCHECKBOX);
       
   467         break; 
       
   468         case RoleSatTextCheckBox: 
       
   469             widgetName.append(LAYOUT_NAME_SATTEXTCHECKBOX);
       
   470         break; 
       
   471         case RoleSettingsDialog: 
       
   472             widgetName.append(LAYOUT_NAME_SETTINGSDIALOG);
       
   473         break;
       
   474         case RoleContainer: 
       
   475             widgetName.append(LAYOUT_NAME_CONTAINER);
       
   476         break;        
       
   477         case RoleSettingsContainer: 
       
   478             widgetName.append(LAYOUT_NAME_SETTINGSCONTAINER);
       
   479         break; 
       
   480         
       
   481         case RoleUndefined: // Fall through 
       
   482         default: 
       
   483             break; 
       
   484         }
       
   485     
       
   486     QGraphicsWidget *widget = qobject_cast<QGraphicsWidget *>(
       
   487             loader.findWidget(widgetName));
       
   488     return widget; 
       
   489 }
       
   490 
       
   491 /*!
       
   492     Loads object by given object role id. 
       
   493 */
       
   494 QObject* InfoWidgetLayoutManager::loadObject(InfoWidgetDocumentLoader &loader, 
       
   495         DisplayRole displayRole, 
       
   496         LayoutItemRole objectRole)
       
   497 {
       
   498     DPRINT;
       
   499     QString objectPrefix; 
       
   500     if (displayRole == InfoDisplay) {
       
   501         objectPrefix = LAYOUT_PREFIX_INFO_DISPLAY;
       
   502     } else if (displayRole == SettingsDialog) {
       
   503         objectPrefix = LAYOUT_PREFIX_SETTINGS_DIALOG;
       
   504     }
       
   505         
       
   506     QString objectName = objectPrefix;
       
   507     switch (objectRole) 
       
   508         {
       
   509         case RoleOkAction: 
       
   510             objectName.append(LAYOUT_NAME_OKACTION);
       
   511         break;
       
   512         case RoleCancelAction: 
       
   513             objectName.append(LAYOUT_NAME_CANCELACTION);
       
   514         break;
       
   515 
       
   516         case RoleUndefined: // Fall through 
       
   517         default: 
       
   518             break; 
       
   519         }
       
   520 
       
   521     QObject *object = qobject_cast<QObject *>(loader.findObject(objectName));
       
   522     if (!object) {
       
   523         DWARNING << ": ERROR, object not found!";
       
   524     }
       
   525            
       
   526     return object; 
       
   527 }
       
   528 
       
   529 /*!
       
   530     Returns supported widget roles for specific display.
       
   531 */
       
   532 const QList<InfoWidgetLayoutManager::LayoutItemRole> InfoWidgetLayoutManager::widgetRoles(
       
   533         DisplayRole displayRole) const
       
   534 {
       
   535     QList<LayoutItemRole> widgetRoles; 
       
   536     switch (displayRole) {
       
   537         case InfoDisplay: 
       
   538             // Fill supported layout item roles for info display
       
   539             widgetRoles.append(RoleContent);
       
   540             widgetRoles.append(RoleSpnIcon);
       
   541             widgetRoles.append(RoleSpnMarqueeItem);
       
   542             widgetRoles.append(RoleMcnIcon);
       
   543             widgetRoles.append(RoleMcnMarqueeItem);
       
   544             widgetRoles.append(RoleSatTextIcon);
       
   545             widgetRoles.append(RoleSatMarqueeItem);
       
   546             break;
       
   547         case SettingsDialog: 
       
   548             // Fill supported layout item roles for settings display
       
   549             widgetRoles.append(RoleSettingsDialog);
       
   550             widgetRoles.append(RoleSettingsContainer);
       
   551             widgetRoles.append(RoleSpnCheckBox);
       
   552             widgetRoles.append(RoleMcnCheckBox);
       
   553             widgetRoles.append(RoleSatTextCheckBox);
       
   554             break;
       
   555             
       
   556         default: 
       
   557             break; 
       
   558     }
       
   559     
       
   560     return widgetRoles; 
       
   561 }    
       
   562     
       
   563 // End of File. 
       
   564 
       
   565