StatusQ(MovableModel): Methods naming refactor, Storybook pages updated/improved

This commit is contained in:
Michał Cieślak 2024-02-16 11:30:54 +01:00 committed by Michał
parent f747791f50
commit b949fb7b3b
5 changed files with 71 additions and 57 deletions

View File

@ -119,7 +119,7 @@ Item {
Layout.fillHeight: true Layout.fillHeight: true
Label { Label {
text: "DETACHED-ORDER MODEL" text: "MOVABLE MODEL (press to drag&drop)"
font.bold: true font.bold: true
font.pixelSize: 17 font.pixelSize: 17
@ -234,15 +234,23 @@ Item {
} }
Button { Button {
text: "detach order explicitely" text: "desynchronize"
onClicked: { onClicked: {
movableModel.detach() movableModel.desyncOrder()
}
}
Button {
text: "synchronize"
onClicked: {
movableModel.syncOrder()
} }
} }
Label { Label {
text: "Detached: " + movableModel.detached text: "Synchronized: " + movableModel.synced
} }
} }
} }

View File

@ -460,13 +460,19 @@ Item {
} }
Button { Button {
text: "detach order explicitely" text: "desynchronize"
onClicked: movableModel.detach() onClicked: movableModel.desyncOrder()
}
Button {
text: "synchronize"
onClicked: movableModel.syncOrder()
} }
Label { Label {
text: `Detached: <b>${movableModel.detached}</b>` text: `Synchronized: <b>${movableModel.synced}</b>`
} }
} }
} }

View File

@ -11,7 +11,7 @@ class MovableModel : public QAbstractListModel
Q_PROPERTY(QAbstractItemModel* sourceModel READ sourceModel Q_PROPERTY(QAbstractItemModel* sourceModel READ sourceModel
WRITE setSourceModel NOTIFY sourceModelChanged) WRITE setSourceModel NOTIFY sourceModelChanged)
Q_PROPERTY(bool detached READ detached NOTIFY detachedChanged) Q_PROPERTY(bool synced READ synced NOTIFY syncedChanged)
public: public:
explicit MovableModel(QObject *parent = nullptr); explicit MovableModel(QObject *parent = nullptr);
@ -22,16 +22,16 @@ public:
QVariant data(const QModelIndex &index, int role) const override; QVariant data(const QModelIndex &index, int role) const override;
QHash<int, QByteArray> roleNames() const override; QHash<int, QByteArray> roleNames() const override;
Q_INVOKABLE void detach(); Q_INVOKABLE void desyncOrder();
Q_INVOKABLE void attach(); Q_INVOKABLE void syncOrder();
Q_INVOKABLE void move(int from, int to, int count = 1); Q_INVOKABLE void move(int from, int to, int count = 1);
Q_INVOKABLE QVector<int> order() const; Q_INVOKABLE QVector<int> order() const;
bool detached() const; bool synced() const;
signals: signals:
void sourceModelChanged(); void sourceModelChanged();
void detachedChanged(); void syncedChanged();
protected slots: protected slots:
void resetInternalData(); void resetInternalData();
@ -41,6 +41,6 @@ private:
void connectSignalsForAttachedState(); void connectSignalsForAttachedState();
bool m_detached = false; bool m_synced = true;
std::vector<QPersistentModelIndex> m_indexes; std::vector<QPersistentModelIndex> m_indexes;
}; };

View File

