StatusQ(MovableModel): Methods naming refactor, Storybook pages updated/improved
This commit is contained in:
parent
f747791f50
commit
b949fb7b3b
|
@ -119,7 +119,7 @@ Item {
|
|||
Layout.fillHeight: true
|
||||
|
||||
Label {
|
||||
text: "DETACHED-ORDER MODEL"
|
||||
text: "MOVABLE MODEL (press to drag&drop)"
|
||||
|
||||
font.bold: true
|
||||
font.pixelSize: 17
|
||||
|
@ -234,15 +234,23 @@ Item {
|
|||
}
|
||||
|
||||
Button {
|
||||
text: "detach order explicitely"
|
||||
text: "desynchronize"
|
||||
|
||||
onClicked: {
|
||||
movableModel.detach()
|
||||
movableModel.desyncOrder()
|
||||
}
|
||||
}
|
||||
|
||||
Button {
|
||||
text: "synchronize"
|
||||
|
||||
onClicked: {
|
||||
movableModel.syncOrder()
|
||||
}
|
||||
}
|
||||
|
||||
Label {
|
||||
text: "Detached: " + movableModel.detached
|
||||
text: "Synchronized: " + movableModel.synced
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -460,13 +460,19 @@ Item {
|
|||
}
|
||||
|
||||
Button {
|
||||
text: "detach order explicitely"
|
||||
text: "desynchronize"
|
||||
|
||||
onClicked: movableModel.detach()
|
||||
onClicked: movableModel.desyncOrder()
|
||||
}
|
||||
|
||||
Button {
|
||||
text: "synchronize"
|
||||
|
||||
onClicked: movableModel.syncOrder()
|
||||
}
|
||||
|
||||
Label {
|
||||
text: `Detached: <b>${movableModel.detached}</b>`
|
||||
text: `Synchronized: <b>${movableModel.synced}</b>`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ class MovableModel : public QAbstractListModel
|
|||
Q_PROPERTY(QAbstractItemModel* sourceModel READ sourceModel
|
||||
WRITE setSourceModel NOTIFY sourceModelChanged)
|
||||
|
||||
Q_PROPERTY(bool detached READ detached NOTIFY detachedChanged)
|
||||
Q_PROPERTY(bool synced READ synced NOTIFY syncedChanged)
|
||||
public:
|
||||
explicit MovableModel(QObject *parent = nullptr);
|
||||
|
||||
|
@ -22,16 +22,16 @@ public:
|
|||
QVariant data(const QModelIndex &index, int role) const override;
|
||||
QHash<int, QByteArray> roleNames() const override;
|
||||
|
||||
Q_INVOKABLE void detach();
|
||||
Q_INVOKABLE void attach();
|
||||
Q_INVOKABLE void desyncOrder();
|
||||
Q_INVOKABLE void syncOrder();
|
||||
Q_INVOKABLE void move(int from, int to, int count = 1);
|
||||
Q_INVOKABLE QVector<int> order() const;
|
||||
|
||||
bool detached() const;
|
||||
bool synced() const;
|
||||
|
||||
signals:
|
||||
void sourceModelChanged();
|
||||
void detachedChanged();
|
||||
void syncedChanged();
|
||||
|
||||
protected slots:
|
||||
void resetInternalData();
|
||||
|
@ -41,6 +41,6 @@ private:
|
|||
|
||||
void connectSignalsForAttachedState();
|
||||
|
||||
bool m_detached = false;
|
||||
bool m_synced = true;
|
||||
std::vector<QPersistentModelIndex> m_indexes;
|
||||
};
|
||||
|
|
|
@ -34,7 +34,7 @@ QAbstractItemModel* MovableModel::sourceModel() const
|
|||
|
||||
int MovableModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
if (m_detached)
|
||||
if (!m_synced)
|
||||
return m_indexes.size();
|
||||
|
||||
if (m_sourceModel == nullptr)
|
||||
|
@ -48,7 +48,7 @@ QVariant MovableModel::data(const QModelIndex &index, int role) const
|
|||
if (!checkIndex(index, CheckIndexOption::IndexIsValid))
|
||||
return {};
|
||||
|
||||
if (m_detached)
|
||||
if (!m_synced)
|
||||
return m_indexes.at(index.row()).data(role);
|
||||
|
||||
if (m_sourceModel == nullptr)
|
||||
|
@ -65,9 +65,9 @@ QHash<int, QByteArray> MovableModel::roleNames() const
|
|||
return m_sourceModel->roleNames();
|
||||
}
|
||||
|
||||
void MovableModel::detach()
|
||||
void MovableModel::desyncOrder()
|
||||
{
|
||||
if (m_detached || m_sourceModel == nullptr)
|
||||
if (!m_synced || m_sourceModel == nullptr)
|
||||
return;
|
||||
|
||||
disconnect(m_sourceModel, &QAbstractItemModel::rowsAboutToBeInserted, this,
|
||||
|
@ -175,13 +175,13 @@ void MovableModel::detach()
|
|||
for (auto i = 0; i < count; i++)
|
||||
m_indexes.emplace_back(m_sourceModel->index(i, 0));
|
||||
|
||||
m_detached = true;
|
||||
emit detachedChanged();
|
||||
m_synced = false;
|
||||
emit syncedChanged();
|
||||
}
|
||||
|
||||
void MovableModel::attach()
|
||||
void MovableModel::syncOrder()
|
||||
{
|
||||
if (!m_detached || m_sourceModel == nullptr)
|
||||
if (m_synced || m_sourceModel == nullptr)
|
||||
return;
|
||||
|
||||
emit layoutAboutToBeChanged();
|
||||
|
@ -212,8 +212,8 @@ void MovableModel::move(int from, int to, int count)
|
|||
const int sourceLast = from + count - 1;
|
||||
const int destinationRow = to < from ? to : to + count;
|
||||
|
||||
if (!m_detached)
|
||||
detach();
|
||||
if (m_synced)
|
||||
desyncOrder();
|
||||
|
||||
beginMoveRows({}, sourceFirst, sourceLast, {}, destinationRow);
|
||||
|
||||
|
@ -236,7 +236,7 @@ QVector<int> MovableModel::order() const
|
|||
{
|
||||
QVector<int> order(rowCount());
|
||||
|
||||
if (m_detached)
|
||||
if (!m_synced)
|
||||
std::transform(m_indexes.begin(), m_indexes.end(), order.begin(),
|
||||
[](auto& idx) { return idx.row(); });
|
||||
else
|
||||
|
@ -245,9 +245,9 @@ QVector<int> MovableModel::order() const
|
|||
return order;
|
||||
}
|
||||
|
||||
bool MovableModel::detached() const
|
||||
bool MovableModel::synced() const
|
||||
{
|
||||
return m_detached;
|
||||
return m_synced;
|
||||
}
|
||||
|
||||
void MovableModel::resetInternalData()
|
||||
|
@ -256,9 +256,9 @@ void MovableModel::resetInternalData()
|
|||
|
||||
m_indexes.clear();
|
||||
|
||||
if (m_detached) {
|
||||
m_detached = false;
|
||||
emit detachedChanged();
|
||||
if (!m_synced) {
|
||||
m_synced = true;
|
||||
emit syncedChanged();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -47,11 +47,11 @@ private slots:
|
|||
MovableModel model;
|
||||
model.setSourceModel(sourceModel);
|
||||
|
||||
QCOMPARE(model.detached(), false);
|
||||
QCOMPARE(model.synced(), true);
|
||||
QVERIFY(isSame(&model, sourceModel));
|
||||
}
|
||||
|
||||
void detachTest()
|
||||
void desyncOrderTest()
|
||||
{
|
||||
QQmlEngine engine;
|
||||
|
||||
|
@ -67,16 +67,16 @@ private slots:
|
|||
MovableModel model;
|
||||
model.setSourceModel(sourceModel);
|
||||
|
||||
QSignalSpy detachChangedSpy(&model, &MovableModel::detachedChanged);
|
||||
model.detach();
|
||||
QSignalSpy syncedChangedSpy(&model, &MovableModel::syncedChanged);
|
||||
model.desyncOrder();
|
||||
|
||||
QCOMPARE(detachChangedSpy.count(), 1);
|
||||
QCOMPARE(model.detached(), true);
|
||||
QCOMPARE(syncedChangedSpy.count(), 1);
|
||||
QCOMPARE(model.synced(), false);
|
||||
QVERIFY(isSame(&model, sourceModel));
|
||||
|
||||
model.setSourceModel(nullptr);
|
||||
QCOMPARE(detachChangedSpy.count(), 2);
|
||||
QCOMPARE(model.detached(), false);
|
||||
QCOMPARE(syncedChangedSpy.count(), 2);
|
||||
QCOMPARE(model.synced(), true);
|
||||
QCOMPARE(model.rowCount(), 0);
|
||||
}
|
||||
|
||||
|
@ -98,7 +98,7 @@ private slots:
|
|||
|
||||
MovableModel model;
|
||||
model.setSourceModel(sourceModel);
|
||||
model.detach();
|
||||
model.desyncOrder();
|
||||
|
||||
ModelSignalsSpy signalsSpy(&model);
|
||||
ModelSignalsSpy referenceSignalsSpy(sourceModelCopy);
|
||||
|
@ -150,7 +150,7 @@ private slots:
|
|||
|
||||
MovableModel model;
|
||||
model.setSourceModel(sourceModel);
|
||||
model.detach();
|
||||
model.desyncOrder();
|
||||
|
||||
ModelSignalsSpy signalsSpy(&model);
|
||||
ModelSignalsSpy referenceSignalsSpy(sourceModelCopy);
|
||||
|
@ -202,7 +202,7 @@ private slots:
|
|||
|
||||
MovableModel model;
|
||||
model.setSourceModel(sourceModel);
|
||||
model.detach();
|
||||
model.desyncOrder();
|
||||
|
||||
ModelSignalsSpy signalsSpy(&model);
|
||||
ModelSignalsSpy referenceSignalsSpy(sourceModelCopy);
|
||||
|
@ -249,7 +249,7 @@ private slots:
|
|||
|
||||
MovableModel model;
|
||||
model.setSourceModel(sourceModel);
|
||||
model.detach();
|
||||
model.desyncOrder();
|
||||
|
||||
ModelSignalsSpy signalsSpy(&model);
|
||||
ModelSignalsSpy referenceSignalsSpy(sourceModelCopy);
|
||||
|
@ -299,7 +299,7 @@ private slots:
|
|||
|
||||
MovableModel model;
|
||||
model.setSourceModel(&sfpm);
|
||||
model.detach();
|
||||
model.desyncOrder();
|
||||
|
||||
model.move(2, 1);
|
||||
sourceModelCopy.move(2, 1);
|
||||
|
@ -340,7 +340,7 @@ private slots:
|
|||
|
||||
MovableModel model;
|
||||
model.setSourceModel(sourceModel);
|
||||
model.detach();
|
||||
model.desyncOrder();
|
||||
model.move(4, 1);
|
||||
|
||||
SnapshotModel snapshot(model);
|
||||
|
@ -459,7 +459,7 @@ private slots:
|
|||
|
||||
MovableModel model;
|
||||
model.setSourceModel(sourceModel);
|
||||
model.detach();
|
||||
model.desyncOrder();
|
||||
|
||||
ModelSignalsSpy signalsSpy(&model);
|
||||
|
||||
|
@ -594,12 +594,12 @@ private slots:
|
|||
|
||||
MovableModel model;
|
||||
model.setSourceModel(sourceModel1);
|
||||
model.detach();
|
||||
model.desyncOrder();
|
||||
|
||||
QCOMPARE(model.detached(), true);
|
||||
QCOMPARE(model.synced(), false);
|
||||
|
||||
ModelSignalsSpy signalsSpy(&model);
|
||||
QSignalSpy detachChangedSpy(&model, &MovableModel::detachedChanged);
|
||||
QSignalSpy syncedChangedSpy(&model, &MovableModel::syncedChanged);
|
||||
|
||||
model.setSourceModel(sourceModel2);
|
||||
|
||||
|
@ -607,14 +607,14 @@ private slots:
|
|||
QCOMPARE(signalsSpy.modelAboutToBeResetSpy.count(), 1);
|
||||
QCOMPARE(signalsSpy.modelResetSpy.count(), 1);
|
||||
|
||||
QCOMPARE(detachChangedSpy.count(), 1);
|
||||
QCOMPARE(model.detached(), false);
|
||||
QCOMPARE(syncedChangedSpy.count(), 1);
|
||||
QCOMPARE(model.synced(), true);
|
||||
QCOMPARE(model.rowCount(), 2);
|
||||
|
||||
QVERIFY(isSame(&model, sourceModel2));
|
||||
}
|
||||
|
||||
void attachTest()
|
||||
void syncOrderTest()
|
||||
{
|
||||
QQmlEngine engine;
|
||||
|
||||
|
@ -632,7 +632,7 @@ private slots:
|
|||
|
||||
{
|
||||
ModelSignalsSpy signalsSpy(&model);
|
||||
model.attach();
|
||||
model.syncOrder();
|
||||
QCOMPARE(signalsSpy.count(), 0);
|
||||
}
|
||||
|
||||
|
@ -640,26 +640,26 @@ private slots:
|
|||
|
||||
{
|
||||
ModelSignalsSpy signalsSpy(&model);
|
||||
model.attach();
|
||||
model.syncOrder();
|
||||
QCOMPARE(signalsSpy.count(), 0);
|
||||
}
|
||||
|
||||
model.detach();
|
||||
model.desyncOrder();
|
||||
sourceModel.move(0, 2, 2);
|
||||
|
||||
QVERIFY(!isSame(&model, sourceModel));
|
||||
|
||||
ModelSignalsSpy signalsSpy(&model);
|
||||
QSignalSpy detachChangedSpy(&model, &MovableModel::detachedChanged);
|
||||
QSignalSpy syncedChangedSpy(&model, &MovableModel::syncedChanged);
|
||||
|
||||
model.attach();
|
||||
model.syncOrder();
|
||||
|
||||
QCOMPARE(signalsSpy.count(), 2);
|
||||
QCOMPARE(signalsSpy.layoutAboutToBeChangedSpy.count(), 1);
|
||||
QCOMPARE(signalsSpy.layoutChangedSpy.count(), 1);
|
||||
|
||||
QCOMPARE(detachChangedSpy.count(), 1);
|
||||
QCOMPARE(model.detached(), false);
|
||||
QCOMPARE(syncedChangedSpy.count(), 1);
|
||||
QCOMPARE(model.synced(), true);
|
||||
|
||||
QVERIFY(isSame(&model, sourceModel));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue