1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#include "groups.hh"
#include "instances.hh"
#include "dictionary.hh"
#include <QMessageBox>
#include <QInputDialog>
using std::vector;
Groups::Groups( QWidget * parent,
vector< sptr< Dictionary::Class > > const & dicts_,
Config::Groups const & groups_,
Config::Group const & order ): QWidget( parent ),
dicts( dicts_ ), groups( groups_ )
{
ui.setupUi( this );
// Populate the dictionaries' list
ui.dictionaries->setAsSource();
ui.dictionaries->populate( Instances::Group( order, dicts, Config::Group() ).dictionaries,
dicts );
ui.searchLine->applyTo( ui.dictionaries );
addAction( ui.searchLine->getFocusAction() );
groupsListMenu = new QMenu( tr( "Group tabs" ), ui.groups );
groupsListButton = new QToolButton( ui.groups );
groupsListButton->setAutoRaise( true );
groupsListButton->setIcon( QIcon( ":/icons/windows-list.svg" ) );
groupsListButton->setMenu( groupsListMenu );
groupsListButton->setToolTip( tr( "Open groups list" ) );
groupsListButton->setPopupMode( QToolButton::InstantPopup );
ui.groups->setCornerWidget( groupsListButton );
groupsListButton->setFocusPolicy( Qt::ClickFocus );
connect( groupsListMenu, &QMenu::aboutToShow, this, &Groups::fillGroupsMenu );
connect( groupsListMenu, &QMenu::triggered, this, &Groups::switchToGroup );
// Populate groups' widget
ui.groups->populate( groups, dicts, ui.dictionaries->getCurrentDictionaries() );
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.autoGroups, &QAbstractButton::clicked, this, &Groups::addAutoGroups );
connect( ui.groups, &DictGroupsWidget::showDictionaryInfo, this, &Groups::showDictionaryInfo );
ui.dictionaries->setContextMenuPolicy( Qt::CustomContextMenu );
connect( ui.dictionaries, &QWidget::customContextMenuRequested, this, &Groups::showDictInfo );
countChanged();
}
void Groups::editGroup( unsigned id )
{
for( int x = 0; x < groups.size(); ++x )
{
if ( groups[ x ].id == id )
{
ui.groups->setCurrentIndex( x );
ui.groups->currentWidget()->setFocus();
break;
}
}
}
void Groups::updateDictionaryOrder( Config::Group const & order )
{
// Make sure it differs from what we have
Instances::Group newOrder( order, dicts, Config::Group() );
if( ui.dictionaries->getCurrentDictionaries() != newOrder.dictionaries )
{
// Repopulate
ui.dictionaries->populate( Instances::Group( order, dicts, Config::Group() ).dictionaries, dicts );
}
}
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 );
ui.removeAllGroups->setEnabled( en );
int stretch = ui.groups->count() / 5;
if( stretch > 3 ) stretch = 3;
ui.gridLayout->setColumnStretch( 2, stretch );
}
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();
}
}
void Groups::addAutoGroups()
{
ui.groups->addAutoGroups();
countChanged();
}
void Groups::renameCurrent()
{
int current = ui.groups->currentIndex();
if ( current < 0 )
return;
bool ok;
QString name = QInputDialog::getText( this, tr("Rename group"),
tr("Give a new name for the group:"), QLineEdit::Normal,
ui.groups->getCurrentGroupName(), &ok );
if ( ok )
ui.groups->renameCurrentGroup( name );
}
void Groups::removeCurrent()
{
int current = ui.groups->currentIndex();
if ( current >= 0 && QMessageBox::question( this, tr( "Remove group" ),
tr( "Are you sure you want to remove the group <b>%1</b>?" ).arg( ui.groups->getCurrentGroupName() ),
QMessageBox::Yes, QMessageBox::Cancel ) == QMessageBox::Yes )
{
ui.groups->removeCurrentGroup();
countChanged();
}
}
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();
}
}
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() );
}
}
void Groups::showDictInfo( QPoint const & pos )
{
QVariant data = ui.dictionaries->getModel()->data(
ui.searchLine->mapToSource(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 );
}
}
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() ) );
}
void Groups::switchToGroup( QAction * act )
{
int idx = act->data().toInt();
ui.groups->setCurrentIndex(idx);
}
|