src/hbcore/gui/hbwidget.cpp
changeset 34 ed14f46c0e55
parent 7 923ff622b8b9
equal deleted inserted replaced
31:7516d6d86cf5 34:ed14f46c0e55
    33 #include "hbgraphicsscene.h"
    33 #include "hbgraphicsscene.h"
    34 #include "hbgraphicsscene_p.h"
    34 #include "hbgraphicsscene_p.h"
    35 #include "hbdeviceprofile.h"
    35 #include "hbdeviceprofile.h"
    36 #include "hbtapgesture.h"
    36 #include "hbtapgesture.h"
    37 #include "hbnamespace_p.h"
    37 #include "hbnamespace_p.h"
       
    38 #include "hbstyle_p.h"
    38 #include <QCoreApplication>
    39 #include <QCoreApplication>
    39 #include <QMetaType>
    40 #include <QMetaType>
    40 #include <QAction>
    41 #include <QAction>
    41 #include <QDebug>
       
    42 #include <QDynamicPropertyChangeEvent>
    42 #include <QDynamicPropertyChangeEvent>
    43 #include <QGraphicsLayout>
    43 #include <QGraphicsLayout>
    44 #include <QInputContext>
    44 #include <QInputContext>
    45 
    45 
    46 #ifdef HB_TESTABILITY
       
    47 /*!
    46 /*!
    48     \internal
    47     \internal
    49  */
    48  */
    50 static void testabilitySignalEnabledChange(bool enabled, void *param)
    49 static void testabilitySignalEnabledChange(bool enabled, void *param)
    51 {
    50 {
    56         // However this should only be done when the testability signal gets
    55         // However this should only be done when the testability signal gets
    57         // enabled because it causes perf regression.
    56         // enabled because it causes perf regression.
    58         widget->setFlag(QGraphicsItem::ItemSendsGeometryChanges, enabled);
    57         widget->setFlag(QGraphicsItem::ItemSendsGeometryChanges, enabled);
    59     }
    58     }
    60 }
    59 }
    61 #endif //HB_TESTABILITY
       
    62 
    60 
    63 HbWidgetPrivate::HbWidgetPrivate()
    61 HbWidgetPrivate::HbWidgetPrivate()
    64   : HbWidgetBasePrivate(),
    62   : HbWidgetBasePrivate(),
    65     style(0),
    63     style(0),
    66     backgroundPrimitiveType(HbStyle::P_None),
    64     backgroundPrimitiveType(HbStylePrivate::P_None),
    67     polished(false),
    65     polished(0),
    68     polishPending(false),
    66     polishPending(false),
    69     themingPending(true),
    67     themingPending(true),
    70     repolishOutstanding(false),
    68     repolishOutstanding(false),
    71     mHandlingRepolishSynchronously(false),
    69     mHandlingRepolishSynchronously(false),
    72     notifyScene(false),
    70     notifyScene(false),
    73     focusGroup(0),
    71     focusGroup(0),
    74     focusActiveType(HbStyle::P_None),
    72     focusActiveType(HbStylePrivate::P_None),
    75     focusResidualType(HbStyle::P_None),
    73     focusResidualType(HbStylePrivate::P_None),
    76     highlightExpired(false),
    74     highlightExpired(false),
    77     backgroundItem(0),
    75     backgroundItem(0),
    78     focusActiveItem(0),
    76     focusActiveItem(0),
    79     focusResidualItem(0)
    77     focusResidualItem(0),
       
    78     visibleBackground(0)
    80 {
    79 {
    81     q_ptr = 0;
    80     q_ptr = 0;
    82 #ifdef HB_TESTABILITY 
    81     testabilitySignal = new HbTestabilitySignal_p();
    83 	testabilitySignal = new HbTestabilitySignal_p();
    82 }
    84 #endif //HB_TESTABILITY 
    83 
       
    84 void HbWidgetPrivate::init()
       
    85 {
       
    86     Q_Q(HbWidget);
       
    87     q->setAttribute(Hb::Widget, true);
       
    88     notifySceneForPolish();
       
    89     if (testabilitySignal) {
       
    90         testabilitySignal->setParent(q);
       
    91         testabilitySignal->notifySignalEnabled(testabilitySignalEnabledChange, q);
       
    92     }
       
    93 }
       
    94 
       
    95 void HbWidgetPrivate::notifySceneForPolish(bool polishWidget)
       
    96 {
       
    97     Q_Q(HbWidget);
       
    98     // If no one is handling repolish/polish synchronously, lets make sure they are handled
       
    99     // before painting. For example view items handle them synchronously.
       
   100     QGraphicsScene *currentScene = q->scene();
       
   101     if (!currentScene || mHandlingRepolishSynchronously || !q->isVisible())
       
   102         return;
       
   103     HbGraphicsScene *hbscene = qobject_cast<HbGraphicsScene*>(currentScene);
       
   104     if (hbscene) {
       
   105         if (polishWidget)
       
   106             HbGraphicsScenePrivate::d_ptr(hbscene)->mPolishWidgets = true;
       
   107         else
       
   108             HbGraphicsScenePrivate::d_ptr(hbscene)->mRepolishWidgets = true;
       
   109     }
    85 }
   110 }
    86 
   111 
    87 HbWidgetPrivate::~HbWidgetPrivate()
   112 HbWidgetPrivate::~HbWidgetPrivate()
    88 {
   113 {
    89     if (backgroundItem) {
   114     if (backgroundItem) {
    90         delete backgroundItem;
   115         delete backgroundItem;
    91         backgroundItem = 0;
   116         backgroundItem = 0;
    92     }
   117     }
    93 }
   118 }
    94 
   119 
    95 void HbWidgetPrivate::setBackgroundItem(HbStyle::Primitive type, int zValue)
   120 void HbWidgetPrivate::setBackgroundItem(HbStylePrivate::Primitive type, int zValue)
    96 {
   121 {
    97     Q_Q(HbWidget);
   122     Q_Q(HbWidget);
    98     if(type!=backgroundPrimitiveType || type == HbStyle::P_None) {
   123     if(type!=backgroundPrimitiveType || type == HbStylePrivate::P_None) {
    99         if (backgroundItem) {
       
   100             delete backgroundItem;
       
   101             backgroundItem = 0;
       
   102         }
       
   103         backgroundPrimitiveType = type;
   124         backgroundPrimitiveType = type;
   104         backgroundItem = q->style()->createPrimitive(backgroundPrimitiveType, const_cast<HbWidget*>(q));
   125         QGraphicsItem *item = HbStylePrivate::createPrimitive(type, const_cast<HbWidget*>(q));
   105         if(backgroundItem) {
   126         storeBackgroundItem(backgroundItem, item, zValue);
   106             backgroundItem->setParentItem(q);
   127         if(!q->hasFocus()) {
   107         }
   128             setVisibleBackground(backgroundItem);
   108     q->updatePrimitives();
   129             q->updatePrimitives();
   109 }
   130         }
   110     if(backgroundItem && zValue != backgroundItem->zValue()) {
       
   111         backgroundItem->setZValue(zValue);
       
   112     }
   131     }
   113 } 
   132 } 
       
   133 
       
   134 /*
       
   135 
       
   136     Sets focus primitives defined in HbStyle. If FocusHighlightNone is set both active
       
   137     and residual focus primitives are deleted.
       
   138 
       
   139     \param type Defines the primitive for the highlight.
       
   140     \param focusHighlight Defines the highlight the primitive is used.
       
   141 */
       
   142 void HbWidgetPrivate::setFocusHighlight(HbStyle::Primitive type, HbWidget::FocusHighlight focusHighlight)
       
   143 {
       
   144     if (focusHighlight == HbWidget::FocusHighlightNone) {
       
   145         focusActiveType = HbStylePrivate::P_None;
       
   146         focusResidualType = HbStylePrivate::P_None;
       
   147 
       
   148         if (focusActiveItem == visibleBackground || focusResidualItem == visibleBackground) {
       
   149             visibleBackground = 0;
       
   150         }
       
   151         delete focusActiveItem;
       
   152         focusActiveItem = 0;
       
   153         delete focusResidualItem;
       
   154         focusResidualItem = 0;
       
   155     } else if (focusHighlight == HbWidget::FocusHighlightActive) {
       
   156         focusActiveType = (HbStylePrivate::Primitive)type;
       
   157     } else {
       
   158         focusResidualType = (HbStylePrivate::Primitive)type;
       
   159     }
       
   160 }
       
   161 
       
   162 /*
       
   163     Returns identifier of the focus primitive defined in HbStyle for focus highlight.
       
   164     \param highlightType defines the highlight type.
       
   165 */
       
   166 HbStyle::Primitive HbWidgetPrivate::focusHighlight(HbWidget::FocusHighlight highlightType)
       
   167 {
       
   168     HbStylePrivate::Primitive primitive(HbStylePrivate::P_None);
       
   169 
       
   170     if (highlightType == HbWidget::FocusHighlightActive) {
       
   171         primitive = focusActiveType;
       
   172     } else if (highlightType == HbWidget::FocusHighlightResidual) {
       
   173         primitive = focusResidualType;
       
   174     }
       
   175     return (HbStyle::Primitive)primitive;
       
   176 }
   114 
   177 
   115 
   178 
   116 /*!
   179 /*!
   117     Sets background item to the widget.
   180     Sets background item to the widget.
   118     
   181     
   130     Previously set background item will be deleted.
   193     Previously set background item will be deleted.
   131  */
   194  */
   132 void HbWidget::setBackgroundItem(QGraphicsItem *item, int zValue)
   195 void HbWidget::setBackgroundItem(QGraphicsItem *item, int zValue)
   133 {
   196 {
   134     Q_D(HbWidget);
   197     Q_D(HbWidget);
   135     if(d->backgroundItem) {
   198     d->backgroundPrimitiveType = HbStylePrivate::P_None;
   136         delete d->backgroundItem;
   199     d->storeBackgroundItem(d->backgroundItem, item, zValue);
   137         d->backgroundItem = 0;
   200 
   138     }
   201     if (d->visibleBackground == 0) {
   139     d->backgroundPrimitiveType = HbStyle::P_None;
   202         d->setVisibleBackground(d->backgroundItem);
   140     d->backgroundItem = item;
   203     }
   141     if(d->backgroundItem) {
       
   142         d->backgroundItem->setParentItem(this);
       
   143         d->backgroundItem->setZValue(zValue);
       
   144         d->updateBackgroundItemSize();
       
   145     }
       
   146     updatePrimitives();
       
   147 }
   204 }
   148 
   205 
   149 /*!
   206 /*!
   150     Returns background item. 0 is returned if there isn't background
   207     Returns background item. 0 is returned if there isn't background
   151     item in the widget.
   208     item in the widget.
   155     Q_D( const HbWidget );
   212     Q_D( const HbWidget );
   156     return d->backgroundItem;
   213     return d->backgroundItem;
   157 }
   214 }
   158 
   215 
   159 /*!
   216 /*!
       
   217     Sets the focus highlight item to the widget. The focus highligh item
       
   218     replaces the background item when the widget is focused.
       
   219 
       
   220     The item will be reparented to be child of the widget. Also Z-value
       
   221     of the background item will be changed to be zValue. By default the
       
   222     zValue is -1, which should be  behind other widget content.
       
   223     Focus highlight item will be always resized to be same size as widgets
       
   224     bounding rect. If the Focus highlight item is widget, the size will
       
   225     be changed with setGeometry to allow e.g. maintaining aspect ratio of
       
   226     images. If item is not widget, the size will be changed with
       
   227     transformations.
       
   228 
       
   229     If item is 0, the background item will be removed from the widget.
       
   230 
       
   231     Previously set Focus highlight item will be deleted.
       
   232  */
       
   233 void HbWidget::setFocusHighlightItem(QGraphicsItem *item, HbWidget::FocusHighlight focusHighlight, int zValue /*-1*/)
       
   234 {
       
   235     Q_D(HbWidget);
       
   236     if (focusHighlight == HbWidget::FocusHighlightActive) {
       
   237         d->focusActiveType = HbStylePrivate::P_None;
       
   238         d->storeBackgroundItem(d->focusActiveItem, item);
       
   239         if (hasFocus()) {
       
   240             d->setVisibleBackground(d->focusActiveItem);
       
   241         }
       
   242     } else if (focusHighlight == HbWidget::FocusHighlightResidual) {
       
   243         d->focusResidualType = HbStylePrivate::P_None;
       
   244         d->storeBackgroundItem(d->focusResidualItem, item, zValue);
       
   245     }
       
   246 }
       
   247 
       
   248 /*!
       
   249     Returns focus highlight item. 0 is returned if there isn't focus highlight
       
   250     item in the widget.
       
   251  */
       
   252 QGraphicsItem *HbWidget::focusHighlightItem(HbWidget::FocusHighlight focusHighlight) const
       
   253 {
       
   254     Q_D( const HbWidget );
       
   255     if (focusHighlight == HbWidget::FocusHighlightActive) {
       
   256         if (d->focusActiveType != HbStylePrivate::P_None) {
       
   257             return d->focusPrimitive(focusHighlight);
       
   258         } else {
       
   259             return d->focusActiveItem;
       
   260         }
       
   261     } else if (focusHighlight == HbWidget::FocusHighlightResidual) {
       
   262         if (d->focusResidualType != HbStylePrivate::P_None) {
       
   263             return d->focusPrimitive(focusHighlight);
       
   264         } else {
       
   265             return d->focusResidualItem;
       
   266         }
       
   267     }
       
   268 
       
   269     // wrong FocusHighlight parameter
       
   270     return 0;
       
   271 }
       
   272 
       
   273 
       
   274 
       
   275 /*!
   160     Returns focusItem primitive items.
   276     Returns focusItem primitive items.
   161     Focus primitive is created if has not been created already.
   277     Focus primitive is created if has not been created already.
   162  */
   278  */
   163 QGraphicsItem *HbWidgetPrivate::focusPrimitive(HbWidget::FocusHighlight highlightType) const
   279 QGraphicsItem *HbWidgetPrivate::focusPrimitive(HbWidget::FocusHighlight highlightType) const
   164 {
   280 {
   165     Q_Q(const HbWidget);
   281     Q_Q(const HbWidget);
   166 
   282 
   167     if (highlightType == HbWidget::FocusHighlightActive) {
   283     if (highlightType == HbWidget::FocusHighlightActive) {
   168         if (!focusActiveItem && focusActiveType != HbStyle::P_None) {
   284         if (!focusActiveItem && focusActiveType != HbStylePrivate::P_None) {
   169             focusActiveItem = q->style()->createPrimitive(
   285             focusActiveItem = HbStylePrivate::createPrimitive(
   170                                 focusActiveType, const_cast<HbWidget*>(q));
   286                                 focusActiveType, const_cast<HbWidget*>(q));
   171             focusActiveItem->hide();
   287             focusActiveItem->hide();
   172         }
   288         }
   173         return focusActiveItem;
   289         return focusActiveItem;
   174     } else if (highlightType == HbWidget::FocusHighlightResidual) {
   290     } else if (highlightType == HbWidget::FocusHighlightResidual) {
   175         if (!focusResidualItem && focusResidualType != HbStyle::P_None) {
   291         if (!focusResidualItem && focusResidualType != HbStylePrivate::P_None) {
   176             focusResidualItem = q->style()->createPrimitive(
   292             focusResidualItem = HbStylePrivate::createPrimitive(
   177                                 focusResidualType, const_cast<HbWidget*>(q));
   293                                 focusResidualType, const_cast<HbWidget*>(q));
   178             focusResidualItem->hide();
   294             focusResidualItem->hide();
   179         }
   295         }
   180         return focusResidualItem;
   296         return focusResidualItem;
   181     }
   297     }
   187     Hides or shows focus primitive depending on the focus state of the widget.
   303     Hides or shows focus primitive depending on the focus state of the widget.
   188 */
   304 */
   189 void HbWidgetPrivate::focusChangeEvent(HbWidget::FocusHighlight focusHighlight)
   305 void HbWidgetPrivate::focusChangeEvent(HbWidget::FocusHighlight focusHighlight)
   190 {
   306 {
   191     Q_Q(HbWidget);
   307     Q_Q(HbWidget);
   192     bool activeHighlight(false);
       
   193     bool residualHighlight(false);
       
   194 
   308 
   195     HbFocusGroup *group = getFocusGroup();
   309     HbFocusGroup *group = getFocusGroup();
   196 
   310 
   197     // Do not show focus if there is focus group and the flag is NeverOn.
   311     // Do not show focus if there is focus group and the flag is NeverOn.
   198     if (group && (group->focusParameters() == HbFocusGroup::NeverOn)) {
   312     if (group && (group->focusParameters() == HbFocusGroup::NeverOn)) {
   202     if (group) {
   316     if (group) {
   203         group->stopHighlightTimer(q);
   317         group->stopHighlightTimer(q);
   204         }
   318         }
   205 
   319 
   206     if (focusHighlight == HbWidget::FocusHighlightActive) {
   320     if (focusHighlight == HbWidget::FocusHighlightActive) {
   207         activeHighlight = true;
       
   208 
       
   209         if (group) {
   321         if (group) {
   210            highlightExpired = false;
   322            highlightExpired = false;
   211            group->startHighlightTimer(q);
   323            group->startHighlightTimer(q);
   212         }
   324         }
   213 
   325     }
   214     } else if (focusHighlight == HbWidget::FocusHighlightResidual) {
   326 
   215         residualHighlight = true;
   327     if (focusPrimitive(focusHighlight)) {
   216     }
   328         setVisibleBackground(focusPrimitive(focusHighlight));
   217 
   329     } else {
   218     if (focusActiveItem) {
   330         setVisibleBackground(q->backgroundItem());
   219         focusActiveItem->setVisible(activeHighlight);
   331     }
   220         focusActiveItem->update();         
       
   221     }
       
   222 
       
   223     if (focusResidualItem) {
       
   224         focusResidualItem->setVisible(residualHighlight);
       
   225         focusResidualItem->update();
       
   226     }
       
   227 
       
   228 }
   332 }
   229 
   333 
   230 /*!
   334 /*!
   231     Find closest parent with focus group and update the focused child.
   335     Find closest parent with focus group and update the focused child.
   232 */
   336 */
   288 }
   392 }
   289 
   393 
   290 void HbWidgetPrivate::updateBackgroundItemSize()
   394 void HbWidgetPrivate::updateBackgroundItemSize()
   291 {
   395 {
   292     Q_Q(HbWidget);
   396     Q_Q(HbWidget);
   293     if(backgroundItem) {
   397     if(visibleBackground) {
   294         if(backgroundItem->isWidget()) {
   398         if(visibleBackground->isWidget()) {
   295             static_cast<QGraphicsWidget*>(backgroundItem)->setGeometry(q->boundingRect());
   399             static_cast<QGraphicsWidget*>(visibleBackground)->setGeometry(q->boundingRect());
   296         } else {
   400         } else {
   297             QRectF widgetRect = q->boundingRect();
   401             QRectF widgetRect = q->boundingRect();
   298             QRectF backgroundRect = backgroundItem->boundingRect();
   402             QRectF backgroundRect = visibleBackground->boundingRect();
   299             if(backgroundRect.width() != 0 && backgroundRect.height() != 0) {
   403             if(backgroundRect.width() != 0 && backgroundRect.height() != 0) {
   300                 backgroundItem->setTransform(
   404                 visibleBackground->setTransform(
   301                     QTransform().scale(widgetRect.width()/backgroundRect.width(), 
   405                     QTransform().scale(widgetRect.width()/backgroundRect.width(), 
   302                                        widgetRect.height()/backgroundRect.height()));
   406                                        widgetRect.height()/backgroundRect.height()));
   303             }
   407             }
   304         }
   408         }
   305     }
   409     }
   306 }
   410 }
   307 
   411 
       
   412 void HbWidgetPrivate::setVisibleBackground(QGraphicsItem* background)
       
   413 {
       
   414    if (background != visibleBackground) {
       
   415         if(visibleBackground) {
       
   416             visibleBackground->hide();
       
   417         }
       
   418         visibleBackground = background;
       
   419         if (visibleBackground) {
       
   420             updateBackgroundItemSize();
       
   421             visibleBackground->show();
       
   422             visibleBackground->update();
       
   423         }
       
   424     }
       
   425 }
       
   426 
       
   427 void HbWidgetPrivate::storeBackgroundItem(QGraphicsItem *&storeItem, QGraphicsItem *item, int zValue)
       
   428 {
       
   429     Q_Q(HbWidget);
       
   430     if (storeItem) {
       
   431         if (storeItem == visibleBackground) {
       
   432             visibleBackground = 0;
       
   433         }
       
   434         delete storeItem;
       
   435         storeItem = 0;
       
   436     }
       
   437 
       
   438     storeItem = item;
       
   439     if (storeItem) {
       
   440         storeItem->setParentItem(q);
       
   441         storeItem->setZValue(zValue);
       
   442         storeItem->hide();
       
   443     }
       
   444 }
       
   445 
       
   446 void HbWidgetPrivate::setFocusHighlightVisible(HbWidget::FocusHighlight focusHighlight, bool enable)
       
   447 {
       
   448     Q_Q(HbWidget);
       
   449     if (enable) {
       
   450         setVisibleBackground(focusPrimitive(focusHighlight));
       
   451     } else {
       
   452         setVisibleBackground(q->backgroundItem());
       
   453     }
       
   454 }
   308 
   455 
   309 /*!
   456 /*!
   310     \class HbWidget
   457     \class HbWidget
   311 
   458 
   312     \brief HbWidget is a base for all Hb widgets. It contains common functionality
   459     \brief HbWidget is a base for all Hb widgets. It contains common functionality
   400 HbWidget::HbWidget( QGraphicsItem *parent, Qt::WindowFlags wFlags ):
   547 HbWidget::HbWidget( QGraphicsItem *parent, Qt::WindowFlags wFlags ):
   401     HbWidgetBase( *new HbWidgetPrivate, parent, wFlags )
   548     HbWidgetBase( *new HbWidgetPrivate, parent, wFlags )
   402 {
   549 {
   403     Q_D( HbWidget );
   550     Q_D( HbWidget );
   404     d->q_ptr = this;
   551     d->q_ptr = this;
   405     setAttribute(Hb::Widget, true);
   552     d->init();
   406 
       
   407 #ifdef HB_TESTABILITY 
       
   408 	if(d->testabilitySignal) {
       
   409 		d->testabilitySignal->setParent(this);
       
   410 		d->testabilitySignal->notifySignalEnabled(testabilitySignalEnabledChange, this);
       
   411 	}
       
   412 #endif //HB_TESTABILITY
       
   413 }
   553 }
   414 
   554 
   415 /*!
   555 /*!
   416     Constructs a widget with the given private object, parent item and window flags.
   556     Constructs a widget with the given private object, parent item and window flags.
   417     The given HbWidgetPrivate instance is installed in the widget and deleted in its destructor.
   557     The given HbWidgetPrivate instance is installed in the widget and deleted in its destructor.
   419 HbWidget::HbWidget(HbWidgetPrivate &dd, QGraphicsItem *parent, Qt::WindowFlags wFlags):
   559 HbWidget::HbWidget(HbWidgetPrivate &dd, QGraphicsItem *parent, Qt::WindowFlags wFlags):
   420     HbWidgetBase( dd, parent, wFlags )
   560     HbWidgetBase( dd, parent, wFlags )
   421 {
   561 {
   422     Q_D( HbWidget );
   562     Q_D( HbWidget );
   423     d->q_ptr = this;
   563     d->q_ptr = this;
   424     setAttribute(Hb::Widget, true);
   564     d->init();
   425 
       
   426 #ifdef HB_TESTABILITY 
       
   427 	if (d->testabilitySignal) {
       
   428 		d->testabilitySignal->setParent(this);
       
   429 		d->testabilitySignal->notifySignalEnabled(testabilitySignalEnabledChange, this);
       
   430 	}
       
   431 #endif //HB_TESTABILITY
       
   432 }
   565 }
   433 
   566 
   434 /*!
   567 /*!
   435  Destroys the widget.
   568  Destroys the widget.
   436 */
   569 */
   498 }
   631 }
   499 
   632 
   500 
   633 
   501 /*!
   634 /*!
   502     Sets a custom style for the widget. 
   635     Sets a custom style for the widget. 
   503 	When a new style is set for the widget the widget's primitives need to recreated and primitives and layout updated.
   636     When a new style is set for the widget the widget's primitives need to recreated and primitives and layout updated.
   504 	In order for the style change to work each widget needs to override recreatePrimitives() method and recreate the
   637     In order for the style change to work each widget needs to override recreatePrimitives() method and recreate the
   505 	primitives. 
   638     primitives. 
   506     \param style Custom style for the widget, note that the ownership is not transferred
   639     \param style Custom style for the widget, note that the ownership is not transferred
   507 */
   640 */
   508 void HbWidget::setStyle( HbStyle *style )
   641 void HbWidget::setStyle( HbStyle *style )
   509 {
   642 {
   510     Q_D( HbWidget );
   643     Q_D( HbWidget );
   511     d->style = style;
   644     d->style = style;
   512 	recreatePrimitives();
   645     recreatePrimitives();
   513 	updatePrimitives();
   646     repolish();
   514 	repolish();
   647     updatePrimitives();
       
   648     
   515 }
   649 }
   516 
   650 
   517 /*!
   651 /*!
   518     As a default returns the hbInstance's platform style. If style is set using
   652     As a default returns the hbInstance's platform style. If style is set using
   519     the setStyle then the set style is returned.
   653     the setStyle then the set style is returned.
   542             delete acts[i];
   676             delete acts[i];
   543     }
   677     }
   544 }
   678 }
   545 
   679 
   546 /*!
   680 /*!
   547     \deprecated HbWidget::pluginBaseId()
       
   548         is deprecated. Style plugins are deprecated.
       
   549 */
       
   550 int HbWidget::pluginBaseId() const
       
   551 {
       
   552     return 0; // deprecated
       
   553 }
       
   554 
       
   555 /*!
       
   556     \deprecated HbWidget::setPluginBaseId(int)
       
   557         is deprecated. Style plugins are deprecated.
       
   558 */
       
   559 void HbWidget::setPluginBaseId( int baseId )
       
   560 {
       
   561     Q_UNUSED(baseId); // deprecated
       
   562 }
       
   563 
       
   564 /*!
       
   565   This function returns the HbMainWindow of the widget or \c 0 if
   681   This function returns the HbMainWindow of the widget or \c 0 if
   566   it doesn't exist.  Note that if the widget is not added to the scene
   682   it doesn't exist.  Note that if the widget is not added to the scene
   567   then this function returns the first mainwindow (if there is one)
   683   then this function returns the first mainwindow (if there is one)
   568   which is not guaranteed to be the proper one in a multi-display
   684   which is not guaranteed to be the proper one in a multi-display
   569   environment.
   685   environment.
   592     and recreate primitives.
   708     and recreate primitives.
   593 */
   709 */
   594 void HbWidget::recreatePrimitives()
   710 void HbWidget::recreatePrimitives()
   595 {
   711 {
   596     Q_D(HbWidget);
   712     Q_D(HbWidget);
   597     if(d->backgroundPrimitiveType != HbStyle::P_None) {
   713     if(d->backgroundPrimitiveType != HbStylePrivate::P_None) {
   598         if(d->backgroundItem) {
   714         if(d->backgroundItem) {
   599             delete d->backgroundItem;
   715             delete d->backgroundItem;
   600             d->backgroundItem = 0;
   716             d->backgroundItem = 0;
   601         }
   717         }
   602         d->backgroundItem = style()->createPrimitive(d->backgroundPrimitiveType, const_cast<HbWidget*>(this));
   718         d->backgroundItem = HbStylePrivate::createPrimitive(d->backgroundPrimitiveType, const_cast<HbWidget*>(this));
   603     }
   719     }
   604 }
   720 }
   605 
   721 
   606 /*!
   722 /*!
   607     A virtual slot to be called on the derived classes to notify in cases when the style
   723     A virtual slot to be called on the derived classes to notify in cases when the style
   614 
   730 
   615     HbStyleOption option;
   731     HbStyleOption option;
   616     initStyleOption(&option);
   732     initStyleOption(&option);
   617 
   733 
   618     if (backgroundItem()) {
   734     if (backgroundItem()) {
   619         if(d->backgroundPrimitiveType != HbStyle::P_None) {
   735         if(d->backgroundPrimitiveType != HbStylePrivate::P_None) {
   620             style()->updatePrimitive(backgroundItem(), d->backgroundPrimitiveType, &option);
   736             HbStylePrivate::updatePrimitive(backgroundItem(), d->backgroundPrimitiveType, &option);
   621         }
   737         }
   622         d->updateBackgroundItemSize();
   738         d->updateBackgroundItemSize();
   623     }
   739     }
   624     if (d->focusPrimitive(HbWidget::FocusHighlightResidual)) {
   740     if (d->focusPrimitive(HbWidget::FocusHighlightResidual)) {
   625         style()->updatePrimitive(d->focusPrimitive(HbWidget::FocusHighlightResidual),
   741         HbStylePrivate::updatePrimitive(d->focusPrimitive(HbWidget::FocusHighlightResidual),
   626             d->focusResidualType, &option);        
   742             d->focusResidualType, &option);        
   627     }
   743     }
   628 
   744 
   629     if (d->focusPrimitive(HbWidget::FocusHighlightActive)) {
   745     if (d->focusPrimitive(HbWidget::FocusHighlightActive)) {
   630         style()->updatePrimitive(d->focusPrimitive(HbWidget::FocusHighlightActive),
   746         HbStylePrivate::updatePrimitive(d->focusPrimitive(HbWidget::FocusHighlightActive),
   631             d->focusActiveType, &option);
   747             d->focusActiveType, &option);
   632     }
   748     }
   633    
   749    
   634 }
   750 }
   635 
   751 
   682         case QEvent::KeyRelease:
   798         case QEvent::KeyRelease:
   683             keyReleaseEvent(static_cast<QKeyEvent *>(event));
   799             keyReleaseEvent(static_cast<QKeyEvent *>(event));
   684             return true;
   800             return true;
   685 
   801 
   686         case QEvent::DynamicPropertyChange:             
   802         case QEvent::DynamicPropertyChange:             
   687             if (!qstrcmp(static_cast<QDynamicPropertyChangeEvent *>(event)->propertyName(), "state")) {
   803             if (!qstrcmp(static_cast<QDynamicPropertyChangeEvent *>(event)->propertyName(), "state") ||
       
   804                 !qstrcmp(static_cast<QDynamicPropertyChangeEvent *>(event)->propertyName(), "toolbutton_extension_layout")) {
   688                 if(!d->themingPending){
   805                 if(!d->themingPending){
   689                     style()->updateThemedParams(this);                    
   806                     style()->updateThemedParams(this);                    
   690                 }
   807                 }
   691             }
   808             }
   692             return true;
   809             return true;
       
   810 
       
   811         case QEvent::ToolTipChange: {
       
   812             HbGraphicsScene *hbScene = qobject_cast<HbGraphicsScene *>(scene());
       
   813             if (hbScene && !hbScene->d_ptr->mToolTip) {
       
   814                 //Tooltip needs to be created
       
   815                 hbScene->d_ptr->toolTip();
       
   816             }
       
   817             break;
       
   818         }
   693 
   819 
   694         default:
   820         default:
   695             if (event->type() == HbEvent::ChildFocusIn) {
   821             if (event->type() == HbEvent::ChildFocusIn) {
   696                 QGraphicsWidget *parentW = parentWidget();
   822                 QGraphicsWidget *parentW = parentWidget();
   697                 //Update last focus child in parent widget's focus group
   823                 //Update last focus child in parent widget's focus group
   718                 return true;
   844                 return true;
   719             } else if (event->type() == HbEvent::DeviceProfileChanged) {
   845             } else if (event->type() == HbEvent::DeviceProfileChanged) {
   720                 HbDeviceProfileChangedEvent* dpEvent = static_cast<HbDeviceProfileChangedEvent*>(event);
   846                 HbDeviceProfileChangedEvent* dpEvent = static_cast<HbDeviceProfileChangedEvent*>(event);
   721                 if ( dpEvent->profile().alternateProfileName() == dpEvent->oldProfile().name() ) {
   847                 if ( dpEvent->profile().alternateProfileName() == dpEvent->oldProfile().name() ) {
   722                     // This is an orientation change event.
   848                     // This is an orientation change event.
   723                     if (style()->hasOrientationSpecificStyleRules(this)) {
   849                     if (d->polished && style()->hasOrientationSpecificStyleRules(this)) {
   724                         polishEvent();
   850                         polishEvent();
   725                     }
   851                     }
   726                 } else {
   852                 } else {
   727                     // Some other device profile event (e.g. resolution change)
   853                     // Some other device profile event (e.g. resolution change)
   728                     polishEvent();
   854                     polishEvent();
   752  */
   878  */
   753 QVariant HbWidget::itemChange ( GraphicsItemChange change, const QVariant & value )
   879 QVariant HbWidget::itemChange ( GraphicsItemChange change, const QVariant & value )
   754 {
   880 {
   755     Q_D(HbWidget);
   881     Q_D(HbWidget);
   756 
   882 
   757 #ifdef HB_TESTABILITY
       
   758     if (d->testabilitySignal && d->testabilitySignal->signalEnabled()) {
   883     if (d->testabilitySignal && d->testabilitySignal->signalEnabled()) {
   759     	emit d->testabilitySignal->propertyChanges(change, value);
   884         emit d->testabilitySignal->propertyChanges(change, value);
   760     }
   885     }
   761 #endif //HB_TESTABILITY
   886 
   762 
   887     if (change == QGraphicsItem::ItemVisibleChange && value.toBool()) {
   763 	if (change == QGraphicsItem::ItemVisibleHasChanged) {
   888         QVariant returnValue  = HbWidgetBase::itemChange(change, value);
       
   889         bool resized = testAttribute(Qt::WA_Resized);
       
   890         if (!resized) {
       
   891             adjustSize();
       
   892             setAttribute(Qt::WA_Resized, false);
       
   893         }
       
   894         return returnValue;
       
   895     }
       
   896     else if (change == QGraphicsItem::ItemVisibleHasChanged) {
   764         if (!d->polished && !value.toBool()) {
   897         if (!d->polished && !value.toBool()) {
   765 
   898 
   766             // temporary solution starts -->>
   899             // temporary solution starts -->>
   767             // bypass QT defect: 251309
   900             // bypass QT defect: 251309
   768             // when the defect is fixed, this needs to be removed to prevent calling the polish
   901             // when the defect is fixed, this needs to be removed to prevent calling the polish
   769             // twice.
   902             // twice.
   770 #if (QT_VERSION < QT_VERSION_CHECK(4, 6, 1))            
   903 #if (QT_VERSION < QT_VERSION_CHECK(4, 6, 1))            
   771 			if ( !scene() ) {
   904             if ( !scene() ) {
   772                 d->polishPending = true;
   905                 d->polishPending = true;
   773             }
   906             }
   774 #endif
   907 #endif
   775             // <<-- temporary solution ends
   908             // <<-- temporary solution ends
   776 
   909 
   777 
   910 
   778             // Don't call baseclass imp to prevent hideEvent()
   911             // Don't call baseclass imp to prevent hideEvent()
   779             // to be sent when the widget is not polished yet.
   912             // to be sent when the widget is not polished yet.
   780             return value;
   913             return value;
   781         }
   914         }
   782         if (d->polishPending && value.toBool()) {
   915         if (value.toBool()) {
   783             d->polishPending = false;
   916             bool notifyGraphicsScene = d->polishPending;
   784             HbStyleParameters params;
   917             if(d->polishPending) {
   785             polish( params );
   918                 d->polishPending = false;
   786             if (scene()) {
   919                 HbStyleParameters params;
   787              // The widget is polished again or is beign polished now. As we set layout to widget in polishEvent,
   920                 polish( params );
   788              // inform scene to polish any new child items and handle any resulting layoutrequest
   921             }
   789              // events before drawing.
   922             //There could be situations where widget is becoming visible and has
   790                 HbGraphicsScene *hbscene = qobject_cast<HbGraphicsScene*>(scene());
   923             //polish request pending in event queue.This make's sure we
   791                 if (hbscene) {
   924             //handle any pending repolish requests of widget before its drawn.
   792                     HbGraphicsScenePrivate::d_ptr(hbscene)->mPolishWidgets = true;
   925             if ((notifyGraphicsScene || d->repolishOutstanding)) {
   793                 }
   926                 //Widget is polished again or is being polished now.
       
   927                 //As we set layout to widget in polishEvent,inform scene to polish any new
       
   928                 //child items and handle any resulting layoutrequest events before drawing.
       
   929                 d->notifySceneForPolish(!d->repolishOutstanding);
   794             }
   930             }
   795         }
   931         }
   796     }
   932     }
   797     else if (change == QGraphicsItem::ItemChildAddedChange) {
   933     else if (change == QGraphicsItem::ItemChildAddedChange) {
   798         if (d->focusGroup) {
   934         if (d->focusGroup) {
   815     }
   951     }
   816     else if (change == QGraphicsItem::ItemSceneHasChanged) {
   952     else if (change == QGraphicsItem::ItemSceneHasChanged) {
   817         if (d->focusGroup) {
   953         if (d->focusGroup) {
   818             d->focusGroup->registerChildren(this);
   954             d->focusGroup->registerChildren(this);
   819         }
   955         }
   820         if (d->notifyScene && scene()) {
   956         if (d->notifyScene) {
   821             // The widget has been added to a new scene and this would result in
   957             // The widget has been added to a new scene and this would result in
   822             // polishing widget. As we set layout to widget in polishEvent,
   958             // polishing widget. As we set layout to widget in polishEvent,
   823             // inform scene to handle polish and any resulting layoutrequest
   959             // inform scene to handle polish and any resulting layoutrequest
   824             // events before drawing.
   960             // events before drawing.
   825             d->notifyScene = false;
   961             d->notifyScene = false;
   826             HbGraphicsScene *hbscene = qobject_cast<HbGraphicsScene*>(scene());
   962             d->notifySceneForPolish();
   827             if (hbscene) {
       
   828                 HbGraphicsScenePrivate::d_ptr(hbscene)->mPolishWidgets = true;
       
   829             }
       
   830         }
   963         }
   831     }
   964     }
   832     else if( change == QGraphicsItem::ItemEnabledHasChanged) {
   965     else if( change == QGraphicsItem::ItemEnabledHasChanged) {
   833          if(!d->themingPending){
   966          if(!d->themingPending){
   834             style()->updateThemedParams(this);
   967             style()->updateThemedParams(this);
   862     Override this method if you want to do something special in polish
   995     Override this method if you want to do something special in polish
   863     (e.g. fetch some custom style parameters to your widget from style
   996     (e.g. fetch some custom style parameters to your widget from style
   864     backend). When overriding, always call the base classes 
   997     backend). When overriding, always call the base classes 
   865     impelentation.
   998     impelentation.
   866 
   999 
   867     \param params, For querying (custom) style parameters from HbStyle.
  1000     \param params For querying (custom) style parameters from HbStyle.
   868 
  1001 
   869     \sa polish()
  1002     \sa polish()
   870 */
  1003 */
   871 void HbWidget::polish( HbStyleParameters& params )
  1004 void HbWidget::polish( HbStyleParameters& params )
   872 {
  1005 {
   873     Q_D(HbWidget);
  1006     Q_D(HbWidget);
   874     if (isVisible()) {
  1007     if (isVisible()) {
   875         style()->polish(this, params);
  1008         style()->polish(this, params);
   876         d->polished = true;
  1009         d->polished = 1;
   877         d->repolishOutstanding = false;
  1010         d->repolishOutstanding = false;
   878         if (d->themingPending) {
  1011         if (d->themingPending) {
   879             style()->updateThemedParams(this);
  1012             style()->updateThemedParams(this);
   880             d->themingPending = false;
  1013             d->themingPending = false;
   881         }
  1014         }
       
  1015         //make sure in case of item's positions being changed  or
       
  1016         //child items being added or deleted the complete region of
       
  1017         //widget is invalidated
       
  1018         prepareGeometryChange();
       
  1019         //invalidate cached sizehints of parentLayoutItem(in case we are managed by layout).
       
  1020         //This would eventually happen anyway but force it here to minimise the layout request iterations.
       
  1021         QGraphicsLayoutItem *parent = parentLayoutItem();
       
  1022         if (parent && parent->isLayout())
       
  1023             updateGeometry();
   882     } else {
  1024     } else {
   883         d->polishPending = true;
  1025         d->polishPending = true;
   884     }
  1026     }
   885 }
  1027 }
   886 
  1028 
   895 {
  1037 {
   896     Q_D(HbWidget);
  1038     Q_D(HbWidget);
   897     // "d->polished" check removed because there's a bug in QGraphicsScene (Qt bug id 251309):
  1039     // "d->polished" check removed because there's a bug in QGraphicsScene (Qt bug id 251309):
   898     // "polishEvent" is never sent to items that were invisible when added to scene.
  1040     // "polishEvent" is never sent to items that were invisible when added to scene.
   899 #if (QT_VERSION < QT_VERSION_CHECK(4, 6, 1))       
  1041 #if (QT_VERSION < QT_VERSION_CHECK(4, 6, 1))       
   900 	if (!d->repolishOutstanding) {
  1042     if (!d->repolishOutstanding) {
   901 #else
  1043 #else
   902 	if (d->polished && !d->repolishOutstanding) {
  1044     if (d->polished && !d->repolishOutstanding) {
   903 #endif
  1045 #endif
   904         d->repolishOutstanding = true;
  1046         d->repolishOutstanding = true;
   905         QEvent* polishEvent = new QEvent( QEvent::Polish );
  1047         QEvent* polishEvent = new QEvent( QEvent::Polish );
   906         QCoreApplication::postEvent(this, polishEvent);
  1048         QCoreApplication::postEvent(this, polishEvent);
   907         // If no one is handling repolish synchronously, lets make sure they are handled
  1049         // The widget needs to be polished again. As we set layout to widget in polishEvent,
   908         // before painting. For example view items handle them synchronously.
  1050         // inform scene to handle polish and any resulting layoutrequest
   909         if (scene() && !d->mHandlingRepolishSynchronously) {
  1051         // events before drawing.
   910             // The widget needs to be polished again. As we set layout to widget in polishEvent,
  1052         d->notifySceneForPolish(false);
   911             // inform scene to handle polish and any resulting layoutrequest
       
   912             // events before drawing.
       
   913             HbGraphicsScene *hbscene = qobject_cast<HbGraphicsScene*>(scene());
       
   914             if (hbscene) {
       
   915                 HbGraphicsScenePrivate::d_ptr(hbscene)->mRepolishWidgets = true;
       
   916             }
       
   917         }
       
   918     }
  1053     }
   919 }
  1054 }
   920 
  1055 
   921 /*!
  1056 /*!
   922 
  1057 
   924         is deprecated. Use HbWidget::primitive(const QString) instead.
  1059         is deprecated. Use HbWidget::primitive(const QString) instead.
   925 */
  1060 */
   926 QGraphicsItem *HbWidget::primitive(HbStyle::Primitive primitive) const
  1061 QGraphicsItem *HbWidget::primitive(HbStyle::Primitive primitive) const
   927 {
  1062 {
   928     Q_D(const HbWidget);
  1063     Q_D(const HbWidget);
   929     if (primitive == d->backgroundPrimitiveType) {
  1064     const HbStylePrivate::Primitive p = (HbStylePrivate::Primitive)primitive;
       
  1065     if (p == d->backgroundPrimitiveType) {
   930         return backgroundItem();
  1066         return backgroundItem();
   931     }
  1067     }
   932     if (primitive == d->focusActiveType) {
  1068     if (p == d->focusActiveType) {
   933         return d->focusPrimitive(HbWidget::FocusHighlightActive);
  1069         return d->focusPrimitive(HbWidget::FocusHighlightActive);
   934     }
  1070     }
   935     if (primitive == d->focusResidualType) {
  1071     if (p == d->focusResidualType) {
   936         return d->focusPrimitive(HbWidget::FocusHighlightResidual);
  1072         return d->focusPrimitive(HbWidget::FocusHighlightResidual);
   937     }
  1073     }
   938 
  1074 
   939     return 0;
  1075     return 0;
   940 }
  1076 }
  1249     Q_D( const HbWidget);
  1385     Q_D( const HbWidget);
  1250     HbFocusGroup *group = d->getFocusGroup(false);
  1386     HbFocusGroup *group = d->getFocusGroup(false);
  1251     return group?group->focusMode():HbWidget::FocusModeNormal;
  1387     return group?group->focusMode():HbWidget::FocusModeNormal;
  1252 }
  1388 }
  1253 
  1389 
  1254 /*
       
  1255 
       
  1256     \deprecated HbWidget::setFocusHighlight(HbStyle::Primitive, HbWidget::FocusHighlight)
       
  1257     is deprecated. This method will be made private and should not be used by public API users.
       
  1258 
       
  1259     Sets focus primitives defined in HbStyle. If FocusHighlightNone is set both active
       
  1260     and residual focus primitives are deleted.
       
  1261 
       
  1262     \param type Defines the primitive for the highlight.
       
  1263     \param focusHighlight Defines the highlight the primitive is used.
       
  1264 */
       
  1265 void HbWidget::setFocusHighlight(HbStyle::Primitive type, HbWidget::FocusHighlight focusHighlight)
       
  1266 {
       
  1267     Q_D(HbWidget);
       
  1268     if (focusHighlight == HbWidget::FocusHighlightNone) {
       
  1269         d->focusActiveType = HbStyle::P_None;
       
  1270         d->focusResidualType = HbStyle::P_None;
       
  1271 
       
  1272         delete d->focusActiveItem;
       
  1273         d->focusActiveItem = 0;
       
  1274         delete d->focusResidualItem;
       
  1275         d->focusResidualItem = 0;
       
  1276     } else if (focusHighlight == HbWidget::FocusHighlightActive) {
       
  1277         d->focusActiveType = type;
       
  1278     } else {
       
  1279         d->focusResidualType = type;
       
  1280     }
       
  1281 }
       
  1282 
       
  1283 /*!
       
  1284 
       
  1285     \deprecated HbWidget::focusHighlight(HbWidget::FocusHighlight)
       
  1286     is deprecated. This method will be made private and should not be used by public API users.
       
  1287 
       
  1288     Returns identifier of the focus primitive defined in HbStyle for focus highlight.
       
  1289     \param highlightType defines the highlight type.
       
  1290 */
       
  1291 HbStyle::Primitive HbWidget::focusHighlight(HbWidget::FocusHighlight highlightType)
       
  1292 {
       
  1293     Q_D(HbWidget);
       
  1294     HbStyle::Primitive primitive(HbStyle::P_None);
       
  1295 
       
  1296     if (highlightType == HbWidget::FocusHighlightActive) {
       
  1297         primitive = d->focusActiveType;
       
  1298     } else if (highlightType == HbWidget::FocusHighlightResidual) {
       
  1299         primitive = d->focusResidualType;
       
  1300     }
       
  1301     return primitive;
       
  1302 }
       
  1303 
  1390 
  1304 bool HbWidget::sceneEventFilter (QGraphicsItem *watched, QEvent *event)
  1391 bool HbWidget::sceneEventFilter (QGraphicsItem *watched, QEvent *event)
  1305 {
  1392 {
  1306     if(event->type() == QEvent::Gesture && watched->type() == Hb::ItemType_TouchArea) {
  1393     if(isEnabled() && event->type() == QEvent::Gesture && watched->type() == Hb::ItemType_TouchArea) {
  1307         QGestureEvent* ge = static_cast<QGestureEvent*>(event);
  1394         QGestureEvent* ge = static_cast<QGestureEvent*>(event);
  1308         HbTapGesture* tap = qobject_cast<HbTapGesture*>(ge->gesture(Qt::TapGesture));
  1395         HbTapGesture* tap = qobject_cast<HbTapGesture*>(ge->gesture(Qt::TapGesture));
  1309 
  1396 
  1310         if (tap && tap->state() == Qt::GestureStarted) {
  1397         if (tap && tap->state() == Qt::GestureStarted) {
  1311             tap->setProperty(HbPrivate::ThresholdRect.latin1(), watched->mapRectToScene(watched->boundingRect()).toRect());
  1398             tap->setProperty(HbPrivate::ThresholdRect.latin1(), watched->mapRectToScene(watched->boundingRect()).toRect());