Skip to content

Commit

Permalink
opt: dictionary group refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyifang committed Jul 1, 2024
1 parent ead9828 commit 069656d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 38 deletions.
53 changes: 28 additions & 25 deletions src/dictionary_group.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,47 @@
#include "dictionary_group.hh"


sptr< Dictionary::Class > DictionaryGroup::getDictionaryByName(QString const & dictName){
sptr< Dictionary::Class > DictionaryGroup::getDictionaryByName( QString const & dictName )
{
// Link to other dictionary
for ( const auto & allDictionarie : allDictionaries ) {
if ( dictName.compare( QString::fromUtf8( allDictionarie->getName().c_str() ) ) == 0 ) {
return allDictionarie;
}
}
return nullptr;

for ( const auto & allDictionarie : allDictionaries ) {
if ( dictName.compare( QString::fromUtf8( allDictionarie->getName().c_str() ) ) == 0 ) {
return allDictionarie;
}
}
return nullptr;
}

std::vector< sptr< Dictionary::Class > > const * DictionaryGroup::getActiveDictionaries(unsigned currentGroup){
const std::vector< sptr< Dictionary::Class > > * DictionaryGroup::getActiveDictionaries( unsigned currentGroup )
{
std::vector< sptr< Dictionary::Class > > const * activeDicts = nullptr;

if ( !groups.empty() ) {
for ( const auto & group : groups )
if ( group.id == currentGroup ) {
activeDicts = &( group.dictionaries );
break;
}
if ( !groups.empty() ) {
for ( const auto & group : groups )
if ( group.id == currentGroup ) {
activeDicts = &( group.dictionaries );
break;
}
else
activeDicts = &allDictionaries;
}
else
activeDicts = &allDictionaries;

return activeDicts;
return activeDicts;
}


sptr< Dictionary::Class > DictionaryGroup::getDictionaryById(std::string dictId){

sptr< Dictionary::Class > DictionaryGroup::getDictionaryById( const std::string & dictId )
{

for ( unsigned x = allDictionaries.size(); x--; ) {
if ( allDictionaries[ x ]->getId() == dictId ) {
return allDictionaries[ x ];
}
if ( allDictionaries[ x ]->getId() == dictId ) {
return allDictionaries[ x ];
}
}
return nullptr;
}

Instances::Group const * DictionaryGroup::getGroupById(unsigned groupId){
return groups.findGroup( group );
Instances::Group const * DictionaryGroup::getGroupById( unsigned groupId )
{
return groups.findGroup( groupId );
}
25 changes: 14 additions & 11 deletions src/dictionary_group.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,30 @@
#ifndef DICTIONARYGROUP_HH_INCLUDED
#define DICTIONARYGROUP_HH_INCLUDED

#include "audioplayerinterface.hh"
#include <memory>

#include "sptr.hh"
#include <QObject>
#include "dictionary.hh"
#include "instances.hh"

class DictionaryGroup: public QObject
{
Q_OBJECT

public:
DictionaryGroup(QObject* parent,std::vector< sptr< Dictionary::Class > > const & allDictionaries_,
Instances::Groups const & groups_):allDictionaries(allDictionaries_),groups(groups_){

}
~DictionaryGroup();

DictionaryGroup( QObject * parent,
std::vector< sptr< Dictionary::Class > > const & allDictionaries_,
Instances::Groups const & groups_ ):
QObject( parent ),
allDictionaries( allDictionaries_ ),
groups( groups_ )
{
}

sptr< Dictionary::Class > getDictionaryByName(QString const & dictionaryName);

std::vector< sptr< Dictionary::Class > > * const getActiveDictionaries(unsigned groupId);
const std::vector< sptr< Dictionary::Class > > * getActiveDictionaries( unsigned groupId );

sptr< Dictionary::Class > getDictionaryById(std::string dictId);
sptr< Dictionary::Class > getDictionaryById( const std::string & dictId );

Instances::Group const * getGroupById(unsigned groupId);

Expand Down
4 changes: 2 additions & 2 deletions src/ui/articleview.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1584,7 +1584,7 @@ void ArticleView::contextMenuRequested( QPoint const & pos )
if ( txt.size() > 60 )
txt = txt.left( 60 ) + "...";

addHeaderToHistoryAction = new QAction( tr( "&Add \"%1\" to history" ).arg( txt ), &menu );
addHeaderToHistoryAction = new QAction( tr( R"(&Add "%1" to history)" ).arg( txt ), &menu );
menu.addAction( addHeaderToHistoryAction );
}

Expand All @@ -1602,7 +1602,7 @@ void ArticleView::contextMenuRequested( QPoint const & pos )
// Add table of contents
QStringList ids = getArticlesList();

if ( !menu.isEmpty() && ids.size() )
if ( !menu.isEmpty() && !ids.empty() )
menu.addSeparator();

unsigned refsAdded = 0;
Expand Down

0 comments on commit 069656d

Please sign in to comment.