src/hbwidgets/itemviews/hbtreeviewitem.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 HbWidgets 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 #include "hbtreeviewitem_p.h"
       
    26 
       
    27 #include "hbtreeviewitem.h"
       
    28 #include "hbtreeview.h"
       
    29 #include "hbnamespace.h"
       
    30 #include "hbabstractitemview.h"
       
    31 #include "hbstyle.h"
       
    32 #include "hbstyleoptiontreeviewitem.h"
       
    33 #include "hbabstractitemcontainer.h"
       
    34 
       
    35 #include <QPersistentModelIndex>
       
    36 #include <QVariant>
       
    37 #include <QDebug>
       
    38 
       
    39 /*!
       
    40     @alpha
       
    41     @hbwidgets
       
    42     \class HbTreeViewItem
       
    43     \brief The HbTreeViewItem class represents a single item in a hierarchical list.    
       
    44 
       
    45     The HbTreeViewItem class provides an item that is used by the HbTreeView class to
       
    46     visualize content within single model index. The item can only be used with HbTreeView
       
    47     or with objects derived from HbTreeView. By default the HbTreeViewItem supports
       
    48     all the same content that HbListViewItem supports. In addition to this it is able to 
       
    49     visualize parent items.
       
    50 
       
    51     \b Subclassing
       
    52 
       
    53     See HbListViewItem for commmon view item subclassing reference. 
       
    54 
       
    55 */
       
    56 
       
    57 HbTreeViewItemPrivate::HbTreeViewItemPrivate(HbTreeViewItem *prototype) :
       
    58     HbListViewItemPrivate(prototype),
       
    59     mExpandItem(0),
       
    60     mExpanded(false)
       
    61 {
       
    62 }
       
    63 
       
    64 HbTreeViewItemPrivate::HbTreeViewItemPrivate(const HbTreeViewItemPrivate &source) :
       
    65     HbListViewItemPrivate(source),
       
    66     mExpandItem(0),
       
    67     mExpanded(source.mExpanded)
       
    68 {
       
    69 }
       
    70 
       
    71 HbTreeViewItemPrivate &HbTreeViewItemPrivate::operator=(const HbTreeViewItemPrivate &source)
       
    72 {
       
    73     HbListViewItemPrivate::operator=(source);
       
    74     mExpandItem = 0;
       
    75     mExpanded = source.mExpanded;
       
    76     return *this;
       
    77 }
       
    78 
       
    79 HbTreeViewItemPrivate::~HbTreeViewItemPrivate()
       
    80 {
       
    81 }
       
    82 
       
    83 int HbTreeViewItemPrivate::modelItemType() const
       
    84 {
       
    85     const QAbstractItemModel *model = mIndex.model();
       
    86 
       
    87     if (model && model->hasChildren(mIndex)) {
       
    88         return Hb::ParentItem;
       
    89     } else {
       
    90         return HbListViewItemPrivate::modelItemType();
       
    91     }
       
    92 }
       
    93 
       
    94 QGraphicsItem *HbTreeViewItemPrivate::updateExpandItem()
       
    95 {
       
    96     Q_Q(HbTreeViewItem);
       
    97     QGraphicsItem *item = mExpandItem;
       
    98 
       
    99     if (!item) {
       
   100         mItemsChanged = true;
       
   101         item = q->style()->createPrimitive(HbStyle::P_TreeViewItem_expandicon, q);
       
   102     }
       
   103 
       
   104     HbStyleOptionTreeViewItem styleOption;
       
   105     q->initStyleOption(&styleOption);
       
   106 
       
   107     q->style()->updatePrimitive(item, HbStyle::P_TreeViewItem_expandicon, &styleOption);
       
   108 
       
   109     return item;
       
   110 }
       
   111 
       
   112 
       
   113 /*!
       
   114     Constructs an tree view item with the given parent.
       
   115 */
       
   116 HbTreeViewItem::HbTreeViewItem(QGraphicsItem *parent) : 
       
   117     HbListViewItem(*new HbTreeViewItemPrivate(this), parent)
       
   118 {
       
   119     Q_D( HbTreeViewItem );
       
   120     d->q_ptr = this;
       
   121 }
       
   122 
       
   123 /*!
       
   124     Creates a separate graphics widget with same tree view item state as \a source.
       
   125 */
       
   126 HbTreeViewItem::HbTreeViewItem(const HbTreeViewItem &source) :
       
   127     HbListViewItem(*new HbTreeViewItemPrivate(*source.d_func()), 0)
       
   128 {
       
   129     Q_D( HbTreeViewItem );
       
   130     d->q_ptr = this;
       
   131 }
       
   132 
       
   133 /*!
       
   134     \internal
       
   135 */
       
   136 HbTreeViewItem::HbTreeViewItem(HbTreeViewItemPrivate &dd, QGraphicsItem * parent) :
       
   137     HbListViewItem(dd, parent)
       
   138 {
       
   139     Q_D( HbTreeViewItem );
       
   140     d->q_ptr = this;
       
   141 }
       
   142 
       
   143 /*!
       
   144     Destroys the tree view item.
       
   145 */
       
   146 HbTreeViewItem::~HbTreeViewItem()
       
   147 {
       
   148 }
       
   149 
       
   150 /*!
       
   151     Assigns the \a source tree view item to this tree view item and returns a reference to this item.
       
   152 */
       
   153 HbTreeViewItem &HbTreeViewItem::operator=(const HbTreeViewItem &source)
       
   154 {
       
   155     Q_D( HbTreeViewItem );
       
   156     *d = *source.d_func();
       
   157     return *this;
       
   158 }
       
   159 
       
   160 
       
   161 /*!
       
   162     Creates a new tree view item.
       
   163 */
       
   164 HbAbstractViewItem *HbTreeViewItem::createItem()
       
   165 {
       
   166     return new HbTreeViewItem(*this);
       
   167 }
       
   168 
       
   169 /*!
       
   170     \reimp
       
   171 */
       
   172 void HbTreeViewItem::updateChildItems()
       
   173 {
       
   174     Q_D(HbTreeViewItem);
       
   175 
       
   176     const QAbstractItemModel *model = d->mIndex.model();
       
   177 
       
   178     if (model && model->hasChildren(d->mIndex)) {
       
   179         d->mExpandItem = d->updateExpandItem();
       
   180     } else if (d->mExpandItem) {
       
   181         d->mItemsChanged = true;
       
   182         delete d->mExpandItem;
       
   183         d->mExpandItem = 0;
       
   184     }
       
   185 
       
   186     HbListViewItem::updateChildItems();
       
   187 }
       
   188 
       
   189 /*!
       
   190     \reimp
       
   191 */
       
   192 void HbTreeViewItem::updatePrimitives()
       
   193 {
       
   194     Q_D(HbTreeViewItem);
       
   195     if(d->mExpandItem) {
       
   196         d->updateExpandItem();
       
   197     }
       
   198     HbListViewItem::updatePrimitives();
       
   199 }
       
   200 
       
   201 
       
   202 /*!
       
   203     \reimp
       
   204 */
       
   205 int HbTreeViewItem::type() const
       
   206 {
       
   207     return HbTreeViewItem::Type;
       
   208 }
       
   209 
       
   210 /*!
       
   211     Sets the item to either collapse or expanded, depending on the value of \a expanded.
       
   212 
       
   213     \sa isExpanded
       
   214 */
       
   215 void HbTreeViewItem::setExpanded(bool expanded)
       
   216 {
       
   217     Q_D(HbTreeViewItem);
       
   218     HB_SD(HbAbstractViewItem);
       
   219 
       
   220     if (d->mExpanded != expanded) {
       
   221         d->mExpanded = expanded;
       
   222 
       
   223 		if (sd->mItemView != 0) {
       
   224             HbTreeView *treeView = qobject_cast<HbTreeView *>(sd->mItemView);
       
   225             if (treeView) {
       
   226                 treeView->setExpanded(this->modelIndex(), expanded);
       
   227             }
       
   228         }
       
   229 
       
   230         if (d->mExpandItem) {
       
   231             d->updateExpandItem();
       
   232         }
       
   233     }
       
   234 }
       
   235 
       
   236 /*!
       
   237     Returns true if the item is expanded; otherwise returns false.
       
   238 
       
   239     \sa setExpanded
       
   240 */
       
   241 bool HbTreeViewItem::isExpanded() const
       
   242 {
       
   243     Q_D(const HbTreeViewItem);
       
   244     return d->mExpanded;
       
   245 }
       
   246 
       
   247 /*!
       
   248     \reimp
       
   249 */
       
   250 QMap<int,QVariant> HbTreeViewItem::state() const
       
   251 {
       
   252     Q_D(const HbTreeViewItem);
       
   253     QMap<int,QVariant> state = HbListViewItem::state();
       
   254     
       
   255     state.insert(ExpansionKey, d->mExpanded);
       
   256 
       
   257     return state;    
       
   258 }
       
   259 
       
   260 /*!
       
   261     \reimp
       
   262 */
       
   263 void HbTreeViewItem::setState(const QMap<int,QVariant> &state)
       
   264 {
       
   265     Q_D(HbTreeViewItem);
       
   266 
       
   267     HbListViewItem::setState(state);
       
   268 
       
   269     if (state.contains(ExpansionKey)) {
       
   270         d->mExpanded = state.value(ExpansionKey).toBool();
       
   271     } else {
       
   272         d->mExpanded = false;
       
   273     }
       
   274 }
       
   275 
       
   276 /*!
       
   277     Initialize option with the values from this HbTreeViewItem. 
       
   278 
       
   279     This method is useful for subclasses when they need a HbStyleOptionTreeViewItem, 
       
   280     but don't want to fill in all the information themselves.
       
   281 */
       
   282 void HbTreeViewItem::initStyleOption(HbStyleOptionTreeViewItem *option) const
       
   283 {
       
   284     Q_D(const HbTreeViewItem);
       
   285 
       
   286     HbListViewItem::initStyleOption(option);
       
   287 
       
   288     option->expanded = d->mExpanded;
       
   289 }
       
   290 
       
   291 /*!
       
   292   Provides access to primitives of HbTreeViewItem.
       
   293   \param primitive is the type of the requested primitive. The available primitives are 
       
   294   \c P_TreeViewItem_expandicon
       
   295  */
       
   296 QGraphicsItem *HbTreeViewItem::primitive(HbStyle::Primitive primitive) const
       
   297 {
       
   298     Q_D(const HbTreeViewItem);
       
   299     if (primitive == HbStyle::P_TreeViewItem_expandicon) {
       
   300         return d->mExpandItem;
       
   301     } else {
       
   302         return HbListViewItem::primitive(primitive);
       
   303     }
       
   304 }
       
   305 
       
   306 #include "moc_hbtreeviewitem.cpp"
       
   307