@ -34,7 +34,7 @@ QAbstractItemModel* MovableModel::sourceModel() const
int MovableModel::rowCount(const QModelIndex &parent) const int MovableModel::rowCount(const QModelIndex &parent) const
{ {
if (m_detached) if (!m_synced)
return m_indexes.size(); return m_indexes.size();
if (m_sourceModel == nullptr) if (m_sourceModel == nullptr)
@ -48,7 +48,7 @@ QVariant MovableModel::data(const QModelIndex &index, int role) const
if (!checkIndex(index, CheckIndexOption::IndexIsValid)) if (!checkIndex(index, CheckIndexOption::IndexIsValid))
return {}; return {};
if (m_detached) if (!m_synced)
return m_indexes.at(index.row()).data(role); return m_indexes.at(index.row()).data(role);
if (m_sourceModel == nullptr) if (m_sourceModel == nullptr)
@ -65,9 +65,9 @@ QHash<int, QByteArray> MovableModel::roleNames() const
return m_sourceModel->roleNames(); return m_sourceModel->roleNames();
} }
void MovableModel::detach() void MovableModel::desyncOrder()
{ {
if (m_detached || m_sourceModel == nullptr) if (!m_synced || m_sourceModel == nullptr)
return; return;
disconnect(m_sourceModel, &QAbstractItemModel::rowsAboutToBeInserted, this, disconnect(m_sourceModel, &QAbstractItemModel::rowsAboutToBeInserted, this,
@ -175,13 +175,13 @@ void MovableModel::detach()
for (auto i = 0; i < count; i++) for (auto i = 0; i < count; i++)
m_indexes.emplace_back(m_sourceModel->index(i, 0)); m_indexes.emplace_back(m_sourceModel->index(i, 0));
m_detached = true; m_synced = false;
emit detachedChanged(); emit syncedChanged();
} }
void MovableModel::attach() void MovableModel::syncOrder()
{ {
if (!m_detached || m_sourceModel == nullptr) if (m_synced || m_sourceModel == nullptr)
return; return;
emit layoutAboutToBeChanged(); emit layoutAboutToBeChanged();
@ -212,8 +212,8 @@ void MovableModel::move(int from, int to, int count)
const int sourceLast = from + count - 1; const int sourceLast = from + count - 1;
const int destinationRow = to < from ? to : to + count; const int destinationRow = to < from ? to : to + count;
if (!m_detached) if (m_synced)
detach(); desyncOrder();
beginMoveRows({}, sourceFirst, sourceLast, {}, destinationRow); beginMoveRows({}, sourceFirst, sourceLast, {}, destinationRow);
@ -236,7 +236,7 @@ QVector<int> MovableModel::order() const
{ {
QVector<int> order(rowCount()); QVector<int> order(rowCount());
if (m_detached) if (!m_synced)
std::transform(m_indexes.begin(), m_indexes.end(), order.begin(), std::transform(m_indexes.begin(), m_indexes.end(), order.begin(),
[](auto& idx) { return idx.row(); }); [](auto& idx) { return idx.row(); });
else else
@ -245,9 +245,9 @@ QVector<int> MovableModel::order() const
return order; return order;
} }
bool MovableModel::detached() const bool MovableModel::synced() const
{ {
return m_detached; return m_synced;
} }
void MovableModel::resetInternalData() void MovableModel::resetInternalData()
@ -256,9 +256,9 @@ void MovableModel::resetInternalData()
m_indexes.clear(); m_indexes.clear();
if (m_detached) { if (!m_synced) {
m_detached = false; m_synced = true;
emit detachedChanged(); emit syncedChanged();
} }
} }

View File

@ -47,11 +47,11 @@ private slots:
MovableModel model; MovableModel model;
model.setSourceModel(sourceModel); model.setSourceModel(sourceModel);
QCOMPARE(model.detached(), false); QCOMPARE(model.synced(), true);
QVERIFY(isSame(&model, sourceModel)); QVERIFY(isSame(&model, sourceModel));
} }
void detachTest() void desyncOrderTest()
{ {
QQmlEngine engine; QQmlEngine engine;
@ -67,16 +67,16 @@ private slots:
MovableModel model; MovableModel model;
model.setSourceModel(sourceModel); model.setSourceModel(sourceModel);
QSignalSpy detachChangedSpy(&model, &MovableModel::detachedChanged); QSignalSpy syncedChangedSpy(&model, &MovableModel::syncedChanged);
model.detach(); model.desyncOrder();
QCOMPARE(detachChangedSpy.count(), 1); QCOMPARE(syncedChangedSpy.count(), 1);
QCOMPARE(model.detached(), true); QCOMPARE(model.synced(), false);
QVERIFY(isSame(&model, sourceModel)); QVERIFY(isSame(&model, sourceModel));
model.setSourceModel(nullptr); model.setSourceModel(nullptr);
QCOMPARE(detachChangedSpy.count(), 2); QCOMPARE(syncedChangedSpy.count(), 2);
QCOMPARE(model.detached(), false); QCOMPARE(model.synced(), true);
QCOMPARE(model.rowCount(), 0); QCOMPARE(model.rowCount(), 0);
} }
@ -98,7 +98,7 @@ private slots:
MovableModel model; MovableModel model;
model.setSourceModel(sourceModel); model.setSourceModel(sourceModel);
model.detach(); model.desyncOrder();
ModelSignalsSpy signalsSpy(&model); ModelSignalsSpy signalsSpy(&model);
ModelSignalsSpy referenceSignalsSpy(sourceModelCopy); ModelSignalsSpy referenceSignalsSpy(sourceModelCopy);
@ -150,7 +150,7 @@ private slots:
MovableModel model; MovableModel model;
model.setSourceModel(sourceModel); model.setSourceModel(sourceModel);
model.detach(); model.desyncOrder();
ModelSignalsSpy signalsSpy(&model); ModelSignalsSpy signalsSpy(&model);
ModelSignalsSpy referenceSignalsSpy(sourceModelCopy); ModelSignalsSpy referenceSignalsSpy(sourceModelCopy);
@ -202,7 +202,7 @@ private slots:
MovableModel model; MovableModel model;
model.setSourceModel(sourceModel); model.setSourceModel(sourceModel);
model.detach(); model.desyncOrder();
ModelSignalsSpy signalsSpy(&model); ModelSignalsSpy signalsSpy(&model);
ModelSignalsSpy referenceSignalsSpy(sourceModelCopy); ModelSignalsSpy referenceSignalsSpy(sourceModelCopy);
@ -249,7 +249,7 @@ private slots:
MovableModel model; MovableModel model;
model.setSourceModel(sourceModel); model.setSourceModel(sourceModel);
model.detach(); model.desyncOrder();
ModelSignalsSpy signalsSpy(&model); ModelSignalsSpy signalsSpy(&model);
ModelSignalsSpy referenceSignalsSpy(sourceModelCopy); ModelSignalsSpy referenceSignalsSpy(sourceModelCopy);
@ -299,7 +299,7 @@ private slots:
MovableModel model; MovableModel model;
model.setSourceModel(&sfpm); model.setSourceModel(&sfpm);
model.detach(); model.desyncOrder();
model.move(2, 1); model.move(2, 1);
sourceModelCopy.move(2, 1); sourceModelCopy.move(2, 1);
@ -340,7 +340,7 @@ private slots:
MovableModel model; MovableModel model;
model.setSourceModel(sourceModel); model.setSourceModel(sourceModel);
model.detach(); model.desyncOrder();
model.move(4, 1); model.move(4, 1);
SnapshotModel snapshot(model); SnapshotModel snapshot(model);
@ -459,7 +459,7 @@ private slots:
MovableModel model; MovableModel model;
model.setSourceModel(sourceModel); model.setSourceModel(sourceModel);
model.detach(); model.desyncOrder();
ModelSignalsSpy signalsSpy(&model); ModelSignalsSpy signalsSpy(&model);
@ -594,12 +594,12 @@ private slots:
MovableModel model; MovableModel model;
model.setSourceModel(sourceModel1); model.setSourceModel(sourceModel1);
model.detach(); model.desyncOrder();
QCOMPARE(model.detached(), true); QCOMPARE(model.synced(), false);
ModelSignalsSpy signalsSpy(&model); ModelSignalsSpy signalsSpy(&model);
QSignalSpy detachChangedSpy(&model, &MovableModel::detachedChanged); QSignalSpy syncedChangedSpy(&model, &MovableModel::syncedChanged);
model.setSourceModel(sourceModel2); model.setSourceModel(sourceModel2);
@ -607,14 +607,14 @@ private slots:
QCOMPARE(signalsSpy.modelAboutToBeResetSpy.count(), 1); QCOMPARE(signalsSpy.modelAboutToBeResetSpy.count(), 1);
QCOMPARE(signalsSpy.modelResetSpy.count(), 1); QCOMPARE(signalsSpy.modelResetSpy.count(), 1);
QCOMPARE(detachChangedSpy.count(), 1); QCOMPARE(syncedChangedSpy.count(), 1);
QCOMPARE(model.detached(), false); QCOMPARE(model.synced(), true);
QCOMPARE(model.rowCount(), 2); QCOMPARE(model.rowCount(), 2);
QVERIFY(isSame(&model, sourceModel2)); QVERIFY(isSame(&model, sourceModel2));
} }
void attachTest() void syncOrderTest()
{ {
QQmlEngine engine; QQmlEngine engine;
@ -632,7 +632,7 @@ private slots:
{ {
ModelSignalsSpy signalsSpy(&model); ModelSignalsSpy signalsSpy(&model);
model.attach(); model.syncOrder();
QCOMPARE(signalsSpy.count(), 0); QCOMPARE(signalsSpy.count(), 0);
} }
@ -640,26 +640,26 @@ private slots:
{ {
ModelSignalsSpy signalsSpy(&model); ModelSignalsSpy signalsSpy(&model);
model.attach(); model.syncOrder();
QCOMPARE(signalsSpy.count(), 0); QCOMPARE(signalsSpy.count(), 0);
} }
model.detach(); model.desyncOrder();
sourceModel.move(0, 2, 2); sourceModel.move(0, 2, 2);
QVERIFY(!isSame(&model, sourceModel)); QVERIFY(!isSame(&model, sourceModel));
ModelSignalsSpy signalsSpy(&model); ModelSignalsSpy signalsSpy(&model);
QSignalSpy detachChangedSpy(&model, &MovableModel::detachedChanged); QSignalSpy syncedChangedSpy(&model, &MovableModel::syncedChanged);
model.attach(); model.syncOrder();
QCOMPARE(signalsSpy.count(), 2); QCOMPARE(signalsSpy.count(), 2);
QCOMPARE(signalsSpy.layoutAboutToBeChangedSpy.count(), 1); QCOMPARE(signalsSpy.layoutAboutToBeChangedSpy.count(), 1);
QCOMPARE(signalsSpy.layoutChangedSpy.count(), 1); QCOMPARE(signalsSpy.layoutChangedSpy.count(), 1);
QCOMPARE(detachChangedSpy.count(), 1); QCOMPARE(syncedChangedSpy.count(), 1);
QCOMPARE(model.detached(), false); QCOMPARE(model.synced(), true);
QVERIFY(isSame(&model, sourceModel)); QVERIFY(isSame(&model, sourceModel));
} }