Skip to content

Commit

Permalink
More efficient use of QStrings
Browse files Browse the repository at this point in the history
- use .isEmpty() instead of == QLatin1String( "" ) to check for
empty strings
- use .clear() instead of = QLatin1String( "" ) to empty a string
- remove unnecessary QString initializations
  • Loading branch information
nyalldawson committed Sep 27, 2017
1 parent 036646f commit 8c3939e
Show file tree
Hide file tree
Showing 102 changed files with 282 additions and 305 deletions.
2 changes: 1 addition & 1 deletion src/app/composer/qgscomposermapgridwidget.cpp
Expand Up @@ -438,7 +438,7 @@ bool QgsComposerMapGridWidget::hasPredefinedScales() const
QgsSettings settings;
QString scalesStr( settings.value( QStringLiteral( "Map/scales" ), PROJECT_SCALES ).toString() );
QStringList myScalesList = scalesStr.split( ',' );
return !myScalesList.isEmpty() && myScalesList[0] != QLatin1String( "" );
return !myScalesList.isEmpty() && !myScalesList[0].isEmpty();
}
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/composer/qgscomposermapwidget.cpp
Expand Up @@ -1051,7 +1051,7 @@ bool QgsComposerMapWidget::hasPredefinedScales() const
QgsSettings settings;
QString scalesStr( settings.value( QStringLiteral( "Map/scales" ), PROJECT_SCALES ).toString() );
QStringList myScalesList = scalesStr.split( ',' );
return !myScalesList.isEmpty() && myScalesList[0] != QLatin1String( "" );
return !myScalesList.isEmpty() && !myScalesList[0].isEmpty();
}
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/composer/qgscomposerpicturewidget.cpp
Expand Up @@ -564,7 +564,7 @@ int QgsComposerPictureWidget::addDirectoryToPreview( const QString &path )
listItem->setIcon( icon );
}

listItem->setText( QLatin1String( "" ) );
listItem->setText( QString() );
//store the absolute icon file path as user data
listItem->setData( Qt::UserRole, fileIt->absoluteFilePath() );
++counter;
Expand Down
10 changes: 5 additions & 5 deletions src/app/main.cpp
Expand Up @@ -168,7 +168,7 @@ void usage( const QString &appName )
// AppleEvent handler as well as by the main routine argv processing

// This behavior will cause QGIS to autoload a project
static QString sProjectFileName = QLatin1String( "" );
static QString sProjectFileName;

// This is the 'leftover' arguments collection
static QStringList sFileList;
Expand Down Expand Up @@ -492,8 +492,8 @@ int main( int argc, char *argv[] )

// This behavior is used to load the app, snapshot the map,
// save the image to disk and then exit
QString mySnapshotFileName = QLatin1String( "" );
QString configLocalStorageLocation = QLatin1String( "" );
QString mySnapshotFileName;
QString configLocalStorageLocation;
QString profileName;
int mySnapshotWidth = 800;
int mySnapshotHeight = 600;
Expand All @@ -520,7 +520,7 @@ int main( int argc, char *argv[] )
// there are no command line arguments. This gives a usable map
// extent when qgis starts with no layers loaded. When layers are
// loaded, we let the layers define the initial extent.
QString myInitialExtent = QLatin1String( "" );
QString myInitialExtent;
if ( argc == 1 )
myInitialExtent = QStringLiteral( "-1,-1,1,1" );

