Commit 82fe1d88 authored by EXT Arnaud Clère's avatar EXT Arnaud Clère

Fixed error handling aborting QModelWriter

parent 012c6a96
......@@ -156,6 +156,12 @@ public:
QJsonWriter( QIODevice* io) : io(io) { Q_ASSERT(io || ba); }
QJsonWriter(QByteArray* ba) : ba(ba) { Q_ASSERT(io || ba); }
~QJsonWriter() { while (!levels.isEmpty()) write(levels.takeLast().end); }
void reset(QByteArray* other) {
levels.clear();
io=nullptr;
ba=other;
Q_ASSERT(ba);
}
// Shortcuts
/**/ QValue value ( ) { return QValueStatus(this).value(); }
......
......@@ -156,19 +156,6 @@ protected:
virtual bool tryBind( QByteArray& r) { return tryBind(QByteArray(r)); }
virtual bool tryBind( QVariant& r) { return tryBind( QVariant(r)); }
bool handleError(QIdentifierLiteral e, QString context = QString()) {
if (errorHandler) {
QString path;
for (auto current = parent; parent.isValid(); current = current.parent()) {
path.prepend(QString('/').append(current.row()).append(',').append(current.column()));
}
return errorHandler(e, path.append(context));
}
else {
return false;
}
}
virtual QAbstractValue* itemBind() = 0;
//! Checks whether current dimension \c d is R or C, inverting R and C dimensions if !rowFirst
......@@ -178,7 +165,9 @@ protected:
QModelIndex current() {
int rows = m-> rowCount(parent); if (rows<=row) m->insertRows (rows,1+row-rows,parent);
int cols = m->columnCount(parent); if (cols<=col) m->insertColumns(cols,1+col-cols,parent);
auto idx = m->index(row,col,parent); Q_ASSERT(hidden() || m->checkIndex(idx)); return idx;
auto idx = m->index(row,col,parent);
Q_ASSERT(hidden() || m->checkIndex(idx));
return idx;
}
//! Returns whether current data must be hidden because a current or parent column or row was filtered out
bool hidden() noexcept {
......@@ -220,8 +209,6 @@ protected:
QList<QByteArray> columnNames;
bool metaColumnNames=false;
QVector<int> sizes;
QValueErrorHandler errorHandler = nullptr;
};
// --------------------------------------------------------------------------
......@@ -405,8 +392,10 @@ protected:
// TODO QDate*, QTime
// TODO QPixmap if metadata suggests a QMimeData image ?
void resetItemData() { ba.clear(); w = std::move(TItemWriter(&ba)); }
QAbstractValue* itemBind() { return static_cast<QAbstractValue*>(&w); }
virtual bool handleError(QIdentifierLiteral e, QString context = QString()) { return errorHandler ? errorHandler(e, context) : true; }
void resetItemData() { ba.clear(); w.reset(&ba); }
QAbstractValue* itemBind() { return static_cast<QAbstractValue*>(&w); }
QByteArray ba;
TItemWriter w;
......@@ -423,7 +412,7 @@ public:
QModelReader(QAbstractItemModel* m, bool rowFirst=true) : QModelBind(m, rowFirst), r(&io) { io.open(QIODevice::ReadOnly); }
virtual QValueMode mode() const noexcept { return QValueMode::Read; }
virtual bool isValid() const noexcept { return true; } //!< Read status ignored by default (QAbstractItemModel is not sequential)
virtual bool isValid() const noexcept { return true; } //!< Read status ignored by default (QAbstractItemModel is not sequential)
protected:
virtual bool tryOut() {
if (I<=d) {
......@@ -630,6 +619,19 @@ protected:
QAbstractValue* itemBind() { return static_cast<QAbstractValue*>(&r); }
void setItemData(QUtf8Data u) { io.buffer() = u.utf8(); io.seek(0); r.reset(&io); }
bool handleError(QIdentifierLiteral e, QString context = QString()) {
if (errorHandler) {
QString path;
for (auto current = parent; parent.isValid(); current = current.parent()) {
path.prepend(QString('/').append(current.row()).append(',').append(current.column()));
}
return errorHandler(e, path.append(context));
}
else {
return false;
}
}
QBuffer io;
TItemReader r;
};
......@@ -225,8 +225,10 @@ struct QAbstractValue {
//! \warning meta() is ignored by default and subject to various interpretation, so users should define or adopt existing meta data standards like XSD for sake of interoperability
virtual void _meta(QIdentifierLiteral&, QAsciiData&) {}
virtual QValueErrorHandler setErrorHandler(QValueErrorHandler newHandler = nullptr) { Q_UNUSED(newHandler) return nullptr; }
virtual bool handleError(QIdentifierLiteral name, QString context = QString()) { Q_UNUSED(name) Q_UNUSED(context) return false; }
virtual QValueErrorHandler setErrorHandler(QValueErrorHandler newHandler = nullptr) { auto previousHandler = errorHandler; errorHandler = newHandler; return previousHandler; }
virtual bool handleError(QIdentifierLiteral e, QString context = QString()) = 0;
protected:
QValueErrorHandler errorHandler = nullptr;
};
// //////////////////////////////////////////////////////////////////////////
......@@ -642,6 +644,8 @@ struct QAbstractValueWriter : public QAbstractValue
virtual bool tryBind(const QVariant& r) { QVariant copy(r); return tryBind(std::move(copy)); }
virtual bool tryAny() { return tryNull(); }
virtual bool handleError(QIdentifierLiteral e, QString context = QString()) { return errorHandler ? errorHandler(e, context) : true; }
};
//! Base QAbstractValue implementations with QValueMode::Read
......@@ -745,8 +749,7 @@ struct QAbstractValueReader : public QAbstractValue
return bound || handleError(qBindUnexpectedValue);
}
QValueErrorHandler setErrorHandler(QValueErrorHandler newHandler = nullptr) { auto previousHandler = errorHandler; errorHandler = newHandler; return previousHandler; }
bool handleError(QIdentifierLiteral e, QString context = QString()) { return errorHandler ? errorHandler(e, context) : false; }
virtual bool handleError(QIdentifierLiteral e, QString context = QString()) { return errorHandler ? errorHandler(e, context) : false; }
protected:
bool toByteArray(QByteArray& b, QUtf8Data s) {
const QByteArray& bytes = s.utf8();
......@@ -769,8 +772,6 @@ protected:
}
v = QVariant(b);
}
QValueErrorHandler errorHandler = nullptr;
};
// //////////////////////////////////////////////////////////////////////////
......
......@@ -1147,6 +1147,7 @@ void doGuiExample() {
QStringList errors;
auto handler = [&errors](QIdentifierLiteral name, QString context){ errors.append(context.append(name.latin1())); return true; };
auto ignore = [](QIdentifierLiteral, QString){ return true; };
// Various possibilities to customize bind
......
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