src/hbwidgets/itemviews/hbtreelayout_p.cpp
changeset 1 f7ac710697a9
parent 0 16d8024aca5e
child 2 06ff229162e9
equal deleted inserted replaced
0:16d8024aca5e 1:f7ac710697a9
    22 ** Nokia at developer.feedback@nokia.com.
    22 ** Nokia at developer.feedback@nokia.com.
    23 **
    23 **
    24 ****************************************************************************/
    24 ****************************************************************************/
    25 
    25 
    26 #include "hbtreelayout_p.h"
    26 #include "hbtreelayout_p.h"
       
    27 #include "hbtreelayout_p_p.h"
    27 #include "hblayoututils_p.h"
    28 #include "hblayoututils_p.h"
    28 
    29 
    29 #include "hbabstractitemcontainer.h"
    30 #include "hbabstractitemcontainer.h"
    30 #include "hbapplication.h"
    31 #include <hbapplication.h>
    31 
    32 #include <QDebug>
    32 #include <QWidget> // for QWIDGETSIZE_MAX
       
    33 
       
    34 namespace
       
    35 {
       
    36 static const qreal INVALID_ITEM_HEIGHT = -1.0;
       
    37 }
       
    38 
       
    39 
       
    40 /*
       
    41     \private
       
    42     \class HbTreeLayout
       
    43     \brief HbTreeLayout manages geometries of hierarchical tree view contents.
       
    44 */
       
    45 
       
    46 class HbTreeLayoutPrivate
       
    47 {
       
    48 public:
       
    49 
       
    50     struct TreeItem 
       
    51     {
       
    52         QGraphicsLayoutItem* mItem;
       
    53         int mLevel;
       
    54     };
       
    55 
       
    56     HbTreeLayoutPrivate(HbTreeLayout *q_ptr);
       
    57     bool uniformSizedItems() const;
       
    58     qreal calculateSmallestItemHeight() const;
       
    59 
       
    60     QList<TreeItem> mItems;
       
    61     HbTreeLayout *q;
       
    62     qreal mIndentation;
       
    63     qreal mSmallestItemHeight;
       
    64 };
       
    65 
       
    66 HbTreeLayoutPrivate::HbTreeLayoutPrivate(HbTreeLayout *q_ptr) :
       
    67     q(q_ptr),
       
    68     mIndentation(15.0),
       
    69     mSmallestItemHeight(INVALID_ITEM_HEIGHT)
       
    70 {
       
    71 }
       
    72 
       
    73 bool HbTreeLayoutPrivate::uniformSizedItems() const
       
    74 {
       
    75     if (q->parentLayoutItem() && (static_cast<HbAbstractItemContainer *>(q->parentLayoutItem()))->uniformItemSizes() ) {
       
    76         return true;
       
    77     } else {
       
    78         return false;
       
    79     }      
       
    80 }
       
    81 
       
    82 /*!
       
    83     Calculates the smallest item height from all items.
       
    84 */
       
    85 qreal HbTreeLayoutPrivate::calculateSmallestItemHeight() const
       
    86 {
       
    87     qreal smallestHeight(0);
       
    88     if (uniformSizedItems()) {
       
    89         QGraphicsLayoutItem *firstItem = mItems.value(0).mItem;
       
    90         if (firstItem) {
       
    91             smallestHeight = firstItem->preferredHeight();  
       
    92         } 
       
    93     } else {  
       
    94         int itemCount = mItems.count();
       
    95         if (itemCount > 0) {
       
    96             smallestHeight = mItems.first().mItem->preferredHeight();
       
    97         }
       
    98         for (int i = 1; i < itemCount; ++i) {       
       
    99             smallestHeight = qMin(smallestHeight, mItems.at(i).mItem->preferredHeight());
       
   100         }
       
   101     }
       
   102     return smallestHeight;
       
   103 }
       
   104 
       
   105 
    33 
   106 /*!
    34 /*!
   107     Constructor.
    35     Constructor.
   108     \param parent parent layout item.
    36     \param parent parent layout item.
   109  */
    37  */
   110 HbTreeLayout::HbTreeLayout(QGraphicsLayoutItem *parent)
    38 HbTreeLayout::HbTreeLayout(QGraphicsLayoutItem *parent)
   111     : QGraphicsLayout(parent), d(new HbTreeLayoutPrivate(this))
    39     : QGraphicsLayout(parent), d(new HbTreeLayoutPrivate())
   112 {
    40 {
       
    41     d->q_ptr = this;
   113 }
    42 }
   114 
    43 
   115 /*!
    44 /*!
   116     Destructor.
    45     Destructor.
   117     Does not clear the parentLayoutItem of it's sub items.
    46     Does not clear the parentLayoutItem of it's sub items.
   128 
    57 
   129     This is convenience function which will internally call the \c insertItem method.
    58     This is convenience function which will internally call the \c insertItem method.
   130 
    59 
   131     \param item layout item to be added to list.
    60     \param item layout item to be added to list.
   132  */
    61  */
   133 void HbTreeLayout::addItem(QGraphicsLayoutItem *item, int level)
    62 void HbTreeLayout::addItem(QGraphicsLayoutItem *item, int level, bool animate)
   134 {
    63 {
   135     insertItem( -1, item, level );
    64     d->insertItem(-1, item, level, animate);
   136 }
    65 }
   137 
    66 
   138 /*!
    67 /*!
   139     \brief  Inserts a layout item to the list layout.
    68     \brief  Inserts a layout item to the list layout.
   140 
    69 
   144     If index is out of range, the item is appended to the end of the list.
    73     If index is out of range, the item is appended to the end of the list.
   145 
    74 
   146     \param  index position where to insert the layout.
    75     \param  index position where to insert the layout.
   147     \param  item layout item to be inserted to stack.
    76     \param  item layout item to be inserted to stack.
   148  */
    77  */
   149 void HbTreeLayout::insertItem(int index, QGraphicsLayoutItem *item, int level)
    78 void HbTreeLayout::insertItem(int index, QGraphicsLayoutItem *item, int level, bool animate)
   150 {
    79 {
   151     index = qMin(index, d->mItems.count());
    80     d->insertItem(index, item, level, animate);
   152     if (index < 0) {
       
   153         index = d->mItems.count();
       
   154     }
       
   155     HbLayoutUtils::addChildItem(this, item);
       
   156 
       
   157     HbTreeLayoutPrivate::TreeItem listItem;
       
   158     listItem.mItem = item;
       
   159     listItem.mLevel = level;
       
   160 
       
   161     d->mItems.insert( index, listItem);
       
   162     invalidate();
       
   163 }
    81 }
   164 
    82 
   165 /*!
    83 /*!
   166     \brief Returns position of layout item in the list layout.
    84     \brief Returns position of layout item in the list layout.
   167     \param item item to look for.
    85     \param item item to look for.
   168     \return position of layout item, or -1 if not found.
    86     \return position of layout item, or -1 if not found.
   169  */
    87  */
   170 int HbTreeLayout::indexOf( QGraphicsLayoutItem *item ) const
    88 int HbTreeLayout::indexOf(QGraphicsLayoutItem *item) const
   171 {
    89 {
   172     for ( int i = 0; i < count(); ++i ) {
    90     for (int i = 0; i < count(); ++i) {
   173         if ( itemAt( i ) == item ) {
    91         if (itemAt( i ) == item) {
   174             return i;
    92             return i;
   175         }
    93         }
   176     }
    94     }
   177     return -1;
    95     return -1;
   178 }
    96 }
   183     Equivalent of calling removeAt(indexOf(item)).
   101     Equivalent of calling removeAt(indexOf(item)).
   184 
   102 
   185     \param item item to be removed.
   103     \param item item to be removed.
   186     \sa removeAt
   104     \sa removeAt
   187  */
   105  */
   188 void HbTreeLayout::removeItem( QGraphicsLayoutItem *item )
   106 void HbTreeLayout::removeItem(QGraphicsLayoutItem *item, bool animate)
   189 {
   107 {
   190     removeAt(indexOf(item));
   108     if (animate) {
       
   109         invalidate();
       
   110     } else {
       
   111         removeAt(indexOf(item));
       
   112     }
   191 }
   113 }
   192 
   114 
   193 /*!
   115 /*!
   194     From QGraphicsLayout.
   116     From QGraphicsLayout.
   195  */
   117  */
   239 
   161 
   240     qreal y = effectiveRect.y();
   162     qreal y = effectiveRect.y();
   241     int itemCount = count();
   163     int itemCount = count();
   242     for (int i = 0; i < itemCount; ++i) {
   164     for (int i = 0; i < itemCount; ++i) {
   243         HbTreeLayoutPrivate::TreeItem listItem = d->mItems.at(i);
   165         HbTreeLayoutPrivate::TreeItem listItem = d->mItems.at(i);
   244 
   166         QGraphicsLayoutItem *item = listItem.mItem;
   245         qreal itemHeight = listItem.mItem->preferredHeight();
   167 
   246         qreal itemWidth = listItem.mItem->preferredWidth();
   168         qreal itemHeight = item->preferredHeight();
       
   169         qreal itemWidth = item->preferredWidth();
   247 
   170 
   248         qreal viewWidth = minimumWidth();
   171         qreal viewWidth = minimumWidth();
   249         if (viewWidth > 0.0) {
   172         if (viewWidth > 0.0) {
   250             if (d->mIndentation * listItem.mLevel + itemWidth < viewWidth) {
   173             if (d->mIndentation * listItem.mLevel + itemWidth < viewWidth) {
   251                 itemWidth = viewWidth - (d->mIndentation * listItem.mLevel);
   174                 itemWidth = viewWidth - (d->mIndentation * listItem.mLevel);
   259             x = effectiveRect.right() - d->mIndentation * listItem.mLevel - itemWidth;
   182             x = effectiveRect.right() - d->mIndentation * listItem.mLevel - itemWidth;
   260         } else {
   183         } else {
   261             x = d->mIndentation * listItem.mLevel + effectiveRect.left();
   184             x = d->mIndentation * listItem.mLevel + effectiveRect.left();
   262         }
   185         }
   263 
   186 
   264         listItem.mItem->setGeometry( QRectF(x, y, itemWidth, itemHeight ) );
   187         if (item->graphicsItem()->transform().isScaling()) {
       
   188             itemHeight *= item->graphicsItem()->transform().m22();
       
   189         }
       
   190 
       
   191         item->setGeometry( QRectF(x, y, itemWidth, itemHeight ) );
   265         y += itemHeight;
   192         y += itemHeight;
   266     }
   193     }
   267 }
   194 }
   268 
   195 
   269 /*!
   196 /*!
   379 /*!
   306 /*!
   380     Returns the smallest item height.
   307     Returns the smallest item height.
   381 */
   308 */
   382 qreal HbTreeLayout::smallestItemHeight() const
   309 qreal HbTreeLayout::smallestItemHeight() const
   383 {
   310 {
   384     if (d->mSmallestItemHeight == INVALID_ITEM_HEIGHT) {
   311     if (d->mSmallestItemHeight == d->INVALID_ITEM_HEIGHT) {
   385         d->mSmallestItemHeight = d->calculateSmallestItemHeight();
   312         d->mSmallestItemHeight = d->calculateSmallestItemHeight();
   386     }
   313     }
   387     return d->mSmallestItemHeight;
   314     return d->mSmallestItemHeight;
   388 }
   315 }
   389 
   316 
   394 */
   321 */
   395 void HbTreeLayout::invalidate()
   322 void HbTreeLayout::invalidate()
   396 {
   323 {
   397     QGraphicsLayout::invalidate();
   324     QGraphicsLayout::invalidate();
   398 
   325 
   399     d->mSmallestItemHeight = INVALID_ITEM_HEIGHT;
   326     d->mSmallestItemHeight = d->INVALID_ITEM_HEIGHT;
   400 }
   327 }
   401 
   328 
   402 
   329