DataModelLoggerDecorator.cpp Example File
listdecorators/src/DataModelLoggerDecorator.cpp
#include "DataModelLoggerDecorator.hpp"
#include <bb/cascades/DataModel>
#include <QDebug>
class IndexMapperLogger: public bb::cascades::DataModel::IndexMapper {
public:
explicit IndexMapperLogger(
QSharedPointer<bb::cascades::DataModel::IndexMapper> indexMapper,
DataModelLoggerDecorator *model);
virtual ~IndexMapperLogger();
virtual bool newIndexPath(QVariantList *pOutIndexPath,
int *pOutReplacementIndex, const QVariantList &oldIndexPath) const;
private:
bb::cascades::DataModel *model() const;
QSharedPointer<bb::cascades::DataModel::IndexMapper> m_indexMapper;
DataModelLoggerDecorator *m_model;
};
IndexMapperLogger::IndexMapperLogger(
QSharedPointer<bb::cascades::DataModel::IndexMapper> indexMapper,
DataModelLoggerDecorator *model)
: m_indexMapper(indexMapper)
, m_model(model)
{
}
IndexMapperLogger::~IndexMapperLogger() {
}
bb::cascades::DataModel *IndexMapperLogger::model() const {
return m_model->model();
}
bool IndexMapperLogger::newIndexPath(QVariantList *pOutIndexPath,
int *pOutReplacementIndex, const QVariantList &oldIndexPath) const {
bool result = m_indexMapper->newIndexPath(pOutIndexPath,
pOutReplacementIndex, oldIndexPath);
if (!result) {
qDebug() << "DEL: " << oldIndexPath;
} else if (oldIndexPath != *pOutIndexPath) {
qDebug() << "MOV: " << oldIndexPath << " -> " << *pOutIndexPath;
} else if (m_model->logAllCallbacks()) {
qDebug() << "NOP: " << oldIndexPath;
}
return result;
}
DataModelLoggerDecorator::DataModelLoggerDecorator(QObject *parent)
: bb::cascades::datamanager::DataModelDecorator(parent)
, m_logBegin(true)
, m_logData(true)
, m_logAllCallbacks(true)
{
}
DataModelLoggerDecorator::~DataModelLoggerDecorator() {
}
int DataModelLoggerDecorator::childCount(const QVariantList &indexPath) {
logStart("childCount()", indexPath);
int result = DataModelDecorator::childCount(indexPath);
logEnd("childCount()", indexPath, QString::number(result));
return result;
}
bool DataModelLoggerDecorator::hasChildren(const QVariantList &indexPath) {
logStart("hasChildren()", indexPath);
bool result = bb::cascades::datamanager::DataModelDecorator::hasChildren(
indexPath);
logEnd("hasChildren()", indexPath, QString(result ? "true" : "false"));
return result;
}
QString DataModelLoggerDecorator::itemType(const QVariantList &indexPath) {
logStart("itemType()", indexPath);
QString result = bb::cascades::datamanager::DataModelDecorator::itemType(
indexPath);
logEnd("itemType()", indexPath, result);
return result;
}
QVariant DataModelLoggerDecorator::data(const QVariantList &indexPath) {
logStart("data()", indexPath);
QVariant result = bb::cascades::datamanager::DataModelDecorator::data(
indexPath);
if (m_logData) {
QString dataStr;
QDebug str(&dataStr);
str << result;
logEnd("data()", indexPath, dataStr);
} else {
logEnd("data()", indexPath,
QString(
result.isNull() || !result.isValid() ?
"NULL" : result.typeName()));
}
return result;
}
void DataModelLoggerDecorator::load() {
if (QMetaObject::invokeMethod(model(), "load", Qt::AutoConnection)) {
QObject::connect(model(), SIGNAL(loaded()), this, SIGNAL(loaded()));
}
}
bool DataModelLoggerDecorator::logData() {
return m_logData;
}
bool DataModelLoggerDecorator::logBegin() {
return m_logBegin;
}
void DataModelLoggerDecorator::setLogData(bool enable) {
m_logData = enable;
}
void DataModelLoggerDecorator::setLogBegin(bool enable) {
m_logBegin = enable;
}
bool DataModelLoggerDecorator::logAllCallbacks() {
return m_logAllCallbacks;
}
void DataModelLoggerDecorator::setLogAllCallbacks(bool enable) {
m_logAllCallbacks = enable;
}
void DataModelLoggerDecorator::onItemAdded(QVariantList indexPath) {
qDebug() << "onItemAdded()->" << indexPath;
bb::cascades::datamanager::DataModelDecorator::onItemAdded(indexPath);
}
void DataModelLoggerDecorator::onItemUpdated(QVariantList indexPath) {
qDebug() << "onItemUpdated()->" << indexPath;
bb::cascades::datamanager::DataModelDecorator::onItemUpdated(indexPath);
}
void DataModelLoggerDecorator::onItemRemoved(QVariantList indexPath) {
qDebug() << "onItemRemoved()->" << indexPath;
bb::cascades::datamanager::DataModelDecorator::onItemRemoved(indexPath);
}
void DataModelLoggerDecorator::onItemsChanged(
bb::cascades::DataModelChangeType::Type eChangeType,
QSharedPointer<bb::cascades::DataModel::IndexMapper> indexMapper) {
qDebug() << "onItemsChanged()->" << this->model()->objectName();
QSharedPointer<IndexMapperLogger> mapperLogger(
new IndexMapperLogger(indexMapper, this));
bb::cascades::datamanager::DataModelDecorator::onItemsChanged(eChangeType,
mapperLogger);
}
void DataModelLoggerDecorator::logStart(const QString &method,
const QVariantList &list) {
m_timer.start();
if (m_logBegin) {
qDebug() << method << "->" << list << "+";
}
}
void DataModelLoggerDecorator::logEnd(const QString &method,
const QVariantList &list, const QString &result) {
if (m_logBegin) {
qDebug() << method << "->" << list << "::" << result << " - "
<< m_timer.elapsed() << "ms";
}
}