From e76c838785c21bf6c2846df42a128c926f2d2a84 Mon Sep 17 00:00:00 2001 From: Fushan Wen Date: Tue, 18 Jan 2022 22:19:36 +0800 Subject: [PATCH] klipper: Replace UserRole with named types This makes the expressions more clear. --- klipper/autotests/historymodeltest.cpp | 76 +++++++++++++------------- klipper/history.cpp | 16 +++--- klipper/historyitem.cpp | 4 +- 3 files changed, 48 insertions(+), 48 deletions(-) diff --git a/klipper/autotests/historymodeltest.cpp b/klipper/autotests/historymodeltest.cpp index eef5bd6f4..2c802572d 100644 --- a/klipper/autotests/historymodeltest.cpp +++ b/klipper/autotests/historymodeltest.cpp @@ -80,84 +80,84 @@ void HistoryModelTest::testInsertRemove() history->insert(QSharedPointer(new HistoryStringItem(fooText))); QModelIndex index = history->index(0, 0); QCOMPARE(index.data().toString(), fooText); - QCOMPARE(index.data(Qt::UserRole).value()->next_uuid(), index.data(Qt::UserRole + 1).toByteArray()); - QCOMPARE(index.data(Qt::UserRole).value()->previous_uuid(), index.data(Qt::UserRole + 1).toByteArray()); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->next_uuid(), index.data(HistoryModel::UuidRole).toByteArray()); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->previous_uuid(), index.data(HistoryModel::UuidRole).toByteArray()); history->insert(QSharedPointer(new HistoryStringItem(barText))); QCOMPARE(index.data().toString(), barText); - QCOMPARE(index.data(Qt::UserRole).value()->next_uuid(), fooUuid); - QCOMPARE(index.data(Qt::UserRole).value()->previous_uuid(), fooUuid); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->next_uuid(), fooUuid); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->previous_uuid(), fooUuid); index = history->indexOf(fooUuid); - QCOMPARE(index.data(Qt::UserRole).value()->next_uuid(), barUuid); - QCOMPARE(index.data(Qt::UserRole).value()->previous_uuid(), barUuid); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->next_uuid(), barUuid); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->previous_uuid(), barUuid); history->insert(QSharedPointer(new HistoryStringItem(fooBarText))); QCOMPARE(history->data(history->index(0, 0)).toString(), fooBarText); index = history->index(0, 0); - QCOMPARE(index.data(Qt::UserRole).value()->next_uuid(), barUuid); - QCOMPARE(index.data(Qt::UserRole).value()->previous_uuid(), fooUuid); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->next_uuid(), barUuid); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->previous_uuid(), fooUuid); index = history->indexOf(fooUuid); - QCOMPARE(index.data(Qt::UserRole).value()->next_uuid(), foobarUuid); - QCOMPARE(index.data(Qt::UserRole).value()->previous_uuid(), barUuid); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->next_uuid(), foobarUuid); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->previous_uuid(), barUuid); index = history->indexOf(barUuid); - QCOMPARE(index.data(Qt::UserRole).value()->next_uuid(), fooUuid); - QCOMPARE(index.data(Qt::UserRole).value()->previous_uuid(), foobarUuid); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->next_uuid(), fooUuid); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->previous_uuid(), foobarUuid); // insert one again - it should be moved to top history->insert(QSharedPointer(new HistoryStringItem(barText))); QCOMPARE(history->data(history->index(0, 0)).toString(), barText); index = history->index(0, 0); - QCOMPARE(index.data(Qt::UserRole).value()->next_uuid(), foobarUuid); - QCOMPARE(index.data(Qt::UserRole).value()->previous_uuid(), fooUuid); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->next_uuid(), foobarUuid); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->previous_uuid(), fooUuid); index = history->indexOf(fooUuid); - QCOMPARE(index.data(Qt::UserRole).value()->next_uuid(), barUuid); - QCOMPARE(index.data(Qt::UserRole).value()->previous_uuid(), foobarUuid); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->next_uuid(), barUuid); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->previous_uuid(), foobarUuid); index = history->indexOf(foobarUuid); - QCOMPARE(index.data(Qt::UserRole).value()->next_uuid(), fooUuid); - QCOMPARE(index.data(Qt::UserRole).value()->previous_uuid(), barUuid); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->next_uuid(), fooUuid); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->previous_uuid(), barUuid); // move one to top using the slot // already on top, shouldn't change anything history->moveToTop(barUuid); QCOMPARE(history->data(history->index(0, 0)).toString(), barText); index = history->index(0, 0); - QCOMPARE(index.data(Qt::UserRole).value()->next_uuid(), foobarUuid); - QCOMPARE(index.data(Qt::UserRole).value()->previous_uuid(), fooUuid); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->next_uuid(), foobarUuid); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->previous_uuid(), fooUuid); index = history->indexOf(fooUuid); - QCOMPARE(index.data(Qt::UserRole).value()->next_uuid(), barUuid); - QCOMPARE(index.data(Qt::UserRole).value()->previous_uuid(), foobarUuid); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->next_uuid(), barUuid); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->previous_uuid(), foobarUuid); index = history->indexOf(foobarUuid); - QCOMPARE(index.data(Qt::UserRole).value()->next_uuid(), fooUuid); - QCOMPARE(index.data(Qt::UserRole).value()->previous_uuid(), barUuid); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->next_uuid(), fooUuid); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->previous_uuid(), barUuid); // another one should change, though history->moveToTop(foobarUuid); QCOMPARE(history->data(history->index(0, 0)).toString(), fooBarText); index = history->index(0, 0); - QCOMPARE(index.data(Qt::UserRole).value()->next_uuid(), barUuid); - QCOMPARE(index.data(Qt::UserRole).value()->previous_uuid(), fooUuid); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->next_uuid(), barUuid); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->previous_uuid(), fooUuid); index = history->indexOf(fooUuid); - QCOMPARE(index.data(Qt::UserRole).value()->next_uuid(), foobarUuid); - QCOMPARE(index.data(Qt::UserRole).value()->previous_uuid(), barUuid); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->next_uuid(), foobarUuid); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->previous_uuid(), barUuid); index = history->indexOf(barUuid); - QCOMPARE(index.data(Qt::UserRole).value()->next_uuid(), fooUuid); - QCOMPARE(index.data(Qt::UserRole).value()->previous_uuid(), foobarUuid); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->next_uuid(), fooUuid); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->previous_uuid(), foobarUuid); // remove them again QVERIFY(history->remove(foobarUuid)); QCOMPARE(history->data(history->index(0, 0)).toString(), barText); index = history->index(0, 0); - QCOMPARE(index.data(Qt::UserRole).value()->next_uuid(), fooUuid); - QCOMPARE(index.data(Qt::UserRole).value()->previous_uuid(), fooUuid); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->next_uuid(), fooUuid); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->previous_uuid(), fooUuid); index = history->indexOf(fooUuid); - QCOMPARE(index.data(Qt::UserRole).value()->next_uuid(), barUuid); - QCOMPARE(index.data(Qt::UserRole).value()->previous_uuid(), barUuid); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->next_uuid(), barUuid); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->previous_uuid(), barUuid); QVERIFY(history->remove(barUuid)); QCOMPARE(history->data(history->index(0, 0)).toString(), fooText); index = history->index(0, 0); - QCOMPARE(index.data(Qt::UserRole).value()->next_uuid(), index.data(Qt::UserRole + 1).toByteArray()); - QCOMPARE(index.data(Qt::UserRole).value()->previous_uuid(), index.data(Qt::UserRole + 1).toByteArray()); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->next_uuid(), index.data(HistoryModel::UuidRole).toByteArray()); + QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value()->previous_uuid(), index.data(HistoryModel::UuidRole).toByteArray()); QVERIFY(history->remove(fooUuid)); QCOMPARE(history->rowCount(), 0); @@ -200,7 +200,7 @@ void HistoryModelTest::testIndexOf() QVERIFY(!history->indexOf(QByteArray()).isValid()); const QByteArray fooUuid = QCryptographicHash::hash(QByteArrayLiteral("foo"), QCryptographicHash::Sha1); QVERIFY(history->indexOf(fooUuid).isValid()); - QCOMPARE(history->indexOf(fooUuid).data(Qt::UserRole + 1).toByteArray(), fooUuid); + QCOMPARE(history->indexOf(fooUuid).data(HistoryModel::UuidRole).toByteArray(), fooUuid); history->clear(); QVERIFY(!history->indexOf(fooUuid).isValid()); @@ -229,7 +229,7 @@ void HistoryModelTest::testType() QFETCH(HistoryItem *, item); QFETCH(HistoryItemType, expectedType); history->insert(QSharedPointer(item)); - QCOMPARE(history->index(0).data(Qt::UserRole + 2).value(), expectedType); + QCOMPARE(history->index(0).data(HistoryModel::TypeRole).value(), expectedType); } QTEST_MAIN(HistoryModelTest) diff --git a/klipper/history.cpp b/klipper/history.cpp index 3675d5bd9..bbb895908 100644 --- a/klipper/history.cpp +++ b/klipper/history.cpp @@ -154,8 +154,8 @@ void History::cycleNext() } if (m_cycleStartUuid.isEmpty()) { - m_cycleStartUuid = m_model->index(0).data(Qt::UserRole + 1).toByteArray(); - } else if (m_cycleStartUuid == m_model->index(1).data(Qt::UserRole + 1).toByteArray()) { + m_cycleStartUuid = m_model->index(0).data(HistoryModel::UuidRole).toByteArray(); + } else if (m_cycleStartUuid == m_model->index(1).data(HistoryModel::UuidRole).toByteArray()) { // end of cycle return; } @@ -170,7 +170,7 @@ void History::cyclePrev() } CycleBlocker blocker; m_model->moveBackToTop(); - if (m_cycleStartUuid == m_model->index(0).data(Qt::UserRole + 1).toByteArray()) { + if (m_cycleStartUuid == m_model->index(0).data(HistoryModel::UuidRole).toByteArray()) { m_cycleStartUuid = QByteArray(); } } @@ -180,11 +180,11 @@ HistoryItemConstPtr History::nextInCycle() const if (m_model->hasIndex(1, 0)) { if (!m_cycleStartUuid.isEmpty()) { // check whether we are not at the end - if (m_cycleStartUuid == m_model->index(1).data(Qt::UserRole + 1).toByteArray()) { + if (m_cycleStartUuid == m_model->index(1).data(HistoryModel::UuidRole).toByteArray()) { return HistoryItemConstPtr(); } } - return m_model->index(1).data(Qt::UserRole).value(); + return m_model->index(1).data(HistoryModel::HistoryItemConstPtrRole).value(); } return HistoryItemConstPtr(); } @@ -194,7 +194,7 @@ HistoryItemConstPtr History::prevInCycle() const if (m_cycleStartUuid.isEmpty()) { return HistoryItemConstPtr(); } - return m_model->index(m_model->rowCount() - 1).data(Qt::UserRole).value(); + return m_model->index(m_model->rowCount() - 1).data(HistoryModel::HistoryItemConstPtrRole).value(); } HistoryItemConstPtr History::find(const QByteArray &uuid) const @@ -203,7 +203,7 @@ HistoryItemConstPtr History::find(const QByteArray &uuid) const if (!index.isValid()) { return HistoryItemConstPtr(); } - return index.data(Qt::UserRole).value(); + return index.data(HistoryModel::HistoryItemConstPtrRole).value(); } bool History::empty() const @@ -222,5 +222,5 @@ HistoryItemConstPtr History::first() const if (!index.isValid()) { return HistoryItemConstPtr(); } - return index.data(Qt::UserRole).value(); + return index.data(HistoryModel::HistoryItemConstPtrRole).value(); } diff --git a/klipper/historyitem.cpp b/klipper/historyitem.cpp index 7ee2496f3..ffe231602 100644 --- a/klipper/historyitem.cpp +++ b/klipper/historyitem.cpp @@ -97,7 +97,7 @@ QByteArray HistoryItem::next_uuid() const return m_uuid; } const int nextRow = (ownIndex.row() + 1) % m_model->rowCount(); - return m_model->index(nextRow, 0).data(Qt::UserRole + 1).toByteArray(); + return m_model->index(nextRow, 0).data(HistoryModel::UuidRole).toByteArray(); } QByteArray HistoryItem::previous_uuid() const @@ -112,7 +112,7 @@ QByteArray HistoryItem::previous_uuid() const return m_uuid; } const int nextRow = ((ownIndex.row() == 0) ? m_model->rowCount() : ownIndex.row()) - 1; - return m_model->index(nextRow, 0).data(Qt::UserRole + 1).toByteArray(); + return m_model->index(nextRow, 0).data(HistoryModel::UuidRole).toByteArray(); } void HistoryItem::setModel(HistoryModel *model)