ProfileBoxManager.cpp Example File
bbmprofilebox/src/ProfileBoxManager.cpp
#include "ProfileBoxManager.hpp"
#include "RegistrationHandler.hpp"
#include <bb/cascades/AbstractPane>
#include <bb/cascades/Application>
#include <bb/cascades/Image>
#include <bb/cascades/QmlDocument>
#include <bb/platform/bbm/ProfileBoxItem>
#include <QtCore/QVariantList>
#include <QtCore/QVariant>
using namespace bb::cascades;
ProfileBoxManager::ProfileBoxManager(bb::platform::bbm::Context &context, QObject *parent)
: QObject(parent)
, m_profileBox(0)
, m_context(&context)
, m_model(new QVariantListDataModel())
{
m_model->setParent(this);
}
void ProfileBoxManager::addProfileBoxItem(const QString &text, int iconId)
{
m_profileBox->requestAddItem(text, iconId);
}
void ProfileBoxManager::removeProfileBoxItem(const QString &itemId)
{
m_profileBox->requestRemoveItem(itemId);
}
void ProfileBoxManager::show()
{
QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
qml->setContextProperty("_profileBoxManager", this);
AbstractPane *root = qml->createRootObject<AbstractPane>();
Application::instance()->setScene(root);
QMetaObject::invokeMethod(this, "loadProfileBoxes", Qt::QueuedConnection);
}
void ProfileBoxManager::loadProfileBoxes()
{
m_profileBox = new bb::platform::bbm::ProfileBox(m_context, this);
bool ok = connect(m_profileBox, SIGNAL(itemAdded(QString)), this, SLOT(itemAdded(QString)));
Q_ASSERT(ok);
ok = connect(m_profileBox, SIGNAL(itemRemoved(QString)), this, SLOT(itemRemoved(QString)));
Q_ASSERT(ok);
ok = connect(m_profileBox, SIGNAL(iconRetrieved(int, bb::platform::bbm::ImageType::Type, QByteArray)),
this, SLOT(iconRetrieved(int, bb::platform::bbm::ImageType::Type, QByteArray)));
Q_ASSERT(ok);
registerIcons();
foreach (const bb::platform::bbm::ProfileBoxItem &item, m_profileBox->items()) {
const int iconId = item.iconId();
QVariantMap entry;
entry["id"] = item.id();
entry["text"] = item.text();
entry["iconId"] = iconId;
m_model->append(entry);
m_profileBox->requestRetrieveIcon(iconId);
}
}
void ProfileBoxManager::itemAdded(const QString &itemId)
{
const bb::platform::bbm::ProfileBoxItem item = m_profileBox->item(itemId);
const int iconId = item.iconId();
QVariantMap entry;
entry["id"] = itemId;
entry["text"] = item.text();
entry["iconId"] = iconId;
m_model->append(entry);
m_profileBox->requestRetrieveIcon(iconId);
}
void ProfileBoxManager::itemRemoved(const QString &itemId)
{
for (int pos = 0; pos < m_model->size(); ++pos) {
if (m_model->value(pos).toMap().value("id").toString() == itemId) {
m_model->removeAt(pos);
return;
}
}
}
void ProfileBoxManager::iconRetrieved(int iconId, bb::platform::bbm::ImageType::Type, const QByteArray &imageData)
{
const bb::cascades::Image image(imageData);
for (int pos = 0; pos < m_model->size(); ++pos) {
QVariantMap entry = m_model->value(pos).toMap();
if (entry.value("iconId").toInt() == iconId) {
entry["icon"] = QVariant::fromValue(image);
m_model->replace(pos, entry);
}
}
}
void ProfileBoxManager::registerIcons()
{
const QString imageDir(QDir::currentPath() + QLatin1String("/app/native/assets/images/"));
registerIcon(imageDir + QLatin1String("apple.png"), 1);
registerIcon(imageDir + QLatin1String("pear.png"), 2);
registerIcon(imageDir + QLatin1String("orange.png"), 3);
}
void ProfileBoxManager::registerIcon(const QString& path, int iconId)
{
QFile file(path);
if (!file.open(QIODevice::ReadOnly))
return;
const QByteArray imageData = file.readAll();
m_profileBox->requestRegisterIcon(iconId, bb::platform::bbm::ImageType::Png, imageData);
}
bb::cascades::DataModel* ProfileBoxManager::model() const
{
return m_model;
}