2012-02-20 21:47:14 +00:00
|
|
|
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
|
2009-01-28 20:55:45 +00:00
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
|
|
|
#include "groups.hh"
|
2009-05-18 18:01:50 +00:00
|
|
|
#include "instances.hh"
|
2023-04-17 20:55:34 +00:00
|
|
|
#include "dict/dictionary.hh"
|
2009-01-28 20:55:45 +00:00
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QInputDialog>
|
|
|
|
|
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
Groups::Groups( QWidget * parent,
|
|
|
|
vector< sptr< Dictionary::Class > > const & dicts_,
|
2009-05-18 18:01:50 +00:00
|
|
|
Config::Groups const & groups_,
|
|
|
|
Config::Group const & order ):
|
|
|
|
QWidget( parent ),
|
2009-01-28 20:55:45 +00:00
|
|
|
dicts( dicts_ ),
|
|
|
|
groups( groups_ )
|
|
|
|
{
|
|
|
|
ui.setupUi( this );
|
|
|
|
|
|
|
|
// Populate the dictionaries' list
|
|
|
|
|
|
|
|
ui.dictionaries->setAsSource();
|
2022-07-08 13:47:09 +00:00
|
|
|
ui.dictionaries->populate( Instances::Group( order, dicts, Config::Group() ).dictionaries, dicts );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2013-01-04 13:24:08 +00:00
|
|
|
ui.searchLine->applyTo( ui.dictionaries );
|
|
|
|
addAction( ui.searchLine->getFocusAction() );
|
|
|
|
|
2014-03-03 13:47:06 +00:00
|
|
|
groupsListMenu = new QMenu( tr( "Group tabs" ), ui.groups );
|
|
|
|
|
|
|
|
groupsListButton = new QToolButton( ui.groups );
|
|
|
|
groupsListButton->setAutoRaise( true );
|
2022-01-24 14:23:38 +00:00
|
|
|
groupsListButton->setIcon( QIcon( ":/icons/windows-list.svg" ) );
|
2014-03-03 13:47:06 +00:00
|
|
|
groupsListButton->setMenu( groupsListMenu );
|
|
|
|
groupsListButton->setToolTip( tr( "Open groups list" ) );
|
|
|
|
groupsListButton->setPopupMode( QToolButton::InstantPopup );
|
|
|
|
ui.groups->setCornerWidget( groupsListButton );
|
|
|
|
groupsListButton->setFocusPolicy( Qt::ClickFocus );
|
|
|
|
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( groupsListMenu, &QMenu::aboutToShow, this, &Groups::fillGroupsMenu );
|
|
|
|
connect( groupsListMenu, &QMenu::triggered, this, &Groups::switchToGroup );
|
2014-03-03 13:47:06 +00:00
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
// Populate groups' widget
|
|
|
|
|
2012-12-18 12:35:11 +00:00
|
|
|
ui.groups->populate( groups, dicts, ui.dictionaries->getCurrentDictionaries() );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( ui.addGroup, &QAbstractButton::clicked, this, &Groups::addNew );
|
|
|
|
connect( ui.renameGroup, &QAbstractButton::clicked, this, &Groups::renameCurrent );
|
|
|
|
connect( ui.removeGroup, &QAbstractButton::clicked, this, &Groups::removeCurrent );
|
|
|
|
connect( ui.removeAllGroups, &QAbstractButton::clicked, this, &Groups::removeAll );
|
|
|
|
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.groups, &DictGroupsWidget::showDictionaryInfo, this, &Groups::showDictionaryInfo );
|
2012-12-20 13:25:03 +00:00
|
|
|
|
2023-04-21 00:45:38 +00:00
|
|
|
connect( ui.autoGroups, &QAbstractButton::clicked, this, &Groups::addAutoGroups );
|
|
|
|
connect( ui.autoGroupsFolders, &QAbstractButton::clicked, this, &Groups::addAutoGroupsByFolders );
|
2023-04-29 06:59:19 +00:00
|
|
|
connect( ui.groupMetadata, &QAbstractButton::clicked, this, &Groups::groupsByMetadata );
|
2023-04-21 00:45:38 +00:00
|
|
|
|
2012-12-20 13:25:03 +00:00
|
|
|
ui.dictionaries->setContextMenuPolicy( Qt::CustomContextMenu );
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( ui.dictionaries, &QWidget::customContextMenuRequested, this, &Groups::showDictInfo );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
countChanged();
|
|
|
|
}
|
|
|
|
|
2010-05-08 14:01:59 +00:00
|
|
|
void Groups::editGroup( unsigned id )
|
|
|
|
{
|
2012-12-10 12:49:45 +00:00
|
|
|
for ( int x = 0; x < groups.size(); ++x ) {
|
2010-05-08 14:01:59 +00:00
|
|
|
if ( groups[ x ].id == id ) {
|
|
|
|
ui.groups->setCurrentIndex( x );
|
|
|
|
ui.groups->currentWidget()->setFocus();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-18 18:01:50 +00:00
|
|
|
void Groups::updateDictionaryOrder( Config::Group const & order )
|
|
|
|
{
|
|
|
|
// Make sure it differs from what we have
|
2022-07-08 13:47:09 +00:00
|
|
|
Instances::Group newOrder( order, dicts, Config::Group() );
|
2009-05-18 18:01:50 +00:00
|
|
|
|
2022-03-13 16:17:34 +00:00
|
|
|
if ( ui.dictionaries->getCurrentDictionaries() != newOrder.dictionaries ) {
|
2009-05-18 18:01:50 +00:00
|
|
|
// Repopulate
|
2023-06-13 00:07:54 +00:00
|
|
|
ui.dictionaries->populate( newOrder.dictionaries, dicts );
|
2009-05-18 18:01:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
Config::Groups Groups::getGroups() const
|
|
|
|
{
|
|
|
|
return ui.groups->makeGroups();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Groups::countChanged()
|
|
|
|
{
|
|
|
|
bool en = ui.groups->count();
|
|
|
|
|
|
|
|
ui.renameGroup->setEnabled( en );
|
|
|
|
ui.removeGroup->setEnabled( en );
|
2009-04-29 23:32:42 +00:00
|
|
|
ui.removeAllGroups->setEnabled( en );
|
2012-12-19 13:00:50 +00:00
|
|
|
|
|
|
|
int stretch = ui.groups->count() / 5;
|
|
|
|
if ( stretch > 3 ) {
|
|
|
|
stretch = 3;
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
2012-12-19 13:00:50 +00:00
|
|
|
ui.gridLayout->setColumnStretch( 2, stretch );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Groups::addNew()
|
|
|
|
{
|
|
|
|
bool ok;
|
|
|
|
|
|
|
|
QString name = QInputDialog::getText( this,
|
|
|
|
tr( "Add group" ),
|
|
|
|
tr( "Give a name for the new group:" ),
|
|
|
|
QLineEdit::Normal,
|
|
|
|
"",
|
|
|
|
&ok );
|
|
|
|
|
|
|
|
if ( ok ) {
|
|
|
|
ui.groups->addNewGroup( name );
|
|
|
|
countChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-16 18:50:31 +00:00
|
|
|
void Groups::addAutoGroups()
|
|
|
|
{
|
|
|
|
ui.groups->addAutoGroups();
|
|
|
|
countChanged();
|
|
|
|
}
|
|
|
|
|
2023-04-21 00:45:38 +00:00
|
|
|
void Groups::addAutoGroupsByFolders()
|
|
|
|
{
|
|
|
|
ui.groups->addAutoGroupsByFolders();
|
|
|
|
countChanged();
|
|
|
|
}
|
|
|
|
|
2023-04-29 06:59:19 +00:00
|
|
|
void Groups::groupsByMetadata()
|
|
|
|
{
|
|
|
|
ui.groups->groupsByMetadata();
|
|
|
|
countChanged();
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
void Groups::renameCurrent()
|
|
|
|
{
|
|
|
|
int current = ui.groups->currentIndex();
|
|
|
|
|
|
|
|
if ( current < 0 ) {
|
|
|
|
return;
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
bool ok;
|
|
|
|
|
|
|
|
QString name = QInputDialog::getText( this,
|
|
|
|
tr( "Rename group" ),
|
|
|
|
tr( "Give a new name for the group:" ),
|
|
|
|
QLineEdit::Normal,
|
2009-04-10 21:37:16 +00:00
|
|
|
ui.groups->getCurrentGroupName(),
|
|
|
|
&ok );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
if ( ok ) {
|
|
|
|
ui.groups->renameCurrentGroup( name );
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Groups::removeCurrent()
|
|
|
|
{
|
|
|
|
int current = ui.groups->currentIndex();
|
|
|
|
|
|
|
|
if ( current >= 0
|
|
|
|
&& QMessageBox::question(
|
|
|
|
this,
|
|
|
|
tr( "Remove group" ),
|
2009-04-10 21:37:16 +00:00
|
|
|
tr( "Are you sure you want to remove the group <b>%1</b>?" ).arg( ui.groups->getCurrentGroupName() ),
|
2009-01-28 20:55:45 +00:00
|
|
|
QMessageBox::Yes,
|
|
|
|
QMessageBox::Cancel )
|
|
|
|
== QMessageBox::Yes ) {
|
|
|
|
ui.groups->removeCurrentGroup();
|
|
|
|
countChanged();
|
|
|
|
}
|
|
|
|
}
|
2009-04-29 23:32:42 +00:00
|
|
|
|
|
|
|
void Groups::removeAll()
|
|
|
|
{
|
|
|
|
int current = ui.groups->currentIndex();
|
|
|
|
|
|
|
|
if ( current >= 0
|
|
|
|
&& QMessageBox::question( this,
|
|
|
|
tr( "Remove all groups" ),
|
|
|
|
tr( "Are you sure you want to remove all the groups?" ),
|
|
|
|
QMessageBox::Yes,
|
|
|
|
QMessageBox::Cancel )
|
|
|
|
== QMessageBox::Yes ) {
|
|
|
|
ui.groups->removeAllGroups();
|
|
|
|
countChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-02 17:12:52 +00:00
|
|
|
void Groups::addToGroup()
|
|
|
|
{
|
|
|
|
int current = ui.groups->currentIndex();
|
|
|
|
|
|
|
|
if ( current >= 0 ) {
|
|
|
|
ui.groups->getCurrentModel()->addSelectedUniqueFromModel( ui.dictionaries->selectionModel() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Groups::removeFromGroup()
|
|
|
|
{
|
|
|
|
int current = ui.groups->currentIndex();
|
|
|
|
|
|
|
|
if ( current >= 0 ) {
|
|
|
|
ui.groups->getCurrentModel()->removeSelectedRows( ui.groups->getCurrentSelectionModel() );
|
|
|
|
}
|
|
|
|
}
|
2012-12-20 13:25:03 +00:00
|
|
|
|
|
|
|
void Groups::showDictInfo( QPoint const & pos )
|
|
|
|
{
|
2013-04-20 14:27:59 +00:00
|
|
|
QVariant data =
|
|
|
|
ui.dictionaries->getModel()->data( ui.searchLine->mapToSource( ui.dictionaries->indexAt( pos ) ), Qt::EditRole );
|
2012-12-20 13:25:03 +00:00
|
|
|
QString id;
|
|
|
|
if ( data.canConvert< QString >() ) {
|
|
|
|
id = data.toString();
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
2012-12-20 13:25:03 +00:00
|
|
|
|
|
|
|
if ( !id.isEmpty() ) {
|
|
|
|
vector< sptr< Dictionary::Class > > const & dicts = ui.dictionaries->getCurrentDictionaries();
|
|
|
|
unsigned n;
|
|
|
|
for ( n = 0; n < dicts.size(); n++ ) {
|
|
|
|
if ( id.compare( QString::fromUtf8( dicts.at( n )->getId().c_str() ) ) == 0 ) {
|
|
|
|
break;
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-20 13:25:03 +00:00
|
|
|
if ( n < dicts.size() ) {
|
|
|
|
emit showDictionaryInfo( id );
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
2012-12-20 13:25:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-03 13:47:06 +00:00
|
|
|
void Groups::fillGroupsMenu()
|
|
|
|
{
|
|
|
|
groupsListMenu->clear();
|
|
|
|
for ( int i = 0; i < ui.groups->count(); i++ ) {
|
|
|
|
QAction * act = groupsListMenu->addAction( ui.groups->tabText( i ) );
|
|
|
|
act->setData( i );
|
|
|
|
if ( ui.groups->currentIndex() == i ) {
|
|
|
|
QFont f( act->font() );
|
|
|
|
f.setBold( true );
|
|
|
|
act->setFont( f );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( groupsListMenu->actions().size() > 1 ) {
|
|
|
|
groupsListMenu->setActiveAction( groupsListMenu->actions().at( ui.groups->currentIndex() ) );
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
2014-03-03 13:47:06 +00:00
|
|
|
}
|
2013-01-04 13:24:08 +00:00
|
|
|
|
2014-03-03 13:47:06 +00:00
|
|
|
void Groups::switchToGroup( QAction * act )
|
|
|
|
{
|
|
|
|
int idx = act->data().toInt();
|
|
|
|
ui.groups->setCurrentIndex( idx );
|
|
|
|
}
|