klipper: Replace UserRole with named types

This makes the expressions more clear.
wilder-5.25
Fushan Wen 4 years ago
parent 87faa4a6f5
commit e76c838785
No known key found for this signature in database
GPG Key ID: 2E48D1487C91DCAA
  1. 76
      klipper/autotests/historymodeltest.cpp
  2. 16
      klipper/history.cpp
  3. 4
      klipper/historyitem.cpp

@ -80,84 +80,84 @@ void HistoryModelTest::testInsertRemove()
history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(fooText))); history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(fooText)));
QModelIndex index = history->index(0, 0); QModelIndex index = history->index(0, 0);
QCOMPARE(index.data().toString(), fooText); QCOMPARE(index.data().toString(), fooText);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->next_uuid(), index.data(Qt::UserRole + 1).toByteArray()); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), index.data(HistoryModel::UuidRole).toByteArray());
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), index.data(Qt::UserRole + 1).toByteArray()); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), index.data(HistoryModel::UuidRole).toByteArray());
history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(barText))); history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(barText)));
QCOMPARE(index.data().toString(), barText); QCOMPARE(index.data().toString(), barText);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->next_uuid(), fooUuid); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), fooUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), fooUuid); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), fooUuid);
index = history->indexOf(fooUuid); index = history->indexOf(fooUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->next_uuid(), barUuid); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), barUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), barUuid); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), barUuid);
history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(fooBarText))); history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(fooBarText)));
QCOMPARE(history->data(history->index(0, 0)).toString(), fooBarText); QCOMPARE(history->data(history->index(0, 0)).toString(), fooBarText);
index = history->index(0, 0); index = history->index(0, 0);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->next_uuid(), barUuid); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), barUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), fooUuid); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), fooUuid);
index = history->indexOf(fooUuid); index = history->indexOf(fooUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->next_uuid(), foobarUuid); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), foobarUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), barUuid); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), barUuid);
index = history->indexOf(barUuid); index = history->indexOf(barUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->next_uuid(), fooUuid); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), fooUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), foobarUuid); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), foobarUuid);
// insert one again - it should be moved to top // insert one again - it should be moved to top
history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(barText))); history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(barText)));
QCOMPARE(history->data(history->index(0, 0)).toString(), barText); QCOMPARE(history->data(history->index(0, 0)).toString(), barText);
index = history->index(0, 0); index = history->index(0, 0);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->next_uuid(), foobarUuid); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), foobarUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), fooUuid); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), fooUuid);
index = history->indexOf(fooUuid); index = history->indexOf(fooUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->next_uuid(), barUuid); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), barUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), foobarUuid); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), foobarUuid);
index = history->indexOf(foobarUuid); index = history->indexOf(foobarUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->next_uuid(), fooUuid); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), fooUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), barUuid); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), barUuid);
// move one to top using the slot // move one to top using the slot
// already on top, shouldn't change anything // already on top, shouldn't change anything
history->moveToTop(barUuid); history->moveToTop(barUuid);
QCOMPARE(history->data(history->index(0, 0)).toString(), barText); QCOMPARE(history->data(history->index(0, 0)).toString(), barText);
index = history->index(0, 0); index = history->index(0, 0);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->next_uuid(), foobarUuid); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), foobarUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), fooUuid); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), fooUuid);
index = history->indexOf(fooUuid); index = history->indexOf(fooUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->next_uuid(), barUuid); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), barUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), foobarUuid); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), foobarUuid);
index = history->indexOf(foobarUuid); index = history->indexOf(foobarUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->next_uuid(), fooUuid); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), fooUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), barUuid); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), barUuid);
// another one should change, though // another one should change, though
history->moveToTop(foobarUuid); history->moveToTop(foobarUuid);
QCOMPARE(history->data(history->index(0, 0)).toString(), fooBarText); QCOMPARE(history->data(history->index(0, 0)).toString(), fooBarText);
index = history->index(0, 0); index = history->index(0, 0);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->next_uuid(), barUuid); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), barUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), fooUuid); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), fooUuid);
index = history->indexOf(fooUuid); index = history->indexOf(fooUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->next_uuid(), foobarUuid); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), foobarUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), barUuid); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), barUuid);
index = history->indexOf(barUuid); index = history->indexOf(barUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->next_uuid(), fooUuid); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), fooUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), foobarUuid); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), foobarUuid);
// remove them again // remove them again
QVERIFY(history->remove(foobarUuid)); QVERIFY(history->remove(foobarUuid));
QCOMPARE(history->data(history->index(0, 0)).toString(), barText); QCOMPARE(history->data(history->index(0, 0)).toString(), barText);
index = history->index(0, 0); index = history->index(0, 0);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->next_uuid(), fooUuid); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), fooUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), fooUuid); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), fooUuid);
index = history->indexOf(fooUuid); index = history->indexOf(fooUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->next_uuid(), barUuid); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), barUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), barUuid); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), barUuid);
QVERIFY(history->remove(barUuid)); QVERIFY(history->remove(barUuid));
QCOMPARE(history->data(history->index(0, 0)).toString(), fooText); QCOMPARE(history->data(history->index(0, 0)).toString(), fooText);
index = history->index(0, 0); index = history->index(0, 0);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->next_uuid(), index.data(Qt::UserRole + 1).toByteArray()); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), index.data(HistoryModel::UuidRole).toByteArray());
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), index.data(Qt::UserRole + 1).toByteArray()); QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), index.data(HistoryModel::UuidRole).toByteArray());
QVERIFY(history->remove(fooUuid)); QVERIFY(history->remove(fooUuid));
QCOMPARE(history->rowCount(), 0); QCOMPARE(history->rowCount(), 0);
@ -200,7 +200,7 @@ void HistoryModelTest::testIndexOf()
QVERIFY(!history->indexOf(QByteArray()).isValid()); QVERIFY(!history->indexOf(QByteArray()).isValid());
const QByteArray fooUuid = QCryptographicHash::hash(QByteArrayLiteral("foo"), QCryptographicHash::Sha1); const QByteArray fooUuid = QCryptographicHash::hash(QByteArrayLiteral("foo"), QCryptographicHash::Sha1);
QVERIFY(history->indexOf(fooUuid).isValid()); 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(); history->clear();
QVERIFY(!history->indexOf(fooUuid).isValid()); QVERIFY(!history->indexOf(fooUuid).isValid());
@ -229,7 +229,7 @@ void HistoryModelTest::testType()
QFETCH(HistoryItem *, item); QFETCH(HistoryItem *, item);
QFETCH(HistoryItemType, expectedType); QFETCH(HistoryItemType, expectedType);
history->insert(QSharedPointer<HistoryItem>(item)); history->insert(QSharedPointer<HistoryItem>(item));
QCOMPARE(history->index(0).data(Qt::UserRole + 2).value<HistoryItemType>(), expectedType); QCOMPARE(history->index(0).data(HistoryModel::TypeRole).value<HistoryItemType>(), expectedType);
} }
QTEST_MAIN(HistoryModelTest) QTEST_MAIN(HistoryModelTest)

