SelectionDataModelDecorator.cpp Example File
listdecorators/src/SelectionDataModelDecorator.cpp
#include "SelectionDataModelDecorator.hpp"
SelectionDataModelDecorator::SelectionDataModelDecorator(QObject *parent)
: m_highlight("#36D986")
{
}
SelectionDataModelDecorator::~SelectionDataModelDecorator() {
}
QVariant SelectionDataModelDecorator::data(const QVariantList& indexPath) {
QVariant data = bb::cascades::datamanager::DataModelDecorator::data(indexPath);
QVariantMap map = data.value<QVariantMap>();
if (indexPath.size() == 1 && data.isValid()) {
map["selected"] =
m_selectedSet.contains(map["id"].toInt()) ?
m_highlight : QString("#000000");
}
return map;
}
void SelectionDataModelDecorator::clearSelections() {
m_selectedSet.clear();
emit itemsChanged(bb::cascades::DataModelChangeType::Update);
}
QString SelectionDataModelDecorator::itemType(const QVariantList& indexPath) {
QVariant data = bb::cascades::datamanager::DataModelDecorator::data(indexPath);
if (data.isValid()) {
return QString("selectable");
}
return QString("");
}
void SelectionDataModelDecorator::changeSelection(QVariantList indexPath,
bool selected) {
if (selected) {
QVariant data = bb::cascades::datamanager::DataModelDecorator::data(indexPath);
if (data.isValid()) {
QVariantMap map = data.value<QVariantMap>();
int id = map["id"].toInt();
if (!m_selectedSet.contains(id)) {
m_selectedSet.insert(id);
} else {
m_selectedSet.remove(id);
}
emit itemUpdated(indexPath);
}
}
}