Skip to content

Commit

Permalink
Own muted dictionaries list for each group
Browse files Browse the repository at this point in the history
  • Loading branch information
Abs62 committed Sep 26, 2012
1 parent f6a3b16 commit 3c2990b
Show file tree
Hide file tree
Showing 11 changed files with 225 additions and 98 deletions.
14 changes: 11 additions & 3 deletions articleview.cc
Expand Up @@ -73,7 +73,6 @@ ArticleView::ArticleView( QWidget * parent, ArticleNetworkAccessManager & nm,
Instances::Groups const & groups_, bool popupView_,
Config::Class const & cfg_,
QAction * dictionaryBarToggled_,
Config::MutedDictionaries * mutedDictionaries_,
GroupComboBox const * groupComboBox_ ):
QFrame( parent ),
articleNetMgr( nm ),
Expand All @@ -89,7 +88,6 @@ ArticleView::ArticleView( QWidget * parent, ArticleNetworkAccessManager & nm,
openSearchAction( this ),
searchIsOpened( false ),
dictionaryBarToggled( dictionaryBarToggled_ ),
mutedDictionaries( mutedDictionaries_ ),
groupComboBox( groupComboBox_ )
{
ui.setupUi( this );
Expand Down Expand Up @@ -587,11 +585,21 @@ bool ArticleView::eventFilter( QObject * obj, QEvent * ev )

QString ArticleView::getMutedForGroup( unsigned group )
{
if ( dictionaryBarToggled && mutedDictionaries && dictionaryBarToggled->isChecked() )
if ( dictionaryBarToggled && dictionaryBarToggled->isChecked() )
{
// Dictionary bar is active -- mute the muted dictionaries
Instances::Group const * groupInstance = groups.findGroup( group );

// Find muted dictionaries for current group
Config::Group const * grp = cfg.getGroup( group );
Config::MutedDictionaries const * mutedDictionaries;
if( group == Instances::Group::AllGroupId )
mutedDictionaries = popupView ? &cfg.popupMutedDictionaries : &cfg.mutedDictionaries;
else
mutedDictionaries = grp ? ( popupView ? &grp->popupMutedDictionaries : &grp->mutedDictionaries ) : 0;
if( !mutedDictionaries )
return QString();

QStringList mutedDicts;

if ( groupInstance )
Expand Down
2 changes: 0 additions & 2 deletions articleview.hh
Expand Up @@ -49,7 +49,6 @@ class ArticleView: public QFrame
QString desktopOpenedTempFile;

QAction * dictionaryBarToggled;
Config::MutedDictionaries * mutedDictionaries;
GroupComboBox const * groupComboBox;

public:
Expand All @@ -63,7 +62,6 @@ public:
bool popupView,
Config::Class const & cfg,
QAction * dictionaryBarToggled = 0,
Config::MutedDictionaries * mutedDictionaries = 0,
GroupComboBox const * groupComboBox = 0 );

/// Sets the currently active group combo box. When looking up selections,
Expand Down
48 changes: 48 additions & 0 deletions config.cc
Expand Up @@ -139,6 +139,22 @@ Romaji::Romaji():
{
}

Group * Class::getGroup( unsigned id )
{
for( unsigned x = 0; x < groups.size(); x++ )
if( groups[ x ].id == id )
return &groups[ x ];
return 0;
}

Group const * Class::getGroup( unsigned id ) const
{
for( unsigned x = 0; x < groups.size(); x++ )
if( groups[ x ].id == id )
return &groups[ x ];
return 0;
}

void Events::signalMutedDictionariesChanged()
{
emit mutedDictionariesChanged();
Expand Down Expand Up @@ -226,6 +242,15 @@ Group loadGroup( QDomElement grp, unsigned * nextId = 0 )
g.dictionaries.push_back( DictionaryRef( dicts.item( y ).toElement().text(),
dicts.item( y ).toElement().attribute( "name" ) ) );

QDomNode muted = grp.namedItem( "mutedDictionaries" );
dicts = muted.toElement().elementsByTagName( "mutedDictionary" );
for( unsigned x = 0; x < dicts.length(); ++x )
g.mutedDictionaries.insert( dicts.item( x ).toElement().text() );

dicts = muted.toElement().elementsByTagName( "popupMutedDictionary" );
for( unsigned x = 0; x < dicts.length(); ++x )
g.popupMutedDictionaries.insert( dicts.item( x ).toElement().text() );

return g;
}

Expand Down Expand Up @@ -764,6 +789,29 @@ void saveGroup( Group const & data, QDomElement & group )

dictionary.setAttributeNode( name );
}

QDomElement muted = dd.createElement( "mutedDictionaries" );
group.appendChild( muted );

for( MutedDictionaries::const_iterator i = data.mutedDictionaries.begin();
i != data.mutedDictionaries.end(); ++i )
{
QDomElement dict = dd.createElement( "mutedDictionary" );
muted.appendChild( dict );

QDomText value = dd.createTextNode( *i );
dict.appendChild( value );
}

for( MutedDictionaries::const_iterator i = data.popupMutedDictionaries.begin();
i != data.popupMutedDictionaries.end(); ++i )
{
QDomElement dict = dd.createElement( "popupMutedDictionary" );
muted.appendChild( dict );

QDomText value = dd.createTextNode( *i );
dict.appendChild( value );
}
}

}
Expand Down
10 changes: 7 additions & 3 deletions config.hh
Expand Up @@ -17,6 +17,9 @@ namespace Config {

using std::vector;

/// Dictionaries which are temporarily disabled via the dictionary bar.
typedef QSet< QString > MutedDictionaries;

/// A path where to search for the dictionaries
struct Path
{
Expand Down Expand Up @@ -77,6 +80,8 @@ struct Group
QByteArray iconData;
QKeySequence shortcut;
vector< DictionaryRef > dictionaries;
Config::MutedDictionaries mutedDictionaries; // Disabled via dictionary bar
Config::MutedDictionaries popupMutedDictionaries; // Disabled via dictionary bar in popup

Group(): id( 0 ) {}

Expand Down Expand Up @@ -349,9 +354,6 @@ struct Program

typedef vector< Program > Programs;

/// Dictionaries which are temporarily disabled via the dictionary bar.
typedef QSet< QString > MutedDictionaries;

struct Class
{
Paths paths;
Expand Down Expand Up @@ -396,6 +398,8 @@ struct Class
pinPopupWindow( false ), showingDictBarNames( false ),
usingSmallIconsInToolbars( false ), maxDictionaryRefsInContextMenu( 20 )
{}
Group * getGroup( unsigned id );
Group const * getGroup( unsigned id ) const;
};

/// Configuration-specific events. Some parts of the program need to react
Expand Down
27 changes: 16 additions & 11 deletions dictionarybar.cc
Expand Up @@ -8,10 +8,9 @@
using std::vector;

DictionaryBar::DictionaryBar( QWidget * parent,
Config::MutedDictionaries & mutedDictionaries_,
Config::Events & events ):
QToolBar( tr( "Dictionary Bar" ), parent ),
mutedDictionaries( mutedDictionaries_ ),
mutedDictionaries( 0 ),
configEvents( events )
{
setObjectName( "dictionaryBar" );
Expand Down Expand Up @@ -64,7 +63,7 @@ void DictionaryBar::setDictionaries( vector< sptr< Dictionary::Class > >

action->setCheckable( true );

action->setChecked( !mutedDictionaries.contains( id ) );
action->setChecked( mutedDictionaries ? !mutedDictionaries->contains( id ) : true );

QList< QSize > sizes = icon.availableSizes();

Expand Down Expand Up @@ -135,14 +134,17 @@ void DictionaryBar::mutedDictionariesChanged()
{
//DPRINTF( "Muted dictionaries changed\n" );

if( !mutedDictionaries )
return;

// Update actions

setUpdatesEnabled( false );

for( QList< QAction * >::iterator i = dictActions.begin();
i != dictActions.end(); ++i )
{
bool isUnmuted = !mutedDictionaries.contains( (*i)->data().toString() );
bool isUnmuted = !mutedDictionaries->contains( (*i)->data().toString() );

if ( isUnmuted != (*i)->isChecked() )
(*i)->setChecked( isUnmuted );
Expand All @@ -153,6 +155,9 @@ void DictionaryBar::mutedDictionariesChanged()

void DictionaryBar::actionWasTriggered( QAction * action )
{
if( !mutedDictionaries )
return;

QString id = action->data().toString();

if ( id.isEmpty() )
Expand Down Expand Up @@ -186,7 +191,7 @@ void DictionaryBar::actionWasTriggered( QAction * action )
// Toggle back all the dictionaries
for( QList< QAction * >::iterator i = dictActions.begin();
i != dictActions.end(); ++i )
mutedDictionaries.remove( (*i)->data().toString() );
mutedDictionaries->remove( (*i)->data().toString() );
}
else
{
Expand All @@ -197,9 +202,9 @@ void DictionaryBar::actionWasTriggered( QAction * action )
QString dictId = (*i)->data().toString();

if ( dictId == id )
mutedDictionaries.remove( dictId );
mutedDictionaries->remove( dictId );
else
mutedDictionaries.insert( dictId );
mutedDictionaries->insert( dictId );
}
}
configEvents.signalMutedDictionariesChanged();
Expand All @@ -212,19 +217,19 @@ void DictionaryBar::actionWasTriggered( QAction * action )
{
// Unmute the dictionary

if ( mutedDictionaries.contains( id ) )
if ( mutedDictionaries->contains( id ) )
{
mutedDictionaries.remove( id );
mutedDictionaries->remove( id );
configEvents.signalMutedDictionariesChanged();
}
}
else
{
// Mute the dictionary

if ( !mutedDictionaries.contains( id ) )
if ( !mutedDictionaries->contains( id ) )
{
mutedDictionaries.insert( id );
mutedDictionaries->insert( id );
configEvents.signalMutedDictionariesChanged();
}
}
Expand Down
7 changes: 5 additions & 2 deletions dictionarybar.hh
Expand Up @@ -17,12 +17,15 @@ public:

/// Constructs an empty dictionary bar
DictionaryBar( QWidget * parent,
Config::MutedDictionaries & mutedDictionaries,
Config::Events & );

/// Sets dictionaries to be displayed in the bar. Their statuses (enabled/
/// disabled) are taken from the configuration data.
void setDictionaries( std::vector< sptr< Dictionary::Class > > const & );
void setMutedDictionaries( Config::MutedDictionaries * mutedDictionaries_ )
{ mutedDictionaries = mutedDictionaries_; }
Config::MutedDictionaries const * getMutedDictionaries() const
{ return mutedDictionaries; }

signals:

Expand All @@ -38,7 +41,7 @@ signals:

private:

Config::MutedDictionaries & mutedDictionaries;
Config::MutedDictionaries * mutedDictionaries;
Config::Events & configEvents;

/// All the actions we have added to the toolbar
Expand Down
13 changes: 13 additions & 0 deletions instances.cc
Expand Up @@ -89,6 +89,19 @@ QIcon Group::makeIcon() const
return i;
}

void Group::checkMutedDictionaries( Config::MutedDictionaries * mutedDictionaries ) const
{
Config::MutedDictionaries temp;

for( unsigned x = 0; x < dictionaries.size(); x++ )
{
QString id = QString::fromStdString( dictionaries[ x ]->getId() );
if( mutedDictionaries->contains( id ) )
temp.insert( id );
}
* mutedDictionaries = temp;
}

Group * Groups::findGroup( unsigned id )
{
for( unsigned x = 0; x < size(); ++x )
Expand Down
3 changes: 3 additions & 0 deletions instances.hh
Expand Up @@ -40,6 +40,9 @@ struct Group
/// Makes an icon object for this group, based on the icon's name or iconData.
QIcon makeIcon() const;

/// Remove id's if not presented in group dictionaries
void checkMutedDictionaries( Config::MutedDictionaries * mutedDictionaries ) const;

// Some constants

/// The id of the 'All' group
Expand Down

0 comments on commit 3c2990b

Please sign in to comment.