vegetablesdatamodel.cpp Example File
vegetablesdatamodel/src/vegetablesdatamodel.cpp
#include "vegetablesdatamodel.hpp"
VegetablesDataModel::VegetablesDataModel(QObject *parent)
: bb::cascades::DataModel(parent)
{
}
int VegetablesDataModel::childCount(const QVariantList& indexPath)
{
const int level = indexPath.size();
if (level == 0) {
return 3;
}
if (level == 1) {
const int header = indexPath[0].toInt();
if (header == 0 || header == 1)
return 3;
else
return 2;
}
return 0;
}
bool VegetablesDataModel::hasChildren(const QVariantList& indexPath)
{
return childCount(indexPath) > 0;
}
QVariant VegetablesDataModel::data(const QVariantList& indexPath)
{
QString value;
if (indexPath.size() == 1) {
switch (indexPath[0].toInt()) {
case 0:
value = tr("Green");
break;
case 1:
value = tr("Red");
break;
case 2:
value = tr("Yellow");
break;
}
}
if (indexPath.size() == 2) {
const int header = indexPath[0].toInt();
const int childItem = indexPath[1].toInt();
switch (header) {
case 0:
switch (childItem) {
case 0:
value = tr("Cucumber");
break;
case 1:
value = tr("Peas");
break;
case 2:
value = tr("Salad");
break;
}
break;
case 1:
switch (childItem) {
case 0:
value = tr("Tomato");
break;
case 1:
value = tr("Red Radish");
break;
case 2:
value = tr("Carrot");
break;
}
break;
case 2:
switch (childItem) {
case 0:
value = tr("Corn");
break;
case 1:
value = tr("Paprika");
break;
}
break;
}
}
qDebug() << "Data for " << indexPath << " is " << value;
return QVariant(value);
}
QString VegetablesDataModel::itemType(const QVariantList& indexPath)
{
switch (indexPath.size()) {
case 0:
return QString();
break;
case 1:
return QLatin1String("header");
break;
default:
return QLatin1String("item");
break;
}
}