ExpandableDataModelDecorator.cpp Example File
listdecorators/src/ExpandableDataModelDecorator.cpp
#include "ExpandableDataModelDecorator.hpp"
#include <bb/cascades/DataModelChangeType>
ExpandableDataModelDecorator::ExpandableDataModelDecorator(QObject* parent)
: bb::cascades::datamanager::DataModelDecorator(parent)
, m_expandedIndex(-1)
{
}
ExpandableDataModelDecorator::~ExpandableDataModelDecorator() {
}
QVariant ExpandableDataModelDecorator::data(const QVariantList& indexPath) {
QVariant data = bb::cascades::datamanager::DataModelDecorator::data(
indexPath);
if (data.isValid()) {
QVariantMap map = data.value<QVariantMap>();
if (indexPath.size() == 1) {
map["expanded"] = (indexPath[0] == m_expandedIndex);
}
return map;
}
return data;
}
bool ExpandableDataModelDecorator::isExpandable(
const QVariantList& indexPath) const {
return indexPath.size() == 1 && indexPath[0].toInt() != m_expandedIndex;
}
int ExpandableDataModelDecorator::childCount(const QVariantList& indexPath) {
if (isExpandable(indexPath)) {
return 0;
}
return bb::cascades::datamanager::DataModelDecorator::childCount(indexPath);
}
bool ExpandableDataModelDecorator::hasChildren(const QVariantList& indexPath) {
if (isExpandable(indexPath)) {
return false;
}
return bb::cascades::datamanager::DataModelDecorator::hasChildren(indexPath);
}
void ExpandableDataModelDecorator::expandHeader(const QVariantList& indexPath,
bool expand) {
if (indexPath.size() == 1 && expand) {
int index = indexPath[0].toInt();
if (index == m_expandedIndex) {
m_expandedIndex = -1;
emit itemsChanged(bb::cascades::DataModelChangeType::AddRemove);
} else {
m_expandedIndex = index;
emit itemsChanged(bb::cascades::DataModelChangeType::AddRemove);
}
}
}