fixed a few bugs in file counting

This commit is contained in:
csoler 2024-03-09 18:31:01 +01:00
parent 67ff37a0d4
commit 9ba4ac6362
3 changed files with 76 additions and 45 deletions

View File

@ -181,16 +181,8 @@ RsCollectionDialog::RsCollectionDialog(const QString& collectionFileName, RsColl
mCollectionModel = new RsCollectionModel(*mCollection); mCollectionModel = new RsCollectionModel(*mCollection);
ui._fileEntriesTW->setModel(mCollectionModel); ui._fileEntriesTW->setModel(mCollectionModel);
#ifdef TO_REMOVE connect(mCollectionModel,SIGNAL(sizesChanged()),this,SLOT(updateSizes()));
ui._fileEntriesTW->setColumnCount(COLUMN_COUNT) ; updateSizes(); // forced because it's only called when the collection is changed, or when the model is created.
QTreeWidgetItem *headerItem = ui._fileEntriesTW->headerItem();
headerItem->setText(COLUMN_FILE, tr("File"));
headerItem->setText(COLUMN_FILEPATH, tr("File Path"));
headerItem->setText(COLUMN_SIZE, tr("Size"));
headerItem->setText(COLUMN_HASH, tr("Hash"));
headerItem->setText(COLUMN_FILEC, tr("File Count"));
#endif
bool wrong_chars = !updateList(); bool wrong_chars = !updateList();
@ -638,8 +630,8 @@ void RsCollectionDialog::directoryLoaded(QString dirLoaded)
*/ */
void RsCollectionDialog::updateSizes() void RsCollectionDialog::updateSizes()
{ {
ui._selectedFiles_TL->setText(QString::number(mCollection->count())); ui._selectedFiles_TL->setText(QString::number(mCollectionModel->totalSelected()));
ui._totalSize_TL->setText(misc::friendlyUnit(mCollection->size())); ui._totalSize_TL->setText(misc::friendlyUnit(mCollectionModel->totalSize()));
} }
/** /**

View File

@ -264,16 +264,31 @@ bool RsCollectionModel::setData(const QModelIndex& index,const QVariant& value,i
} }
else else
{ {
std::function<void(RsFileTree::DirIndex i,bool s)> recursSetCheckFlag = [&](RsFileTree::DirIndex i,bool s) -> void std::function<void(RsFileTree::DirIndex,bool)> recursSetCheckFlag = [&](RsFileTree::DirIndex index,bool s) -> void
{ {
mDirInfos[i].check_state = (s)?SELECTED:UNSELECTED; mDirInfos[index].check_state = (s)?SELECTED:UNSELECTED;
auto& dir_data(mCollection.fileTree().directoryData(i)); auto& dir_data(mCollection.fileTree().directoryData(index));
mDirInfos[index].total_size = 0;
mDirInfos[index].total_count = 0;
for(uint32_t i=0;i<dir_data.subdirs.size();++i) for(uint32_t i=0;i<dir_data.subdirs.size();++i)
{
recursSetCheckFlag(dir_data.subdirs[i],s); recursSetCheckFlag(dir_data.subdirs[i],s);
mDirInfos[index].total_size += mDirInfos[dir_data.subdirs[i]].total_size ;
++mDirInfos[index].total_count;
}
for(uint32_t i=0;i<dir_data.subfiles.size();++i) for(uint32_t i=0;i<dir_data.subfiles.size();++i)
{
mFileInfos[dir_data.subfiles[i]].is_checked = s; mFileInfos[dir_data.subfiles[i]].is_checked = s;
if(s)
{
mDirInfos[index].total_size += mCollection.fileTree().fileData(dir_data.subfiles[i]).size;
++mDirInfos[index].total_count;
}
}
}; };
recursSetCheckFlag(e.index,value.toBool()); recursSetCheckFlag(e.index,value.toBool());
dir_index = mDirInfos[e.index].parent_index; dir_index = mDirInfos[e.index].parent_index;
@ -290,9 +305,14 @@ bool RsCollectionModel::setData(const QModelIndex& index,const QVariant& value,i
bool locally_all_checked = true; bool locally_all_checked = true;
bool locally_all_unchecked = true; bool locally_all_unchecked = true;
for(uint32_t i=0;i<dir_data.subdirs.size() && (locally_all_checked || locally_all_unchecked);++i) dit.total_size = 0;
dit.total_count = 0;
for(uint32_t i=0;i<dir_data.subdirs.size();++i)
{ {
const auto& dit2(mDirInfos[dir_data.subdirs[i]]); const auto& dit2(mDirInfos[dir_data.subdirs[i]]);
dit.total_size += dit2.total_size;
dit.total_count += dit2.total_count;
if(dit2.check_state == UNSELECTED || dit2.check_state == PARTIALLY_SELECTED) if(dit2.check_state == UNSELECTED || dit2.check_state == PARTIALLY_SELECTED)
locally_all_checked = false; locally_all_checked = false;
@ -300,12 +320,16 @@ bool RsCollectionModel::setData(const QModelIndex& index,const QVariant& value,i
if(dit2.check_state == SELECTED || dit2.check_state == PARTIALLY_SELECTED) if(dit2.check_state == SELECTED || dit2.check_state == PARTIALLY_SELECTED)
locally_all_unchecked = false; locally_all_unchecked = false;
} }
for(uint32_t i=0;i<dir_data.subfiles.size() && (locally_all_checked || locally_all_unchecked);++i) for(uint32_t i=0;i<dir_data.subfiles.size();++i)
{ {
const auto& fit2(mFileInfos[dir_data.subfiles[i]]); const auto& fit2(mFileInfos[dir_data.subfiles[i]]);
if(fit2.is_checked) if(fit2.is_checked)
{
dit.total_size += mCollection.fileTree().fileData(dir_data.subfiles[i]).size;
++dit.total_count;
locally_all_unchecked = false; locally_all_unchecked = false;
}
else else
locally_all_checked = false; locally_all_checked = false;
} }
@ -332,6 +356,8 @@ bool RsCollectionModel::setData(const QModelIndex& index,const QVariant& value,i
(void*)NULL), (void*)NULL),
{ Qt::CheckStateRole }); { Qt::CheckStateRole });
emit sizesChanged();
return true; return true;
} }
else else
@ -370,27 +396,23 @@ QVariant RsCollectionModel::displayRole(const EntryIndex& i,int col) const
{ {
switch(col) switch(col)
{ {
case 0: return (i.is_file)? case COLLECTION_MODEL_FILENAME: return (i.is_file)?
(QString::fromUtf8(mCollection.fileTree().fileData(i.index).name.c_str())) (QString::fromUtf8(mCollection.fileTree().fileData(i.index).name.c_str()))
: (QString::fromUtf8(mCollection.fileTree().directoryData(i.index).name.c_str())); : (QString::fromUtf8(mCollection.fileTree().directoryData(i.index).name.c_str()));
case 1: if(i.is_file) case COLLECTION_MODEL_SIZE: if(i.is_file)
return QVariant((qulonglong)mCollection.fileTree().fileData(i.index).size) ; return QVariant((qulonglong)mCollection.fileTree().fileData(i.index).size) ;
else
return QVariant((qulonglong)mDirInfos[i.index].total_size);
{ case COLLECTION_MODEL_HASH: return (i.is_file)?
// auto it = mDirInfos[i.index];
// if(it == mDirInfos.end())
// return QVariant();
// else
return QVariant((qulonglong)mDirInfos[i.index].total_size);
}
case 2: return (i.is_file)?
QString::fromStdString(mCollection.fileTree().fileData(i.index).hash.toStdString()) QString::fromStdString(mCollection.fileTree().fileData(i.index).hash.toStdString())
:QVariant(); :QVariant();
case 3: return (i.is_file)?((qulonglong)1):((qulonglong)(mCollection.fileTree().directoryData(i.index).subdirs.size())); case COLLECTION_MODEL_COUNT: if(i.is_file)
return (qulonglong)mFileInfos[i.index].is_checked;
else
return (qulonglong)(mDirInfos[i.index].total_count);
} }
return QVariant(); return QVariant();
} }
@ -423,16 +445,17 @@ void RsCollectionModel::postMods()
#ifdef DEBUG_COLLECTION_MODEL #ifdef DEBUG_COLLECTION_MODEL
std::cerr << "Updating from tree: " << std::endl; std::cerr << "Updating from tree: " << std::endl;
#endif #endif
uint64_t s; recursUpdateLocalStructures(mCollection.fileTree().root(),0);
recursUpdateLocalStructures(mCollection.fileTree().root(),s,0);
mUpdating = false; mUpdating = false;
emit layoutChanged(); emit layoutChanged();
emit sizesChanged();
} }
void RsCollectionModel::recursUpdateLocalStructures(RsFileTree::DirIndex dir_index,uint64_t& total_size,int depth) void RsCollectionModel::recursUpdateLocalStructures(RsFileTree::DirIndex dir_index,int depth)
{ {
total_size = 0; uint64_t total_size = 0;
uint64_t total_count = 0;
bool all_checked = true; bool all_checked = true;
bool all_unchecked = false; bool all_unchecked = false;
@ -444,11 +467,14 @@ void RsCollectionModel::recursUpdateLocalStructures(RsFileTree::DirIndex dir_ind
for(int j=0;j<depth;++j) std::cerr << " "; for(int j=0;j<depth;++j) std::cerr << " ";
std::cerr << "File \"" << mCollection.fileTree().fileData(dd.subfiles[i]).name << "\"" << std::endl; std::cerr << "File \"" << mCollection.fileTree().fileData(dd.subfiles[i]).name << "\"" << std::endl;
#endif #endif
total_size += mCollection.fileTree().fileData(dd.subfiles[i]).size;
auto& ref(mFileInfos[dd.subfiles[i]]); auto& ref(mFileInfos[dd.subfiles[i]]);
if(ref.is_checked)
{
total_size += mCollection.fileTree().fileData(dd.subfiles[i]).size;
++total_count;
}
ref.parent_index = dir_index; ref.parent_index = dir_index;
ref.parent_row = i + dd.subdirs.size(); ref.parent_row = i + dd.subdirs.size();
@ -463,22 +489,24 @@ void RsCollectionModel::recursUpdateLocalStructures(RsFileTree::DirIndex dir_ind
std::cerr << "Dir \"" << mCollection.fileTree().directoryData(dd.subdirs[i]).name << "\"" << std::endl ; std::cerr << "Dir \"" << mCollection.fileTree().directoryData(dd.subdirs[i]).name << "\"" << std::endl ;
#endif #endif
uint64_t ss; recursUpdateLocalStructures(dd.subdirs[i],depth+1);
recursUpdateLocalStructures(dd.subdirs[i],ss,depth+1);
total_size += ss;
auto& ref(mDirInfos[dd.subdirs[i]]); auto& ref(mDirInfos[dd.subdirs[i]]);
total_size += ref.total_size;
total_count += ref.total_count;
ref.parent_index = dir_index; ref.parent_index = dir_index;
ref.parent_row = i; ref.parent_row = i;
all_checked = all_checked && (ref.check_state == SELECTED); all_checked = all_checked && (ref.check_state == SELECTED);
all_unchecked = all_unchecked && (ref.check_state == UNSELECTED); all_unchecked = all_unchecked && (ref.check_state == UNSELECTED);
} }
auto& r(mDirInfos[dir_index]); auto& r(mDirInfos[dir_index]);
r.total_size = total_size; r.total_size = total_size;
r.total_count = total_count;
if(all_checked) if(all_checked)
r.check_state = SELECTED; r.check_state = SELECTED;

View File

@ -45,11 +45,17 @@ class RsCollectionModel: public QAbstractItemModel
bool is_file; // false=dir, true=file bool is_file; // false=dir, true=file
uint64_t index; uint64_t index;
}; };
uint64_t totalSize() const { return mDirInfos[0].total_size; }
uint64_t totalSelected() const { return mDirInfos[0].total_count; }
signals:
void sizesChanged(); // tells that the total size of the top level dir has changed (due to selection)
private: private:
static bool convertIndexToInternalId(const EntryIndex& e,quintptr& ref); static bool convertIndexToInternalId(const EntryIndex& e,quintptr& ref);
static bool convertInternalIdToIndex(quintptr ref, EntryIndex& e); static bool convertInternalIdToIndex(quintptr ref, EntryIndex& e);
void recursUpdateLocalStructures(RsFileTree::DirIndex dir_index, uint64_t& total_size, int depth); void recursUpdateLocalStructures(RsFileTree::DirIndex dir_index, int depth);
QVariant displayRole(const EntryIndex&,int col) const ; QVariant displayRole(const EntryIndex&,int col) const ;
QVariant sortRole(const EntryIndex&,int col) const ; QVariant sortRole(const EntryIndex&,int col) const ;
@ -68,13 +74,18 @@ class RsCollectionModel: public QAbstractItemModel
}; };
struct ModelDirInfo { struct ModelDirInfo {
ModelDirInfo() :parent_index(0),parent_row(0),check_state(SELECTED),total_size(0),total_count(0){}
RsFileTree::DirIndex parent_index; // index of the parent RsFileTree::DirIndex parent_index; // index of the parent
RsFileTree::DirIndex parent_row; // row of that child, in this parent RsFileTree::DirIndex parent_row; // row of that child, in this parent
DirCheckState check_state; DirCheckState check_state;
uint64_t total_size; uint64_t total_size;
uint64_t total_count;
}; };
struct ModelFileInfo { struct ModelFileInfo {
ModelFileInfo() :parent_index(0),parent_row(0),is_checked(true){}
RsFileTree::DirIndex parent_index; // index of the parent RsFileTree::DirIndex parent_index; // index of the parent
RsFileTree::DirIndex parent_row; // row of that child, in this parent RsFileTree::DirIndex parent_row; // row of that child, in this parent
bool is_checked; bool is_checked;