Commit 3119490e authored by EXT Arnaud Clère's avatar EXT Arnaud Clère
Browse files

WIP: QAbstractItemModel Writer handling homogeneous multi-dimensional...

WIP: QAbstractItemModel Writer handling homogeneous multi-dimensional structures efficiently (definite sequence of homogeneous record, indefinite tree of homogeneous record, etc.)
parent fd1e4905
/****************************************************************************
* **
* ** Copyright (C) 2018 MinMaxMedical.
* ** All rights reserved.
* ** Contact: MinMaxMedical <InCAS@MinMaxMedical.com>
* **
* ** This file is part of the modmedLog module.
* **
* ** $QT_BEGIN_LICENSE:BSD$
* ** You may use this file under the terms of the BSD license as follows:
* **
* ** "Redistribution and use in source and binary forms, with or without
* ** modification, are permitted provided that the following conditions are
* ** met:
* ** * Redistributions of source code must retain the above copyright
* ** notice, this list of conditions and the following disclaimer.
* ** * Redistributions in binary form must reproduce the above copyright
* ** notice, this list of conditions and the following disclaimer in
* ** the documentation and/or other materials provided with the
* ** distribution.
* ** * Neither the name of MinMaxMedical S.A.S. and its Subsidiary(-ies) nor
* ** the names of its contributors may be used to endorse or promote
* ** products derived from this software without specific prior written
* ** permission.
* **
* ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
* ** $QT_END_LICENSE$
* **
* ****************************************************************************/
#pragma once
#include <type_traits>
#include <QtCore/qabstractitemmodel.h>
#include "QBind_impl.h"
// //////////////////////////////////////////////////////////////////////////
// QBind<QModel*,T> support
class ModelWriter : public QMovedWriter<ModelWriter>, public IWriter
{
protected:
ModelWriter(const ModelWriter &o) : QMovedWriter(), m(o.m) {}
ModelWriter &operator=(const ModelWriter &) = delete;
public:
Q_ENABLE_MOVE(ModelWriter, std::swap(m, o.m); std::swap(idx, o.idx); )
ModelWriter(QAbstractItemModel* m) : m(m), idx(), d(Vertical) { Q_ASSERT(m); }
operator bool() { return true; }
protected:
friend class QMovedWriter<ModelWriter>;
template<class T_> friend class Val; // calls methods below
bool _sequence(quint32* rows=nullptr) { if (d==Child || !rows) { d=Vertical ; idx=idx.child(0,0); } else
if (d==Vertical ) { d=Horizontal; } else
if (d==Horizontal ) { d=Child ; }
return true; }
bool _record (quint32* cols=nullptr) { if (d==Child || !cols) { d=Vertical ; idx=idx.child(0,0); } else
if (d==Vertical ) { d=Horizontal; } else
if (d==Horizontal ) { d=Child ; }
return true; }
bool _null ( ) { return m->setData(idx, QVariant( )); }
bool _bind ( const char* s) { return m->setData(idx, QVariant(s)); }
bool _bind ( QString& s) { return m->setData(idx, QVariant(s)); }
bool _bind ( bool& b) { return m->setData(idx, QVariant(b)); }
bool _bind ( float& f) { return m->setData(idx, QVariant(f)); }
bool _bind ( double& d) { return m->setData(idx, QVariant(d)); }
bool _bind ( qint32& i) { return m->setData(idx, QVariant(i)); }
bool _bind ( quint32& u) { return m->setData(idx, QVariant(u)); }
// TODO BindNative all types in IWriter and http://doc.qt.io/qt-5/qstyleditemdelegate.html#details ?
template<class T_> friend class Seq; // calls methods below
template<class T_> friend class Rec; // calls methods below
bool _item(QByteArray& k) { idx = d==Vertical ? idx.siblingAtRow(idx.row()+1) : idx.siblingAtColumn(idx.column()+1);
m->setHeaderData(d==Vertical ? idx.row() : idx.column(), d==Vertical ? Qt::Vertical : Qt::Horizontal, k);
return true; }
bool _item( ) { idx = d==Vertical ? idx.siblingAtRow(idx.row()+1) : idx.siblingAtColumn(idx.column()+1);
return true; }
bool _out( ) { if (d==Horizontal) d=Vertical; else
if (d==Vertical ) d=Child ; else
idx = d==Child ? idx.parent() : ? idx.siblingAtColumn(idx.column()+1); }
private:
QAbstractItemModel* m ;
QModelIndex idx;
enum Dimension { Vertical = Qt::Vertical, Horizontal = Qt::Horizontal, Child } d;
// TODO
// Encode _sequence(n) or _record(n) as above
// setHeaderData(n) with numbers or names on 1st item
// Encode _sequence(0) or _record(0) as empty QVariantList() or QVariantMap() at idx
// Encode _sequence( ) as empty QVariantList() at idx with children allowing lazy reading
// Encode _record( ) as QVariantMap () at idx
// Encode cols or rows > 0 as
// TODO setHeaderData on 1st item and
};
template<> struct BindSupport<ModelWriter,const char*> : BindNative {};
template<> struct BindSupport<ModelWriter, QString&> : BindNative {};
template<> struct BindSupport<ModelWriter, bool&> : BindNative {};
template<> struct BindSupport<ModelWriter, float&> : BindNative {};
template<> struct BindSupport<ModelWriter, double&> : BindNative {};
template<> struct BindSupport<ModelWriter, qint32&> : BindNative {};
template<> struct BindSupport<ModelWriter, quint32&> : BindNative {};
// TODO BindNative a subset of QVariant types
// --------------------------------------------------------------------------
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment