fix: group widget null check

This commit is contained in:
YiFang Xiao 2023-06-22 16:30:41 +08:00
parent 2359352781
commit 43c2cfdd65
4 changed files with 44 additions and 19 deletions

View file

@ -5,6 +5,7 @@
#include "config.hh"
#include <QTextToSpeech>
#include <memory>
#include <QDebug>
class SpeechClient: public QObject
{
@ -40,7 +41,7 @@ public:
sp( std::make_unique< QTextToSpeech >( e.engine_name ) ),
engine( e )
{
qDebug() << "initialize tts" << e.engine_name;
qDebug() << QStringLiteral( "initialize tts" ) << e.engine_name;
#if ( QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) )
if ( !sp || sp->state() == QTextToSpeech::Error )
return;
@ -75,10 +76,6 @@ public:
bool tell( QString const & text, int volume, int rate ) const;
bool tell( QString const & text ) const;
signals:
void started( bool ok );
void finished();
private:
std::unique_ptr< InternalData > internalData;
};

View file

@ -42,8 +42,8 @@ EditDictionaries::EditDictionaries( QWidget * parent,
ui.tabs->clear();
ui.tabs->addTab( &sources, QIcon( ":/icons/sources.png" ), tr( "&Sources" ) );
ui.tabs->addTab( orderAndProps.get(), QIcon( ":/icons/book.svg" ), tr( "&Dictionaries" ) );
ui.tabs->addTab( groups.get(), QIcon(":/icons/bookcase.svg"), tr( "&Groups" ) );
ui.tabs->addTab( orderAndProps, QIcon( ":/icons/book.svg" ), tr( "&Dictionaries" ) );
ui.tabs->addTab( groups, QIcon( ":/icons/bookcase.svg" ), tr( "&Groups" ) );
connect( ui.buttons, &QDialogButtonBox::clicked, this, &EditDictionaries::buttonBoxClicked );
@ -57,7 +57,7 @@ EditDictionaries::EditDictionaries( QWidget * parent,
helpAction.setShortcutContext( Qt::WidgetWithChildrenShortcut );
connect( &helpAction, &QAction::triggered, [ this ]() {
if ( ui.tabs->currentWidget() == this->groups.get() ) {
if ( ui.tabs->currentWidget() == this->groups ) {
Help::openHelpWebpage( Help::section::manage_groups );
}
else {
@ -240,8 +240,8 @@ void EditDictionaries::acceptChangedSources( bool rebuildGroups )
orderAndProps = new OrderAndProps( this, savedOrder, savedInactive, dictionaries );
groups = new Groups( this, dictionaries, savedGroups, orderAndProps->getCurrentDictionaryOrder() );
ui.tabs->insertTab( 1, orderAndProps.get(), QIcon( ":/icons/book.svg" ), tr( "&Dictionaries" ) );
ui.tabs->insertTab( 2, groups.get(), QIcon( ":/icons/bookcase.svg" ), tr( "&Groups" ) );
ui.tabs->insertTab( 1, orderAndProps, QIcon( ":/icons/book.svg" ), tr( "&Dictionaries" ) );
ui.tabs->insertTab( 2, groups, QIcon( ":/icons/bookcase.svg" ), tr( "&Groups" ) );
connect( groups, &Groups::showDictionaryInfo, this, &EditDictionaries::showDictionaryInfo );
connect( orderAndProps, &OrderAndProps::showDictionaryHeadwords, this, &EditDictionaries::showDictionaryHeadwords );

View file

@ -575,8 +575,8 @@ void DictGroupsWidget::populate( Config::Groups const & groups,
connect( gr, &DictGroupWidget::showDictionaryInfo,this, &DictGroupsWidget::showDictionaryInfo );
connect( gr->getModel(), &DictListModel::contentChanged, this, &DictGroupsWidget::tabDataChanged );
QString toolTipStr = "\"" + tabText( x ) + "\"\n" + tr( "Dictionaries: " )
+ QString::number( getModelAt( x )->getCurrentDictionaries().size() );
QString toolTipStr =
"\"" + tabText( x ) + "\"\n" + tr( "Dictionaries: " ) + QString::number( getDictionaryCountAt( x ) );
setTabToolTip( x, toolTipStr );
}
@ -617,13 +617,31 @@ DictListModel * DictGroupsWidget::getCurrentModel() const
DictListModel * DictGroupsWidget::getModelAt( int current ) const
{
if ( current >= 0 && current < count() ) {
const auto w = (DictGroupWidget *)widget( current );
const auto w = static_cast< DictGroupWidget * >( widget( current ) );
if ( !w )
return nullptr;
return w->getModel();
}
return nullptr;
}
int DictGroupsWidget::getDictionaryCountAt( int current ) const
{
const auto model = getModelAt( current );
if ( !model )
return 0;
return model->getCurrentDictionaries().size();
}
std::vector< sptr< Dictionary::Class > > DictGroupsWidget::getDictionaryAt( int current ) const
{
const auto model = getModelAt( current );
if ( !model )
return {};
return model->getCurrentDictionaries();
}
QItemSelectionModel * DictGroupsWidget::getCurrentSelectionModel() const
{
const int current = currentIndex();
@ -653,8 +671,8 @@ int DictGroupsWidget::addNewGroup( QString const & name )
connect( gr->getModel(), &DictListModel::contentChanged, this, &DictGroupsWidget::tabDataChanged );
const QString toolTipStr = "\"" + tabText( idx ) + "\"\n" + tr( "Dictionaries: " )
+ QString::number( getModelAt( idx )->getCurrentDictionaries().size() );
const QString toolTipStr =
"\"" + tabText( idx ) + "\"\n" + tr( "Dictionaries: " ) + QString::number( getDictionaryCountAt( idx ) );
setTabToolTip( idx, toolTipStr );
return idx;
}
@ -746,6 +764,8 @@ void DictGroupsWidget::addAutoGroups()
// add dictionaries into the current group
QVector< sptr< Dictionary::Class > > vd = dictMap[ gr ];
DictListModel * model = getModelAt( idx );
if ( !model )
continue;
for( int i = 0; i < vd.count(); i++ )
model->addRow(QModelIndex(), vd.at( i ) );
}
@ -850,6 +870,9 @@ void DictGroupsWidget::addGroupBasedOnMap( const QMultiMap<QString, sptr<Diction
const auto idx = addUniqueGroup( group );
DictListModel * model = getModelAt( idx );
if ( !model ) {
continue;
}
for ( const auto & dict : groupToDicts.values( group ) ) {
model->addRow( QModelIndex(), dict );
}
@ -939,10 +962,13 @@ void DictGroupsWidget::combineGroups( int source, int target )
if( source < 0 || source >= count() || target < 0 || target >= count() )
return;
vector< sptr< Dictionary::Class > > const & dicts = getModelAt( source )->getCurrentDictionaries();
vector< sptr< Dictionary::Class > > const & dicts = getDictionaryAt( source );
const auto model = getModelAt( target );
if ( !model )
return;
disconnect( model, &DictListModel::contentChanged, this, &DictGroupsWidget::tabDataChanged );
for ( const auto & dict : dicts ) {

View file

@ -193,6 +193,8 @@ public:
DictListModel * getCurrentModel() const;
DictListModel * getModelAt( int current ) const;
int getDictionaryCountAt( int current ) const;
std::vector< sptr< Dictionary::Class > > getDictionaryAt( int current ) const;
QItemSelectionModel * getCurrentSelectionModel() const;