datacontrolmapper.cpp Example File
sqlwidgetmapper/src/datacontrolmapper.cpp
#include "datacontrolmapper.hpp"
#include <bb/cascades/CheckBox>
#include <bb/cascades/ImageView>
#include <bb/cascades/Label>
#include <bb/cascades/TextArea>
#include <bb/cascades/TextField>
using namespace ::bb::cascades;
struct Mapping
{
QPointer<Control> control;
QString section;
QByteArray propertyName;
};
class DataControlMapperPrivate
{
public:
DataControlMapperPrivate()
: m_model(0), m_currentIndex(-1)
{
}
void updateMapping(Mapping &mapping, Control *control, const QString §ion, const QByteArray &propertyName);
void update();
QPointer<bb::cascades::DataModel> m_model;
QVector<Mapping> m_mappings;
int m_currentIndex;
};
void DataControlMapperPrivate::updateMapping(Mapping &mapping, Control *control, const QString §ion, const QByteArray &propertyName)
{
mapping.control = control;
mapping.section = section;
mapping.propertyName = (propertyName.isEmpty() ? "text" : propertyName);
}
void DataControlMapperPrivate::update()
{
if (!m_model)
return;
if (m_mappings.isEmpty())
return;
if (m_currentIndex == -1)
return;
const QVariant data = m_model->data(QVariantList() << QVariant(m_currentIndex));
const QVariantMap map = data.toMap();
if (map.isEmpty()) {
qWarning("received incompatible record from model");
return;
}
foreach (const Mapping &mapping, m_mappings) {
const QVariant value = map.value(mapping.section);
if (mapping.control) {
mapping.control->setProperty(mapping.propertyName, value);
}
}
}
DataControlMapper::DataControlMapper(QObject *parent)
: QObject(parent), d(new DataControlMapperPrivate())
{
}
DataControlMapper::~DataControlMapper()
{
delete d;
}
void DataControlMapper::addMapping(bb::cascades::Control *control, const QString §ion)
{
QByteArray propertyName;
if (qobject_cast<CheckBox*>(control))
propertyName = "checked";
else if (qobject_cast<ImageView*>(control))
propertyName = "image";
else if (qobject_cast<Label*>(control))
propertyName = "text";
else if (qobject_cast<TextArea*>(control))
propertyName = "text";
else if (qobject_cast<TextField*>(control))
propertyName = "text";
addMapping(control, section, propertyName);
}
void DataControlMapper::addMapping(bb::cascades::Control *control, const QString §ion, const QByteArray &propertyName)
{
for (int i = 0; i < d->m_mappings.count(); ++i) {
Mapping &mapping = d->m_mappings[i];
if (mapping.control == control) {
d->updateMapping(mapping, control, section, propertyName);
d->update();
return;
}
}
Mapping mapping;
d->updateMapping(mapping, control, section, propertyName);
d->m_mappings.append(mapping);
d->update();
}
void DataControlMapper::clearMapping()
{
d->m_mappings.clear();
}
int DataControlMapper::currentIndex() const
{
return d->m_currentIndex;
}
int DataControlMapper::count() const
{
if (!d->m_model)
return 0;
return d->m_model->childCount(QVariantList());
}
QByteArray DataControlMapper::mappedPropertyName(bb::cascades::Control *control) const
{
foreach(const Mapping &mapping, d->m_mappings) {
if (mapping.control == control)
return mapping.propertyName;
}
return QByteArray();
}
QString DataControlMapper::mappedSection(bb::cascades::Control *control) const
{
foreach(const Mapping &mapping, d->m_mappings) {
if (mapping.control == control)
return mapping.section;
}
return QString();
}
bb::cascades::Control* DataControlMapper::mappedControlAt(const QString §ion) const
{
foreach(const Mapping &mapping, d->m_mappings) {
if (mapping.section == section)
return mapping.control;
}
return 0;
}
bb::cascades::DataModel* DataControlMapper::model() const
{
return d->m_model;
}
void DataControlMapper::removeMapping(bb::cascades::Control *control)
{
for (int i = 0; i < d->m_mappings.count(); ++i) {
if (d->m_mappings[i].control == control) {
d->m_mappings.remove(i);
return;
}
}
}
void DataControlMapper::setModel(bb::cascades::DataModel *model)
{
d->m_model = model;
d->m_currentIndex = 0;
d->update();
emit countChanged();
}
void DataControlMapper::revert()
{
d->update();
}
void DataControlMapper::setCurrentIndex(int index)
{
if (!d->m_model)
return;
const int rowCount = d->m_model->childCount(QVariantList());
if (index < 0 || index >= rowCount)
return;
d->m_currentIndex = index;
d->update();
emit currentIndexChanged(d->m_currentIndex);
}
void DataControlMapper::toFirst()
{
setCurrentIndex(0);
}
void DataControlMapper::toLast()
{
if (!d->m_model)
return;
const int rowCount = d->m_model->childCount(QVariantList());
setCurrentIndex(rowCount - 1);
}
void DataControlMapper::toNext()
{
setCurrentIndex(d->m_currentIndex + 1);
}
void DataControlMapper::toPrevious()
{
setCurrentIndex(d->m_currentIndex - 1);
}