Home · All Classes · Main Classes · Deprecated

Example of an application menu navigation pattern

There are three steps in the implementation process of this navigation pattern:

1. Add actions to the MApplicationWindow and set their locations to MAction::ApplicationMenuLocation so that they appear in the application menu.

action->setLocation(MAction::ApplicationMenuLocation);
applicationWindow->addAction(action);

2. Connect the triggered() signal of the actions to the slots that display the corresponding pages.

window->connect(settingsAction, SIGNAL(triggered()), SLOT(showSettingsPage()));

3. Set the MApplicationPage::escapeMode() of your pages to MApplicationPageModel::EscapeCloseWindow. Otherwise the Escape button behaves as in a drill-down navigation pattern.

page->setEscapeMode(MApplicationPageModel::EscapeCloseWindow);

You can optionally hide or disable the menu option (in other words, MApplicationWindow action located in the application menu) that corresponds to the currently displayed page.

In this example, the actions are grouped in a QActionGroup so that the same method is called whenever any of the actions is triggered.

All files can be found at:

libmeegotouch/examples/pagenavigation_menu

main.cpp:

/***************************************************************************
**
** Copyright (C) 2010, 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (directui@nokia.com)
**
** This file is part of libmeegotouch.
**
** If you have questions regarding the use of this file, please contact
** Nokia at directui@nokia.com.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/
#include <MApplication>

#include "samplewindow.h"

int main(int argc, char **argv)
{
    MApplication app(argc, argv);
    SampleWindow window;

    window.show();

    return app.exec();
}

samplewindow.cpp:

/***************************************************************************
**
** Copyright (C) 2010, 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (directui@nokia.com)
**
** This file is part of libmeegotouch.
**
** If you have questions regarding the use of this file, please contact
** Nokia at directui@nokia.com.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/
#include "samplewindow.h"

#include <MLabel>

SampleWindow::SampleWindow(QWidget *parent) : MApplicationWindow(parent)
{
    currentAction = 0;

    actionGroup = new QActionGroup(this);

    QAction *alphaAction = createAction("Alpha");
    createAction("Beta");
    createAction("Gamma");
    createAction("Delta");
    createAction("Epsilon");

    connect(actionGroup, SIGNAL(triggered(QAction*)), SLOT(showPageForAction(QAction*)));

    showPageForAction(alphaAction);
}

void SampleWindow::showPageForAction(QAction *action)
{
    if (currentAction == action)
        return;

    MApplicationPage *page = createPage(action->text());
    page->appear(this, MSceneWindow::DestroyWhenDone);

    if (currentAction) {
        currentAction->setVisible(true);
    }

    currentAction = action;
    action->setVisible(false);
}

MApplicationPage *SampleWindow::createPage(const QString &name)
{
    MApplicationPage *page = new MApplicationPage;

    page->setTitle(name);

    QString contentText = QString("%1 Content").arg(name);
    page->setCentralWidget(new MLabel(contentText));

    page->setEscapeMode(MApplicationPageModel::EscapeCloseWindow);

    return page;
}

QAction *SampleWindow::createAction(const QString &name)
{
    MAction *action = new MAction(name, this);
    action->setLocation(MAction::ApplicationMenuLocation);
    action->setActionGroup(actionGroup);
    addAction(action);

    return action;
}

samplewindow.h:

/***************************************************************************
**
** Copyright (C) 2010, 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (directui@nokia.com)
**
** This file is part of libmeegotouch.
**
** If you have questions regarding the use of this file, please contact
** Nokia at directui@nokia.com.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/
#ifndef SAMPLEWINDOW_H
#define SAMPLEWINDOW_H

#include <MApplicationWindow>

#include <MApplicationPage>
#include <MAction>
#include <QActionGroup>

class SampleWindow : public MApplicationWindow {
    Q_OBJECT

public:
    SampleWindow(QWidget *parent = 0);

public slots:
    void showPageForAction(QAction *action);

private:
    MApplicationPage *createPage(const QString &name);
    QAction *createAction(const QString &name);

    QAction *currentAction;
    QActionGroup *actionGroup;
};

#endif

Copyright © 2010 Nokia Corporation
MeeGo Touch