fix(autoGroupByFolder): prepend upper level folder name when two

groups share same name but in different folder
This commit is contained in:
shenleban tongying 2023-04-26 03:37:46 -04:00 committed by xiaoyifang
parent 867f6a2d82
commit dfc5d701ea
2 changed files with 81 additions and 11 deletions

View file

@ -780,6 +780,12 @@ void DictGroupsWidget::addAutoGroupsByFolders()
return; return;
} }
auto cdUpWentWrong = [ this ]( const QString & path ) {
QMessageBox::warning( this,
tr( "Auto group by folder failed." ),
tr( "The parent directory of %1 can be reached." ).arg( path ) );
};
if ( QMessageBox::information( this, if ( QMessageBox::information( this,
tr( "Confirmation" ), tr( "Confirmation" ),
tr( "Are you sure you want to generate a set of groups " tr( "Are you sure you want to generate a set of groups "
@ -790,26 +796,74 @@ void DictGroupsWidget::addAutoGroupsByFolders()
return; return;
} }
QMultiMap< QString, sptr< Dictionary::Class > > dictMap; // Map from dict to ContainerFolder's parent
QMap< sptr< Dictionary::Class >, QString > dictToContainerFolder;
for ( const auto & dict : *activeDicts ) { for ( const auto & dict : *activeDicts ) {
QString dictFolder = dict->getContainingFolder(); QDir c = dict->getContainingFolder();
if ( dictFolder.isEmpty() ) { if ( !c.cdUp() ) {
continue; cdUpWentWrong( c.absolutePath() );
return;
} }
QDir dir = dictFolder; dictToContainerFolder.insert( dict, c.absolutePath() );
if ( dir.cdUp() ) { }
dictMap.insert( dir.dirName(), dict );
/*
Check for duplicated ContainerFolder's parent folder names, and prepend the upper parent's name.
.
epistularum
Japanese <- Group
DictA <- Dict Files's container folder
| DictA Files
Mastameta
Japanese <- Group
| DictB <- Dict Files's container folder
| DictB Files
will be grouped into epistularum/Japanese and Mastameta/Japanese
*/
// set a dirname to true if there are duplications like `epistularum/Japanese` and `Mastameta/Japanese`
QHash< QString, bool > dirNeedPrepend{};
for ( const auto & path : dictToContainerFolder.values() ) {
auto dir = QDir( path );
if ( dirNeedPrepend.contains( dir.dirName() ) ) {
dirNeedPrepend[ dir.dirName() ] = true;
} }
else { else {
qWarning() << "Cannot auto group because parent folder cannot be reached: " << dir; dirNeedPrepend.insert( dir.dirName(), false );
continue;
} }
} }
for ( const auto & group : dictMap.uniqueKeys() ) { // map from GroupName to dicts
QMultiMap< QString, sptr< Dictionary::Class > > groupToDicts;
for ( const auto & dict : dictToContainerFolder.keys() ) {
QDir path = dictToContainerFolder[ dict ];
QString groupName;
if ( !dirNeedPrepend[ path.dirName() ] ) {
groupName = path.dirName();
}
else {
QString directFolder = path.dirName();
if ( !path.cdUp() ) {
cdUpWentWrong( path.absolutePath() );
return;
}
QString upperFolder = path.dirName();
groupName = upperFolder + "/" + directFolder;
}
groupToDicts.insert( groupName, dict );
}
// create and insert groups
// modifying user's groups begins here
for ( const auto & group : groupToDicts.uniqueKeys() ) {
if ( count() != 0 ) { if ( count() != 0 ) {
setCurrentIndex( count() - 1 ); setCurrentIndex( count() - 1 );
} }
@ -817,7 +871,7 @@ void DictGroupsWidget::addAutoGroupsByFolders()
addUniqueGroup( group ); addUniqueGroup( group );
DictListModel * model = getCurrentModel(); DictListModel * model = getCurrentModel();
for ( auto dict : dictMap.values( group ) ) { for ( const auto& dict : groupToDicts.values( group ) ) {
model->addRow( QModelIndex(), dict ); model->addRow( QModelIndex(), dict );
} }
} }

View file

@ -36,3 +36,19 @@ For example, the structure below will be auto grouped into two groups:
├── Wikipedia_ru.slob ├── Wikipedia_ru.slob
└── Wikipedia.zim └── Wikipedia.zim
``` ```
Note that if two groups share the same name but in different folder, then upper level's folder name will be prepended with group name. The Example below will be grouped into `epistularum/Japanese` and `Mastameta/Japanese`.
More levels of folder nesting are not supported.
```
.
├─epistularum
│ └─Japanese <- Group
│ └─DictA <- Dict Files's container folder
| └─ DictA Files
├─Mastameta
│ └─Japanese <- Group
| └─DictB <- Dict Files's container folder
| └─ DictB Files
```