Menu
Home
Log in / Register
 
Home arrow Computer Science arrow Learn BlackBerry 10 App Development
< Prev   CONTENTS   Next >

Application Delegate

Until now, I have used the term “application delegate” in a relatively informal way without really explaining what I meant. The application delegate is the ApplicationUI class generated for you by the New Cascades Application Wizard. The class's responsibility is to load the QML scene graph from main.qml, wire signals and slots between UI controls and domain objects and add itself to the QML document context if necessary. The application delegate therefore plays a central role in a Cascades application. Here again, Cascades does not enforce the presence of an application delegate and you could simply load main.qml in your application's main function. However, centralizing the interactions between UI controls and C++ domain objects in a dedicated object will greatly simplify your application's design in the long run. The role of the application delegate is therefore to

n Define signals reflecting the state of model objects used for updating Cascades controls.

n Define slots used by the QML layer in order to update the domain model according to user interactions.

n Define properties used in QML bindings. The properties can be used to selectively expose QObject subclasses to the QML layer of your application. There are really no limitations in what the properties can represent. For example, a property could be a DataModel used by a ListView in order to display a list of items (see Chapter 6) and another property could represent a list of contacts from the contacts database (see Chapter 8), and so on.

n Centralize all interactions between QML and C++ objects (in other words, use the application delegate as your main app controller).

To illustrate the previous points, Listing 3-30 shows you a hypothetical application delegate definition for our financial instruments. (I will not provide the member function definitions. The most important point to keep in mind is how the application delegate is used as an interface to the C++ data model. Also note that the properties' accessors are defined inline.)

Listing 3-30. Application Delegate

#include "Stock.h"

#include "Option.h"

#include "CompositeInstrument.h"

class ApplicationUI : public QObject

{

// used for displaying instruments in ListView Q_PROPERTY(bb::cascades::ArrayDataModel* READ instrumentsModel CONSTANT)

Q_PROPERTY(QList<CompositeInstrument*> READ composites NOTIFY compositesChanged) Q_PROPERTY(QList<Option*> options READ options NOTIFY optionsChanged) Q_PROPERTY(QList<Stocks*> stocks READ stocks NOTIFY stocksChanged)

public:

ApplicationUI(bb::cascades::Application *app); virtual ~ApplicationUI() { }

// load financial instruments in ArrayDataModel Q_INVOKABLE void loadInstruments() { // code not shown};

signals:

void compositesChanged(); void stocksChanged(); void optionsChanged();

private:

bb::cascades::ArrayDataModel* dataModel() {return m_instrumentsModel};

QList<CompositeInstrument*> composites() {return m_composites}; QList<Option*> options() {return m_options};

QList<Stocks*> stocks() {return m_stocks};

QList<Stock*> m_stocks; QList<Option*> m_options;

QList<CompositeInstrument*> m_composites;

bb::cascades::ArrayDataModel* m_instrumentsModel;

};

The properties defined in the application delegate are accessible from QML and represent the domain model. A Q_INVOKABLE function is also provided in order to load the instruments from a database, for example (here again the function is callable from QML). Finally, the model property can be used by a ListView in order display the current list of instruments (ListViews and DataModels are covered in Chapter 6). As mentioned previously, you need to register the Stock, Option, and CompositeInstrument classes with the QML type system before being able to use them in QML.

The application delegate's constructor is one possible place where you perform this. You also need to add the application delegate to the QML document context (see Listing 3-31).

Listing 3-31. Application Delegate Constructor

ApplicationUI::ApplicationUI(bb::cascades::Application *app) : QObject(app){

// Create scene document from main.qml asset, the parent is set

// to ensure the document gets destroyed properly at shut down.

qmlRegisterType<Stock>("ludin.instruments", 1, 0, "Stock");

qmlRegisterType<Option>("ludin.instruments", 1, 0, "OptionType");

qmlRegisterType<CompositeInstrument>("ludin.instruments", 1, 0, "Composite");

QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this); qml->setContextProperty("_app", this);

}

And finally, Listing 3-32 shows you how to access the application delegate in your QML document.

Listing 3-32. main.qml

Page {

id: page

function optionsTotalPrice() { var total = 0;

var options = _app.options;

for (var i = 0; i < options.length(); i ++) { total += options[i].price();

}

return total;

}

Container {

Label {

text: "Options total price: " + page.optionsTotalPrice()

}

ListView {

dataModel: _app.instrumentsModel

}

}

onCreationCompleted: {

_app.loadInstruments(); // loads intruments from db and popultates data model

}

}

 
Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
 
Subjects
Accounting
Business & Finance
Communication
Computer Science
Economics
Education
Engineering
Environment
Geography
Health
History
Language & Literature
Law
Management
Marketing
Philosophy
Political science
Psychology
Religion
Sociology
Travel