save/restore expanded items and selection

This commit is contained in:
csoler 2019-08-22 13:13:04 +02:00
parent 15e43dce01
commit 6a8f6bf93d
No known key found for this signature in database
GPG Key ID: 7BCA522266C0804C
3 changed files with 102 additions and 10 deletions

View File

@ -600,7 +600,7 @@ public:
QVariant RsFriendListModel::displayRole(const EntryIndex& e, int col) const QVariant RsFriendListModel::displayRole(const EntryIndex& e, int col) const
{ {
#ifndef DEBUG_MODEL #ifdef DEBUG_MODEL
std::cerr << " Display role " << e.type << ", (" << (int)e.group_index << ","<< (int)e.profile_index << ","<< (int)e.node_index << ") col="<< col<<": "; std::cerr << " Display role " << e.type << ", (" << (int)e.group_index << ","<< (int)e.profile_index << ","<< (int)e.node_index << ") col="<< col<<": ";
AutoEndel x; AutoEndel x;
#endif #endif
@ -627,7 +627,7 @@ QVariant RsFriendListModel::displayRole(const EntryIndex& e, int col) const
switch(col) switch(col)
{ {
case COLUMN_THREAD_NAME: case COLUMN_THREAD_NAME:
#ifndef DEBUG_MODEL #ifdef DEBUG_MODEL
std::cerr << group->group_info.name.c_str() ; std::cerr << group->group_info.name.c_str() ;
#endif #endif
@ -636,6 +636,7 @@ QVariant RsFriendListModel::displayRole(const EntryIndex& e, int col) const
else else
return QVariant(QString::fromUtf8(group->group_info.name.c_str())); return QVariant(QString::fromUtf8(group->group_info.name.c_str()));
case COLUMN_THREAD_ID: return QVariant(QString::fromStdString(group->group_info.id.toStdString()));
default: default:
return QVariant(); return QVariant();
} }
@ -649,7 +650,7 @@ QVariant RsFriendListModel::displayRole(const EntryIndex& e, int col) const
if(!profile) if(!profile)
return QVariant(); return QVariant();
#ifndef DEBUG_MODEL #ifdef DEBUG_MODEL
std::cerr << profile->profile_info.name.c_str() ; std::cerr << profile->profile_info.name.c_str() ;
#endif #endif
switch(col) switch(col)

View File

@ -325,6 +325,78 @@ void NewFriendList::addToolButton(QToolButton *toolButton)
ui->titleBarFrame->layout()->addWidget(toolButton); ui->titleBarFrame->layout()->addWidget(toolButton);
} }
void NewFriendList::saveExpandedPathsAndSelection(std::set<QString>& expanded_indexes, std::set<QString>& selected_indexes)
{
#ifdef DEBUG_NEW_FRIEND_LIST
std::cerr << "Saving expended paths and selection..." << std::endl;
#endif
for(int row = 0; row < mProxyModel->rowCount(); ++row)
recursSaveExpandedItems(mProxyModel->index(row,0),QString(),expanded_indexes,selected_indexes);
}
void NewFriendList::restoreExpandedPathsAndSelection(const std::set<QString>& expanded_indexes, const std::set<QString>& selected_indexes)
{
#ifdef DEBUG_NEW_FRIEND_LIST
std::cerr << "Restoring expended paths and selection..." << std::endl;
#endif
ui->peerTreeWidget->blockSignals(true) ;
for(int row = 0; row < mProxyModel->rowCount(); ++row)
recursRestoreExpandedItems(mProxyModel->index(row,0),QString(),expanded_indexes,selected_indexes);
ui->peerTreeWidget->blockSignals(false) ;
}
void NewFriendList::recursSaveExpandedItems(const QModelIndex& index,const QString& parent_path,std::set<QString>& exp, std::set<QString>& sel)
{
QString local_path = parent_path + index.sibling(index.row(),RsFriendListModel::COLUMN_THREAD_ID).data(Qt::DisplayRole).toString() + " ";
if(ui->peerTreeWidget->selectionModel()->selection().contains(index))
sel.insert(local_path) ;
if(ui->peerTreeWidget->isExpanded(index))
{
#ifdef DEBUG_NEW_FRIEND_LIST
std::cerr << "Index " << local_path.toStdString() << " is expanded." << std::endl;
#endif
if(index.isValid())
exp.insert(local_path) ;
for(int row=0;row<mProxyModel->rowCount(index);++row)
recursSaveExpandedItems(index.child(row,0),local_path,exp,sel) ;
}
#ifdef DEBUG_NEW_FRIEND_LIST
else
std::cerr << "Index " << local_path.toStdString() << " is not expanded." << std::endl;
#endif
}
void NewFriendList::recursRestoreExpandedItems(const QModelIndex& index, const QString& parent_path, const std::set<QString>& exp, const std::set<QString> &sel)
{
QString local_path = parent_path + index.sibling(index.row(),RsFriendListModel::COLUMN_THREAD_ID).data(Qt::DisplayRole).toString() + " ";
#ifdef DEBUG_NEW_FRIEND_LIST
std::cerr << "at index " << index.row() << ". data[1]=" << local_path.toStdString() << std::endl;
#endif
if(sel.find(local_path) != sel.end())
ui->peerTreeWidget->selectionModel()->select(index, QItemSelectionModel::Select | QItemSelectionModel::Rows);
if(exp.find(local_path) != exp.end())
{
#ifdef DEBUG_NEW_FRIEND_LIST
std::cerr << "re expanding index " << local_path.toStdString() << std::endl;
#endif
ui->peerTreeWidget->setExpanded(index,true) ;
for(int row=0;row<mProxyModel->rowCount(index);++row)
recursRestoreExpandedItems(index.child(row,0),local_path,exp,sel) ;
}
}
void NewFriendList::processSettings(bool load) void NewFriendList::processSettings(bool load)
{ {
// state of peer tree // state of peer tree
@ -968,16 +1040,16 @@ void NewFriendList::addToGroup()
// add to group // add to group
rsPeers->assignPeerToGroup(groupId, gpgId, true); rsPeers->assignPeerToGroup(groupId, gpgId, true);
mModel->checkInternalData(true); checkInternalData(true);
} }
void NewFriendList::forceUpdateDisplay() void NewFriendList::forceUpdateDisplay()
{ {
mModel->checkInternalData(true); checkInternalData(true);
} }
void NewFriendList::updateDisplay() void NewFriendList::updateDisplay()
{ {
mModel->checkInternalData(false); checkInternalData(false);
} }
void NewFriendList::moveToGroup() void NewFriendList::moveToGroup()
@ -1001,7 +1073,7 @@ void NewFriendList::moveToGroup()
// add to group // add to group
rsPeers->assignPeerToGroup(groupId, gpgId, true); rsPeers->assignPeerToGroup(groupId, gpgId, true);
mModel->checkInternalData(true); checkInternalData(true);
} }
void NewFriendList::removeFromGroup() void NewFriendList::removeFromGroup()
@ -1019,7 +1091,7 @@ void NewFriendList::removeFromGroup()
// remove from (all) group(s) // remove from (all) group(s)
rsPeers->assignPeerToGroup(groupId, gpgId, false); rsPeers->assignPeerToGroup(groupId, gpgId, false);
mModel->checkInternalData(true); checkInternalData(true);
} }
void NewFriendList::editGroup() void NewFriendList::editGroup()
@ -1036,7 +1108,7 @@ void NewFriendList::editGroup()
CreateGroup editGrpDialog(groupId, this); CreateGroup editGrpDialog(groupId, this);
editGrpDialog.exec(); editGrpDialog.exec();
} }
mModel->checkInternalData(true); checkInternalData(true);
} }
void NewFriendList::removeGroup() void NewFriendList::removeGroup()
@ -1047,7 +1119,19 @@ void NewFriendList::removeGroup()
return; return;
rsPeers->removeGroup(pinfo.id); rsPeers->removeGroup(pinfo.id);
mModel->checkInternalData(true); checkInternalData(true);
}
void NewFriendList::checkInternalData(bool force)
{
std::set<QString> expanded_indexes;
std::set<QString> selected_indexes;
saveExpandedPathsAndSelection(expanded_indexes, selected_indexes);
mModel->checkInternalData(force);
restoreExpandedPathsAndSelection(expanded_indexes, selected_indexes);
} }
void NewFriendList::exportFriendlistClicked() void NewFriendList::exportFriendlistClicked()

View File

@ -105,6 +105,13 @@ private:
RsFriendListModel *mModel; RsFriendListModel *mModel;
QAction *mActionSortByState; QAction *mActionSortByState;
void recursRestoreExpandedItems(const QModelIndex& index, const QString& parent_path, const std::set<QString>& exp, const std::set<QString> &sel);
void recursSaveExpandedItems(const QModelIndex& index,const QString& parent_path,std::set<QString>& exp, std::set<QString>& sel);
void saveExpandedPathsAndSelection(std::set<QString>& expanded_indexes, std::set<QString>& selected_indexes);
void restoreExpandedPathsAndSelection(const std::set<QString>& expanded_indexes, const std::set<QString>& selected_indexes);
void checkInternalData(bool force);
QModelIndex getCurrentSourceIndex() const; QModelIndex getCurrentSourceIndex() const;
bool getCurrentNode(RsFriendListModel::RsNodeDetails& prof) const; bool getCurrentNode(RsFriendListModel::RsNodeDetails& prof) const;