[plasmalnf] Signal more changes to the model

- also individual changes need to be signalled
- use QSignalBlocker to avoid spamming changes when calling
  aggregate change methods
- refactor findById() so that also a row number can be
  obtained, which is needed for the change signals.
This commit is contained in:
Adriaan de Groot 2020-11-17 11:17:47 +01:00
parent f93cec031b
commit 3909459563
2 changed files with 25 additions and 22 deletions

View File

@ -65,10 +65,11 @@ ThemesModel::roleNames() const
void void
ThemesModel::setThemeImage( const QString& id, const QString& imagePath ) ThemesModel::setThemeImage( const QString& id, const QString& imagePath )
{ {
auto* theme = m_themes.findById( id ); auto [ i, theme ] = m_themes.indexById( id );
if ( theme ) if ( theme )
{ {
theme->imagePath = imagePath; theme->imagePath = imagePath;
emit dataChanged( index( i, 0 ), index( i, 0 ), { ImageRole } );
} }
} }
@ -80,9 +81,13 @@ ThemesModel::setThemeImage( const QMap< QString, QString >& images )
return; return;
} }
for ( const auto& k : images ) // Don't emit signals from each call, aggregate to one call (below this block)
{ {
setThemeImage( k, images[ k ] ); QSignalBlocker b( this );
for ( const auto& k : images )
{
setThemeImage( k, images[ k ] );
}
} }
emit dataChanged( index( 0, 0 ), index( m_themes.count() - 1 ), { ImageRole } ); emit dataChanged( index( 0, 0 ), index( m_themes.count() - 1 ), { ImageRole } );
} }
@ -90,10 +95,11 @@ ThemesModel::setThemeImage( const QMap< QString, QString >& images )
void void
ThemesModel::showTheme( const QString& id, bool show ) ThemesModel::showTheme( const QString& id, bool show )
{ {
auto* theme = m_themes.findById( id ); auto [ i, theme ] = m_themes.indexById( id );
if ( theme ) if ( theme )
{ {
theme->show = show; theme->show = show;
emit dataChanged( index( i, 0 ), index( i, 0 ), { ShownRole } );
} }
} }
@ -105,6 +111,8 @@ ThemesModel::showOnlyThemes( const QMap< QString, QString >& onlyThese )
return; return;
} }
// No signal blocker block needed here because we're not calling showTheme()
// QSignalBlocker b( this );
for ( auto& t : m_themes ) for ( auto& t : m_themes )
{ {
t.show = onlyThese.contains( t.id ); t.show = onlyThese.contains( t.id );

View File

@ -52,10 +52,10 @@ struct ThemeInfo
class ThemeInfoList : public QList< ThemeInfo > class ThemeInfoList : public QList< ThemeInfo >
{ {
public: public:
std::pair< int, ThemeInfo* > indexById( const QString& id ) std::pair< int, const ThemeInfo* > indexById( const QString& id ) const
{ {
int index = 0; int index = 0;
for ( ThemeInfo& i : *this ) for ( const ThemeInfo& i : *this )
{ {
if ( i.id == id ) if ( i.id == id )
{ {
@ -65,31 +65,26 @@ public:
return { -1, nullptr }; return { -1, nullptr };
} }
std::pair< int, ThemeInfo* > indexById( const QString& id )
{
// Call the const version and then munge the types
auto [ i, p ] = const_cast< const ThemeInfoList* >( this )->indexById( id );
return { i, const_cast< ThemeInfo* >( p ) };
}
/** @brief Looks for a given @p id in the list of themes, returns nullptr if not found. */ /** @brief Looks for a given @p id in the list of themes, returns nullptr if not found. */
ThemeInfo* findById( const QString& id ) ThemeInfo* findById( const QString& id )
{ {
for ( ThemeInfo& i : *this ) auto [ i, p ] = indexById( id );
{ return p;
if ( i.id == id )
{
return &i;
}
}
return nullptr;
} }
/** @brief Looks for a given @p id in the list of themes, returns nullptr if not found. */ /** @brief Looks for a given @p id in the list of themes, returns nullptr if not found. */
const ThemeInfo* findById( const QString& id ) const const ThemeInfo* findById( const QString& id ) const
{ {
for ( const ThemeInfo& i : *this ) auto [ i, p ] = indexById( id );
{ return p;
if ( i.id == id )
{
return &i;
}
}
return nullptr;
} }
/** @brief Checks if a given @p id is in the list of themes. */ /** @brief Checks if a given @p id is in the list of themes. */