feat(group): Auto Grouping by Dict' Containning Folder

This commit is contained in:
shenleban tongying 2023-04-20 20:45:38 -04:00 committed by xiaoyifang
parent 1798025d0b
commit a8c898192f
6 changed files with 125 additions and 15 deletions

View file

@ -173,7 +173,11 @@ vector< wstring > Class::getAlternateWritings( wstring const & )
QString Class::getContainingFolder()
{
return QFileInfo( QString::fromStdString( dictionaryFiles[ 0 ] ) ).absolutePath();
if ( !dictionaryFiles.empty() ) {
return QFileInfo( QString::fromStdString( dictionaryFiles[ 0 ] ) ).absolutePath();
}
return QString();
}
sptr< DataRequest > Class::getResource( string const & /*name*/ )

View file

@ -51,9 +51,11 @@ Groups::Groups( QWidget * parent,
connect( ui.addDictsToGroup, &QAbstractButton::clicked, this, &Groups::addToGroup );
connect( ui.dictionaries, &QAbstractItemView::doubleClicked, this, &Groups::addToGroup );
connect( ui.removeDictsFromGroup, &QAbstractButton::clicked, this, &Groups::removeFromGroup );
connect( ui.autoGroups, &QAbstractButton::clicked, this, &Groups::addAutoGroups );
connect( ui.groups, &DictGroupsWidget::showDictionaryInfo, this, &Groups::showDictionaryInfo );
connect( ui.autoGroups, &QAbstractButton::clicked, this, &Groups::addAutoGroups );
connect( ui.autoGroupsFolders, &QAbstractButton::clicked, this, &Groups::addAutoGroupsByFolders );
ui.dictionaries->setContextMenuPolicy( Qt::CustomContextMenu );
connect( ui.dictionaries, &QWidget::customContextMenuRequested, this, &Groups::showDictInfo );
@ -124,6 +126,11 @@ void Groups::addAutoGroups()
countChanged();
}
void Groups::addAutoGroupsByFolders() {
ui.groups->addAutoGroupsByFolders();
countChanged();
}
void Groups::renameCurrent()
{
int current = ui.groups->currentIndex();

View file

@ -49,7 +49,12 @@ private slots:
void removeAll();
void addToGroup();
void removeFromGroup();
/// Traditional Add Group by Language
void addAutoGroups();
/// by Containing Folder
void addAutoGroupsByFolders();
void showDictInfo( const QPoint & pos );
void fillGroupsMenu();
void switchToGroup( QAction * act );

View file

@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>662</width>
<width>791</width>
<height>319</height>
</rect>
</property>
@ -147,16 +147,6 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="autoGroups">
<property name="toolTip">
<string>Create language-based groups</string>
</property>
<property name="text">
<string>Auto groups</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="renameGroup">
<property name="toolTip">
@ -187,6 +177,56 @@
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QPushButton" name="autoGroups">
<property name="toolTip">
<string>Create language-based groups</string>
</property>
<property name="text">
<string>Auto groups by Languages</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="autoGroupsFolders">
<property name="toolTip">
<string>Create folder-based groups.</string>
</property>
<property name="text">
<string>Auto groups by Folders</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
@ -227,7 +267,6 @@
<tabstop>removeDictsFromGroup</tabstop>
<tabstop>groups</tabstop>
<tabstop>addGroup</tabstop>
<tabstop>autoGroups</tabstop>
<tabstop>renameGroup</tabstop>
<tabstop>removeGroup</tabstop>
<tabstop>removeAllGroups</tabstop>

View file

@ -15,6 +15,7 @@
#include <QDir>
#include <QIcon>
#include <QMap>
#include <QMultiMap>
#include <QVector>
#include <QFileInfo>
#include <QFileDialog>
@ -772,6 +773,57 @@ void DictGroupsWidget::addAutoGroups()
}
}
void DictGroupsWidget::addAutoGroupsByFolders()
{
if ( activeDicts->empty() ) {
return;
}
if ( QMessageBox::information( this,
tr( "Confirmation" ),
tr( "Are you sure you want to generate a set of groups "
"based on containing folders?" ),
QMessageBox::Yes,
QMessageBox::Cancel )
!= QMessageBox::Yes ) {
return;
}
QMultiMap< QString, sptr< Dictionary::Class > > dictMap;
for ( const auto & dict : *activeDicts ) {
QString dictFolder = dict->getContainingFolder();
if ( dictFolder.isEmpty() ) {
continue;
}
QDir dir = dictFolder;
if ( dir.cdUp() ) {
dictMap.insert( dir.dirName(), dict );
}
else {
qWarning() << "Cannot auto group because parent folder cannot be reached: " << dir;
continue;
}
}
for ( const auto & group : dictMap.uniqueKeys() ) {
if ( count() != 0 ) {
setCurrentIndex( count() - 1 );
}
addUniqueGroup( group );
DictListModel * model = getCurrentModel();
for ( auto dict : dictMap.values( group ) ) {
model->addRow( QModelIndex(), dict );
}
}
}
QString DictGroupsWidget::getCurrentGroupName() const
{
int current = currentIndex();

View file

@ -164,6 +164,9 @@ public:
void addAutoGroups();
/// auto grouping by containning folder
void addAutoGroupsByFolders();
/// Returns currently chosen group's name
QString getCurrentGroupName() const;
@ -187,7 +190,7 @@ private:
/// Add source group to target group
void combineGroups( int source, int target );
unsigned nextId;
std::vector< sptr< Dictionary::Class > > const * allDicts;
std::vector< sptr< Dictionary::Class > > const * activeDicts;