@ -154,8 +154,8 @@ void History::cycleNext()
} }
if (m_cycleStartUuid.isEmpty()) { if (m_cycleStartUuid.isEmpty()) {
m_cycleStartUuid = m_model->index(0).data(Qt::UserRole + 1).toByteArray(); m_cycleStartUuid = m_model->index(0).data(HistoryModel::UuidRole).toByteArray();
} else if (m_cycleStartUuid == m_model->index(1).data(Qt::UserRole + 1).toByteArray()) { } else if (m_cycleStartUuid == m_model->index(1).data(HistoryModel::UuidRole).toByteArray()) {
// end of cycle // end of cycle
return; return;
} }
@ -170,7 +170,7 @@ void History::cyclePrev()
} }
CycleBlocker blocker; CycleBlocker blocker;
m_model->moveBackToTop(); 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(); m_cycleStartUuid = QByteArray();
} }
} }
@ -180,11 +180,11 @@ HistoryItemConstPtr History::nextInCycle() const
if (m_model->hasIndex(1, 0)) { if (m_model->hasIndex(1, 0)) {
if (!m_cycleStartUuid.isEmpty()) { if (!m_cycleStartUuid.isEmpty()) {
// check whether we are not at the end // 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 HistoryItemConstPtr();
} }
} }
return m_model->index(1).data(Qt::UserRole).value<HistoryItemConstPtr>(); return m_model->index(1).data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>();
} }
return HistoryItemConstPtr(); return HistoryItemConstPtr();
} }
@ -194,7 +194,7 @@ HistoryItemConstPtr History::prevInCycle() const
if (m_cycleStartUuid.isEmpty()) { if (m_cycleStartUuid.isEmpty()) {
return HistoryItemConstPtr(); return HistoryItemConstPtr();
} }
return m_model->index(m_model->rowCount() - 1).data(Qt::UserRole).value<HistoryItemConstPtr>(); return m_model->index(m_model->rowCount() - 1).data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>();
} }
HistoryItemConstPtr History::find(const QByteArray &uuid) const HistoryItemConstPtr History::find(const QByteArray &uuid) const
@ -203,7 +203,7 @@ HistoryItemConstPtr History::find(const QByteArray &uuid) const
if (!index.isValid()) { if (!index.isValid()) {
return HistoryItemConstPtr(); return HistoryItemConstPtr();
} }
return index.data(Qt::UserRole).value<HistoryItemConstPtr>(); return index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>();
} }
bool History::empty() const bool History::empty() const
@ -222,5 +222,5 @@ HistoryItemConstPtr History::first() const
if (!index.isValid()) { if (!index.isValid()) {
return HistoryItemConstPtr(); return HistoryItemConstPtr();
} }
return index.data(Qt::UserRole).value<HistoryItemConstPtr>(); return index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>();
} }

@ -97,7 +97,7 @@ QByteArray HistoryItem::next_uuid() const
return m_uuid; return m_uuid;
} }
const int nextRow = (ownIndex.row() + 1) % m_model->rowCount(); 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 QByteArray HistoryItem::previous_uuid() const
@ -112,7 +112,7 @@ QByteArray HistoryItem::previous_uuid() const
return m_uuid; return m_uuid;
} }
const int nextRow = ((ownIndex.row() == 0) ? m_model->rowCount() : ownIndex.row()) - 1; 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) void HistoryItem::setModel(HistoryModel *model)

Loading…
Cancel
Save