save/restore expanded state in file list tree view so that view gets updated seamlessly

This commit is contained in:
mr-alice 2016-08-28 23:14:39 +02:00
parent 494d2071c9
commit 7c2ed3fca0
5 changed files with 118 additions and 49 deletions

View File

@ -129,7 +129,9 @@ int p3FileDatabase::tick()
{
RS_STACK_MUTEX(mFLSMtx) ;
#ifdef DEBUG_FILE_HIERARCHY
mLocalSharedDirs->print();
#endif
last_print_time = now ;
//#warning this should be removed, but it's necessary atm for updating the GUI
@ -160,7 +162,9 @@ int p3FileDatabase::tick()
if(online_peers.find(mRemoteDirectories[i]->peerId()) != online_peers.end())
{
std::cerr << "Launching recurs sweep of friend directory " << mRemoteDirectories[i]->peerId() << ". Content currently is:" << std::endl;
#ifdef DEBUG_FILE_HIERARCHY
mRemoteDirectories[i]->print();
#endif
locked_recursSweepRemoteDirectory(mRemoteDirectories[i],mRemoteDirectories[i]->root()) ;
}
@ -842,7 +846,9 @@ void p3FileDatabase::handleDirSyncResponse(RsFileListsSyncResponseItem *item)
std::cerr << "(EE) Cannot deserialise dir entry. ERROR. "<< std::endl;
std::cerr << " new content after update: " << std::endl;
#ifdef DEBUG_FILE_HIERARCHY
mRemoteDirectories[fi]->print();
#endif
}
}

View File

