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_widgets.hh"
|
|
|
|
|
|
|
|
#include "instances.hh"
|
|
|
|
#include "config.hh"
|
2009-04-22 21:37:32 +00:00
|
|
|
#include "langcoder.hh"
|
2009-05-24 16:38:08 +00:00
|
|
|
#include "language.hh"
|
2011-09-09 12:05:28 +00:00
|
|
|
#include "fsencoding.hh"
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2010-09-16 18:50:31 +00:00
|
|
|
//#include "initializing.hh"
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
#include <QMenu>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QIcon>
|
2012-12-18 12:35:11 +00:00
|
|
|
#include <QMap>
|
|
|
|
#include <QVector>
|
2012-12-19 12:59:38 +00:00
|
|
|
#include <QFileInfo>
|
2013-05-30 13:24:21 +00:00
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QMessageBox>
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
/// DictGroupWidget
|
|
|
|
|
|
|
|
DictGroupWidget::DictGroupWidget( QWidget * parent,
|
|
|
|
vector< sptr< Dictionary::Class > > const & dicts,
|
|
|
|
Config::Group const & group ):
|
2009-04-10 12:48:40 +00:00
|
|
|
QWidget( parent ),
|
|
|
|
groupId( group.id )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
ui.setupUi( this );
|
2022-07-08 13:47:09 +00:00
|
|
|
ui.dictionaries->populate( Instances::Group( group, dicts, Config::Group() ).dictionaries, dicts );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2022-12-29 12:10:21 +00:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
|
|
|
|
ui.shortcut->setClearButtonEnabled(true);
|
|
|
|
ui.clearShortCut->hide();
|
|
|
|
#endif
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
// Populate icons' list
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
QStringList icons = QDir( ":/flags/" ).entryList( QDir::Files, QDir::NoSort );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2010-07-05 18:36:03 +00:00
|
|
|
ui.groupIcon->addItem( tr( "None" ), "" );
|
|
|
|
|
|
|
|
bool usesIconData = !group.iconData.isEmpty();
|
|
|
|
|
|
|
|
if ( !usesIconData )
|
|
|
|
ui.groupIcon->addItem( tr( "From file..." ), "" );
|
|
|
|
else
|
2010-07-05 18:50:54 +00:00
|
|
|
ui.groupIcon->addItem( Instances::iconFromData( group.iconData ), group.icon, group.icon );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
for( int x = 0; x < icons.size(); ++x )
|
|
|
|
{
|
|
|
|
QString n( icons[ x ] );
|
|
|
|
n.chop( 4 );
|
|
|
|
n[ 0 ] = n[ 0 ].toUpper();
|
|
|
|
|
|
|
|
ui.groupIcon->addItem( QIcon( ":/flags/" + icons[ x ] ), n, icons[ x ] );
|
|
|
|
|
2010-07-05 18:36:03 +00:00
|
|
|
if ( !usesIconData && icons[ x ] == group.icon )
|
|
|
|
ui.groupIcon->setCurrentIndex( x + 2 );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
2010-07-05 14:13:29 +00:00
|
|
|
|
2010-07-05 18:36:03 +00:00
|
|
|
if ( usesIconData )
|
|
|
|
ui.groupIcon->setCurrentIndex( 1 );
|
|
|
|
|
2022-12-29 12:10:21 +00:00
|
|
|
ui.shortcut->setKeySequence( group.shortcut );
|
2010-07-05 18:36:03 +00:00
|
|
|
|
2017-05-05 14:39:51 +00:00
|
|
|
ui.favoritesFolder->setText( group.favoritesFolder );
|
|
|
|
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( ui.groupIcon, &QComboBox::activated, this, &DictGroupWidget::groupIconActivated, Qt::QueuedConnection );
|
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, &DictGroupWidget::showDictInfo );
|
2012-12-20 13:25:38 +00:00
|
|
|
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( ui.dictionaries, &QAbstractItemView::doubleClicked, this, &DictGroupWidget::removeCurrentItem );
|
2010-07-05 18:36:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DictGroupWidget::groupIconActivated( int index )
|
|
|
|
{
|
|
|
|
if ( index == 1 )
|
|
|
|
{
|
2010-07-05 18:50:54 +00:00
|
|
|
QList< QByteArray > supImageFormats = QImageReader::supportedImageFormats();
|
|
|
|
|
|
|
|
QString formatList( " (" );
|
|
|
|
|
|
|
|
for( int x = 0; x < supImageFormats.size(); ++x )
|
2013-02-03 20:19:55 +00:00
|
|
|
formatList += "*." + QString::fromLatin1( supImageFormats[ x ] ) + " ";
|
2010-07-05 18:50:54 +00:00
|
|
|
|
|
|
|
formatList.chop( 1 );
|
|
|
|
formatList.append( ")" );
|
|
|
|
|
2010-07-05 18:36:03 +00:00
|
|
|
QString chosenFile =
|
|
|
|
QFileDialog::getOpenFileName( this, tr( "Choose a file to use as group icon" ),
|
2010-07-05 18:50:54 +00:00
|
|
|
QString(),
|
|
|
|
tr( "Images" ) + formatList + ";;" +
|
|
|
|
tr( "All files" ) + " (*.*)" );
|
2010-07-05 18:36:03 +00:00
|
|
|
|
|
|
|
if ( !chosenFile.isEmpty() )
|
|
|
|
{
|
|
|
|
QIcon icon( chosenFile );
|
|
|
|
|
|
|
|
if ( icon.isNull() )
|
|
|
|
QMessageBox::critical( this, tr( "Error" ), tr( "Can't read the specified image file." ) );
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ui.groupIcon->setItemIcon( 1, icon );
|
|
|
|
|
|
|
|
QString baseName = QFileInfo( chosenFile ).completeBaseName();
|
|
|
|
ui.groupIcon->setItemText( 1, baseName );
|
|
|
|
ui.groupIcon->setItemData( 1, baseName );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Config::Group DictGroupWidget::makeGroup() const
|
|
|
|
{
|
|
|
|
Instances::Group g( "" );
|
|
|
|
|
2009-04-10 12:48:40 +00:00
|
|
|
g.id = groupId;
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
g.dictionaries = ui.dictionaries->getCurrentDictionaries();
|
|
|
|
|
2010-07-05 18:36:03 +00:00
|
|
|
int currentIndex = ui.groupIcon->currentIndex();
|
|
|
|
|
|
|
|
if ( currentIndex == 1 ) // File
|
|
|
|
g.iconData = ui.groupIcon->itemIcon( currentIndex );
|
|
|
|
|
|
|
|
g.icon = ui.groupIcon->itemData( currentIndex ).toString();
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2022-12-29 12:10:21 +00:00
|
|
|
g.shortcut = ui.shortcut->keySequence();
|
2010-07-05 14:13:29 +00:00
|
|
|
|
2017-05-05 14:39:51 +00:00
|
|
|
g.favoritesFolder = ui.favoritesFolder->text().replace( '\\', '/' );
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
return g.makeConfigGroup();
|
|
|
|
}
|
|
|
|
|
2012-12-20 13:25:03 +00:00
|
|
|
void DictGroupWidget::showDictInfo( QPoint const & pos )
|
|
|
|
{
|
|
|
|
QVariant data = ui.dictionaries->getModel()->data( ui.dictionaries->indexAt( pos ), Qt::EditRole );
|
|
|
|
QString id;
|
|
|
|
if( data.canConvert< QString >() )
|
|
|
|
id = data.toString();
|
|
|
|
|
|
|
|
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;
|
|
|
|
if( n < dicts.size() )
|
|
|
|
emit showDictionaryInfo( id );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-20 13:25:38 +00:00
|
|
|
void DictGroupWidget::removeCurrentItem( QModelIndex const & index )
|
|
|
|
{
|
|
|
|
(void)index;
|
|
|
|
ui.dictionaries->getModel()->removeSelectedRows( ui.dictionaries->selectionModel() );
|
|
|
|
}
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
/// DictListModel
|
|
|
|
|
|
|
|
void DictListModel::populate(
|
|
|
|
std::vector< sptr< Dictionary::Class > > const & active,
|
|
|
|
std::vector< sptr< Dictionary::Class > > const & available )
|
|
|
|
{
|
|
|
|
dictionaries = active;
|
|
|
|
allDicts = &available;
|
|
|
|
|
2013-05-30 13:24:21 +00:00
|
|
|
beginResetModel();
|
|
|
|
endResetModel();
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2012-12-18 12:33:53 +00:00
|
|
|
void DictListModel::populate(
|
|
|
|
std::vector< sptr< Dictionary::Class > > const & active )
|
|
|
|
{
|
|
|
|
dictionaries = active;
|
2013-05-30 13:24:21 +00:00
|
|
|
beginResetModel();
|
|
|
|
endResetModel();
|
2012-12-18 12:33:53 +00:00
|
|
|
}
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
void DictListModel::setAsSource()
|
|
|
|
{
|
|
|
|
isSource = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector< sptr< Dictionary::Class > > const &
|
|
|
|
DictListModel::getCurrentDictionaries() const
|
|
|
|
{
|
|
|
|
return dictionaries;
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::ItemFlags DictListModel::flags( QModelIndex const & index ) const
|
|
|
|
{
|
2009-04-27 09:49:40 +00:00
|
|
|
Qt::ItemFlags defaultFlags = QAbstractListModel::flags( index );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
if (index.isValid())
|
|
|
|
return Qt::ItemIsDragEnabled | defaultFlags;
|
|
|
|
else
|
|
|
|
return Qt::ItemIsDropEnabled | defaultFlags;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DictListModel::rowCount( QModelIndex const & ) const
|
|
|
|
{
|
|
|
|
return dictionaries.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant DictListModel::data( QModelIndex const & index, int role ) const
|
|
|
|
{
|
2012-12-20 13:25:03 +00:00
|
|
|
if( index.row() < 0 )
|
|
|
|
return QVariant();
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
sptr< Dictionary::Class > const & item = dictionaries[ index.row() ];
|
|
|
|
|
|
|
|
if ( !item )
|
|
|
|
return QVariant();
|
|
|
|
|
2009-04-27 09:49:40 +00:00
|
|
|
switch ( role )
|
2009-04-21 19:03:16 +00:00
|
|
|
{
|
2009-05-05 18:32:23 +00:00
|
|
|
case Qt::ToolTipRole:
|
|
|
|
{
|
|
|
|
QString tt = "<b>" + QString::fromUtf8( item->getName().c_str() ) + "</b>";
|
|
|
|
|
2009-05-24 16:38:08 +00:00
|
|
|
QString lfrom( Language::localizedNameForId( item->getLangFrom() ) );
|
|
|
|
QString lto( Language::localizedNameForId( item->getLangTo() ) );
|
2009-05-05 18:32:23 +00:00
|
|
|
if ( !lfrom.isEmpty() )
|
|
|
|
{
|
|
|
|
if ( lfrom == lto )
|
|
|
|
tt += "<br>" + lfrom;
|
|
|
|
else
|
|
|
|
tt += "<br>" + lfrom + " - " + lto;
|
|
|
|
}
|
|
|
|
|
|
|
|
int entries = item->getArticleCount();
|
|
|
|
if ( !entries )
|
|
|
|
entries = item->getWordCount();
|
|
|
|
if ( entries )
|
|
|
|
tt += "<br>" + tr( "%1 entries" ).arg( entries );
|
|
|
|
|
|
|
|
const std::vector< std::string > & dirs = item->getDictionaryFilenames();
|
|
|
|
|
|
|
|
if ( dirs.size() )
|
|
|
|
{
|
|
|
|
tt += "<hr>";
|
2011-09-09 12:05:28 +00:00
|
|
|
tt += FsEncoding::decode( dirs.at( 0 ).c_str() );
|
2009-05-05 18:32:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tt.replace( " ", " " );
|
|
|
|
return tt;
|
|
|
|
}
|
|
|
|
|
2009-04-27 09:49:40 +00:00
|
|
|
case Qt::DisplayRole :
|
|
|
|
return QString::fromUtf8( item->getName().c_str() );
|
2009-04-21 19:03:16 +00:00
|
|
|
|
2009-04-27 09:49:40 +00:00
|
|
|
case Qt::EditRole :
|
|
|
|
return QString::fromUtf8( item->getId().c_str() );
|
2009-04-21 19:03:16 +00:00
|
|
|
|
2009-04-27 09:49:40 +00:00
|
|
|
case Qt::DecorationRole:
|
2011-07-15 12:11:08 +00:00
|
|
|
// make all icons of the same size to avoid visual size/alignment problems
|
2022-03-14 15:18:53 +00:00
|
|
|
return item->getIcon();
|
2009-04-21 19:03:16 +00:00
|
|
|
|
|
|
|
default:;
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2009-05-03 19:24:25 +00:00
|
|
|
bool DictListModel::insertRows( int row, int count, const QModelIndex & parent )
|
|
|
|
{
|
|
|
|
if ( isSource )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
beginInsertRows( parent, row, row + count - 1 );
|
|
|
|
dictionaries.insert( dictionaries.begin() + row, count,
|
|
|
|
sptr< Dictionary::Class >() );
|
|
|
|
endInsertRows();
|
2012-12-21 20:20:54 +00:00
|
|
|
emit contentChanged();
|
2009-05-03 19:24:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-09-16 18:50:31 +00:00
|
|
|
void DictListModel::addRow(const QModelIndex & parent, sptr< Dictionary::Class > dict)
|
|
|
|
{
|
2012-09-26 13:00:35 +00:00
|
|
|
for (unsigned i = 0; i < dictionaries.size(); i++)
|
2010-09-16 18:50:31 +00:00
|
|
|
{
|
|
|
|
if (dictionaries[i]->getId() == dict->getId())
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
beginInsertRows( parent, dictionaries.size(), dictionaries.size()+1 );
|
|
|
|
dictionaries.push_back(dict);
|
|
|
|
endInsertRows();
|
2012-12-21 20:20:54 +00:00
|
|
|
emit contentChanged();
|
2010-09-16 18:50:31 +00:00
|
|
|
}
|
|
|
|
|
2009-05-03 19:24:25 +00:00
|
|
|
bool DictListModel::removeRows( int row, int count,
|
|
|
|
const QModelIndex & parent )
|
|
|
|
{
|
|
|
|
if ( isSource )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
beginRemoveRows( parent, row, row + count - 1 );
|
|
|
|
dictionaries.erase( dictionaries.begin() + row,
|
|
|
|
dictionaries.begin() + row + count );
|
|
|
|
endRemoveRows();
|
2012-12-21 20:20:54 +00:00
|
|
|
emit contentChanged();
|
2009-05-03 19:24:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
bool DictListModel::setData( QModelIndex const & index, const QVariant & value,
|
|
|
|
int role )
|
|
|
|
{
|
|
|
|
if ( isSource || !allDicts || !index.isValid() ||
|
|
|
|
index.row() >= (int)dictionaries.size() )
|
|
|
|
return false;
|
|
|
|
|
2009-04-21 19:03:16 +00:00
|
|
|
if ( ( role == Qt::DisplayRole ) || ( role == Qt::DecorationRole ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
// Allow changing that, but do nothing
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-05-03 19:24:25 +00:00
|
|
|
if ( role == Qt::EditRole )
|
|
|
|
{
|
|
|
|
Config::Group g;
|
|
|
|
|
|
|
|
g.dictionaries.push_back( Config::DictionaryRef( value.toString(), QString() ) );
|
|
|
|
|
2022-07-08 13:47:09 +00:00
|
|
|
Instances::Group i( g, *allDicts, Config::Group() );
|
2009-05-03 19:24:25 +00:00
|
|
|
|
|
|
|
if ( i.dictionaries.size() == 1 )
|
|
|
|
{
|
|
|
|
// Found that dictionary
|
|
|
|
dictionaries[ index.row() ] = i.dictionaries.front();
|
|
|
|
|
|
|
|
emit dataChanged( index, index );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-27 09:49:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::DropActions DictListModel::supportedDropActions() const
|
|
|
|
{
|
|
|
|
return Qt::MoveAction;
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2009-05-02 17:12:52 +00:00
|
|
|
void DictListModel::removeSelectedRows( QItemSelectionModel * source )
|
|
|
|
{
|
|
|
|
if ( !source )
|
|
|
|
return;
|
|
|
|
|
|
|
|
QModelIndexList rows = source->selectedRows();
|
|
|
|
|
|
|
|
if ( !rows.count() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
for ( int i = rows.count()-1; i >= 0; --i )
|
|
|
|
{
|
|
|
|
dictionaries.erase( dictionaries.begin() + rows.at( i ).row() );
|
|
|
|
}
|
|
|
|
|
2013-05-30 13:24:21 +00:00
|
|
|
beginResetModel();
|
|
|
|
endResetModel();
|
2012-12-21 20:20:54 +00:00
|
|
|
emit contentChanged();
|
2009-05-02 17:12:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DictListModel::addSelectedUniqueFromModel( QItemSelectionModel * source )
|
|
|
|
{
|
|
|
|
if ( !source )
|
|
|
|
return;
|
|
|
|
|
|
|
|
QModelIndexList rows = source->selectedRows();
|
|
|
|
|
|
|
|
if ( !rows.count() )
|
|
|
|
return;
|
|
|
|
|
2013-01-04 13:24:08 +00:00
|
|
|
const QSortFilterProxyModel * proxyModel = dynamic_cast< const QSortFilterProxyModel * > ( source->model() );
|
|
|
|
|
|
|
|
const DictListModel * baseModel;
|
|
|
|
|
|
|
|
if ( proxyModel )
|
|
|
|
{
|
|
|
|
baseModel = dynamic_cast< const DictListModel * > ( proxyModel->sourceModel() );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
baseModel = dynamic_cast< const DictListModel * > ( source->model() );
|
|
|
|
}
|
|
|
|
|
2009-05-02 17:12:52 +00:00
|
|
|
if ( !baseModel )
|
|
|
|
return;
|
|
|
|
|
|
|
|
QVector< std::string > list;
|
|
|
|
QVector< std::string > dicts;
|
2009-05-18 11:58:19 +00:00
|
|
|
for ( unsigned i = 0; i < dictionaries.size(); i++ )
|
2009-05-02 17:12:52 +00:00
|
|
|
dicts.append( dictionaries.at( i )->getId() );
|
|
|
|
|
|
|
|
for ( int i = 0; i < rows.count(); i++ )
|
|
|
|
{
|
2013-01-04 13:24:08 +00:00
|
|
|
QModelIndex idx = proxyModel ? proxyModel->mapToSource(rows.at( i )) : rows.at( i );
|
|
|
|
std::string id = baseModel->dictionaries.at( idx.row() )->getId();
|
2009-05-02 17:12:52 +00:00
|
|
|
|
|
|
|
if ( !dicts.contains( id ) )
|
|
|
|
list.append( id );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( list.empty() )
|
|
|
|
return;
|
|
|
|
|
2017-05-30 18:27:45 +00:00
|
|
|
for ( int j = 0; j < list.size(); j++ )
|
2009-05-02 17:12:52 +00:00
|
|
|
{
|
2017-05-30 18:27:45 +00:00
|
|
|
for ( unsigned i = 0; i < allDicts->size(); i++ )
|
2009-05-02 17:12:52 +00:00
|
|
|
{
|
|
|
|
if ( allDicts->at( i )->getId() == list.at( j ) )
|
|
|
|
{
|
|
|
|
dictionaries.push_back( allDicts->at( i ) );
|
2009-05-02 19:57:52 +00:00
|
|
|
break;
|
2009-05-02 17:12:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-30 13:24:21 +00:00
|
|
|
beginResetModel();
|
|
|
|
endResetModel();
|
2012-12-21 20:20:54 +00:00
|
|
|
emit contentChanged();
|
2009-05-02 17:12:52 +00:00
|
|
|
}
|
|
|
|
|
2009-05-03 19:24:25 +00:00
|
|
|
void DictListModel::filterDuplicates()
|
|
|
|
{
|
|
|
|
QSet< QString > ids;
|
|
|
|
bool doReset = false;
|
|
|
|
|
2009-05-18 11:58:19 +00:00
|
|
|
for ( unsigned i = 0; i < dictionaries.size(); i++ )
|
2009-05-03 19:24:25 +00:00
|
|
|
{
|
|
|
|
QString id = QString::fromStdString( dictionaries.at( i )->getId() );
|
|
|
|
|
|
|
|
if ( ids.contains( id ) )
|
|
|
|
{
|
2009-05-05 18:32:23 +00:00
|
|
|
dictionaries.erase( dictionaries.begin() + i-- );
|
2009-05-03 19:24:25 +00:00
|
|
|
doReset = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ids.insert( id );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( doReset )
|
2012-12-21 20:20:54 +00:00
|
|
|
{
|
2013-05-30 13:24:21 +00:00
|
|
|
beginResetModel();
|
|
|
|
endResetModel();
|
2012-12-21 20:20:54 +00:00
|
|
|
emit contentChanged();
|
|
|
|
}
|
2009-05-03 19:24:25 +00:00
|
|
|
}
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
/// DictListWidget
|
|
|
|
|
2009-04-27 09:49:40 +00:00
|
|
|
DictListWidget::DictListWidget( QWidget * parent ): QListView( parent ),
|
2009-01-28 20:55:45 +00:00
|
|
|
model( this )
|
|
|
|
{
|
|
|
|
setModel( &model );
|
|
|
|
|
|
|
|
setSelectionMode( ExtendedSelection );
|
|
|
|
|
2009-04-27 09:49:40 +00:00
|
|
|
setDragEnabled( true );
|
|
|
|
setAcceptDrops( true );
|
|
|
|
setDropIndicatorShown( true );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DictListWidget::~DictListWidget()
|
|
|
|
{
|
|
|
|
setModel( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
void DictListWidget::populate(
|
|
|
|
std::vector< sptr< Dictionary::Class > > const & active,
|
|
|
|
std::vector< sptr< Dictionary::Class > > const & available )
|
|
|
|
{
|
|
|
|
model.populate( active, available );
|
|
|
|
}
|
|
|
|
|
2012-12-18 12:33:53 +00:00
|
|
|
void DictListWidget::populate(
|
|
|
|
std::vector< sptr< Dictionary::Class > > const & active )
|
|
|
|
{
|
|
|
|
model.populate( active );
|
|
|
|
}
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
void DictListWidget::setAsSource()
|
|
|
|
{
|
|
|
|
setDropIndicatorShown( false );
|
|
|
|
model.setAsSource();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector< sptr< Dictionary::Class > > const &
|
|
|
|
DictListWidget::getCurrentDictionaries() const
|
|
|
|
{
|
|
|
|
return model.getCurrentDictionaries();
|
|
|
|
}
|
|
|
|
|
2009-05-24 14:24:13 +00:00
|
|
|
void DictListWidget::dropEvent( QDropEvent * event )
|
2009-05-02 19:57:52 +00:00
|
|
|
{
|
2009-05-18 11:58:19 +00:00
|
|
|
DictListWidget * sourceList = dynamic_cast< DictListWidget * > ( event->source() );
|
2009-05-03 18:46:19 +00:00
|
|
|
|
2009-05-03 19:24:25 +00:00
|
|
|
QListView::dropEvent( event );
|
2009-05-03 18:46:19 +00:00
|
|
|
|
2009-05-18 11:58:19 +00:00
|
|
|
if ( sourceList != this )
|
2009-05-03 18:46:19 +00:00
|
|
|
{
|
2009-05-03 19:24:25 +00:00
|
|
|
model.filterDuplicates();
|
2009-05-02 19:57:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-07 08:52:36 +00:00
|
|
|
void DictListWidget::focusInEvent( QFocusEvent * )
|
|
|
|
{
|
|
|
|
emit gotFocus();
|
|
|
|
}
|
|
|
|
|
2009-05-24 14:24:13 +00:00
|
|
|
void DictListWidget::rowsInserted( QModelIndex const & parent, int start, int end )
|
|
|
|
{
|
|
|
|
QListView::rowsInserted( parent, start, end );
|
|
|
|
|
|
|
|
// When inserting new rows, make the first of them current
|
|
|
|
selectionModel()->setCurrentIndex( model.index( start, 0, parent ),
|
|
|
|
QItemSelectionModel::NoUpdate );
|
|
|
|
}
|
|
|
|
|
|
|
|
void DictListWidget::rowsAboutToBeRemoved( QModelIndex const & parent, int start, int end )
|
|
|
|
{
|
|
|
|
// When removing rows, if the current row is among the removed ones, select
|
|
|
|
// an item just before the first row to be removed, if there's one.
|
|
|
|
QModelIndex current = currentIndex();
|
|
|
|
|
|
|
|
if ( current.isValid() && current.row() &&
|
|
|
|
current.row() >= start && current.row() <= end )
|
|
|
|
selectionModel()->setCurrentIndex( model.index( current.row() - 1, 0, parent ),
|
|
|
|
QItemSelectionModel::NoUpdate );
|
|
|
|
|
|
|
|
QListView::rowsAboutToBeRemoved( parent, start, end );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
// DictGroupsWidget
|
|
|
|
|
2009-04-21 19:03:16 +00:00
|
|
|
DictGroupsWidget::DictGroupsWidget( QWidget * parent ):
|
2012-12-18 12:35:11 +00:00
|
|
|
QTabWidget( parent ), nextId( 1 ), allDicts( 0 ), activeDicts( 0 )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
setMovable( true );
|
2012-12-21 11:48:41 +00:00
|
|
|
setContextMenuPolicy( Qt::CustomContextMenu );
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( this, &QWidget::customContextMenuRequested, this, &DictGroupsWidget::contextMenu );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2009-04-10 21:37:16 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
QString escapeAmps( QString const & str )
|
|
|
|
{
|
|
|
|
QString result( str );
|
|
|
|
result.replace( "&", "&&" );
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString unescapeAmps( QString const & str )
|
|
|
|
{
|
|
|
|
QString result( str );
|
|
|
|
result.replace( "&&", "&" );
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
void DictGroupsWidget::populate( Config::Groups const & groups,
|
2012-12-18 12:35:11 +00:00
|
|
|
vector< sptr< Dictionary::Class > > const & allDicts_,
|
|
|
|
vector< sptr< Dictionary::Class > > const & activeDicts_ )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
while( count() )
|
|
|
|
removeCurrentGroup();
|
|
|
|
|
|
|
|
allDicts = &allDicts_;
|
2012-12-18 12:35:11 +00:00
|
|
|
activeDicts = &activeDicts_;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2012-12-10 12:49:45 +00:00
|
|
|
for( int x = 0; x < groups.size(); ++x )
|
2012-12-20 13:25:03 +00:00
|
|
|
{
|
|
|
|
DictGroupWidget *gr = new DictGroupWidget( this, *allDicts, groups[ x ] );
|
|
|
|
addTab( gr, escapeAmps( groups[ x ].name ) );
|
2022-03-14 11:52:18 +00:00
|
|
|
connect( gr, &DictGroupWidget::showDictionaryInfo,this, &DictGroupsWidget::showDictionaryInfo );
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( gr->getModel(), &DictListModel::contentChanged, this, &DictGroupsWidget::tabDataChanged );
|
2012-12-21 20:20:54 +00:00
|
|
|
|
|
|
|
setCurrentIndex( x );
|
|
|
|
QString toolTipStr = "\"" + tabText( x ) + "\"\n" + tr( "Dictionaries: " )
|
|
|
|
+ QString::number( getCurrentModel()->getCurrentDictionaries().size() );
|
|
|
|
setTabToolTip( x, toolTipStr );
|
2012-12-20 13:25:03 +00:00
|
|
|
}
|
2009-04-10 12:48:40 +00:00
|
|
|
|
|
|
|
nextId = groups.nextId;
|
2012-11-28 17:50:31 +00:00
|
|
|
|
2012-12-21 20:20:54 +00:00
|
|
|
setCurrentIndex( 0 );
|
|
|
|
|
2012-11-28 17:50:31 +00:00
|
|
|
setUsesScrollButtons( count() > 3 );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates groups from what is currently set up
|
|
|
|
Config::Groups DictGroupsWidget::makeGroups() const
|
|
|
|
{
|
|
|
|
Config::Groups result;
|
|
|
|
|
2009-04-10 12:48:40 +00:00
|
|
|
result.nextId = nextId;
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
for( int x = 0; x < count(); ++x )
|
|
|
|
{
|
|
|
|
result.push_back( dynamic_cast< DictGroupWidget & >( *widget( x ) ).makeGroup() );
|
2009-04-10 21:37:16 +00:00
|
|
|
result.back().name = unescapeAmps( tabText( x ) );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-05-02 17:12:52 +00:00
|
|
|
DictListModel * DictGroupsWidget::getCurrentModel() const
|
|
|
|
{
|
|
|
|
int current = currentIndex();
|
|
|
|
|
|
|
|
if ( current >= 0 )
|
|
|
|
{
|
|
|
|
DictGroupWidget * w = ( DictGroupWidget * ) widget( current );
|
|
|
|
return w->getModel();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
QItemSelectionModel * DictGroupsWidget::getCurrentSelectionModel() const
|
|
|
|
{
|
|
|
|
int current = currentIndex();
|
|
|
|
|
|
|
|
if ( current >= 0 )
|
|
|
|
{
|
|
|
|
DictGroupWidget * w = ( DictGroupWidget * ) widget( current );
|
|
|
|
return w->getSelectionModel();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
void DictGroupsWidget::addNewGroup( QString const & name )
|
|
|
|
{
|
|
|
|
if ( !allDicts )
|
|
|
|
return;
|
|
|
|
|
|
|
|
int idx = currentIndex() + 1;
|
|
|
|
|
2009-04-10 12:48:40 +00:00
|
|
|
Config::Group newGroup;
|
|
|
|
|
|
|
|
newGroup.id = nextId++;
|
|
|
|
|
2012-12-20 13:25:03 +00:00
|
|
|
DictGroupWidget *gr = new DictGroupWidget( this, *allDicts, newGroup );
|
|
|
|
insertTab( idx, gr, escapeAmps( name ) );
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( gr, &DictGroupWidget::showDictionaryInfo, this, &DictGroupsWidget::showDictionaryInfo );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
setCurrentIndex( idx );
|
2012-10-19 15:30:15 +00:00
|
|
|
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( gr->getModel(), &DictListModel::contentChanged, this, &DictGroupsWidget::tabDataChanged );
|
2012-12-21 20:20:54 +00:00
|
|
|
|
|
|
|
QString toolTipStr = "\"" + tabText( idx ) + "\"\n" + tr( "Dictionaries: " )
|
|
|
|
+ QString::number( getCurrentModel()->getCurrentDictionaries().size() );
|
|
|
|
setTabToolTip( idx, toolTipStr );
|
|
|
|
|
2012-10-19 15:30:15 +00:00
|
|
|
setUsesScrollButtons( count() > 3 );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2012-12-21 11:48:41 +00:00
|
|
|
int DictGroupsWidget::addUniqueGroup( const QString & name )
|
|
|
|
{
|
|
|
|
for( int n = 0; n < count(); n++ )
|
|
|
|
if( tabText( n ) == name )
|
|
|
|
{
|
|
|
|
setCurrentIndex( n );
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
addNewGroup( name );
|
|
|
|
return currentIndex();
|
|
|
|
}
|
|
|
|
|
2010-09-16 18:50:31 +00:00
|
|
|
void DictGroupsWidget::addAutoGroups()
|
|
|
|
{
|
2012-12-18 12:35:11 +00:00
|
|
|
if( !activeDicts )
|
2010-09-16 18:50:31 +00:00
|
|
|
return;
|
|
|
|
|
2010-11-13 22:56:25 +00:00
|
|
|
if ( QMessageBox::information( this, tr( "Confirmation" ),
|
|
|
|
tr( "Are you sure you want to generate a set of groups "
|
|
|
|
"based on language pairs?" ), QMessageBox::Yes,
|
|
|
|
QMessageBox::Cancel ) != QMessageBox::Yes )
|
|
|
|
return;
|
|
|
|
|
2012-12-18 12:35:11 +00:00
|
|
|
QMap< QString, QVector< sptr<Dictionary::Class> > > dictMap;
|
2012-12-19 12:59:38 +00:00
|
|
|
QMap< QString, QVector< sptr<Dictionary::Class> > > morphoMap;
|
|
|
|
|
|
|
|
// Put active dictionaries into lists
|
2010-09-16 18:50:31 +00:00
|
|
|
|
2012-12-18 12:35:11 +00:00
|
|
|
for ( unsigned i = 0; i < activeDicts->size(); i++ )
|
2010-09-16 18:50:31 +00:00
|
|
|
{
|
2012-12-18 12:35:11 +00:00
|
|
|
sptr<Dictionary::Class> dict = activeDicts->at( i );
|
2010-09-16 18:50:31 +00:00
|
|
|
|
2012-12-19 12:59:38 +00:00
|
|
|
int idFrom = dict->getLangFrom();
|
|
|
|
int idTo = dict->getLangTo();
|
|
|
|
if( idFrom == 0)
|
|
|
|
{
|
|
|
|
// Attempt to find language pair in dictionary name
|
|
|
|
|
|
|
|
QPair<quint32,quint32> ids = LangCoder::findIdsForName( QString::fromUtf8( dict->getName().c_str() ) );
|
|
|
|
idFrom = ids.first;
|
|
|
|
idTo = ids.second;
|
|
|
|
}
|
|
|
|
|
2013-01-22 10:13:39 +00:00
|
|
|
QString name( tr( "Unassigned" ) );
|
2012-12-19 12:59:38 +00:00
|
|
|
if ( idFrom != 0 && idTo != 0 )
|
2010-09-16 18:50:31 +00:00
|
|
|
{
|
2012-12-19 12:59:38 +00:00
|
|
|
QString lfrom = LangCoder::intToCode2( idFrom );
|
|
|
|
QString lto = LangCoder::intToCode2( idTo );
|
2012-12-18 12:35:11 +00:00
|
|
|
lfrom[ 0 ] = lfrom[ 0 ].toTitleCase();
|
|
|
|
lto[ 0 ] = lto[ 0 ].toTitleCase();
|
|
|
|
name = lfrom + " - " + lto;
|
2010-09-16 18:50:31 +00:00
|
|
|
}
|
2012-12-19 12:59:38 +00:00
|
|
|
else if( !dict->getDictionaryFilenames().empty() )
|
|
|
|
{
|
|
|
|
// Handle special case - morphology dictionaries
|
|
|
|
|
|
|
|
QString fileName = QFileInfo( FsEncoding::decode( dict->getDictionaryFilenames()[ 0 ].c_str() ) ).fileName();
|
|
|
|
if( fileName.endsWith( ".aff", Qt::CaseInsensitive ) )
|
|
|
|
{
|
|
|
|
QString code = fileName.left( 2 ).toLower();
|
|
|
|
QVector<sptr<Dictionary::Class> > vd = morphoMap[ code ];
|
|
|
|
vd.append( dict );
|
|
|
|
morphoMap[ code ] = vd;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2010-09-16 18:50:31 +00:00
|
|
|
|
2012-12-18 12:35:11 +00:00
|
|
|
QVector<sptr<Dictionary::Class> > vd = dictMap[ name ];
|
|
|
|
vd.append( dict );
|
|
|
|
dictMap[ name ] = vd;
|
|
|
|
}
|
|
|
|
|
2022-01-08 13:16:22 +00:00
|
|
|
QStringList groupList = dictMap.keys();
|
|
|
|
QStringList morphoList = morphoMap.keys();
|
2012-12-19 12:59:38 +00:00
|
|
|
|
|
|
|
// Insert morphology dictionaries into corresponding lists
|
|
|
|
|
|
|
|
for( QStringList::ConstIterator ln = morphoList.begin(); ln != morphoList.end(); ++ln )
|
|
|
|
{
|
|
|
|
for( QStringList::ConstIterator gr = groupList.begin(); gr != groupList.end(); ++gr )
|
|
|
|
if( ln->compare( gr->left( 2 ), Qt::CaseInsensitive ) == 0 )
|
|
|
|
{
|
|
|
|
QVector<sptr<Dictionary::Class> > vdg = dictMap[ *gr ];
|
|
|
|
vdg += morphoMap[ *ln ];
|
|
|
|
dictMap[ *gr ] = vdg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make groups
|
|
|
|
|
2012-12-18 12:35:11 +00:00
|
|
|
for( QStringList::ConstIterator gr = groupList.begin(); gr != groupList.end(); ++gr )
|
|
|
|
{
|
2012-12-21 11:48:41 +00:00
|
|
|
if( count() )
|
|
|
|
setCurrentIndex( count() - 1 );
|
2010-09-16 18:50:31 +00:00
|
|
|
|
2012-12-21 11:48:41 +00:00
|
|
|
addUniqueGroup( *gr );
|
2010-09-16 18:50:31 +00:00
|
|
|
|
2012-12-18 12:35:11 +00:00
|
|
|
// add dictionaries into the current group
|
|
|
|
QVector< sptr<Dictionary::Class> > vd = dictMap[ *gr ];
|
2010-09-16 18:50:31 +00:00
|
|
|
DictListModel *model = getCurrentModel();
|
2012-12-18 12:35:11 +00:00
|
|
|
for( int i = 0; i < vd.count(); i++ )
|
|
|
|
model->addRow(QModelIndex(), vd.at( i ) );
|
2010-09-16 18:50:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-10 21:37:16 +00:00
|
|
|
QString DictGroupsWidget::getCurrentGroupName() const
|
|
|
|
{
|
|
|
|
int current = currentIndex();
|
|
|
|
|
|
|
|
if ( current >= 0 )
|
|
|
|
return unescapeAmps( tabText( current ) );
|
|
|
|
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
void DictGroupsWidget::renameCurrentGroup( QString const & name )
|
|
|
|
{
|
|
|
|
int current = currentIndex();
|
|
|
|
|
|
|
|
if ( current >= 0 )
|
2009-04-10 21:37:16 +00:00
|
|
|
setTabText( current, escapeAmps( name ) );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DictGroupsWidget::removeCurrentGroup()
|
|
|
|
{
|
|
|
|
int current = currentIndex();
|
|
|
|
|
|
|
|
if ( current >= 0 )
|
|
|
|
{
|
|
|
|
QWidget * w = widget( current );
|
|
|
|
removeTab( current );
|
|
|
|
delete w;
|
|
|
|
}
|
2012-10-19 15:30:15 +00:00
|
|
|
|
|
|
|
setUsesScrollButtons( count() > 3 );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
2009-04-29 23:32:42 +00:00
|
|
|
|
|
|
|
void DictGroupsWidget::removeAllGroups()
|
|
|
|
{
|
|
|
|
while ( count() )
|
|
|
|
{
|
|
|
|
QWidget * w = widget( 0 );
|
|
|
|
removeTab( 0 );
|
|
|
|
delete w;
|
|
|
|
}
|
|
|
|
}
|
2012-12-21 11:48:41 +00:00
|
|
|
|
2012-12-21 16:46:17 +00:00
|
|
|
void DictGroupsWidget::combineGroups( int source, int target )
|
|
|
|
{
|
|
|
|
if( source < 0 || source >= count() || target < 0 || target >= count() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
setCurrentIndex( source );
|
|
|
|
vector< sptr< Dictionary::Class > > const & dicts = getCurrentModel()->getCurrentDictionaries();
|
|
|
|
|
|
|
|
setCurrentIndex( target );
|
|
|
|
DictListModel *model = getCurrentModel();
|
|
|
|
|
2022-12-26 02:08:17 +00:00
|
|
|
disconnect( model, &DictListModel::contentChanged, this, &DictGroupsWidget::tabDataChanged );
|
2012-12-21 20:20:54 +00:00
|
|
|
|
2012-12-21 16:46:17 +00:00
|
|
|
for( unsigned i = 0; i < dicts.size(); i++ )
|
|
|
|
model->addRow( QModelIndex(), dicts[ i ] );
|
2012-12-21 20:20:54 +00:00
|
|
|
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( model, &DictListModel::contentChanged, this, &DictGroupsWidget::tabDataChanged );
|
2012-12-21 20:20:54 +00:00
|
|
|
|
|
|
|
QString toolTipStr = "\"" + tabText( target ) + "\"\n" + tr( "Dictionaries: " )
|
|
|
|
+ QString::number( model->getCurrentDictionaries().size() );
|
|
|
|
setTabToolTip( target, toolTipStr );
|
2012-12-21 16:46:17 +00:00
|
|
|
}
|
|
|
|
|
2012-12-21 11:48:41 +00:00
|
|
|
void DictGroupsWidget::contextMenu( QPoint const & pos )
|
|
|
|
{
|
|
|
|
int clickedGroup = tabBar()->tabAt( pos );
|
|
|
|
if( clickedGroup < 0 )
|
|
|
|
return;
|
|
|
|
QString name = tabText( clickedGroup );
|
|
|
|
if( name.length() != 7 || name.mid( 2, 3 ) != " - " )
|
|
|
|
return;
|
|
|
|
|
|
|
|
QMenu menu( this );
|
|
|
|
|
|
|
|
QAction *combineSourceAction = new QAction( QString( tr( "Combine groups by source language to \"%1->\"" ) )
|
|
|
|
.arg( name.left( 2 ) ), &menu );
|
|
|
|
combineSourceAction->setEnabled( false );
|
2012-12-21 16:46:17 +00:00
|
|
|
|
|
|
|
QString grLeft = name.left( 2 );
|
|
|
|
QString grRight = name.right( 2 );
|
2012-12-21 11:48:41 +00:00
|
|
|
for( int i = 0; i < count(); i++ )
|
|
|
|
{
|
|
|
|
QString str = tabText( i );
|
2012-12-21 16:46:17 +00:00
|
|
|
if( i != clickedGroup && str.length() == 7 && str.mid( 2, 3 ) == " - " && str.startsWith( grLeft ) )
|
2012-12-21 11:48:41 +00:00
|
|
|
{
|
|
|
|
combineSourceAction->setEnabled( true );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
menu.addAction( combineSourceAction );
|
|
|
|
|
|
|
|
QAction *combineTargetAction = new QAction( QString( tr( "Combine groups by target language to \"->%1\"" ) )
|
|
|
|
.arg( name.right( 2 ) ), &menu );
|
|
|
|
combineTargetAction->setEnabled( false );
|
2012-12-21 16:46:17 +00:00
|
|
|
|
2012-12-21 11:48:41 +00:00
|
|
|
for( int i = 0; i < count(); i++ )
|
|
|
|
{
|
|
|
|
QString str = tabText( i );
|
2012-12-21 16:46:17 +00:00
|
|
|
if( i != clickedGroup && str.length() == 7 && str.mid( 2, 3 ) == " - " && str.endsWith( grRight ) )
|
2012-12-21 11:48:41 +00:00
|
|
|
{
|
|
|
|
combineTargetAction->setEnabled( true );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
menu.addAction( combineTargetAction );
|
|
|
|
|
|
|
|
QAction *combineTwoSidedAction = NULL;
|
2012-12-21 16:46:17 +00:00
|
|
|
if( grLeft != grRight )
|
2012-12-21 11:48:41 +00:00
|
|
|
{
|
2012-12-21 16:46:17 +00:00
|
|
|
combineTwoSidedAction = new QAction( QString( tr( "Make two-side translate group \"%1-%2-%1\"" ) )
|
|
|
|
.arg( grLeft ).arg( grRight ), &menu );
|
2012-12-21 11:48:41 +00:00
|
|
|
|
|
|
|
combineTwoSidedAction->setEnabled( false );
|
|
|
|
|
2012-12-21 16:46:17 +00:00
|
|
|
QString str = grRight + " - " + grLeft;
|
2012-12-21 11:48:41 +00:00
|
|
|
for( int i = 0; i < count(); i++ )
|
|
|
|
{
|
|
|
|
if( str == tabText( i ) )
|
|
|
|
{
|
|
|
|
combineTwoSidedAction->setEnabled( true );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
menu.addAction( combineTwoSidedAction );
|
|
|
|
}
|
|
|
|
|
2012-12-21 16:46:17 +00:00
|
|
|
QAction *combineFirstAction = new QAction( QString( tr( "Combine groups with \"%1\"" ) )
|
|
|
|
.arg( grLeft ), &menu );
|
|
|
|
combineFirstAction->setEnabled( false );
|
|
|
|
for( int i = 0; i < count(); i++ )
|
|
|
|
{
|
|
|
|
QString str = tabText( i );
|
|
|
|
if( i != clickedGroup && str.length() == 7 && str.mid( 2, 3 ) == " - " &&
|
|
|
|
( str.startsWith( grLeft ) || str.endsWith( grLeft ) ) )
|
|
|
|
{
|
|
|
|
combineFirstAction->setEnabled( true );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
menu.addAction( combineFirstAction );
|
|
|
|
|
|
|
|
QAction *combineSecondAction = NULL;
|
|
|
|
|
|
|
|
if( grLeft != grRight )
|
|
|
|
{
|
|
|
|
combineSecondAction = new QAction( QString( tr( "Combine groups with \"%1\"" ) )
|
|
|
|
.arg( grRight ), &menu );
|
|
|
|
combineSecondAction->setEnabled( false );
|
|
|
|
|
|
|
|
for( int i = 0; i < count(); i++ )
|
|
|
|
{
|
|
|
|
QString str = tabText( i );
|
|
|
|
if( i != clickedGroup && str.length() == 7 && str.mid( 2, 3 ) == " - " &&
|
|
|
|
( str.startsWith( grRight ) || str.endsWith( grRight ) ) )
|
|
|
|
{
|
|
|
|
combineSecondAction->setEnabled( true );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
menu.addAction( combineSecondAction );
|
|
|
|
}
|
|
|
|
|
2012-12-21 11:48:41 +00:00
|
|
|
QAction *result = menu.exec( mapToGlobal( pos ) );
|
|
|
|
|
|
|
|
setUpdatesEnabled( false );
|
|
|
|
int targetGroup;
|
|
|
|
|
|
|
|
if( result && result == combineSourceAction )
|
|
|
|
{
|
|
|
|
setCurrentIndex( clickedGroup );
|
2012-12-21 16:46:17 +00:00
|
|
|
targetGroup = addUniqueGroup( grLeft + "->" );
|
2012-12-21 11:48:41 +00:00
|
|
|
|
|
|
|
for( int i = 0; i < count(); i++ )
|
|
|
|
{
|
|
|
|
QString str = tabText( i );
|
2012-12-21 16:46:17 +00:00
|
|
|
if( str.length() == 7 && str.mid( 2, 3 ) == " - " && str.startsWith( grLeft ) )
|
|
|
|
combineGroups( i, targetGroup );
|
2012-12-21 11:48:41 +00:00
|
|
|
}
|
2012-12-21 16:46:17 +00:00
|
|
|
|
2012-12-21 11:48:41 +00:00
|
|
|
setCurrentIndex( targetGroup );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if( result && result == combineTargetAction )
|
|
|
|
{
|
|
|
|
setCurrentIndex( clickedGroup );
|
2012-12-21 16:46:17 +00:00
|
|
|
targetGroup = addUniqueGroup( "->" + grRight );
|
2012-12-21 11:48:41 +00:00
|
|
|
|
|
|
|
for( int i = 0; i < count(); i++ )
|
|
|
|
{
|
|
|
|
QString str = tabText( i );
|
2012-12-21 16:46:17 +00:00
|
|
|
if( str.length() == 7 && str.mid( 2, 3 ) == " - " && str.endsWith( grRight ) )
|
|
|
|
combineGroups( i, targetGroup );
|
2012-12-21 11:48:41 +00:00
|
|
|
}
|
2012-12-21 16:46:17 +00:00
|
|
|
|
2012-12-21 11:48:41 +00:00
|
|
|
setCurrentIndex( targetGroup );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if( result && result == combineTwoSidedAction )
|
|
|
|
{
|
|
|
|
setCurrentIndex( clickedGroup );
|
2012-12-21 16:46:17 +00:00
|
|
|
targetGroup = addUniqueGroup( name + " - " + grLeft );
|
|
|
|
QString str = grRight + " - " + grLeft;
|
2012-12-21 11:48:41 +00:00
|
|
|
|
|
|
|
for( int i = 0; i < count(); i++ )
|
|
|
|
if( tabText( i ) == name || tabText( i ) == str )
|
2012-12-21 16:46:17 +00:00
|
|
|
combineGroups( i, targetGroup );
|
2012-12-21 11:48:41 +00:00
|
|
|
|
2012-12-21 16:46:17 +00:00
|
|
|
setCurrentIndex( targetGroup );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if( result && ( result == combineFirstAction || result == combineSecondAction ) )
|
|
|
|
{
|
|
|
|
QString const & grBase = result == combineFirstAction ? grLeft : grRight;
|
|
|
|
setCurrentIndex( clickedGroup );
|
|
|
|
targetGroup = addUniqueGroup( grBase );
|
2012-12-21 11:48:41 +00:00
|
|
|
|
2012-12-21 16:46:17 +00:00
|
|
|
for( int i = 0; i < count(); i++ )
|
|
|
|
{
|
|
|
|
QString str = tabText( i );
|
|
|
|
if( str.length() == 7 && str.mid( 2, 3 ) == " - " &&
|
|
|
|
( str.startsWith( grBase ) || str.endsWith( grBase ) ) )
|
|
|
|
combineGroups( i, targetGroup );
|
2012-12-21 11:48:41 +00:00
|
|
|
}
|
2012-12-21 16:46:17 +00:00
|
|
|
|
2012-12-21 11:48:41 +00:00
|
|
|
setCurrentIndex( targetGroup );
|
|
|
|
}
|
|
|
|
|
|
|
|
setUpdatesEnabled( true );
|
|
|
|
}
|
2012-12-21 20:20:54 +00:00
|
|
|
|
|
|
|
void DictGroupsWidget::tabDataChanged()
|
|
|
|
{
|
|
|
|
QString toolTipStr = "\"" + tabText( currentIndex() ) + "\"\n" + tr( "Dictionaries: " )
|
|
|
|
+ QString::number( getCurrentModel()->getCurrentDictionaries().size() );
|
|
|
|
setTabToolTip( currentIndex(), toolTipStr );
|
|
|
|
}
|
2013-01-04 13:24:08 +00:00
|
|
|
|
|
|
|
QuickFilterLine::QuickFilterLine( QWidget * parent ): ExtLineEdit( parent ), m_focusAction(this)
|
|
|
|
{
|
|
|
|
m_proxyModel.setFilterCaseSensitivity( Qt::CaseInsensitive );
|
|
|
|
|
|
|
|
setPlaceholderText( tr( "Dictionary search/filter (Ctrl+F)" ) );
|
|
|
|
|
|
|
|
m_focusAction.setShortcut( QKeySequence( "Ctrl+F" ) );
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( &m_focusAction, &QAction::triggered, this, &QuickFilterLine::focusFilterLine );
|
2013-01-04 13:24:08 +00:00
|
|
|
|
2022-01-24 14:23:38 +00:00
|
|
|
QPixmap image(":/icons/system-search.svg");
|
2013-01-04 13:24:08 +00:00
|
|
|
setButtonPixmap(ExtLineEdit::Left, image.scaled(18, 18, Qt::KeepAspectRatio, Qt::SmoothTransformation));
|
|
|
|
setButtonToolTip(ExtLineEdit::Left, tr("Quick Search"));
|
|
|
|
setButtonVisible(ExtLineEdit::Left, true);
|
|
|
|
|
2022-01-28 00:30:01 +00:00
|
|
|
QPixmap right(":/icons/clear.png");
|
2013-01-04 13:24:08 +00:00
|
|
|
setButtonPixmap(ExtLineEdit::Right, right);
|
|
|
|
setButtonToolTip(ExtLineEdit::Right, tr("Clear Search"));
|
|
|
|
setButtonVisible(ExtLineEdit::Right, true);
|
|
|
|
setButtonAutoHide(ExtLineEdit::Right, true);
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( this, &ExtLineEdit::rightButtonClicked, this, &QLineEdit::clear );
|
2013-01-04 13:24:08 +00:00
|
|
|
|
|
|
|
setFocusPolicy(Qt::StrongFocus);
|
|
|
|
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( this, &QLineEdit::textChanged, this, &QuickFilterLine::filterChangedInternal );
|
2013-01-04 13:24:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QuickFilterLine::~QuickFilterLine()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void QuickFilterLine::applyTo( QAbstractItemView * source )
|
|
|
|
{
|
2013-01-07 08:52:36 +00:00
|
|
|
m_source = source;
|
2013-01-04 13:24:08 +00:00
|
|
|
m_proxyModel.setSourceModel( source->model() );
|
|
|
|
source->setModel( &m_proxyModel );
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex QuickFilterLine::mapToSource( QModelIndex const & idx )
|
|
|
|
{
|
2013-01-07 08:52:36 +00:00
|
|
|
if ( &m_proxyModel == idx.model() )
|
|
|
|
{
|
|
|
|
return m_proxyModel.mapToSource( idx );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return idx;
|
|
|
|
}
|
2013-01-04 13:24:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void QuickFilterLine::filterChangedInternal()
|
|
|
|
{
|
|
|
|
// emit signal in async manner, to avoid UI slowdown
|
2022-12-26 02:08:17 +00:00
|
|
|
QTimer::singleShot( 1, this, &QuickFilterLine::emitFilterChanged );
|
2013-01-04 13:24:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void QuickFilterLine::emitFilterChanged()
|
|
|
|
{
|
|
|
|
m_proxyModel.setFilterFixedString(text());
|
|
|
|
emit filterChanged( text() );
|
|
|
|
}
|
|
|
|
|
|
|
|
void QuickFilterLine::focusFilterLine()
|
|
|
|
{
|
|
|
|
setFocus();
|
|
|
|
selectAll();
|
|
|
|
}
|
2013-01-07 08:52:36 +00:00
|
|
|
|
|
|
|
void QuickFilterLine::keyPressEvent( QKeyEvent * event )
|
|
|
|
{
|
|
|
|
switch ( event->key() )
|
|
|
|
{
|
|
|
|
case Qt::Key_Down:
|
|
|
|
if ( m_source )
|
|
|
|
{
|
|
|
|
m_source->setCurrentIndex( m_source->model()->index( 0,0 ) );
|
|
|
|
m_source->setFocus();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ExtLineEdit::keyPressEvent( event );
|
|
|
|
}
|
|
|
|
}
|