ganeswidgets/inc/HgContainer.h
changeset 6 1cdcc61142d2
parent 5 4fa04caf0f43
child 7 5ebec3429918
equal deleted inserted replaced
5:4fa04caf0f43 6:1cdcc61142d2
     1 /*
       
     2 * Copyright (c) 2010 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 #ifndef HGCONTAINER_H
       
    19 #define HGCONTAINER_H
       
    20 
       
    21 #include <QTime>
       
    22 #include <hbwidget.h>
       
    23 #include <hgwidgets/hgwidgets.h>
       
    24 
       
    25 #include "hgmediawalldataprovider.h"
       
    26 #include "hgdrag.h"
       
    27 #include "hgspring.h"
       
    28 
       
    29 class HgWidgetItem;
       
    30 class HgQuadRenderer;
       
    31 class HgMediaWallRenderer;
       
    32 class QPanGesture;
       
    33 class HbAbstractViewItem;
       
    34 class HbGestureSceneFilter;
       
    35 class HgLongPressVisualizer;
       
    36 
       
    37 class HgContainer: public HbWidget, public HgMediaWallDataProvider
       
    38 {
       
    39     Q_OBJECT
       
    40     Q_DISABLE_COPY(HgContainer)
       
    41 
       
    42 public:
       
    43     explicit HgContainer(QGraphicsItem* parent = 0);
       
    44     virtual ~HgContainer();
       
    45 
       
    46     void setItemCount(int count);
       
    47     int itemCount() const;
       
    48     int rowCount() const;
       
    49 
       
    50     QList<HgWidgetItem*> items() const;
       
    51     HgWidgetItem* itemByIndex(const QModelIndex &index) const;
       
    52     HgWidgetItem* itemByIndex(const int &index) const;
       
    53 
       
    54     void setSelectionModel(QItemSelectionModel *selectionModel, const QModelIndex &defaultItem);
       
    55     QItemSelectionModel *selectionModel() const;
       
    56     void setSelectionMode(HgWidget::SelectionMode mode, bool resetSelection);
       
    57     HgWidget::SelectionMode selectionMode() const;
       
    58 
       
    59     void dimensions(qreal &screenSize, qreal &worldSize);
       
    60     Qt::Orientation orientation() const;
       
    61     void setOrientation(Qt::Orientation orientation, bool animate=true);
       
    62 
       
    63     // new size for the widget. calls resize.
       
    64     void scrollToPosition(qreal value, bool animate = false);
       
    65     virtual void scrollToPosition(const QPointF& pos, bool animate);
       
    66     void scrollTo(const QModelIndex &index);
       
    67 
       
    68     void itemDataChanged(const QModelIndex &firstIndex, const QModelIndex &lastIndex);
       
    69 
       
    70     void addItems(int start, int end);
       
    71     void removeItems(int start, int end);
       
    72     void moveItems(int start, int end, int destination);
       
    73 
       
    74     bool getItemPoints(int index, QPolygonF& points);
       
    75 
       
    76     QList<QModelIndex> getVisibleItemIndices() const;
       
    77 
       
    78     virtual void itemDataChanged(const int &firstIndex, const int &lastIndex);
       
    79 
       
    80     void init(Qt::Orientation scrollDirection);
       
    81 
       
    82     void setDefaultImage(QImage defaultImage);
       
    83 
       
    84     void setItemSizePolicy(HgWidget::ItemSizePolicy policy);
       
    85     HgWidget::ItemSizePolicy itemSizePolicy() const;
       
    86 
       
    87     void setItemSize(const QSizeF& size);
       
    88     QSizeF itemSize() const;
       
    89 
       
    90     void setItemSpacing(const QSizeF& size);
       
    91     QSizeF itemSpacing() const;
       
    92 
       
    93     Qt::Orientation scrollDirection() const;
       
    94     qreal scrollPosition() const;
       
    95 
       
    96 signals:
       
    97 
       
    98     // emit this signal when scrolling. for example scrollbar can be connected to this signal.
       
    99     void scrollPositionChanged(qreal value, bool scrollBarAnimation);
       
   100     void centerItemChanged(const QModelIndex &index);
       
   101     void activated(const QModelIndex &index);
       
   102     void longPressed(const QModelIndex &index, const QPointF &coords);
       
   103     void scrollingStarted();
       
   104     void scrollingEnded();
       
   105 
       
   106 protected slots:
       
   107 
       
   108     virtual void onScrollingStarted();
       
   109     virtual void onScrollingEnded();
       
   110 
       
   111 private slots:
       
   112 
       
   113     void updateBySpringPosition();
       
   114     void redraw();
       
   115     void updateLongPressVisualizer();
       
   116 
       
   117 protected: // from HgMediaWallDataProvider
       
   118 
       
   119     int imageCount() const;
       
   120     const HgImage *image(int index) const;
       
   121     int flags(int index) const;
       
   122     const HgImage *indicator(int flags) const;
       
   123 
       
   124 protected: // events
       
   125 
       
   126     virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
       
   127 
       
   128     virtual void resizeEvent(QGraphicsSceneResizeEvent *event);
       
   129 
       
   130     // this needs to be implemented for gesture framework to work
       
   131     virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
       
   132     // From HbWidget for gestures.
       
   133     virtual void gestureEvent(QGestureEvent *event);
       
   134 protected:
       
   135 
       
   136     virtual HgMediaWallRenderer* createRenderer(Qt::Orientation scrollDirection)=0;
       
   137     virtual qreal getCameraDistance(qreal springVelocity);
       
   138     virtual qreal getCameraRotationY(qreal springVelocity);
       
   139     virtual void handleTapAction(const QPointF& pos, HgWidgetItem* hitItem, int hitItemIndex);
       
   140     virtual void handleLongTapAction(const QPointF& pos, HgWidgetItem* hitItem, int hitItemIndex);
       
   141     virtual void onScrollPositionChanged(qreal pos);
       
   142 
       
   143 protected:
       
   144 
       
   145     enum ItemActionType
       
   146     {
       
   147         LongTap = 1,
       
   148         DoubleTap,
       
   149         NormalTap,
       
   150         TapStart,
       
   151         LongTapStart
       
   152     };
       
   153 
       
   154     qreal worldWidth() const;
       
   155     void initSpringForScrollBar();
       
   156     void initSpringForScrolling();
       
   157     void boundSpring();
       
   158     bool handlePanning(QPanGesture *gesture);
       
   159     bool handleTap(Qt::GestureState state, const QPointF &pos);
       
   160     bool handleLongTap(Qt::GestureState state, const QPointF &pos);
       
   161     bool handleItemAction(const QPointF &pos, ItemActionType action);
       
   162 
       
   163     void selectItem(int index);
       
   164     void updateSelectedItem();
       
   165     void unselectItem();
       
   166 
       
   167     bool hasItemAt(const QPointF& pos);
       
   168     HgWidgetItem* getItemAt(const QPointF& pos, int& index);
       
   169     void startLongPressWatcher(const QPointF& pos);
       
   170     void stopLongPressWatcher();
       
   171     bool updateSelectionModel(HgWidgetItem* item);
       
   172 
       
   173 
       
   174     virtual void updateItemSizeAndSpacing();
       
   175     virtual QSizeF getAutoItemSize() const;
       
   176     virtual QSizeF getAutoItemSpacing() const;
       
   177 
       
   178 protected: // data
       
   179 
       
   180     QList<HgWidgetItem *> mItems;
       
   181     //int mRowCount;
       
   182     //int mColumnCount;
       
   183 
       
   184     //HbLabel *mLabel;
       
   185     HgQuadRenderer *mQuadRenderer;
       
   186     HgMediaWallRenderer *mRenderer;
       
   187 
       
   188     HgSpring mSpring;
       
   189     HgDrag mDrag;
       
   190 
       
   191     QTime mTapDuration; // Temp hack until HbTapGesture is available
       
   192     QTime mDoubleTapDuration;
       
   193     int mTapCount;
       
   194 
       
   195     bool mAnimateUsingScrollBar;
       
   196 
       
   197     HgWidget::SelectionMode mSelectionMode;
       
   198     QItemSelectionModel *mSelectionModel;
       
   199     HgImage *mMarkImageOn;
       
   200     HgImage *mMarkImageOff;
       
   201 
       
   202     qreal mSpringVelAtDragStart;
       
   203     bool mDragged;
       
   204     int mFramesDragged;
       
   205 
       
   206     HbAbstractViewItem* mHitItemView;
       
   207     QPixmap mHitItemPixmap;
       
   208 
       
   209     HgLongPressVisualizer* mLongPressVisualizer;
       
   210     QTimer* mLongPressTimer;
       
   211     HgWidgetItem* mHitItem;
       
   212     int mHitItemIndex;
       
   213 
       
   214     QPointF mOffsetAtDragStart;
       
   215     QTime mLongTapDuration;
       
   216     bool mScrollBarPressed;
       
   217 
       
   218     HgWidget::ItemSizePolicy mItemSizePolicy;
       
   219     QSizeF mUserItemSize;
       
   220     QSizeF mUserItemSpacing;
       
   221 
       
   222     Qt::Orientation mOrientation;
       
   223     QModelIndex mDelayedScrollToIndex;
       
   224     bool mIgnoreGestureAction;
       
   225 };
       
   226 
       
   227 #endif