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)));
QModelIndex index = history->index(0, 0);
QCOMPARE(index.data().toString(), fooText);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->next_uuid(), index.data(Qt::UserRole + 1).toByteArray());
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), index.data(Qt::UserRole + 1).toByteArray());
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), index.data(HistoryModel::UuidRole).toByteArray());
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), index.data(HistoryModel::UuidRole).toByteArray());
history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(barText)));
QCOMPARE(index.data().toString(), barText);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->next_uuid(), fooUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), fooUuid);
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), fooUuid);
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), fooUuid);
index = history->indexOf(fooUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->next_uuid(), barUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), barUuid);
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), barUuid);
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), barUuid);
history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(fooBarText)));
QCOMPARE(history->data(history->index(0, 0)).toString(), fooBarText);
index = history->index(0, 0);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->next_uuid(), barUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), fooUuid);
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), barUuid);
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), fooUuid);
index = history->indexOf(fooUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->next_uuid(), foobarUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), barUuid);
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), foobarUuid);
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), barUuid);
index = history->indexOf(barUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->next_uuid(), fooUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), foobarUuid);
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), fooUuid);
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), foobarUuid);
// insert one again - it should be moved to top
history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(barText)));
QCOMPARE(history->data(history->index(0, 0)).toString(), barText);
index = history->index(0, 0);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->next_uuid(), foobarUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), fooUuid);
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), foobarUuid);
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), fooUuid);
index = history->indexOf(fooUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->next_uuid(), barUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), foobarUuid);
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), barUuid);
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), foobarUuid);
index = history->indexOf(foobarUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->next_uuid(), fooUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), barUuid);
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), fooUuid);
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->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<HistoryItemConstPtr>()->next_uuid(), foobarUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), fooUuid);
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), foobarUuid);
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), fooUuid);
index = history->indexOf(fooUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->next_uuid(), barUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), foobarUuid);
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), barUuid);
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), foobarUuid);
index = history->indexOf(foobarUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->next_uuid(), fooUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), barUuid);
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), fooUuid);
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->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<HistoryItemConstPtr>()->next_uuid(), barUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), fooUuid);
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), barUuid);
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), fooUuid);
index = history->indexOf(fooUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->next_uuid(), foobarUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), barUuid);
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), foobarUuid);
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), barUuid);
index = history->indexOf(barUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->next_uuid(), fooUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), foobarUuid);
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), fooUuid);
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->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<HistoryItemConstPtr>()->next_uuid(), fooUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), fooUuid);
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), fooUuid);
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), fooUuid);
index = history->indexOf(fooUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->next_uuid(), barUuid);
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), barUuid);
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), barUuid);
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->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<HistoryItemConstPtr>()->next_uuid(), index.data(Qt::UserRole + 1).toByteArray());
QCOMPARE(index.data(Qt::UserRole).value<HistoryItemConstPtr>()->previous_uuid(), index.data(Qt::UserRole + 1).toByteArray());
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), index.data(HistoryModel::UuidRole).toByteArray());
QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->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<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)

@ -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<HistoryItemConstPtr>();
return m_model->index(1).data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>();
}
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<HistoryItemConstPtr>();
return m_model->index(m_model->rowCount() - 1).data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>();
}
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<HistoryItemConstPtr>();
return index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>();
}
bool History::empty() const
@ -222,5 +222,5 @@ HistoryItemConstPtr History::first() const
if (!index.isValid()) {
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;
}
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)

Loading…
Cancel
Save