Expand Down Expand Up @@ -1238,7 +1238,7 @@ int main( int argc, char *argv[] )
/////////////////////////////////`////////////////////////////////////
// Take a snapshot of the map view then exit if snapshot mode requested
/////////////////////////////////////////////////////////////////////
if ( mySnapshotFileName != QLatin1String( "" ) )
if ( !mySnapshotFileName.isEmpty() )
{
/*You must have at least one paintEvent() delivered for the window to be
rendered properly.
Expand Down
29 changes: 14 additions & 15 deletions src/app/pluginmanager/qgspluginmanager.cpp
Expand Up @@ -622,20 +622,19 @@ void QgsPluginManager::showPluginDetails( QStandardItem *item )
return;
}

QString html = QLatin1String( "" );
html += "<style>"
" body, table {"
" padding:0px;"
" margin:0px;"
" font-family:verdana;"
" font-size: 10pt;"
" }"
" div#votes {"
" width:360px;"
" margin-left:98px;"
" padding-top:3px;"
" }"
"</style>";
QString html = "<style>"
" body, table {"
" padding:0px;"
" margin:0px;"
" font-family:verdana;"
" font-size: 10pt;"
" }"
" div#votes {"
" width:360px;"
" margin-left:98px;"
" padding-top:3px;"
" }"
"</style>";

if ( !metadata->value( QStringLiteral( "plugin_id" ) ).isEmpty() )
{
Expand Down Expand Up @@ -1135,7 +1134,7 @@ void QgsPluginManager::setCurrentTab( int idx )
mModelProxy->setAcceptedStatuses( acceptedStatuses );

// load tab description HTML to the detail browser
QString tabInfoHTML = QLatin1String( "" );
QString tabInfoHTML;
QMap<QString, QString>::const_iterator it = mTabDescriptions.constFind( tabTitle );
if ( it != mTabDescriptions.constEnd() )
{
Expand Down
4 changes: 2 additions & 2 deletions src/app/qgisapp.cpp
Expand Up @@ -5792,7 +5792,7 @@ void QgisApp::saveMapAsPdf()
//overloaded version of the above function
void QgisApp::saveMapAsImage( const QString &imageFileNameQString, QPixmap *theQPixmap )
{
if ( imageFileNameQString == QLatin1String( "" ) )
if ( imageFileNameQString.isEmpty() )
{
//no fileName chosen
return;
Expand Down Expand Up @@ -9913,7 +9913,7 @@ void QgisApp::setExtent( const QgsRectangle &rect )
*/
bool QgisApp::saveDirty()
{
QString whyDirty = QLatin1String( "" );
QString whyDirty;
bool hasUnsavedEdits = false;
// extra check to see if there are any vector layers with unsaved provider edits
// to ensure user has opportunity to save any editing
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsabout.cpp
Expand Up @@ -186,7 +186,7 @@ void QgsAbout::init()
#endif
if ( translatorFile.open( QIODevice::ReadOnly ) )
{
QString translatorHTML = QLatin1String( "" );
QString translatorHTML;
QTextStream translatorStream( &translatorFile );
// Always use UTF-8
translatorStream.setCodec( "UTF-8" );
Expand Down
22 changes: 11 additions & 11 deletions src/app/qgscustomprojectiondialog.cpp
Expand Up @@ -308,7 +308,7 @@ bool QgsCustomProjectionDialog::saveCrs( QgsCoordinateReferenceSystem myCRS, con
void QgsCustomProjectionDialog::on_pbnAdd_clicked()
{
QString name = tr( "new CRS" );
QString id = QLatin1String( "" );
QString id;
QgsCoordinateReferenceSystem parameters;

QTreeWidgetItem *newItem = new QTreeWidgetItem( leNameList, QStringList() );
Expand All @@ -331,7 +331,7 @@ void QgsCustomProjectionDialog::on_pbnRemove_clicked()
}
QTreeWidgetItem *item = leNameList->takeTopLevelItem( i );
delete item;
if ( customCRSids[i] != QLatin1String( "" ) )
if ( !customCRSids[i].isEmpty() )
{
deletedCRSs.push_back( customCRSids[i] );
}
Expand Down Expand Up @@ -361,8 +361,8 @@ void QgsCustomProjectionDialog::on_leNameList_currentItemChanged( QTreeWidgetIte
else
{
//Can happen that current is null, for example if we just deleted the last element
leName->setText( QLatin1String( "" ) );
teParameters->setPlainText( QLatin1String( "" ) );
leName->clear();
teParameters->clear();
return;
}
}
Expand Down Expand Up @@ -415,7 +415,7 @@ void QgsCustomProjectionDialog::on_buttonBox_accepted()
{
CRS.createFromProj4( customCRSparameters[i] );
//Test if we just added this CRS (if it has no existing ID)
if ( customCRSids[i] == QLatin1String( "" ) )
if ( !customCRSids[i].isEmpty() )
{
save_success &= saveCrs( CRS, customCRSnames[i], QLatin1String( "" ), true );
}
Expand Down Expand Up @@ -462,8 +462,8 @@ void QgsCustomProjectionDialog::on_pbnCalculate_clicked()
{
QMessageBox::information( this, tr( "QGIS Custom Projection" ),
tr( "This proj4 projection definition is not valid." ) );
projectedX->setText( QLatin1String( "" ) );
projectedY->setText( QLatin1String( "" ) );
projectedX->clear();
projectedY->clear();
pj_free( myProj );
pj_ctx_free( pContext );
return;
Expand All @@ -478,8 +478,8 @@ void QgsCustomProjectionDialog::on_pbnCalculate_clicked()
{
QMessageBox::information( this, tr( "QGIS Custom Projection" ),
tr( "Northing and Easthing must be in decimal form." ) );
projectedX->setText( QLatin1String( "" ) );
projectedY->setText( QLatin1String( "" ) );
projectedX->clear();
projectedY->clear();
pj_free( myProj );
pj_ctx_free( pContext );
return;
Expand All @@ -491,8 +491,8 @@ void QgsCustomProjectionDialog::on_pbnCalculate_clicked()
{
QMessageBox::information( this, tr( "QGIS Custom Projection" ),
tr( "Internal Error (source projection invalid?)" ) );
projectedX->setText( QLatin1String( "" ) );
projectedY->setText( QLatin1String( "" ) );
projectedX->clear();
projectedY->clear();
pj_free( wgs84Proj );
pj_ctx_free( pContext );
return;
Expand Down
8 changes: 4 additions & 4 deletions src/app/qgsdecorationgrid.cpp
Expand Up @@ -110,8 +110,8 @@ void QgsDecorationGrid::projectRead()
mGridAnnotationPosition = InsideMapFrame; // don't allow outside frame, doesn't make sense
mGridAnnotationDirection = static_cast< GridAnnotationDirection >( QgsProject::instance()->readNumEntry( mNameConfig,
QStringLiteral( "/AnnotationDirection" ), 0 ) );
QString fontStr = QgsProject::instance()->readEntry( mNameConfig, QStringLiteral( "/AnnotationFont" ), QLatin1String( "" ) );
if ( fontStr != QLatin1String( "" ) )
QString fontStr = QgsProject::instance()->readEntry( mNameConfig, QStringLiteral( "/AnnotationFont" ), QString() );
if ( !fontStr.isEmpty() )
{
mGridAnnotationFont.fromString( fontStr );
}
Expand All @@ -134,7 +134,7 @@ void QgsDecorationGrid::projectRead()
if ( mLineSymbol )
setLineSymbol( nullptr );
xml = QgsProject::instance()->readEntry( mNameConfig, QStringLiteral( "/LineSymbol" ) );
if ( xml != QLatin1String( "" ) )
if ( !xml.isEmpty() )
{
doc.setContent( xml );
elem = doc.documentElement();
Expand All @@ -146,7 +146,7 @@ void QgsDecorationGrid::projectRead()
if ( mMarkerSymbol )
setMarkerSymbol( nullptr );
xml = QgsProject::instance()->readEntry( mNameConfig, QStringLiteral( "/MarkerSymbol" ) );
if ( xml != QLatin1String( "" ) )
if ( !xml.isEmpty() )
{
doc.setContent( xml );
elem = doc.documentElement();
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsmapsavedialog.cpp
Expand Up @@ -380,7 +380,7 @@ void QgsMapSaveDialog::onAccepted()
if ( mDialogType == Image )
{
QPair< QString, QString> fileNameAndFilter = QgsGuiUtils::getSaveAsImageName( QgisApp::instance(), tr( "Choose a file name to save the map image as" ) );
if ( fileNameAndFilter.first != QLatin1String( "" ) )
if ( !fileNameAndFilter.first.isEmpty() )
{
QgsMapSettings ms = QgsMapSettings();
applyMapSettings( ms );
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsnewspatialitelayerdialog.cpp
Expand Up @@ -340,7 +340,7 @@ bool QgsNewSpatialiteLayerDialog::apply()
{
// Build up the sql statement for creating the table
QString sql = QStringLiteral( "create table %1(" ).arg( quotedIdentifier( leLayerName->text() ) );
QString delim = QLatin1String( "" );
QString delim;

if ( checkBoxPrimaryKey->isChecked() )
{
Expand Down
4 changes: 2 additions & 2 deletions src/app/qgsprojectproperties.cpp
Expand Up @@ -1867,8 +1867,8 @@ void QgsProjectProperties::updateEllipsoidUI( int newIndex )
mEllipsoidIndex = newIndex;
leSemiMajor->setEnabled( false );
leSemiMinor->setEnabled( false );
leSemiMajor->setText( QLatin1String( "" ) );
leSemiMinor->setText( QLatin1String( "" ) );
leSemiMajor->clear();
leSemiMinor->clear();

cmbEllipsoid->setEnabled( projectionSelector->crs().isValid() );
cmbEllipsoid->setToolTip( QLatin1String( "" ) );
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsrelationmanagerdialog.cpp
Expand Up @@ -88,7 +88,7 @@ void QgsRelationManagerDialog::on_mBtnAddRelation_clicked()
relation.setReferencingLayer( addDlg.referencingLayerId() );
relation.setReferencedLayer( addDlg.referencedLayerId() );
QString relationId = addDlg.relationId();
if ( addDlg.relationId() == QLatin1String( "" ) )
if ( addDlg.relationId().isEmpty() )
relationId = QStringLiteral( "%1_%2_%3_%4" )
.arg( addDlg.referencingLayerId(),
addDlg.references().at( 0 ).first,
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsversioninfo.cpp
Expand Up @@ -79,7 +79,7 @@ void QgsVersionInfo::versionReplyFinished()
mErrorString = tr( "The host name %1 could not be resolved. Check your DNS settings or contact your system administrator." ).arg( reply->request().url().host() );
break;
case QNetworkReply::NoError:
mErrorString = QLatin1String( "" );
mErrorString.clear();
break;
default:
mErrorString = reply->errorString();
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgswelcomepageitemsmodel.cpp
Expand Up @@ -150,7 +150,7 @@ QVariant QgsWelcomePageItemsModel::data( const QModelIndex &index, int role ) co
case PathRole:
return QDir::toNativeSeparators( mRecentProjects.at( index.row() ).path );
case CrsRole:
if ( mRecentProjects.at( index.row() ).crs != QLatin1String( "" ) )
if ( !mRecentProjects.at( index.row() ).crs.isEmpty() )
{
QgsCoordinateReferenceSystem crs = QgsCoordinateReferenceSystem::fromOgcWmsCrs( mRecentProjects.at( index.row() ).crs );
return QStringLiteral( "%1 (%2)" ).arg( mRecentProjects.at( index.row() ).crs, crs.description() );
Expand Down
3 changes: 0 additions & 3 deletions src/auth/pkipaths/qgsauthpkipathsedit.cpp
Expand Up @@ -162,10 +162,7 @@ void QgsAuthPkiPathsEdit::writePkiMessage( QLineEdit *lineedit, const QString &m
txt = tr( "Invalid: %1" ).arg( msg );
break;
case Unknown:
ss = QLatin1String( "" );
break;
default:
ss = QLatin1String( "" );
}
lineedit->setStyleSheet( ss );
lineedit->setText( txt );
Expand Down
3 changes: 0 additions & 3 deletions src/auth/pkipkcs12/qgsauthpkcs12edit.cpp
Expand Up @@ -167,10 +167,7 @@ void QgsAuthPkcs12Edit::writePkiMessage( QLineEdit *lineedit, const QString &msg
txt = tr( "Invalid: %1" ).arg( msg );
break;
case Unknown:
ss = QLatin1String( "" );
break;
default:
ss = QLatin1String( "" );
}
lineedit->setStyleSheet( ss );
lineedit->setText( txt );
Expand Down
2 changes: 1 addition & 1 deletion src/core/annotations/qgshtmlannotation.cpp
Expand Up @@ -61,7 +61,7 @@ void QgsHtmlAnnotation::setSourceFile( const QString &htmlFile )
mHtmlFile = htmlFile;
if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
{
mHtmlSource = QLatin1String( "" );
mHtmlSource.clear();
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions src/core/auth/qgsauthmanager.cpp
Expand Up @@ -804,7 +804,7 @@ const QString QgsAuthManager::uniqueConfigId() const

while ( true )
{
id = QLatin1String( "" );
id.clear();
for ( int i = 0; i < len; i++ )
{
switch ( qrand() % 2 )
Expand Down Expand Up @@ -3051,7 +3051,7 @@ void QgsAuthManager::setPasswordHelperLoggingEnabled( const bool enabled )
void QgsAuthManager::passwordHelperClearErrors()
{
mPasswordHelperErrorCode = QKeychain::NoError;
mPasswordHelperErrorMessage = QLatin1String( "" );
mPasswordHelperErrorMessage.clear();
}

void QgsAuthManager::passwordHelperProcessError()
Expand Down
8 changes: 4 additions & 4 deletions src/core/providers/memory/qgsmemoryprovider.cpp
Expand Up @@ -158,22 +158,22 @@ QgsMemoryProvider::QgsMemoryProvider( const QString &uri )
length = -1;
}

if ( reFieldDef.cap( 2 ) != QLatin1String( "" ) )
if ( !reFieldDef.cap( 2 ).isEmpty() )
{
length = reFieldDef.cap( 2 ).toInt();
}
if ( reFieldDef.cap( 3 ) != QLatin1String( "" ) )
if ( !reFieldDef.cap( 3 ).isEmpty() )
{
precision = reFieldDef.cap( 3 ).toInt();
}
if ( reFieldDef.cap( 4 ) != QLatin1String( "" ) )
if ( !reFieldDef.cap( 4 ).isEmpty() )
{
//array
subType = type;
type = ( subType == QVariant::String ? QVariant::StringList : QVariant::List );
}
}
if ( name != QLatin1String( "" ) )
if ( !name.isEmpty() )
attributes.append( QgsField( name, type, typeName, length, precision, QLatin1String( "" ), subType ) );
}
addAttributes( attributes );
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgscolorramp.cpp
Expand Up @@ -691,7 +691,7 @@ QgsStringMap QgsCptCityColorRamp::properties() const

QString QgsCptCityColorRamp::fileName() const
{
if ( mSchemeName == QLatin1String( "" ) )
if ( mSchemeName.isEmpty() )
return QString();
else
{
Expand Down

0 comments on commit 8c3939e

Please sign in to comment.