Vous avez reçu un message "Your GitLab account has been locked ..." ? Pas d'inquiétude : lisez cet article https://docs.gricad-pages.univ-grenoble-alpes.fr/help/unlock/

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

WIP: Factor part common to QModelWriter and QModelReader

parent 331e925e
......@@ -52,23 +52,22 @@
// //////////////////////////////////////////////////////////////////////////
// QBind<T,Q*Model> support
template <class TItemWriter=QJsonWriter>
class QModelWriter : public IWriter
class QModelBind : public IBind
{
Q_DISABLE_COPY(QModelWriter)
enum : int {
Q_DISABLE_COPY(QModelBind)
enum : int {
T=0, //!< initial state
R=1, //!< 1st and odd model dimensions where items bind to row (views being row-wise by default)
C=2, //!< 2nd and even model dimensions where items bind to col or index(row,col,parent).model().index(0,0) where row>sizes[?] or QIdentifierLiteral n==childrenName
I=3, //!< final model dimension handled by TItemWriter to set leaf items depending on their type
};
public:
QModelWriter(QAbstractItemModel* m, bool rowFirst=true) : m(m), rowFirst(rowFirst), w(&io) { Q_ASSERT(m); }
QModelBind(QAbstractItemModel* m, bool rowFirst=true) : m(m), rowFirst(rowFirst) { Q_ASSERT(m); }
Val<Cursor> value () { return Cursor(this).value(); }
Seq<Cursor> sequence() { return Cursor(this).value().sequence(); }
protected:
void _meta(QIdentifierLiteral& n, QAsciiData& meta) {
virtual void _meta(QIdentifierLiteral& n, QAsciiData& meta) {
if (T==d) {
if (n == qmChildren) {
childrenName = QIdentifier(meta.latin1());
......@@ -96,42 +95,40 @@ protected:
}
}
else {
itemWriter()->_meta(n, meta);
itemBind()->_meta(n, meta);
}
}
bool _null( ) { return hidden() ? true : I<=d ? itemWriter()->_null() : m->setData(current(), QVariant()); }
virtual bool _null( ) { return hidden() ? true : I<=d ? itemBind()->_null() : m->setData(current(), QVariant()); }
bool _bind(QUtf8DataView u) { return hidden() ? true : I<=d ?
itemWriter()->_bind(std::move(u)) :
m->setData(current(), QString::fromUtf8(u.data(), u.size())); }
bool _bind( bool&& b) { return hidden() ? true : I<=d ? itemWriter()->_bind(std::move(b)) : m->setData(current(), b); }
bool _bind( float&& f) { return hidden() ? true : I<=d ? itemWriter()->_bind(std::move(f)) : m->setData(current(), f); }
bool _bind( double&& f) { return hidden() ? true : I<=d ? itemWriter()->_bind(std::move(f)) : m->setData(current(), f); }
bool _bind( quint64&& i) { return hidden() ? true : I<=d ? itemWriter()->_bind(std::move(i)) :
i<=quint64(std::numeric_limits<unsigned int>::max()) ? m->setData(current(), static_cast<unsigned int>(i)) // with QSpinBox delegate
: m->setData(current(), i ); }
bool _bind( qint64&& i) { return hidden() ? true : I<=d ? itemWriter()->_bind(std::move( i)) :
i<= qint64(std::numeric_limits< int>::max()) ? m->setData(current(), static_cast< int>(i)) // with QSpinBox delegate
: m->setData(current(), i ); }
bool _bind( QVariant&& t) { return hidden() ? true : I<=d ? static_cast<IWriter*>(&w)->_bind(QVariant(t)) : m->setData(current(), t); }
virtual bool _bind(QUtf8DataView u) { return hidden() ? true : I<=d ? itemBind()->_bind(std::move(u)) : m->setData(current(), QString::fromUtf8(u.data(), u.size())); }
virtual bool _bind( bool&& b) { return hidden() ? true : I<=d ? itemBind()->_bind(std::move(b)) : m->setData(current(), b); }
virtual bool _bind( float&& f) { return hidden() ? true : I<=d ? itemBind()->_bind(std::move(f)) : m->setData(current(), f); }
virtual bool _bind( double&& f) { return hidden() ? true : I<=d ? itemBind()->_bind(std::move(f)) : m->setData(current(), f); }
virtual bool _bind( quint64&& i) { return hidden() ? true : I<=d ? itemBind()->_bind(std::move(i)) : i<=quint64(std::numeric_limits<unsigned int>::max())
? m->setData(current(), static_cast<unsigned int>(i)) // with QSpinBox delegate
: m->setData(current(), i); }
virtual bool _bind( qint64&& i) { return hidden() ? true : I<=d ? itemBind()->_bind(std::move(i)) : i<= qint64(std::numeric_limits<int>::max())
? m->setData(current(), static_cast< int>(i)) // with QSpinBox delegate
: m->setData(current(), i); }
virtual bool _bind( QVariant&& t) { return hidden() ? true : I<=d ? itemBind()->_bind( QVariant(t)) : m->setData(current(), t); }
// TODO QDate*, QTime
// TODO QPixmap if metadata suggests a QMimeData image ?
bool _out ( ) { if (d--==I) {
return hidden() || (itemWriter()->_out() && _bind(QUtf8DataView(io)));
virtual bool _out ( ) { if (d--==I) {
return hidden() || (itemBind()->_out() && _bind(QUtf8DataView(io)));
}
if (d==T && parent.isValid()) {
row=parent.row(); parent=parent.parent(); d=C; // even if hidden()
}
return d<I || hidden() ? true : itemWriter()->_out();
return d<I || hidden() ? true : itemBind()->_out();
}
bool _sequence(quint32* s=nullptr) { ++d;
virtual bool _sequence(quint32* s=nullptr) { ++d;
if (hidden()) {
return true;
}
if (d==I) {
io.clear(); w = std::move(TItemWriter(&io));
io.clear(); resetItemBind();
}
if (is(R)) {
row=-1; // set to 0 by mandatory _item() following
......@@ -139,14 +136,14 @@ protected:
if (is(C)) {
col=-1; // set to 0 by mandatory _item() following
}
return d<I ? true : itemWriter()->_sequence(s);
return d<I ? true : itemBind()->_sequence(s);
}
bool _record (quint32* s=nullptr) { ++d;
virtual bool _record (quint32* s=nullptr) { ++d;
if (hidden()) {
return true;
}
if (d==I) {
io.clear(); w = std::move(TItemWriter(&io));
io.clear(); resetItemBind();
}
if (is(R)) {
row=-1; // set to 0 by mandatory _item() following
......@@ -154,9 +151,9 @@ protected:
if (is(C)) {
col=-1; // set to 0 by mandatory _item() following
}
return d<I ? true : itemWriter()->_record (s); }
return d<I ? true : itemBind()->_record (s); }
bool _item ( ) { Q_ASSERT(d!=T);
virtual bool _item ( ) { Q_ASSERT(d!=T);
if (is(C)) { col++; // mandatory after _sequence()
if (hidden()) {
return true;
......@@ -185,9 +182,9 @@ protected:
}
return true;
}
return hidden() || itemWriter()->_item();
return hidden() || itemBind()->_item();
}
bool _item ( QIdentifier& n) { Q_ASSERT(d!=T);
virtual bool _item ( QIdentifier& n) { Q_ASSERT(d!=T);
if (is(R)) { row++; // mandatory after _record()
if (hidden()) {
return true;
......@@ -228,9 +225,62 @@ protected:
d=T; parent=m->index(row,0,parent); row=col=0;
return true;
}
return itemWriter()->_item(n);
return itemBind()->_item(n);
}
virtual bool _item(QIdentifierLiteral n) { QIdentifier id(n); return _item(id); }
virtual bool _bind( const char* u) { return _bind(QUtf8DataView(u)); } // required to match better than QString overloads
virtual bool _bind( QAsciiDataView a) { return _bind(QUtf8DataView(a.data(), a.size())); }
virtual bool _bind( QLatin1String l) { return _bind(QString(l)); }
virtual bool _bind( QStringView s) { return _bind(QUtf8DataView(s.toUtf8())); }
virtual bool _bind( QUtf8Data&& s) { return _bind(QUtf8DataView(s)); }
virtual bool _bind( QString&& s) { return _bind(QStringView(s.data(), s.size())); }
virtual bool _bind( qint8&& n) { return _bind( qint64(n)); }
virtual bool _bind( quint8&& n) { return _bind(quint64(n)); }
virtual bool _bind( qint16&& n) { return _bind( qint64(n)); }
virtual bool _bind( quint16&& n) { return _bind(quint64(n)); }
virtual bool _bind( qint32&& n) { return _bind( qint64(n)); }
virtual bool _bind( quint32&& n) { return _bind(quint64(n)); }
virtual bool _bind( QByteArray&& s) { QByteArray h; h.reserve(s.size()*2+2+1); h.append("0x").append(s.toHex()) ; return _bind(QAsciiDataView(h.constData(),h.size())); }
virtual bool _bind( QUtf8Data& r) { QUtf8Data copy(r); return _bind(std::move(copy)); }
virtual bool _bind( QString& r) { QString copy(r); return _bind(std::move(copy)); }
virtual bool _bind( bool& r) { bool copy(r); return _bind(std::move(copy)); }
virtual bool _bind( qint8& r) { qint8 copy(r); return _bind(std::move(copy)); }
virtual bool _bind( quint8& r) { quint8 copy(r); return _bind(std::move(copy)); }
virtual bool _bind( qint16& r) { qint16 copy(r); return _bind(std::move(copy)); }
virtual bool _bind( quint16& r) { quint16 copy(r); return _bind(std::move(copy)); }
virtual bool _bind( qint32& r) { qint32 copy(r); return _bind(std::move(copy)); }
virtual bool _bind( quint32& r) { quint32 copy(r); return _bind(std::move(copy)); }
virtual bool _bind( qint64& r) { qint64 copy(r); return _bind(std::move(copy)); }
virtual bool _bind( quint64& r) { quint64 copy(r); return _bind(std::move(copy)); }
virtual bool _bind( float& r) { float copy(r); return _bind(std::move(copy)); }
virtual bool _bind( double& r) { double copy(r); return _bind(std::move(copy)); }
virtual bool _bind( QByteArray& r) { QByteArray copy(r); return _bind(std::move(copy)); }
virtual bool _bind( QVariant& r) { QVariant copy(r); return _bind(std::move(copy)); }
virtual bool _bind(const QUtf8Data& r) { QUtf8Data copy(r); return _bind(std::move(copy)); }
virtual bool _bind(const QString& r) { QString copy(r); return _bind(std::move(copy)); }
virtual bool _bind(const bool& r) { bool copy(r); return _bind(std::move(copy)); }
virtual bool _bind(const qint8& r) { qint8 copy(r); return _bind(std::move(copy)); }
virtual bool _bind(const quint8& r) { quint8 copy(r); return _bind(std::move(copy)); }
virtual bool _bind(const qint16& r) { qint16 copy(r); return _bind(std::move(copy)); }
virtual bool _bind(const quint16& r) { quint16 copy(r); return _bind(std::move(copy)); }
virtual bool _bind(const qint32& r) { qint32 copy(r); return _bind(std::move(copy)); }
virtual bool _bind(const quint32& r) { quint32 copy(r); return _bind(std::move(copy)); }
virtual bool _bind(const qint64& r) { qint64 copy(r); return _bind(std::move(copy)); }
virtual bool _bind(const quint64& r) { quint64 copy(r); return _bind(std::move(copy)); }
virtual bool _bind(const float& r) { float copy(r); return _bind(std::move(copy)); }
virtual bool _bind(const double& r) { double copy(r); return _bind(std::move(copy)); }
virtual bool _bind(const QByteArray& r) { QByteArray copy(r); return _bind(std::move(copy)); }
virtual bool _bind(const QVariant& r) { QVariant copy(r); return _bind(std::move(copy)); }
virtual void resetItemBind() = 0;
virtual IBind* itemBind() = 0;
QByteArray io;
private:
//! Checks whether current dimension \c d is R or C, inverting R and C dimensions if !rowFirst
bool is(int rc) { Q_ASSERT(rc==R || rc==C); return rowFirst ? d==rc : d==(rc==R ? C : R); }
......@@ -264,17 +314,14 @@ private:
dimension<sizes.size() ? sizes.at(dimension)
: 0;
}
IBind* itemWriter() { return static_cast<IBind*>(&w); }
QAbstractItemModel* m;
bool rowFirst;
QByteArray io;
TItemWriter w;
// Current bind state
int d=T;
QModelIndex parent;
int row=0, col=0; //!< Allows handling {null} equally to {} (but not equally to null) without requiring QModelIndex to reference rows/columns before they actually exist
int row=0, col=0; //!< Allows handling {null} equally to {} (but not equally to null) without requiring QModelIndex to reference rows/columns before they actually exist
// Supported meta()
QIdentifier childrenName;
......@@ -284,3 +331,37 @@ private:
};
// --------------------------------------------------------------------------
template <class TItemWriter=QJsonWriter>
class QModelWriter : public QModelBind
{
Q_DISABLE_COPY(QModelWriter)
public:
QModelWriter(QAbstractItemModel* m, bool rowFirst=true) : QModelBind(m, rowFirst) { resetItemBind(); }
virtual BindMode mode() const noexcept { return BindMode::Write; }
virtual bool _isOk() noexcept { return true; } //!< Write status ignored by default (no need to test intermediate status to choose between _sequence(), etc.
protected:
void resetItemBind() { w = std::move(TItemWriter(&io)); }
IBind* itemBind() { return static_cast<IBind*>(&w); }
TItemWriter w;
};
// --------------------------------------------------------------------------
template <class TItemReader=QJsonReader>
class QModelReader : public QModelBind
{
Q_DISABLE_COPY(QModelReader)
public:
QModelReader(QAbstractItemModel* m, bool rowFirst=true) : QModelBind(m, rowFirst) { resetItemBind(); }
virtual BindMode mode() const noexcept { return BindMode::Read; }
virtual bool _isOk() noexcept { return true; } //!< Read status ignored by default (QAbstractItemModel is not sequential)
protected:
void resetItemBind() { r = std::move(TItemReader(&io)); }
IBind* itemBind() { return static_cast<IBind*>(&r); }
TItemReader r;
};
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