navstack.cpp 3.1 KB
Newer Older
1
/***************************************************************************
2
 * Copyright (C) 2015-2016 by Savoir-faire Linux                                *
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * Author: Edric Ladent Milaret <edric.ladent-milaret@savoirfairelinux.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 3 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, see <http://www.gnu.org/licenses/>.   *
 **************************************************************************/

#include "navstack.h"

NavStack::NavStack(QStackedWidget* bar, QStackedWidget* stack, QWidget *parent)
    : bar_(bar)
    , stack_(stack)
{
25 26
    Q_UNUSED(parent)

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
    navList_.append(new NavBar());

    navList_.append(new CallWidget());
    navList_.append(new ConfigurationWidget());

    for (int i = 0; i < END; i++) {
        if (i < CallScreen)
            bar_->addWidget(navList_[i]);
        else
            stack_->addWidget(navList_[i]);
        connect(navList_[i], SIGNAL(NavigationRequested(ScreenEnum)),
                this, SLOT(onNavigationRequested(ScreenEnum)));
        connect(navList_[i], SIGNAL(BackRequested()),
                this, SLOT(onBackRequested()));
    }
42
    bar_->hide();
43 44 45
}

NavStack::~NavStack()
Edric Milaret's avatar
Edric Milaret committed
46 47 48 49 50
{
    for (int i = 0; i < END; i++) {
        delete navList_[i];
    }
}
51

52 53 54 55 56 57
NavWidget*
NavStack::getNavWidget(ScreenEnum wantedNavWidget)
{
    return navList_[wantedNavWidget];
}

58 59
void
NavStack::onNavigationRequested(ScreenEnum screen) {
60 61
    if (navList_[screen] == stack_->currentWidget())
        return;
62
    if (screen < CallScreen) {
63 64 65 66 67
        bar_->show();
        if (auto barItem = navList_[screen])
            bar_->setCurrentWidget(barItem);
        else
            bar_->hide();
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    } else {
        stack_->setCurrentWidget(navList_[screen]);
        setNavBar(navList_[screen]);
        stackNav_.append(screen);
    }
}

void
NavStack::onBackRequested() {

    navList_[stackNav_.pop()]->atExit();
    ScreenEnum screen;

    if (stackNav_.size() == 0)
        screen = CallScreen;
    else
        screen = stackNav_.first();

    stack_->setCurrentWidget(navList_[screen]);
    setNavBar(navList_[screen]);
}

void
NavStack::setNavBar(NavWidget* navW) {
92
    if (navW->barDesired != END) {
93
        bar_->setCurrentWidget(navList_[navW->barDesired]);
94 95 96 97
        bar_->show();
    } else {
        bar_->hide();
    }
98
}