ganeswidgets/src/hgindexfeedback.cpp
changeset 0 89c329efa980
child 1 e48454f237ca
equal deleted inserted replaced
-1:000000000000 0:89c329efa980
       
     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 "hgindexfeedback.h"
       
    19 #include "hgindexfeedback_p.h"
       
    20 
       
    21 #include <hbscrollbar.h>
       
    22 #include <hbstyleoptionindexfeedback.h>
       
    23 #include <hbstyleparameters.h>
       
    24 #include <hbstyle.h>
       
    25 #include <hbdeviceprofile.h>
       
    26 #include <hgwidgets/hgwidgets.h>
       
    27 #include <hbstyleloader.h>
       
    28 
       
    29 #include <QEvent>
       
    30 #include <QObject>
       
    31 #include <QGraphicsScene>
       
    32 
       
    33 
       
    34 /* rather than magic numbers, let's define some constants. */
       
    35 namespace {
       
    36 /*
       
    37     string name from the index feedback .css for the single character height parameter.
       
    38 */
       
    39 static const QString ONE_CHAR_HEIGHT = QLatin1String("one-char-height");
       
    40 
       
    41 /*
       
    42     string name from the index feedback .css for the three character height parameter.
       
    43 */
       
    44 static const QString THREE_CHAR_HEIGHT = QLatin1String("three-char-height");
       
    45 
       
    46 /*
       
    47     string name from the index feedback .css for the three character width parameter.
       
    48 */
       
    49 static const QString THREE_CHAR_WIDTH = QLatin1String("three-char-width");
       
    50 
       
    51 /*
       
    52     string name from the index feedback .css for the string mode offest parameter.
       
    53 */
       
    54 static const QString STRING_OFFSET = QLatin1String("string-offset");
       
    55 }
       
    56 
       
    57 /*!
       
    58     Constructs a new HgIndexFeedback with a parent.
       
    59 */
       
    60 HgIndexFeedback::HgIndexFeedback(QGraphicsItem *parent)
       
    61     : HbWidget( *new HgIndexFeedbackPrivate, parent, 0)
       
    62     
       
    63 {
       
    64     Q_D( HgIndexFeedback );
       
    65     d->q_ptr = this;
       
    66 
       
    67     HbStyleLoader::registerFilePath(":/hgindexfeedback.css");
       
    68     
       
    69     d->init();
       
    70 }
       
    71 
       
    72 /*!
       
    73     Destructs the index feedback.
       
    74 */
       
    75 HgIndexFeedback::~HgIndexFeedback()
       
    76 {
       
    77     HbStyleLoader::unregisterFilePath(":/hgindexfeedback.css");
       
    78 }
       
    79 
       
    80 /*!
       
    81     \brief sets the index feedback policy.
       
    82 
       
    83     \param policy - The HgIndexFeedback::IndexFeedbackPolicy to use.
       
    84 
       
    85     \sa HgIndexFeedback::IndexFeedbackPolicy
       
    86 */
       
    87 void HgIndexFeedback::setIndexFeedbackPolicy(HgWidget::IndexFeedbackPolicy policy)
       
    88 {
       
    89     Q_D( HgIndexFeedback );
       
    90 
       
    91     if (policy != d->mIndexFeedbackPolicy) {
       
    92         d->_q_hideIndexFeedbackNow();
       
    93         d->mIndexFeedbackPolicy = policy;
       
    94         d->calculatePopupRects();
       
    95         d->updatePrimitives();
       
    96     }
       
    97 }
       
    98 
       
    99 /*!
       
   100     \brief Returns the index feedback policy.
       
   101 
       
   102     \return The HgIndexFeedback::IndexFeedbackPolicy that's currently in use.
       
   103 */
       
   104 HgWidget::IndexFeedbackPolicy HgIndexFeedback::indexFeedbackPolicy() const
       
   105 {
       
   106     Q_D( const HgIndexFeedback );
       
   107 
       
   108     return d->mIndexFeedbackPolicy;
       
   109 }
       
   110 
       
   111 /*!
       
   112     \brief sets the item view for the index feedback.
       
   113 
       
   114     Disconnects from the existing item view, then connects to the specified one.
       
   115 
       
   116     If set to NULL the index feedback is simply disconnected.
       
   117 
       
   118     \param itemView The HbAbstractItemView* to provide index feedback for.
       
   119 */
       
   120 void HgIndexFeedback::setWidget(HgWidget *widget)
       
   121 {
       
   122     Q_D( HgIndexFeedback );
       
   123 
       
   124     if (widget == d->mWidget) {
       
   125         return;
       
   126     }
       
   127 
       
   128     d->disconnectItemView();
       
   129 
       
   130     d->mWidget = widget;
       
   131 
       
   132     if (!d->mWidget) {
       
   133         return;
       
   134     }
       
   135 
       
   136     d->connectModelToIndexFeedback(d->mWidget->selectionModel());
       
   137     
       
   138     d->connectScrollBarToIndexFeedback(d->mWidget->scrollBar());
       
   139 
       
   140     connect(d->mWidget, SIGNAL(destroyed(QObject*)),
       
   141         this, SLOT(_q_itemViewDestroyed()));
       
   142 
       
   143     if (d->mWidget->scene()) {
       
   144         d->mWidget->scene()->addItem(this);
       
   145         d->mWidget->installSceneEventFilter(this);
       
   146     }
       
   147 
       
   148     d->calculatePopupRects();
       
   149     updatePrimitives();
       
   150 }
       
   151 
       
   152 /*!
       
   153     Returns the HbAbstractItemView which the index feedback currently monitors.
       
   154 
       
   155     \return HbAbstractItemView*.
       
   156 */
       
   157 HgWidget* HgIndexFeedback::widget() const
       
   158 {
       
   159     Q_D( const HgIndexFeedback );
       
   160 
       
   161     return d->mWidget;
       
   162 }
       
   163 
       
   164 /*!
       
   165     Returns the primitives used in HgIndexFeedback.
       
   166 
       
   167     \param primitive The primitive type requested.
       
   168 
       
   169     \return A pointer for the primitive requested.
       
   170 */
       
   171 QGraphicsItem* HgIndexFeedback::primitive(HbStyle::Primitive primitive) const
       
   172 {
       
   173     Q_D( const HgIndexFeedback );
       
   174 
       
   175     QGraphicsItem* retVal = HbWidget::primitive(primitive);
       
   176 
       
   177     switch (primitive) {
       
   178         case HbStyle::P_IndexFeedback_popup_text:
       
   179             retVal = d->mTextItem;
       
   180             break;
       
   181             
       
   182         case HbStyle::P_IndexFeedback_popup_background:
       
   183             retVal = d->mPopupItem;
       
   184             break;
       
   185 
       
   186         default:
       
   187             qt_noop();
       
   188             break;
       
   189     }
       
   190 
       
   191     return retVal;
       
   192 }
       
   193 
       
   194 
       
   195 /*
       
   196     A scene event filter.  It's purpose is to call calculatePopupRects on
       
   197     a resize event for the item view.
       
   198 */
       
   199 bool HgIndexFeedback::sceneEventFilter(QGraphicsItem *watched, QEvent *ev)
       
   200 {
       
   201     Q_D( HgIndexFeedback );
       
   202 
       
   203     if (ev->type() == QEvent::GraphicsSceneResize) {
       
   204         d->calculatePopupRects();
       
   205     }
       
   206     
       
   207     return QGraphicsItem::sceneEventFilter(watched, ev);
       
   208 }
       
   209 
       
   210 /*
       
   211     Rather than adding signals to HbScrollBar specifically to implement 
       
   212     index feedback, an event filter is used.
       
   213 
       
   214     Specifically, if a scrollbar which is interactive is pressed or released
       
   215     this function will call the appropriate function in HgIndexFeedbackPrivate.
       
   216 */
       
   217 bool HgIndexFeedback::eventFilter(QObject *obj, QEvent *ev)
       
   218 {
       
   219     Q_D( HgIndexFeedback );
       
   220     HbScrollBar* scrollBar = qobject_cast<HbScrollBar*>(obj);
       
   221 
       
   222     if (d->mIndexFeedbackPolicy != HgWidget::IndexFeedbackNone
       
   223         && scrollBar) {
       
   224         switch (ev->type()) {
       
   225             case QEvent::GraphicsSceneMousePress:
       
   226             case QEvent::MouseButtonPress:
       
   227                 if (scrollBar->isInteractive()) {
       
   228                     d->scrollBarPressed();
       
   229                 }
       
   230                 break;
       
   231 
       
   232             case QEvent::GraphicsSceneMouseRelease:
       
   233             case QEvent::MouseButtonRelease:
       
   234                 if (scrollBar->isInteractive()) {
       
   235                     d->scrollBarReleased();
       
   236                 }
       
   237                 break;
       
   238         
       
   239             case QEvent::GraphicsSceneResize:
       
   240             case QEvent::Resize:
       
   241                     d->_q_hideIndexFeedbackNow();
       
   242                     d->calculatePopupRects();
       
   243                     d->updatePrimitives();
       
   244                 break;
       
   245 
       
   246             default:
       
   247                 // do nothing, ignore other events.
       
   248                 break;
       
   249         }
       
   250     }
       
   251 
       
   252     return QObject::eventFilter(obj, ev);
       
   253 }
       
   254 
       
   255 /*
       
   256     For use with HbStyle.
       
   257 
       
   258     Provide the correct data to use in the 'model.'
       
   259 */
       
   260 void HgIndexFeedback::initStyleOption(HbStyleOptionIndexFeedback *option) const
       
   261 {
       
   262     Q_D( const HgIndexFeedback );
       
   263 
       
   264     HbWidget::initStyleOption(option);
       
   265 
       
   266     if (!d->mWidget) {
       
   267         return;
       
   268     }
       
   269 
       
   270     HbFontSpec fontSpec;
       
   271     qreal margin = 0;
       
   272 
       
   273     style()->parameter(QLatin1String("hb-param-margin-gene-popup"), margin);
       
   274 
       
   275     switch (d->mIndexFeedbackPolicy) {
       
   276         case HgWidget::IndexFeedbackSingleCharacter:
       
   277             {
       
   278                 fontSpec = HbFontSpec(HbFontSpec::Primary);
       
   279                 fontSpec.setTextPaneHeight(d->textHeight());
       
   280             }
       
   281             break;
       
   282             
       
   283         case HgWidget::IndexFeedbackThreeCharacter:
       
   284             {
       
   285                 fontSpec = HbFontSpec(HbFontSpec::Primary);
       
   286                 fontSpec.setTextPaneHeight(d->textHeight());
       
   287             }
       
   288             break;
       
   289 
       
   290         case HgWidget::IndexFeedbackString:
       
   291             {
       
   292                 fontSpec = HbFontSpec(HbFontSpec::Primary);
       
   293                 qreal textPaneHeight = 0;
       
   294                 style()->parameter(QLatin1String("hb-param-text-height-primary"), textPaneHeight);
       
   295                 fontSpec.setTextPaneHeight( textPaneHeight );
       
   296             }
       
   297             break;
       
   298 
       
   299         case HgWidget::IndexFeedbackNone:
       
   300             // leave the HbStyleOption uninitialized
       
   301             return;
       
   302     }
       
   303 
       
   304     option->text = d->mPopupContent;
       
   305     option->fontSpec = fontSpec;
       
   306     option->textRect = d->mPopupTextRect;
       
   307     option->popupRect = d->mPopupBackgroundRect;
       
   308 }
       
   309 
       
   310 void HgIndexFeedback::polish(HbStyleParameters& params)
       
   311 {
       
   312     Q_D( HgIndexFeedback );
       
   313 
       
   314     params.addParameter( ONE_CHAR_HEIGHT );
       
   315     params.addParameter( THREE_CHAR_HEIGHT );
       
   316     params.addParameter( THREE_CHAR_WIDTH );
       
   317     params.addParameter( STRING_OFFSET );
       
   318 
       
   319     HbWidget::polish( params );
       
   320 
       
   321     d->mOneCharHeight = params.value( ONE_CHAR_HEIGHT ).toDouble();
       
   322     d->mThreeCharHeight = params.value( THREE_CHAR_HEIGHT ).toDouble();
       
   323     d->mThreeCharWidth = params.value( THREE_CHAR_WIDTH ).toDouble();
       
   324     d->mStringOffset = params.value( STRING_OFFSET ).toDouble();
       
   325 
       
   326     d->calculatePopupRects();
       
   327 }
       
   328 
       
   329 #include "moc_hgindexfeedback.cpp"