Commit a951e71f authored by Ingo Klöcker's avatar Ingo Klöcker
Browse files

Allow removing a group from the model

GnuPG-bug-id: 5175, 5239
parent b81d6c8c
......@@ -239,6 +239,34 @@ void AbstractKeyListModelTest::testSetData()
QCOMPARE( groupInModel.keys().size(), updatedGroup.keys().size() );
}
void AbstractKeyListModelTest::testRemoveGroup()
{
QScopedPointer<AbstractKeyListModel> model(createModel());
const KeyGroup group = createGroup("test");
model->setGroups({group});
{
const bool result = model->removeGroup(KeyGroup());
QVERIFY( !result );
QCOMPARE( model->rowCount(), 1 );
}
{
const KeyGroup otherGroup = createGroup("test2");
const bool result = model->removeGroup(otherGroup);
QVERIFY( !result );
QCOMPARE( model->rowCount(), 1 );
}
{
const bool result = model->removeGroup(group);
QVERIFY( result );
QCOMPARE( model->rowCount(), 0 );
}
}
void AbstractKeyListModelTest::testClear()
{
QScopedPointer<AbstractKeyListModel> model(createModel());
......
......@@ -30,6 +30,7 @@ private Q_SLOTS:
void testIndexForGroup();
void testAddGroup();
void testSetData();
void testRemoveGroup();
void testClear();
private:
......
......@@ -237,6 +237,14 @@ QModelIndex AbstractKeyListModel::addGroup(const KeyGroup &group)
return doAddGroup(group);
}
bool AbstractKeyListModel::removeGroup(const KeyGroup &group)
{
if (group.isNull()) {
return false;
}
return doRemoveGroup(group);
}
void AbstractKeyListModel::clear(ItemTypes types)
{
beginResetModel();
......@@ -536,6 +544,7 @@ private:
void doSetGroups(const std::vector<KeyGroup> &groups) override;
QModelIndex doAddGroup(const KeyGroup &group) override;
bool doSetGroupData(const QModelIndex &index, const KeyGroup &group) override;
bool doRemoveGroup(const KeyGroup &group) override;
void doClear(ItemTypes types) override
{
......@@ -600,6 +609,7 @@ private:
void doSetGroups(const std::vector<KeyGroup> &groups) override;
QModelIndex doAddGroup(const KeyGroup &group) override;
bool doSetGroupData(const QModelIndex &index, const KeyGroup &group) override;
bool doRemoveGroup(const KeyGroup &group) override;
void doClear(ItemTypes types) override
{
......@@ -793,6 +803,23 @@ bool FlatKeyListModel::doSetGroupData(const QModelIndex &index, const KeyGroup &
return true;
}
bool FlatKeyListModel::doRemoveGroup(const KeyGroup &group)
{
const QModelIndex modelIndex = doMapFromGroup(group, 0);
if (!modelIndex.isValid()) {
return false;
}
const int groupIndex = this->groupIndex(modelIndex);
Q_ASSERT(groupIndex != -1);
if (groupIndex == -1) {
return false;
}
beginRemoveRows(QModelIndex(), modelIndex.row(), modelIndex.row());
mGroups.erase(mGroups.begin() + groupIndex);
endRemoveRows();
return true;
}
HierarchicalKeyListModel::HierarchicalKeyListModel(QObject *p)
: AbstractKeyListModel(p),
mKeysByFingerprint(),
......@@ -1271,6 +1298,23 @@ bool HierarchicalKeyListModel::doSetGroupData(const QModelIndex &index, const Ke
return true;
}
bool HierarchicalKeyListModel::doRemoveGroup(const KeyGroup &group)
{
const QModelIndex modelIndex = doMapFromGroup(group, 0);
if (!modelIndex.isValid()) {
return false;
}
const int groupIndex = this->groupIndex(modelIndex);
Q_ASSERT(groupIndex != -1);
if (groupIndex == -1) {
return false;
}
beginRemoveRows(QModelIndex(), modelIndex.row(), modelIndex.row());
mGroups.erase(mGroups.begin() + groupIndex);
endRemoveRows();
return true;
}
void AbstractKeyListModel::useKeyCache(bool value, KeyList::Options options)
{
d->m_keyListOptions = options;
......
......@@ -74,6 +74,7 @@ public Q_SLOTS:
void setGroups(const std::vector<KeyGroup> &groups);
QModelIndex addGroup(const KeyGroup &group);
bool removeGroup(const KeyGroup &group);
void clear(ItemTypes types = All);
......@@ -113,6 +114,7 @@ private:
virtual void doSetGroups(const std::vector<KeyGroup> &groups) = 0;
virtual QModelIndex doAddGroup(const KeyGroup &group) = 0;
virtual bool doSetGroupData(const QModelIndex &index, const KeyGroup &group) = 0;
virtual bool doRemoveGroup(const KeyGroup &group) = 0;
virtual void doClear(ItemTypes types) = 0;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment