StackViewController.cpp 3.4 KB
Newer Older
1 2 3
/*****************************************************************************
 * StackViewController.cpp
 *****************************************************************************
Ludovic Fauvet's avatar
Ludovic Fauvet committed
4
 * Copyright (C) 2008-2010 VideoLAN
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * Authors: Thomas Boquet <thomas.boquet@gmail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

Thomas Boquet's avatar
Thomas Boquet committed
23
#include "StackViewController.h"
Thomas Boquet's avatar
Thomas Boquet committed
24
#include "ImportController.h"
Thomas Boquet's avatar
Thomas Boquet committed
25

26 27
StackViewController::StackViewController( QWidget* parent ) :
        QWidget( parent ), m_current( 0 )
Thomas Boquet's avatar
Thomas Boquet committed
28 29
{
    m_nav     = new StackViewNavController( this );
30
    m_layout  = new QVBoxLayout( this );
Thomas Boquet's avatar
Thomas Boquet committed
31 32
    m_controllerStack = new QStack<ViewController*>();

33
    connect( m_nav->previousButton(), SIGNAL( clicked() ),
Thomas Boquet's avatar
Thomas Boquet committed
34 35 36 37 38 39 40 41 42 43 44
                     this, SLOT( previous() ) );
    m_layout->addWidget( m_nav );
    parent->setLayout( m_layout );
}

StackViewController::~StackViewController()
{
    delete m_nav;
    delete m_controllerStack;
}

45 46
void
StackViewController::pushViewController( ViewController* viewController,
Thomas Boquet's avatar
Thomas Boquet committed
47 48 49 50
                                                 bool animated )
{
    animated = false;

51 52
    connect( viewController, SIGNAL( destroyed() ),
             this, SLOT( viewDestroyed() ) );
Thomas Boquet's avatar
Thomas Boquet committed
53 54 55 56 57 58 59 60 61 62 63 64 65
    if ( m_current )
    {
        m_layout->removeWidget( m_current->view() );
        m_current->view()->hide();
        m_controllerStack->push( m_current );

        m_nav->previousButton()->setHidden( false );
        m_nav->previousButton()->setText( "< " + m_current->title() );
    }

    m_current = viewController;
    m_nav->setTitle( m_current->title() );
    m_layout->insertWidget( 1, m_current->view() );
66 67
    if ( m_current->view()->isHidden() )
        m_current->view()->show();
68
    emit viewChanged( viewController );
Thomas Boquet's avatar
Thomas Boquet committed
69 70
}

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
void
StackViewController::restorePrevious()
{
    m_current = m_controllerStack->pop();

    m_nav->setTitle( m_current->title() );
    m_layout->insertWidget( 1, m_current->view() );
    m_current->view()->setHidden( false );

    if ( !m_controllerStack->size() )
        m_nav->previousButton()->setHidden( true );
    else
    {
       m_nav->previousButton()->setText( "< " +
       m_controllerStack->value( m_controllerStack->size() - 1 )->title() );
    }
87
    emit viewChanged( m_current );
88 89
}

90 91 92
void
StackViewController::viewDestroyed()
{
93 94
    Q_ASSERT( m_controllerStack->isEmpty() == false );

95 96
    if ( QObject::sender() == m_current )
    {
97
        restorePrevious();
98 99 100
    }
}

101 102
void
StackViewController::popViewController( bool animated )
Thomas Boquet's avatar
Thomas Boquet committed
103 104 105 106 107 108 109 110
{
    animated = false;

    if ( !m_controllerStack->size() )
        return ;

    m_layout->removeWidget( m_current->view() );
    m_current->view()->hide();
111
    delete m_current;
Thomas Boquet's avatar
Thomas Boquet committed
112 113
}

114
void
115
StackViewController::previous()
Thomas Boquet's avatar
Thomas Boquet committed
116 117 118
{
    popViewController();
}
119