@ -744,6 +744,9 @@ QModelIndex TreeStyle_RDM::index(int row, int column, const QModelIndex & parent
/* we can just grab the reference now */
#ifdef RDM_DEBUG
std::cerr << "Creating index 1 row=" << row << ", column=" << column << ", ref=" << (void*)details.children[row].ref << std::endl;
#endif
return createIndex(row, column, details.children[row].ref);
}
QModelIndex FlatStyle_RDM::index(int row, int column, const QModelIndex & parent) const
@ -761,7 +764,10 @@ QModelIndex FlatStyle_RDM::index(int row, int column, const QModelIndex & parent
{
void *ref = _ref_entries[row].first ;
return createIndex(row, column, ref);
#ifdef RDM_DEBUG
std::cerr << "Creating index 2 row=" << row << ", column=" << column << ", ref=" << (void*)ref << std::endl;
#endif
return createIndex(row, column, ref);
}
else
return QModelIndex();
@ -810,6 +816,7 @@ QModelIndex TreeStyle_RDM::parent( const QModelIndex & index ) const
std::cerr << "success index(" << details->prow << ",0," << details->parent << ")";
std::cerr << std::endl;
std::cerr << "Creating index 3 row=" << details.prow << ", column=" << 0 << ", ref=" << (void*)details.parent << std::endl;
#endif
return createIndex(details.prow, 0, details.parent);
}
@ -1155,11 +1162,29 @@ void RetroshareDirModel::openSelected(const QModelIndexList &qmil)
#endif
}
void RetroshareDirModel::getFilePath(const QModelIndex& index, std::string& fullpath)
{
void *ref = index.sibling(index.row(),1).internalPointer();
DirDetails details ;
if (!requestDirDetails(ref, false,details) )
{
#ifdef RDM_DEBUG
std::cerr << "getFilePaths() Bad Request" << std::endl;
#endif
return;
}
fullpath = details.path + "/" + details.name;
}
void RetroshareDirModel::getFilePaths(const QModelIndexList &list, std::list<std::string> &fullpaths)
{
#ifdef RDM_DEBUG
std::cerr << "RetroshareDirModel::getFilePaths()" << std::endl;
#endif
#warning make sure we atually output something here
if (RemoteMode)
{
#ifdef RDM_DEBUG
@ -1168,48 +1193,18 @@ void RetroshareDirModel::getFilePaths(const QModelIndexList &list, std::list<std
return;
}
/* translate */
QModelIndexList::const_iterator it;
for(it = list.begin(); it != list.end(); ++it)
for(QModelIndexList::const_iterator it = list.begin(); it != list.end(); ++it)
{
void *ref = it -> internalPointer();
std::string path ;
DirDetails details ;
if (!requestDirDetails(ref, false,details) )
{
getFilePath(*it,path) ;
#ifdef RDM_DEBUG
std::cerr << "getFilePaths() Bad Request" << std::endl;
std::cerr << "Constructed FilePath: " << path << std::endl;
#endif
continue;
}
if (details.type != DIR_TYPE_FILE)
{
#ifdef RDM_DEBUG
std::cerr << "getFilePaths() Not File" << std::endl;
#endif
continue; /* not file! */
}
#ifdef RDM_DEBUG
std::cerr << "::::::::::::File Details:::: " << std::endl;
std::cerr << "Name: " << details.name << std::endl;
std::cerr << "Hash: " << details.hash << std::endl;
std::cerr << "Size: " << details.count << std::endl;
std::cerr << "Path: " << details.path << std::endl;
#endif
std::string filepath = details.path + "/";
filepath += details.name;
#ifdef RDM_DEBUG
std::cerr << "Constructed FilePath: " << filepath << std::endl;
#endif
if (fullpaths.end() == std::find(fullpaths.begin(), fullpaths.end(), filepath))
{
fullpaths.push_back(filepath);
}
}
#warning TERRIBLE COST here. Use a std::set!
if (fullpaths.end() == std::find(fullpaths.begin(), fullpaths.end(), path))
fullpaths.push_back(path);
}
#ifdef RDM_DEBUG
std::cerr << "::::::::::::Done getFilePaths" << std::endl;
#endif

View File

@ -72,7 +72,8 @@ class RetroshareDirModel : public QAbstractItemModel
void getFileInfoFromIndexList(const QModelIndexList& list, std::list<DirDetails>& files_info) ;
void openSelected(const QModelIndexList &list);
void getFilePaths(const QModelIndexList &list, std::list<std::string> &fullpaths);
void changeAgeIndicator(uint32_t indicator) { ageIndicator = indicator; }
void getFilePath(const QModelIndex& index, std::string& fullpath);
void changeAgeIndicator(uint32_t indicator) { ageIndicator = indicator; }
bool requestDirDetails(void *ref, bool remote,DirDetails& d) const;
virtual void update() {}

View File

@ -19,6 +19,8 @@
* Boston, MA 02110-1301, USA.
****************************************************************/
#include <set>
#include <QString>
#include <QTreeView>
#include <QClipboard>
@ -327,8 +329,8 @@ void RemoteSharedFilesDialog::processSettings(bool bLoad)
void SharedFilesDialog::changeCurrentViewModel(int viewTypeIndex)
{
//disconnect( ui.dirTreeView, SIGNAL( collapsed(const QModelIndex & ) ), NULL, NULL );
//disconnect( ui.dirTreeView, SIGNAL( expanded(const QModelIndex & ) ), NULL, NULL );
// disconnect( ui.dirTreeView, SIGNAL( collapsed(const QModelIndex & ) ), NULL, NULL );
// disconnect( ui.dirTreeView, SIGNAL( expanded(const QModelIndex & ) ), NULL, NULL );
if(model!=NULL)
model->setVisible(false) ;
@ -352,8 +354,8 @@ void SharedFilesDialog::changeCurrentViewModel(int viewTypeIndex)
model->update() ;
}
//connect( ui.dirTreeView, SIGNAL( collapsed(const QModelIndex & ) ), model, SLOT( collapsed(const QModelIndex & ) ) );
//connect( ui.dirTreeView, SIGNAL( expanded(const QModelIndex & ) ), model, SLOT( expanded(const QModelIndex & ) ) );
// connect( ui.dirTreeView, SIGNAL( collapsed(const QModelIndex & ) ), this, SLOT( collapsed(const QModelIndex & ) ) );
// connect( ui.dirTreeView, SIGNAL( expanded(const QModelIndex & ) ), this, SLOT( expanded(const QModelIndex & ) ) );
ui.dirTreeView->setModel(proxyModel);
ui.dirTreeView->update();
@ -363,6 +365,7 @@ void SharedFilesDialog::changeCurrentViewModel(int viewTypeIndex)
ui.dirTreeView->header()->headerDataChanged(Qt::Horizontal, COLUMN_NAME, COLUMN_DIR) ;
// recursRestoreExpandedItems(ui.dirTreeView->rootIndex(),expanded_indexes);
FilterItems();
}
@ -823,20 +826,79 @@ void SharedFilesDialog::preModDirectories(bool local)
flat_model->preMods();
}
void SharedFilesDialog::saveExpandedPaths(std::set<std::string>& expanded_indexes)
{
for(int row = 0; row < ui.dirTreeView->model()->rowCount(); ++row)
{
std::string path = ui.dirTreeView->model()->index(row,0).data(Qt::DisplayRole).toString().toStdString();
recursSaveExpandedItems(ui.dirTreeView->model()->index(row,0),path,expanded_indexes);
}
}
void SharedFilesDialog::restoreExpandedPaths(const std::set<std::string>& expanded_indexes)
{
std::cerr << "Restoring expanded items. " << std::endl;
for(int row = 0; row < ui.dirTreeView->model()->rowCount(); ++row)
{
std::string path = ui.dirTreeView->model()->index(row,0).data(Qt::DisplayRole).toString().toStdString();
recursRestoreExpandedItems(ui.dirTreeView->model()->index(row,0),path,expanded_indexes);
}
}
void SharedFilesDialog::recursSaveExpandedItems(const QModelIndex& index,const std::string& path,std::set<std::string>& exp)
{
std::string local_path = path+"/"+index.data(Qt::DisplayRole).toString().toStdString();
std::cerr << "at index " << index.row() << ". data[1]=" << local_path << std::endl;
if(ui.dirTreeView->isExpanded(index))
{
std::cerr << "Index " << local_path << " is expanded." << std::endl;
if(index.isValid())
exp.insert(local_path) ;
for(int row=0;row<ui.dirTreeView->model()->rowCount(index);++row)
recursSaveExpandedItems(index.child(row,0),local_path,exp) ;
}
else
std::cerr << "Index is not expanded." << std::endl;
}
void SharedFilesDialog::recursRestoreExpandedItems(const QModelIndex& index, const std::string &path, const std::set<std::string>& exp)
{
std::string local_path = path+"/"+index.data(Qt::DisplayRole).toString().toStdString();
std::cerr << "at index " << index.row() << ". data[1]=" << local_path << std::endl;
if(exp.find(local_path) != exp.end())
{
std::cerr << "re expanding index " << local_path << std::endl;
ui.dirTreeView->setExpanded(index,true) ;
for(int row=0;row<ui.dirTreeView->model()->rowCount(index);++row)
recursRestoreExpandedItems(index.child(row,0),local_path,exp) ;
}
}
void SharedFilesDialog::postModDirectories(bool local)
{
if (isRemote() == local) {
return;
}
return;
}
std::set<std::string> expanded_indexes;
saveExpandedPaths(expanded_indexes) ;
std::cerr << "Saving expanded items. " << expanded_indexes.size() << " items found" << std::endl;
/* Notify both models, only one is visible */
/* Notify both models, only one is visible */
tree_model->postMods();
flat_model->postMods();
ui.dirTreeView->update() ;
if (ui.filterPatternLineEdit->text().isEmpty() == false)
restoreExpandedPaths(expanded_indexes) ;
if (ui.filterPatternLineEdit->text().isEmpty() == false)
FilterItems();
std::cerr << "****** updated directories! ******" << std::endl;
QCoreApplication::flush();
}

View File

@ -22,6 +22,7 @@
#ifndef _SHAREDFILESDIALOG_H
#define _SHAREDFILESDIALOG_H
#include <set>
#include "RsAutoUpdatePage.h"
#include "ui_SharedFilesDialog.h"
@ -51,7 +52,6 @@ protected slots:
virtual void spawnCustomPopupMenu(QPoint point) = 0;
private slots:
/* For handling the model updates */
void preModDirectories(bool local) ;
void postModDirectories(bool local) ;
@ -94,6 +94,11 @@ protected:
Ui::SharedFilesDialog ui;
virtual void processSettings(bool bLoad) = 0;
void recursRestoreExpandedItems(const QModelIndex& index,const std::string& path,const std::set<std::string>& exp);
void recursSaveExpandedItems(const QModelIndex& index, const std::string &path, std::set<std::string> &exp);
void saveExpandedPaths(std::set<std::string>& paths) ;
void restoreExpandedPaths(const std::set<std::string>& paths) ;
protected:
//now context menu are created again every time theu are called ( in some
//slots.. Maybe it's not good...