src/hbcore/gui/hbstackedwidget.cpp
changeset 5 627c4a0fd0e7
parent 2 06ff229162e9
child 21 4633027730f5
equal deleted inserted replaced
3:11d3954df52a 5:627c4a0fd0e7
    30 /*!
    30 /*!
    31     @stable
    31     @stable
    32     @hbcore
    32     @hbcore
    33     \class HbStackedWidget
    33     \class HbStackedWidget
    34     \brief HbStackedWidget manages geometries of stacked layout contents.
    34     \brief HbStackedWidget manages geometries of stacked layout contents.
    35     
    35 
    36     HbStackedLayout is a simple container that allows client add several widgets
    36     HbStackedLayout is a simple container that allows client add several widgets
    37     and then select one widget to be shown. This widget will manage visibility 
    37     and then select one widget to be shown. This widget will manage visibility
    38     and focus in addition to size and position. 
    38     and focus in addition to size and position.
    39 
    39 
    40     Example code:
    40     Example code:
    41     \snippet{stackedwidgetsample.cpp,1}
    41     \snippet{stackedwidgetsample.cpp,1}
    42     
    42 
    43     \sa HbStackedLayout
    43     \sa HbStackedLayout
    44  */
    44  */
    45 
    45 
    46 
    46 
    47 class HbStackedWidgetPrivate : public HbWidgetPrivate
    47 class HbStackedWidgetPrivate : public HbWidgetPrivate
    50 
    50 
    51 public:
    51 public:
    52     HbStackedWidgetPrivate() : mCurrentIndex(-1), mLayout(0) {}
    52     HbStackedWidgetPrivate() : mCurrentIndex(-1), mLayout(0) {}
    53 
    53 
    54     void setCurrentIndex(int index, QGraphicsWidget *prev, bool hideOld = true, bool showNew = true);
    54     void setCurrentIndex(int index, QGraphicsWidget *prev, bool hideOld = true, bool showNew = true);
    55     
    55 
    56     int mCurrentIndex;
    56     int mCurrentIndex;
    57     HbStackedLayout *mLayout;
    57     HbStackedLayout *mLayout;
    58 };
    58 };
    59 
    59 
    60 /*!
    60 /*!
    69         qWarning("HbStackedWidget::setCurrentIndex: index %i is out of range", index);
    69         qWarning("HbStackedWidget::setCurrentIndex: index %i is out of range", index);
    70         return;
    70         return;
    71     }
    71     }
    72 
    72 
    73     QGraphicsWidget *next = q->widgetAt(index);
    73     QGraphicsWidget *next = q->widgetAt(index);
    74     if (next == prev)
    74     if (next == prev) {
    75         return;
    75         return;
    76 
    76     }
    77     QGraphicsWidget* focused = 0;
    77 
       
    78     QGraphicsWidget *focused = 0;
    78     if (prev) {
    79     if (prev) {
    79         focused = prev->focusWidget();
    80         focused = prev->focusWidget();
    80         // Set previous widget invisible. Focus will be reset after this statement.
    81         // Set previous widget invisible. Focus will be reset after this statement.
    81         if (hideOld) {
    82         if (hideOld) {
    82             prev->setVisible(false);
    83             prev->setVisible(false);
   103 {
   104 {
   104     Q_D(HbStackedWidget);
   105     Q_D(HbStackedWidget);
   105     d->q_ptr = this;
   106     d->q_ptr = this;
   106 
   107 
   107     HbStackedLayout *layout = new HbStackedLayout;
   108     HbStackedLayout *layout = new HbStackedLayout;
   108     setLayout( layout );
   109     setLayout(layout);
   109     d->mLayout = layout;
   110     d->mLayout = layout;
   110 }
   111 }
   111 
   112 
   112 /*!
   113 /*!
   113     Destructor
   114     Destructor
   161  */
   162  */
   162 int HbStackedWidget::insertWidget(int index, QGraphicsWidget *widget)
   163 int HbStackedWidget::insertWidget(int index, QGraphicsWidget *widget)
   163 {
   164 {
   164     Q_D(HbStackedWidget);
   165     Q_D(HbStackedWidget);
   165     int addedIndex = d->mLayout->insertItem(index, widget); // this will usually reparent 'widget'
   166     int addedIndex = d->mLayout->insertItem(index, widget); // this will usually reparent 'widget'
   166     if ( addedIndex != -1 ) {
   167     if (addedIndex != -1) {
   167         // Need to store current index, since it might change
   168         // Need to store current index, since it might change
   168         // during "widgetAdded" signal (someone might call back
   169         // during "widgetAdded" signal (someone might call back
   169         // e.g. "setCurrentIndex".
   170         // e.g. "setCurrentIndex".
   170         int currentIndex = d->mCurrentIndex;
   171         int currentIndex = d->mCurrentIndex;
   171         emit widgetAdded(addedIndex);
   172         emit widgetAdded(addedIndex);
   172         if ( currentIndex == d->mCurrentIndex ) {
   173         if (currentIndex == d->mCurrentIndex) {
   173             // Current index not touched from outside.
   174             // Current index not touched from outside.
   174             if (d->mCurrentIndex < 0) {
   175             if (d->mCurrentIndex < 0) {
   175                 setCurrentIndex(addedIndex);
   176                 setCurrentIndex(addedIndex);
   176             } else  {
   177             } else  {
   177                 widget->setVisible(false);
   178                 widget->setVisible(false);
   201  */
   202  */
   202 void HbStackedWidget::removeWidget(QGraphicsWidget *widget)
   203 void HbStackedWidget::removeWidget(QGraphicsWidget *widget)
   203 {
   204 {
   204     Q_D(HbStackedWidget);
   205     Q_D(HbStackedWidget);
   205     int index = indexOf(widget);
   206     int index = indexOf(widget);
   206     if ( index == -1 ) {
   207     if (index == -1) {
   207         return;
   208         return;
   208     }
   209     }
   209     d->mLayout->removeAt(index);
   210     d->mLayout->removeAt(index);
   210 
   211 
   211     if ( index == d->mCurrentIndex ) {
   212     if (index == d->mCurrentIndex) {
   212         d->mCurrentIndex = -1;
   213         d->mCurrentIndex = -1;
   213         int c = count();
   214         int c = count();
   214         if ( c > 0 ) {
   215         if (c > 0) {
   215             int newIndex = (index == c) ? index-1 : index;
   216             int newIndex = (index == c) ? index - 1 : index;
   216             d->setCurrentIndex(newIndex, widget);
   217             d->setCurrentIndex(newIndex, widget);
   217         } else {
   218         } else {
   218             // The last widget was removed.
   219             // The last widget was removed.
   219             widget->setVisible(false);
   220             widget->setVisible(false);
   220             emit currentChanged( -1 );
   221             emit currentChanged(-1);
   221         }
   222         }
   222     } else if ( index <= d->mCurrentIndex ) {
   223     } else if (index <= d->mCurrentIndex) {
   223         d->mCurrentIndex--;
   224         d->mCurrentIndex--;
   224         emit currentChanged(d->mCurrentIndex);
   225         emit currentChanged(d->mCurrentIndex);
   225     }
   226     }
   226     widget->setParentItem(0);
   227     widget->setParentItem(0);
   227     emit widgetRemoved(index);
   228     emit widgetRemoved(index);
   239     \return the removed widget.
   240     \return the removed widget.
   240  */
   241  */
   241 QGraphicsWidget *HbStackedWidget::removeAt(int index)
   242 QGraphicsWidget *HbStackedWidget::removeAt(int index)
   242 {
   243 {
   243     QGraphicsWidget *widget = widgetAt(index);
   244     QGraphicsWidget *widget = widgetAt(index);
   244     if ( widget ) {
   245     if (widget) {
   245         removeWidget(widget);
   246         removeWidget(widget);
   246     }
   247     }
   247     return widget;
   248     return widget;
   248 }
   249 }
   249 
   250 
   273     \param index position of widget.
   274     \param index position of widget.
   274  */
   275  */
   275 QGraphicsWidget *HbStackedWidget::widgetAt(int index) const
   276 QGraphicsWidget *HbStackedWidget::widgetAt(int index) const
   276 {
   277 {
   277     Q_D(const HbStackedWidget);
   278     Q_D(const HbStackedWidget);
   278     return static_cast<QGraphicsWidget*>(d->mLayout->itemAt(index));
   279     return static_cast<QGraphicsWidget *>(d->mLayout->itemAt(index));
   279 }
   280 }
   280 
   281 
   281 /*!
   282 /*!
   282     \brief Returns current widget.
   283     \brief Returns current widget.
   283     \return current widget, or 0 if undefined.
   284     \return current widget, or 0 if undefined.
   313     widget changes but the current index doesn't.
   314     widget changes but the current index doesn't.
   314 
   315 
   315     \param index position of the new current widget.
   316     \param index position of the new current widget.
   316  */
   317  */
   317 
   318 
   318  /*!
   319 /*!
   319     \brief Sets current widget from specified index.
   320    \brief Sets current widget from specified index.
   320 
   321 
   321     This method will set the widget at given \a index visible and 
   322    This method will set the widget at given \a index visible and
   322     the previous widget invisible.
   323    the previous widget invisible.
   323 
   324 
   324     \param index position of desired widget.
   325    \param index position of desired widget.
   325  */
   326 */
   326 void HbStackedWidget::setCurrentIndex(int index)
   327 void HbStackedWidget::setCurrentIndex(int index)
   327 {
   328 {
   328     Q_D(HbStackedWidget);
   329     Q_D(HbStackedWidget);
   329     d->setCurrentIndex(index, currentWidget());
   330     d->setCurrentIndex(index, currentWidget());
   330 }
   331 }