From 8106ae25067162666feb16e9cb57bc293ca8a6b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20H=C3=BCbschle-Schneider?= Date: Tue, 21 Oct 2014 14:49:57 +0200 Subject: [PATCH] Reduce variable scopes Also reduces some boolean logic verbosity. --- src/libtomahawk/Source.cpp | 21 ++++++++----------- src/libtomahawk/playlist/AlbumModel.cpp | 9 +++----- src/libtomahawk/playlist/PlayableModel.cpp | 3 +-- src/libtomahawk/playlist/PlaylistModel.cpp | 3 +-- src/libtomahawk/playlist/TreeModel.cpp | 9 +++----- src/libtomahawk/resolvers/JSResolver.cpp | 3 +-- .../resolvers/JSResolverHelper.cpp | 2 +- src/libtomahawk/resolvers/ScriptEngine.cpp | 3 +-- 8 files changed, 20 insertions(+), 33 deletions(-) diff --git a/src/libtomahawk/Source.cpp b/src/libtomahawk/Source.cpp index 630456814..f4cda51c9 100644 --- a/src/libtomahawk/Source.cpp +++ b/src/libtomahawk/Source.cpp @@ -235,9 +235,6 @@ Source::friendlyNamesLessThan( const QString& first, const QString& second ) favored.append( QRegExp( "\\b([A-Z][a-z']* ?){2,10}" ) ); //properly capitalized person's name favored.append( QRegExp( "[a-zA-Z ']+" ) ); //kind of person's name - bool matchFirst = false; - bool matchSecond = false; - //We check if the strings match the regexps. The regexps represent friendly name patterns we do //*not* want (penalties) or want (favored), prioritized. If none of the strings match a regexp, //we go to the next regexp. If one of the strings matches, and we're matching penalties, we say @@ -261,23 +258,23 @@ Source::friendlyNamesLessThan( const QString& first, const QString& second ) isPenalty = false; } - matchFirst = rx.exactMatch( first ); - matchSecond = rx.exactMatch( second ); + const bool matchFirst = rx.exactMatch( first ); + const bool matchSecond = rx.exactMatch( second ); - if ( matchFirst == false && matchSecond == false ) + if ( !matchFirst && !matchSecond ) continue; - if ( matchFirst == true && matchSecond == true ) + if ( matchFirst && matchSecond ) break; - if ( matchFirst == true && matchSecond == false ) - return isPenalty ? false : true; + if ( matchFirst && !matchSecond ) + return !isPenalty; - if ( matchFirst == false && matchSecond == true ) - return isPenalty ? true : false; + if ( !matchFirst && matchSecond) + return isPenalty; } - return ( first.compare( second ) == -1 ) ? true : false; + return first.compare( second ) == -1; } diff --git a/src/libtomahawk/playlist/AlbumModel.cpp b/src/libtomahawk/playlist/AlbumModel.cpp index 9996c7871..a144215de 100644 --- a/src/libtomahawk/playlist/AlbumModel.cpp +++ b/src/libtomahawk/playlist/AlbumModel.cpp @@ -144,10 +144,9 @@ AlbumModel::addAlbums( const QList& albums ) emit beginInsertRows( QModelIndex(), crows.first, crows.second ); - PlayableItem* albumitem; foreach( const album_ptr& album, trimmedAlbums ) { - albumitem = new PlayableItem( album, rootItem() ); + PlayableItem* albumitem = new PlayableItem( album, rootItem() ); albumitem->index = createIndex( rootItem()->children.count() - 1, 0, albumitem ); connect( albumitem, SIGNAL( dataChanged() ), SLOT( onDataChanged() ) ); @@ -190,10 +189,9 @@ AlbumModel::addArtists( const QList& artists ) emit beginInsertRows( QModelIndex(), crows.first, crows.second ); - PlayableItem* albumitem; foreach ( const artist_ptr& artist, trimmedArtists ) { - albumitem = new PlayableItem( artist, rootItem() ); + PlayableItem* albumitem = new PlayableItem( artist, rootItem() ); albumitem->index = createIndex( rootItem()->children.count() - 1, 0, albumitem ); connect( albumitem, SIGNAL( dataChanged() ), SLOT( onDataChanged() ) ); @@ -219,10 +217,9 @@ AlbumModel::addQueries( const QList& queries ) emit beginInsertRows( QModelIndex(), crows.first, crows.second ); - PlayableItem* albumitem; foreach ( const query_ptr& query, queries ) { - albumitem = new PlayableItem( query, rootItem() ); + PlayableItem* albumitem = new PlayableItem( query, rootItem() ); albumitem->index = createIndex( rootItem()->children.count() - 1, 0, albumitem ); connect( albumitem, SIGNAL( dataChanged() ), SLOT( onDataChanged() ) ); diff --git a/src/libtomahawk/playlist/PlayableModel.cpp b/src/libtomahawk/playlist/PlayableModel.cpp index d00ae45c6..0f261c98e 100644 --- a/src/libtomahawk/playlist/PlayableModel.cpp +++ b/src/libtomahawk/playlist/PlayableModel.cpp @@ -679,11 +679,10 @@ PlayableModel::insertInternal( const QList< T >& items, int row, const QList< To emit beginInsertRows( parent, crows.first, crows.second ); int i = 0; - PlayableItem* plitem; foreach ( const T& item, items ) { PlayableItem* pItem = itemFromIndex( parent ); - plitem = new PlayableItem( item, pItem, row + i ); + PlayableItem* plitem = new PlayableItem( item, pItem, row + i ); plitem->index = createIndex( row + i, 0, plitem ); if ( plitem->query() ) diff --git a/src/libtomahawk/playlist/PlaylistModel.cpp b/src/libtomahawk/playlist/PlaylistModel.cpp index 6051d2ff8..46b955443 100644 --- a/src/libtomahawk/playlist/PlaylistModel.cpp +++ b/src/libtomahawk/playlist/PlaylistModel.cpp @@ -290,11 +290,10 @@ PlaylistModel::insertEntries( const QList< Tomahawk::plentry_ptr >& entries, int QList< Tomahawk::query_ptr > queries; int i = 0; - PlayableItem* plitem; foreach( const plentry_ptr& entry, entries ) { PlayableItem* pItem = itemFromIndex( parent ); - plitem = new PlayableItem( entry, pItem, row + i ); + PlayableItem* plitem = new PlayableItem( entry, pItem, row + i ); plitem->index = createIndex( row + i, 0, plitem ); if ( logs.count() > i ) diff --git a/src/libtomahawk/playlist/TreeModel.cpp b/src/libtomahawk/playlist/TreeModel.cpp index 45cbdc2b5..47c27ed0d 100644 --- a/src/libtomahawk/playlist/TreeModel.cpp +++ b/src/libtomahawk/playlist/TreeModel.cpp @@ -193,10 +193,9 @@ TreeModel::addAlbums( const QModelIndex& parent, const QListindex = createIndex( parentItem->children.count() - 1, 0, albumitem ); connect( albumitem, SIGNAL( dataChanged() ), SLOT( onDataChanged() ) ); @@ -283,10 +282,9 @@ TreeModel::onArtistsAdded( const QList& artists ) emit beginInsertRows( QModelIndex(), crows.first, crows.second ); - PlayableItem* artistitem; foreach( const artist_ptr& artist, artists ) { - artistitem = new PlayableItem( artist, rootItem() ); + PlayableItem* artistitem = new PlayableItem( artist, rootItem() ); artistitem->index = createIndex( rootItem()->children.count() - 1, 0, artistitem ); connect( artistitem, SIGNAL( dataChanged() ), SLOT( onDataChanged() ) ); } @@ -312,10 +310,9 @@ TreeModel::onTracksAdded( const QList& tracks, const QModel emit beginInsertRows( parent, crows.first, crows.second ); - PlayableItem* item = 0; foreach( const query_ptr& query, tracks ) { - item = new PlayableItem( query, parentItem ); + PlayableItem* item = new PlayableItem( query, parentItem ); item->index = createIndex( parentItem->children.count() - 1, 0, item ); connect( item, SIGNAL( dataChanged() ), SLOT( onDataChanged() ) ); diff --git a/src/libtomahawk/resolvers/JSResolver.cpp b/src/libtomahawk/resolvers/JSResolver.cpp index 66d5086f1..f08581951 100644 --- a/src/libtomahawk/resolvers/JSResolver.cpp +++ b/src/libtomahawk/resolvers/JSResolver.cpp @@ -844,12 +844,11 @@ JSResolver::loadCollections() if ( collectionInfo.contains( "iconfile" ) ) { - bool ok = false; QString iconPath = QFileInfo( filePath() ).path() + "/" + collectionInfo.value( "iconfile" ).toString(); QPixmap iconPixmap; - ok = iconPixmap.load( iconPath ); + bool ok = iconPixmap.load( iconPath ); if ( ok && !iconPixmap.isNull() ) sc->setIcon( QIcon( iconPixmap ) ); } diff --git a/src/libtomahawk/resolvers/JSResolverHelper.cpp b/src/libtomahawk/resolvers/JSResolverHelper.cpp index 58d2b1935..f3eaa3ad6 100644 --- a/src/libtomahawk/resolvers/JSResolverHelper.cpp +++ b/src/libtomahawk/resolvers/JSResolverHelper.cpp @@ -440,7 +440,7 @@ JSResolverHelper::addCustomUrlHandler( const QString& protocol, const QString& callbackFuncName, const QString& isAsynchronous ) { - m_urlCallbackIsAsync = ( isAsynchronous.toLower() == "true" ) ? true : false; + m_urlCallbackIsAsync = ( isAsynchronous.toLower() == "true" ); std::function< void( const Tomahawk::result_ptr&, const QString&, std::function< void( const QString&, QSharedPointer< QIODevice >& ) > )> fac = diff --git a/src/libtomahawk/resolvers/ScriptEngine.cpp b/src/libtomahawk/resolvers/ScriptEngine.cpp index 933d7b7e1..a347cd729 100644 --- a/src/libtomahawk/resolvers/ScriptEngine.cpp +++ b/src/libtomahawk/resolvers/ScriptEngine.cpp @@ -74,7 +74,6 @@ ScriptEngine::sslErrorHandler( QNetworkReply* qnr, const QList& errli tDebug() << Q_FUNC_INFO; QByteArray digest = errlist.first().certificate().digest(); - int result = -1; if ( !TomahawkSettings::instance()->isSslCertKnown( digest ) ) { @@ -92,7 +91,7 @@ ScriptEngine::sslErrorHandler( QNetworkReply* qnr, const QList& errli question.setStandardButtons( QMessageBox::No ); question.addButton( tr( "Trust certificate" ), QMessageBox::AcceptRole ); - result = question.exec(); + int result = question.exec(); //FIXME: discuss whether we want to store rejects, too (needs settings management to remove the decision?) if ( result == QMessageBox::AcceptRole )