src/hbcore/layouts/hbstackedlayout.cpp
changeset 5 627c4a0fd0e7
parent 0 16d8024aca5e
equal deleted inserted replaced
3:11d3954df52a 5:627c4a0fd0e7
    64 
    64 
    65 /*!
    65 /*!
    66     Constructor.
    66     Constructor.
    67     \param parent parent layout item.
    67     \param parent parent layout item.
    68  */
    68  */
    69 HbStackedLayout::HbStackedLayout(QGraphicsLayoutItem * parent)
    69 HbStackedLayout::HbStackedLayout(QGraphicsLayoutItem *parent)
    70     : QGraphicsLayout(parent), d(new HbStackedLayoutPrivate())
    70     : QGraphicsLayout(parent), d(new HbStackedLayoutPrivate())
    71 {
    71 {
    72 }
    72 }
    73 
    73 
    74 /*!
    74 /*!
    81 {
    81 {
    82     if (d) {
    82     if (d) {
    83         for (int i = count() - 1; i >= 0; --i) {
    83         for (int i = count() - 1; i >= 0; --i) {
    84             QGraphicsLayoutItem *item = itemAt(i);
    84             QGraphicsLayoutItem *item = itemAt(i);
    85             // The following lines can be removed, but this removes the item
    85             // The following lines can be removed, but this removes the item
    86             // from the layout more efficiently than the implementation of 
    86             // from the layout more efficiently than the implementation of
    87             // ~QGraphicsLayoutItem.
    87             // ~QGraphicsLayoutItem.
    88             removeAt(i);
    88             removeAt(i);
    89             if (item) {
    89             if (item) {
    90                 item->setParentLayoutItem(0);
    90                 item->setParentLayoutItem(0);
    91                 if (item->ownedByLayout()) {
    91                 if (item->ownedByLayout()) {
    92                     delete item;
    92                     delete item;
    93                 }
    93                 }
    94             }
    94             }
    95         }
    95         }
    96     }
    96     }
    97     
    97 
    98     delete d;
    98     delete d;
    99 }
    99 }
   100 
   100 
   101 /*!
   101 /*!
   102     \brief Appends a layout item to the stacked layout.
   102     \brief Appends a layout item to the stacked layout.
   126     \param  item layout item to be inserted to stack.
   126     \param  item layout item to be inserted to stack.
   127     \return index to which layout item was added.
   127     \return index to which layout item was added.
   128  */
   128  */
   129 int HbStackedLayout::insertItem(int index, QGraphicsLayoutItem *item)
   129 int HbStackedLayout::insertItem(int index, QGraphicsLayoutItem *item)
   130 {
   130 {
   131     if( !item ) {
   131     if (!item) {
   132         qWarning("HbStackedLayout::insertItem: item is NULL");
   132         qWarning("HbStackedLayout::insertItem: item is NULL");
   133         return -1;
   133         return -1;
   134     }
   134     }
   135 
   135 
   136     if (item->parentLayoutItem()) {
   136     if (item->parentLayoutItem()) {
   158     \param item item to be removed.
   158     \param item item to be removed.
   159     \sa removeAt
   159     \sa removeAt
   160  */
   160  */
   161 void HbStackedLayout::removeItem(QGraphicsLayoutItem *item)
   161 void HbStackedLayout::removeItem(QGraphicsLayoutItem *item)
   162 {
   162 {
   163     removeAt( indexOf( item ) );
   163     removeAt(indexOf(item));
   164 }
   164 }
   165 
   165 
   166 /*!
   166 /*!
   167     \brief Returns position of layout item in the stacked layout.
   167     \brief Returns position of layout item in the stacked layout.
   168     \return position of layout item, or -1 if not found.
   168     \return position of layout item, or -1 if not found.
   169  */
   169  */
   170 int HbStackedLayout::indexOf(QGraphicsLayoutItem *item) const
   170 int HbStackedLayout::indexOf(QGraphicsLayoutItem *item) const
   171 {
   171 {
   172     int c = d->mList.count();
   172     int c = d->mList.count();
   173     for ( int i = 0; i < c; i++ ) {
   173     for (int i = 0; i < c; i++) {
   174         if ( d->mList.at(i) == item ) {
   174         if (d->mList.at(i) == item) {
   175             return i;
   175             return i;
   176         }
   176         }
   177     }
   177     }
   178     return -1;
   178     return -1;
   179 }
   179 }
   198     From QGraphicsLayout.
   198     From QGraphicsLayout.
   199     Clears the parentLayoutItem of removed item.
   199     Clears the parentLayoutItem of removed item.
   200  */
   200  */
   201 void HbStackedLayout::removeAt(int index)
   201 void HbStackedLayout::removeAt(int index)
   202 {
   202 {
   203     if (index <0 || index >= d->mList.size()) {
   203     if (index < 0 || index >= d->mList.size()) {
   204         return;
   204         return;
   205     }
   205     }
   206     itemAt(index)->setParentLayoutItem(0);
   206     itemAt(index)->setParentLayoutItem(0);
   207     d->mList.removeAt(index);
   207     d->mList.removeAt(index);
   208     invalidate();
   208     invalidate();
   230  */
   230  */
   231 QSizeF HbStackedLayout::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
   231 QSizeF HbStackedLayout::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
   232 {
   232 {
   233     if (which == Qt::MaximumSize) {
   233     if (which == Qt::MaximumSize) {
   234         // Sub items do not affect the maximum size hint.
   234         // Sub items do not affect the maximum size hint.
   235         return QSizeF( QWIDGETSIZE_MAX, QWIDGETSIZE_MAX );
   235         return QSizeF(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
   236     }
   236     }
   237     QSizeF sizeHint(0, 0);
   237     QSizeF sizeHint(0, 0);
   238     int c = d->mList.count();
   238     int c = d->mList.count();
   239     for ( int i=0; i < c; ++i ) {
   239     for (int i = 0; i < c; ++i) {
   240         QGraphicsLayoutItem* item = d->mList.at(i);
   240         QGraphicsLayoutItem *item = d->mList.at(i);
   241         QSizeF itemSize(item->effectiveSizeHint(which, constraint));
   241         QSizeF itemSize(item->effectiveSizeHint(which, constraint));
   242         if (item->sizePolicy().horizontalPolicy() == QSizePolicy::Ignored) {
   242         if (item->sizePolicy().horizontalPolicy() == QSizePolicy::Ignored) {
   243             itemSize.setWidth(0);
   243             itemSize.setWidth(0);
   244         }
   244         }
   245         if (item->sizePolicy().verticalPolicy() == QSizePolicy::Ignored) {
   245         if (item->sizePolicy().verticalPolicy() == QSizePolicy::Ignored) {
   246             itemSize.setHeight(0);
   246             itemSize.setHeight(0);
   247         }
   247         }
   248         if ( itemSize.isValid() ) {
   248         if (itemSize.isValid()) {
   249             sizeHint = sizeHint.expandedTo(itemSize);
   249             sizeHint = sizeHint.expandedTo(itemSize);
   250         }
   250         }
   251     }
   251     }
   252     // Apply margins
   252     // Apply margins
   253     qreal left, top, right, bottom;
   253     qreal left, top, right, bottom;
   263 void HbStackedLayout::setGeometry(const QRectF &rect)
   263 void HbStackedLayout::setGeometry(const QRectF &rect)
   264 {
   264 {
   265     QGraphicsLayout::setGeometry(rect);
   265     QGraphicsLayout::setGeometry(rect);
   266 
   266 
   267     int c = d->mList.count();
   267     int c = d->mList.count();
   268     for ( int i=0; i < c; ++i ) {
   268     for (int i = 0; i < c; ++i) {
   269         QGraphicsLayoutItem* item = d->mList.at(i);
   269         QGraphicsLayoutItem *item = d->mList.at(i);
   270         QSizeF itemMax = item->effectiveSizeHint(Qt::MaximumSize);
   270         QSizeF itemMax = item->effectiveSizeHint(Qt::MaximumSize);
   271         QSizeF itemMin = item->effectiveSizeHint(Qt::MinimumSize);
   271         QSizeF itemMin = item->effectiveSizeHint(Qt::MinimumSize);
   272         QRectF itemRect(rect);
   272         QRectF itemRect(rect);
   273         // Have to check min and max "manually" in order
   273         // Have to check min and max "manually" in order
   274         // to make mirroring work.
   274         // to make mirroring work.
   275         if ( itemMax.width() < itemRect.width() ) {
   275         if (itemMax.width() < itemRect.width()) {
   276             itemRect.setWidth( itemMax.width() );
   276             itemRect.setWidth(itemMax.width());
   277         }
   277         }
   278         if ( itemMax.height() < itemRect.height() ) {
   278         if (itemMax.height() < itemRect.height()) {
   279             itemRect.setHeight( itemMax.height() );
   279             itemRect.setHeight(itemMax.height());
   280         }
   280         }
   281         if ( itemMin.width() > itemRect.width() ) {
   281         if (itemMin.width() > itemRect.width()) {
   282             itemRect.setWidth( itemMin.width() );
   282             itemRect.setWidth(itemMin.width());
   283         }
   283         }
   284         if ( itemMin.height() > itemRect.height() ) {
   284         if (itemMin.height() > itemRect.height()) {
   285             itemRect.setHeight( itemMin.height() );
   285             itemRect.setHeight(itemMin.height());
   286         }
   286         }
   287         alignRect(itemRect, rect);
   287         alignRect(itemRect, rect);
   288         item->setGeometry(itemRect);
   288         item->setGeometry(itemRect);
   289     }
   289     }
   290 }
   290 }
   291 
   291 
   292 /*!
   292 /*!
   293     \internal
   293     \internal
   294  */
   294  */
   295 void HbStackedLayout::alignRect( QRectF &rect, const QRectF &boundingRect )
   295 void HbStackedLayout::alignRect(QRectF &rect, const QRectF &boundingRect)
   296 {
   296 {
   297     if (HbApplication::layoutDirection() == Qt::RightToLeft) {
   297     if (HbApplication::layoutDirection() == Qt::RightToLeft) {
   298         rect.moveLeft( 2 * boundingRect.x() + boundingRect.width() - rect.x() - rect.width() );
   298         rect.moveLeft(2 * boundingRect.x() + boundingRect.width() - rect.x() - rect.width());
   299     }
   299     }
   300 }
   300 }