src/hbwidgets/itemviews/hbabstractviewitem_p.h
changeset 21 4633027730f5
parent 7 923ff622b8b9
child 23 e6ad4ef83b23
equal deleted inserted replaced
7:923ff622b8b9 21:4633027730f5
    29 
    29 
    30 #include <hbwidget_p.h>
    30 #include <hbwidget_p.h>
    31 #include <hbeffect.h>
    31 #include <hbeffect.h>
    32 #include <hbframebackground.h>
    32 #include <hbframebackground.h>
    33 #include <hbnamespace.h>
    33 #include <hbnamespace.h>
       
    34 #include <hboogmwatcher_p.h>
       
    35 #include <hbabstractitemview.h>
       
    36 #include <hbstyleoptionabstractviewitem_p.h>
    34 
    37 
    35 #include <QObject>
    38 #include <QObject>
    36 #include <QPersistentModelIndex>
    39 #include <QPersistentModelIndex>
    37 #include <QPointer>
    40 #include <QPointer>
    38 #include <QExplicitlySharedDataPointer>
    41 #include <QExplicitlySharedDataPointer>
    39 #include <QSharedData>
    42 #include <QSharedData>
    40 
    43 #include <QPainter>
    41 class HbAbstractItemView;
    44 
    42 class QGraphicsItem;
    45 class QGraphicsItem;
    43 class QTimer;
    46 class QTimer;
    44 class QGestureEvent;
    47 class QGestureEvent;
    45 
    48 
    46 #define HB_SD(Class) Class##Shared * sd = (Class##Shared *)(d->mSharedData.data())
    49 #define HB_SD(Class) Class##Shared * sd = (Class##Shared *)(d->mSharedData.data())
    47 #define HB_SDD(Class) Q_D(Class); Class##Shared * sd = (Class##Shared *)(d->mSharedData.data())
    50 #define HB_SDD(Class) Q_D(Class); Class##Shared * sd = (Class##Shared *)(d->mSharedData.data())
       
    51 
       
    52 class HbViewItemPixmapPainter : public QGraphicsItem
       
    53 {
       
    54 public:
       
    55 
       
    56     HbViewItemPixmapPainter(int zValue, QGraphicsItem *parent) :
       
    57         QGraphicsItem(parent),
       
    58         mPixmap(0),
       
    59         mBoundingRect(parent->boundingRect())
       
    60     {
       
    61         setZValue(zValue);
       
    62     }
       
    63 
       
    64     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0)
       
    65     {
       
    66         Q_UNUSED(option);
       
    67         Q_UNUSED(widget);
       
    68 
       
    69         QTransform restoreTransform = painter->worldTransform();
       
    70         painter->setWorldTransform(QTransform());
       
    71 
       
    72         if (mPixmap) {
       
    73             painter->drawPixmap(mDeviceRect.topLeft(), *mPixmap);
       
    74         }
       
    75 
       
    76         painter->setWorldTransform(restoreTransform);
       
    77     }
       
    78 
       
    79     QRectF boundingRect() const
       
    80     {
       
    81         return mBoundingRect;
       
    82     }
       
    83     
       
    84     void setPixmap(QPixmap *pixmap) 
       
    85     {
       
    86         if (pixmap) {
       
    87             if (!mPixmap) {
       
    88                 setFlag(QGraphicsItem::ItemHasNoContents, false);
       
    89             }
       
    90         } else {
       
    91             if (mPixmap) {
       
    92                 setFlag(QGraphicsItem::ItemHasNoContents, true);
       
    93             }
       
    94         }
       
    95 
       
    96         mPixmap = pixmap;
       
    97     }
       
    98 
       
    99     void setDeviceRect(const QRect &deviceRect) {
       
   100         mDeviceRect = deviceRect;
       
   101     }
       
   102 
       
   103     void setSize(const QSizeF &size) {
       
   104         prepareGeometryChange();
       
   105         mBoundingRect = QRectF(QPointF(), size);
       
   106     }
       
   107 
       
   108 private:
       
   109 
       
   110     QPixmap *mPixmap;
       
   111     QRect mDeviceRect;
       
   112     QRectF mBoundingRect;
       
   113 };
    48 
   114 
    49 class HbAbstractViewItemShared : public QObject, public QSharedData
   115 class HbAbstractViewItemShared : public QObject, public QSharedData
    50 {
   116 {
    51     Q_OBJECT
   117     Q_OBJECT
    52 
   118 
    56           mPrototype(0),
   122           mPrototype(0),
    57           mItemView(0),
   123           mItemView(0),
    58           mDefaultFrame(),
   124           mDefaultFrame(),
    59           mItemType("viewitem"),
   125           mItemType("viewitem"),
    60           mPressStateChangeTimer(0),
   126           mPressStateChangeTimer(0),
    61           mPressedItem(0)
   127           mPressedItem(0),
    62         {
   128           mLowGraphicsMemory(false),
       
   129           mStyleOption(0)
       
   130         {
       
   131             HbOogmWatcher *watcher = HbOogmWatcher::instance();
       
   132             
       
   133             connect(watcher, SIGNAL(graphicsMemoryLow()), this, SLOT(disablePixmapCaches()));
       
   134             connect(watcher, SIGNAL(graphicsMemoryGood()), this, SLOT(enablePixmapCaches()));
       
   135             
       
   136             mStyleOption = new HbStyleOptionAbstractViewItem;
       
   137         }
       
   138         
       
   139         virtual ~HbAbstractViewItemShared()
       
   140         {
       
   141             delete mStyleOption;
    63         }
   142         }
    64 
   143 
    65     public slots:
   144     public slots:
    66 
   145 
    67         void pressStateChangeTimerTriggered();
   146         void pressStateChangeTimerTriggered();
       
   147         void disablePixmapCaches();
       
   148         void enablePixmapCaches();
    68 
   149 
    69     public:
   150     public:
    70 
   151 
    71         HbAbstractViewItem *mPrototype;
   152         HbAbstractViewItem *mPrototype;
    72         HbAbstractItemView *mItemView;
   153         HbAbstractItemView *mItemView;
    78         static const int ViewItemDeferredDeleteEvent;
   159         static const int ViewItemDeferredDeleteEvent;
    79 
   160 
    80         QTimer *mPressStateChangeTimer;
   161         QTimer *mPressStateChangeTimer;
    81         HbAbstractViewItem *mPressedItem;
   162         HbAbstractViewItem *mPressedItem;
    82         bool mAnimatePress;
   163         bool mAnimatePress;
       
   164 
       
   165         bool mLowGraphicsMemory;
       
   166         
       
   167         HbStyleOptionAbstractViewItem *mStyleOption;
    83 };
   168 };
    84 
   169 
    85 class HbAbstractViewItemPrivate : public HbWidgetPrivate
   170 class HbAbstractViewItemPrivate : public HbWidgetPrivate
    86 {
   171 {
    87     Q_DECLARE_PUBLIC( HbAbstractViewItem )
   172     Q_DECLARE_PUBLIC( HbAbstractViewItem )
    88 
   173 
    89     public:
   174     public:
    90 
   175 
    91         explicit HbAbstractViewItemPrivate(HbAbstractViewItem *prototype, HbAbstractViewItemShared *shared = 0) :
   176         explicit HbAbstractViewItemPrivate(HbAbstractViewItem *prototype, HbAbstractViewItemShared *shared = 0) :
    92           HbWidgetPrivate(),
   177           HbWidgetPrivate(),
    93           mFocused(false),
       
    94           mBackgroundItem(0),
   178           mBackgroundItem(0),
    95           mFrame(0),
   179           mFrame(0),
    96           mCheckState(Qt::Unchecked),
   180           mCheckState(Qt::Unchecked),
    97           mSelectionItem(0),
   181           mSelectionItem(0),
    98           mModelItemType(Hb::StandardItem),
   182           mModelItemType(Hb::StandardItem),
   100           mContentChangedSupported(false),
   184           mContentChangedSupported(false),
   101           mItemsChanged(false),
   185           mItemsChanged(false),
   102           mPressed(false),
   186           mPressed(false),
   103           mFocusItem(0),
   187           mFocusItem(0),
   104           mMultiSelectionTouchArea(0),                    
   188           mMultiSelectionTouchArea(0),                    
   105           mSharedData(shared)
   189           mSharedData(shared),
       
   190           mBackPixmapPainter(0),
       
   191           mFrontPixmapPainter(0),
       
   192           mBackCachePixmap(0),
       
   193           mFrontCachePixmap(0),
       
   194           mInPaintItems(false),
       
   195           mNonCachableItem(0),
       
   196           mResetPixmapCache(true)
   106         {
   197         {
   107             if (!mSharedData) {
   198             if (!mSharedData) {
   108                 mSharedData = new HbAbstractViewItemShared;
   199                 mSharedData = new HbAbstractViewItemShared;
   109             }
   200             }
   110             mSharedData->mPrototype = prototype;
   201             mSharedData->mPrototype = prototype;
   111         }
   202         }
   112         
   203         
   113         HbAbstractViewItemPrivate(const HbAbstractViewItemPrivate &source) :
   204         HbAbstractViewItemPrivate(const HbAbstractViewItemPrivate &source) :
   114             HbWidgetPrivate(),
   205             HbWidgetPrivate(),
   115             mIndex(source.mIndex),
   206             mIndex(source.mIndex),
   116             mFocused(source.mFocused),
       
   117             mBackgroundItem(0),
   207             mBackgroundItem(0),
   118             mFrame(0),
   208             mFrame(0),
   119             mCheckState(source.mCheckState),
   209             mCheckState(source.mCheckState),
   120             mSelectionItem(0),
   210             mSelectionItem(0),
   121             mModelItemType(source.mModelItemType),
   211             mModelItemType(source.mModelItemType),
   123             mContentChangedSupported(source.mContentChangedSupported),
   213             mContentChangedSupported(source.mContentChangedSupported),
   124             mItemsChanged(false),
   214             mItemsChanged(false),
   125             mPressed(false),
   215             mPressed(false),
   126             mFocusItem(0),
   216             mFocusItem(0),
   127             mMultiSelectionTouchArea(0),
   217             mMultiSelectionTouchArea(0),
   128             mSharedData(source.mSharedData)
   218             mSharedData(source.mSharedData),
       
   219             mBackPixmapPainter(0),
       
   220             mFrontPixmapPainter(0),
       
   221             mBackCachePixmap(0),
       
   222             mFrontCachePixmap(0),
       
   223             mInPaintItems(false),
       
   224             mNonCachableItem(source.mNonCachableItem),
       
   225             mResetPixmapCache(true)
   129         {
   226         {
   130         }
   227         }
   131         
   228         
   132         HbAbstractViewItemPrivate &operator=(const HbAbstractViewItemPrivate &source)
   229         HbAbstractViewItemPrivate &operator=(const HbAbstractViewItemPrivate &source)
   133         {
   230         {
   134             mIndex = source.mIndex;
   231             mIndex = source.mIndex;
   135             mFocused = source.mFocused;
       
   136             mBackgroundItem = 0;
   232             mBackgroundItem = 0;
   137             mBackground = QVariant();
   233             mBackground = QVariant();
   138             mFrame = 0;
   234             mFrame = 0;
   139             mCheckState = Qt::Unchecked;
   235             mCheckState = Qt::Unchecked;
   140             mModelItemType = source.mModelItemType;
   236             mModelItemType = source.mModelItemType;
   144             mItemsChanged = false;
   240             mItemsChanged = false;
   145             mPressed = false;
   241             mPressed = false;
   146             mFocusItem = 0;
   242             mFocusItem = 0;
   147             mSharedData = source.mSharedData;
   243             mSharedData = source.mSharedData;
   148             mMultiSelectionTouchArea = 0;
   244             mMultiSelectionTouchArea = 0;
       
   245             mBackPixmapPainter = 0;
       
   246             mFrontPixmapPainter = 0;
       
   247             mBackCachePixmap = 0;
       
   248             mFrontCachePixmap = 0;
       
   249             mInPaintItems = false;
       
   250             mNonCachableItem = source.mNonCachableItem;
       
   251             mResetPixmapCache = true;
   149 
   252 
   150             return *this;
   253             return *this;
   151         }
   254         }
   152 
   255 
   153         void init();
   256         void init();
   172 
   275 
   173         void revealItem();
   276         void revealItem();
   174 
   277 
   175         void setPressed(bool pressed, bool animate);
   278         void setPressed(bool pressed, bool animate);
   176 
   279 
       
   280         void paintItems(QPainter *painter, QStyleOptionGraphicsItem *option, QGraphicsItem *startItem, QGraphicsItem *endItem);
       
   281 
       
   282         void setChildFlags(QGraphicsItem *child, bool pixmapCacheEnabled);
       
   283 
       
   284         inline bool usePixmapCache() const;
       
   285 
       
   286         void updatePixmap(QPixmap *pixmap, 
       
   287             QPainter *painter, 
       
   288             const QTransform &itemToPixmapTransform, 
       
   289             const QStyleOptionGraphicsItem *option, 
       
   290             QGraphicsItem *startItem, 
       
   291             QGraphicsItem *endItem);
       
   292 
       
   293         void releasePixmaps();
       
   294 
   177 public:
   295 public:
   178         QPersistentModelIndex mIndex;
   296         QPersistentModelIndex mIndex;
   179         bool mFocused;
       
   180         
   297         
   181         QGraphicsItem *mBackgroundItem;
   298         QGraphicsItem *mBackgroundItem;
   182         QVariant mBackground;
   299         QVariant mBackground;
   183 
   300 
   184         QGraphicsItem *mFrame;
   301         QGraphicsItem *mFrame;
   198         QGraphicsItem *mFocusItem;
   315         QGraphicsItem *mFocusItem;
   199 
   316 
   200         QGraphicsItem *mMultiSelectionTouchArea;
   317         QGraphicsItem *mMultiSelectionTouchArea;
   201 
   318 
   202         QExplicitlySharedDataPointer<HbAbstractViewItemShared> mSharedData;
   319         QExplicitlySharedDataPointer<HbAbstractViewItemShared> mSharedData;
       
   320 
       
   321         HbViewItemPixmapPainter *mBackPixmapPainter;
       
   322         HbViewItemPixmapPainter *mFrontPixmapPainter;
       
   323 
       
   324         QPixmap *mBackCachePixmap;
       
   325         QPixmap *mFrontCachePixmap;
       
   326 
       
   327         bool mInPaintItems;
       
   328 
       
   329         QGraphicsItem *mNonCachableItem;
       
   330         bool mResetPixmapCache;
       
   331         
       
   332         friend class HbAbstractViewItemShared;
   203 };
   333 };
   204 
   334 
       
   335 bool HbAbstractViewItemPrivate::usePixmapCache() const
       
   336 {
       
   337     if (mSharedData->mItemView && mSharedData->mItemView->itemPixmapCacheEnabled() && !mSharedData->mLowGraphicsMemory) {
       
   338         return true;
       
   339     } else {
       
   340         return false;
       
   341     }
       
   342 }
       
   343 
   205 #endif /*HBABSTRACTVIEWITEM_P_H*/
   344 #endif /*HBABSTRACTVIEWITEM_P_H*/