Introduce Group::aboutToMove() and moved() signals.

Emit them instead of remove/add when groups are moved within
a database.
This commit is contained in:
Felix Geyer 2012-04-23 16:57:08 +02:00
parent 6340ed8993
commit bddc29101c
10 changed files with 239 additions and 37 deletions

View file

@ -95,6 +95,8 @@ Q_SIGNALS:
void groupAdded();
void groupAboutToRemove(Group* group);
void groupRemoved();
void groupAboutToMove(Group* group, Group* toGroup, int index);
void groupMoved();
void modified();
private:

View file

@ -260,35 +260,47 @@ void Group::setParent(Group* parent, int index)
// setting a new parent for root groups is not allowed
Q_ASSERT(!m_db || (m_db->rootGroup() != this));
bool moveWithinDatabase = (m_db && m_db == parent->m_db);
if (index == -1) {
index = parent->children().size();
}
if (m_parent == parent && index == parent->children().indexOf(this)) {
if (m_parent == parent && parent->children().indexOf(this) == index) {
return;
}
cleanupParent();
m_parent = parent;
if (m_db != parent->m_db) {
recCreateDelObjects();
recSetDatabase(parent->m_db);
if (!moveWithinDatabase) {
cleanupParent();
m_parent = parent;
if (parent->m_db) {
recCreateDelObjects();
recSetDatabase(parent->m_db);
}
QObject::setParent(parent);
Q_EMIT aboutToAdd(this, index);
parent->m_children.insert(index, this);
}
else {
Q_EMIT aboutToMove(this, parent, index);
m_parent->m_children.removeAll(this);
m_parent = parent;
QObject::setParent(parent);
parent->m_children.insert(index, this);
}
QObject::setParent(parent);
Q_EMIT aboutToAdd(this, index);
parent->m_children.insert(index, this);
if (m_updateTimeinfo) {
m_timeInfo.setLocationChanged(QDateTime::currentDateTimeUtc());
}
Q_EMIT modified();
Q_EMIT added();
if (!moveWithinDatabase) {
Q_EMIT added();
}
else {
Q_EMIT moved();
}
}
void Group::setParent(Database* db)
@ -389,6 +401,8 @@ void Group::recSetDatabase(Database* db)
disconnect(SIGNAL(removed()), m_db);
disconnect(SIGNAL(aboutToAdd(Group*,int)), m_db);
disconnect(SIGNAL(added()), m_db);
disconnect(SIGNAL(aboutToMove(Group*,Group*,int)), m_db);
disconnect(SIGNAL(moved()), m_db);
disconnect(SIGNAL(modified()), m_db);
}
@ -404,6 +418,8 @@ void Group::recSetDatabase(Database* db)
connect(this, SIGNAL(removed()), db, SIGNAL(groupRemoved()));
connect(this, SIGNAL(aboutToAdd(Group*,int)), db, SIGNAL(groupAboutToAdd(Group*,int)));
connect(this, SIGNAL(added()), db, SIGNAL(groupAdded()));
connect(this, SIGNAL(aboutToMove(Group*,Group*,int)), db, SIGNAL(groupAboutToMove(Group*,Group*,int)));
connect(this, SIGNAL(moved()), db, SIGNAL(groupMoved()));
connect(this, SIGNAL(modified()), db, SIGNAL(modified()));
m_db = db;

View file

@ -85,6 +85,11 @@ Q_SIGNALS:
void added();
void aboutToRemove(Group* group);
void removed();
/**
* Group moved within the database.
*/
void aboutToMove(Group* group, Group* toGroup, int index);
void moved();
void entryAboutToAdd(Entry* entry);
void entryAdded();

View file

@ -29,6 +29,8 @@ GroupModel::GroupModel(Database* db, QObject* parent)
connect(db, SIGNAL(groupAdded()), SLOT(groupAdded()));
connect(db, SIGNAL(groupAboutToRemove(Group*)), SLOT(groupAboutToRemove(Group*)));
connect(db, SIGNAL(groupRemoved()), SLOT(groupRemoved()));
connect(db, SIGNAL(groupAboutToMove(Group*,Group*,int)), SLOT(groupAboutToMove(Group*,Group*,int)));
connect(db, SIGNAL(groupMoved()), SLOT(groupMoved()));
}
int GroupModel::rowCount(const QModelIndex& parent) const
@ -180,3 +182,24 @@ void GroupModel::groupAdded()
{
endInsertRows();
}
void GroupModel::groupAboutToMove(Group* group, Group* toGroup, int pos)
{
Q_ASSERT(group->parentGroup());
QModelIndex oldParentIndex = parent(group);
QModelIndex newParentIndex = index(toGroup);
int oldPos = group->parentGroup()->children().indexOf(group);
if (group->parentGroup() == toGroup && pos > oldPos) {
// beginMoveRows() has a bit different semantics than Group::setParent() and
// QList::move() when the new position is greater than the old
pos++;
}
beginMoveRows(oldParentIndex, oldPos, oldPos, newParentIndex, pos);
}
void GroupModel::groupMoved()
{
endMoveRows();
}

View file

@ -48,6 +48,8 @@ private Q_SLOTS:
void groupRemoved();
void groupAboutToAdd(Group* group, int index);
void groupAdded();
void groupAboutToMove(Group* group, Group* toGroup, int pos);
void groupMoved();
private:
Group* m_root;