src/hbcore/primitives/hbframeitem.cpp
changeset 0 16d8024aca5e
child 1 f7ac710697a9
equal deleted inserted replaced
-1:000000000000 0:16d8024aca5e
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (developer.feedback@nokia.com)
       
     6 **
       
     7 ** This file is part of the HbCore module of the UI Extensions for Mobile.
       
     8 **
       
     9 ** GNU Lesser General Public License Usage
       
    10 ** This file may be used under the terms of the GNU Lesser General Public
       
    11 ** License version 2.1 as published by the Free Software Foundation and
       
    12 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
       
    13 ** Please review the following information to ensure the GNU Lesser General
       
    14 ** Public License version 2.1 requirements will be met:
       
    15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    16 **
       
    17 ** In addition, as a special exception, Nokia gives you certain additional
       
    18 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    20 **
       
    21 ** If you have questions regarding the use of this file, please contact
       
    22 ** Nokia at developer.feedback@nokia.com.
       
    23 **
       
    24 ****************************************************************************/
       
    25 
       
    26 #include "hbframeitem.h"
       
    27 #include "hbevent.h"
       
    28 #include "hbframedrawerpool_p.h"
       
    29 #include <QPainter>
       
    30 #include <QGraphicsWidget>
       
    31 
       
    32 /*!
       
    33 	@stable
       
    34     @hbcore
       
    35     \class HbFrameItem
       
    36     \brief A graphics item that draws a frame using the given HbFrameDrawer instance.
       
    37     
       
    38     This class is not intended to be derived from.
       
    39 
       
    40     Example of how to create a graphics frame item and use it.
       
    41 
       
    42     \dontinclude ultimatecodesnippet/ultimatecodesnippet.cpp
       
    43     \skip for the frame item
       
    44     \until }
       
    45 
       
    46     \sa class HbFrameDrawer
       
    47 */
       
    48 
       
    49 /*!
       
    50     \reimp
       
    51     \fn int HbFrameItem::type() const
       
    52  */
       
    53 
       
    54 class HbFrameItemPrivate
       
    55 {
       
    56 public:
       
    57     HbFrameItemPrivate(HbFrameItem *q);
       
    58     HbFrameItemPrivate(HbFrameItem *q, HbFrameDrawer *drawer);
       
    59     HbFrameItemPrivate(HbFrameItem *q,
       
    60                        const QString &frameGraphicsName,
       
    61                        HbFrameDrawer::FrameType frameGraphicsType);
       
    62     ~HbFrameItemPrivate();
       
    63 
       
    64     void init();
       
    65     void destroyFrameDrawer();
       
    66 
       
    67     HbFrameItem *item;
       
    68     HbFrameDrawer *frameDrawer;
       
    69 
       
    70 private:
       
    71     Q_DISABLE_COPY(HbFrameItemPrivate)
       
    72 };
       
    73 
       
    74 HbFrameItemPrivate::HbFrameItemPrivate(HbFrameItem *q)
       
    75     : item(q),
       
    76       frameDrawer(new HbFrameDrawer())
       
    77 {
       
    78     init();
       
    79 }
       
    80 
       
    81 HbFrameItemPrivate::HbFrameItemPrivate(HbFrameItem *q, HbFrameDrawer *drawer)
       
    82     : item(q),        
       
    83       frameDrawer(drawer)
       
    84 {
       
    85     init();
       
    86 }
       
    87 
       
    88 HbFrameItemPrivate::HbFrameItemPrivate(HbFrameItem *q,
       
    89                                        const QString &frameGraphicsName,
       
    90                                        HbFrameDrawer::FrameType frameGraphicsType)
       
    91     : item(q)
       
    92 {
       
    93     frameDrawer = HbFrameDrawerPool::get(frameGraphicsName, frameGraphicsType);
       
    94     init();
       
    95 }
       
    96 
       
    97 HbFrameItemPrivate::~HbFrameItemPrivate()
       
    98 {
       
    99     destroyFrameDrawer();
       
   100 }
       
   101 
       
   102 void HbFrameItemPrivate::init()
       
   103 {
       
   104     if ( QGraphicsWidget *parent = item->parentWidget() ) {
       
   105         frameDrawer->setLayoutDirection(parent->layoutDirection());
       
   106     }
       
   107 
       
   108     frameDrawer->setGraphicsItem(item);
       
   109 }
       
   110 
       
   111 void HbFrameItemPrivate::destroyFrameDrawer()
       
   112 {
       
   113     HbFrameDrawerPool::release(frameDrawer); // works also when frameDrawer is not from the pool
       
   114 }
       
   115 
       
   116 /*! Constructs a new graphics frame item with the given \a parent graphics item.
       
   117 */
       
   118 HbFrameItem::HbFrameItem(QGraphicsItem *parent) :
       
   119     HbWidgetBase(parent)
       
   120 {
       
   121     d = new HbFrameItemPrivate(this);
       
   122 }
       
   123 
       
   124 /*! Constructs a new graphics frame item with the given frame \a drawer and \a parent graphics item.
       
   125 * The ownership of the given frame drawer object is transferred. It can be accessed with method
       
   126 * HbFrameItem::frameDrawer().
       
   127 */
       
   128 HbFrameItem::HbFrameItem(HbFrameDrawer *drawer, QGraphicsItem *parent)
       
   129     : HbWidgetBase(parent)
       
   130 {
       
   131     d = new HbFrameItemPrivate(this, drawer);
       
   132 }
       
   133 
       
   134 /*!
       
   135   Constructs a new graphics frame item by automatically creating (or reusing) a framedrawer internally.
       
   136 
       
   137   Sharing is done by using the global HbFrameDrawerPool instance.
       
   138 
       
   139   This is the most efficient way of creating frame items that show the same graphics because they can
       
   140   share the same HbFrameDrawer instance internally between each other.
       
   141 
       
   142   \sa HbFrameDrawerPool
       
   143 */
       
   144 HbFrameItem::HbFrameItem(const QString &frameGraphicsName,
       
   145                          HbFrameDrawer::FrameType frameGraphicsType,
       
   146                          QGraphicsItem *parent)
       
   147     : HbWidgetBase(parent)
       
   148 {
       
   149     d = new HbFrameItemPrivate(this, frameGraphicsName, frameGraphicsType);
       
   150 }
       
   151 
       
   152 /*!
       
   153 * Destroys the graphics frame item.
       
   154 */
       
   155 HbFrameItem::~HbFrameItem()
       
   156 {
       
   157     delete d;
       
   158 }
       
   159 
       
   160 /*!
       
   161 * Returns a reference to the frame drawer used for painting the frame.
       
   162 * The bounding rectangle for the frame can be changed by calling
       
   163 * setRect() to the returned frame drawer.
       
   164 *
       
   165 * Note that if the HbFrameItem(const QString &, HbFrameDrawer::FrameType, QGraphicsItem *)
       
   166 * constructor was used then the frame drawer returned by this function may be an instance
       
   167 * that is shared between several frame items.
       
   168 *
       
   169 * \sa HbFrameItem::setFrameDrawer()
       
   170 */
       
   171 HbFrameDrawer &HbFrameItem::frameDrawer() const
       
   172 {
       
   173     return *(d->frameDrawer);
       
   174 }
       
   175 
       
   176 /*!
       
   177 * Sets the frame \a drawer used for painting the frame.
       
   178 * The ownership of the given frame drawer object is transferred.
       
   179 * It can be accessed with method HbFrameItem::frameDrawer().
       
   180 *
       
   181 * \sa HbFrameItem::frameDrawer()
       
   182 */
       
   183 void HbFrameItem::setFrameDrawer(HbFrameDrawer *drawer)
       
   184 {
       
   185     if (drawer && drawer != d->frameDrawer) {
       
   186         d->destroyFrameDrawer();
       
   187         d->frameDrawer = drawer;
       
   188         d->init();
       
   189     }
       
   190 }
       
   191 
       
   192 /*!
       
   193 * Paints the frame.
       
   194 */
       
   195 void HbFrameItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
       
   196 {
       
   197     Q_UNUSED(option);
       
   198     Q_UNUSED(widget);
       
   199 
       
   200     d->frameDrawer->paint(painter, boundingRect());
       
   201 }
       
   202 
       
   203  /*!
       
   204     \reimp
       
   205  */
       
   206 void HbFrameItem::changeEvent(QEvent *event)
       
   207 {
       
   208     if ( event->type() == QEvent::LayoutDirectionChange ) {
       
   209         if ( QGraphicsWidget *parent = parentWidget() ) {
       
   210             d->frameDrawer->setLayoutDirection( parent->layoutDirection() );
       
   211         }
       
   212     } else if ( event->type() == HbEvent::ThemeChanged ) {
       
   213         d->frameDrawer->themeChanged();
       
   214     }
       
   215 
       
   216     switch (event->type()) {
       
   217         // suppress events (optimize)
       
   218     case QEvent::FontChange:
       
   219     case QEvent::PaletteChange:
       
   220     case QEvent::ParentChange:
       
   221         break;
       
   222     case QEvent::StyleChange: // flow through
       
   223     default:
       
   224         HbWidgetBase::changeEvent( event );
       
   225         break;
       
   226     }
       
   227 }
       
   228 
       
   229 // End of File