Skip to content

Commit

Permalink
Some gui QRegExp removal
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 28, 2021
1 parent ee5381c commit 55608c2
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 44 deletions.
30 changes: 18 additions & 12 deletions src/gui/editorwidgets/qgsvaluemapconfigdlg.cpp
Expand Up @@ -26,6 +26,7 @@
#include <QClipboard>
#include <QKeyEvent>
#include <QMimeData>
#include <QRegularExpression>

QgsValueMapConfigDlg::QgsValueMapConfigDlg( QgsVectorLayer *vl, int fieldIdx, QWidget *parent )
: QgsEditorConfigWidget( vl, fieldIdx, parent )
Expand Down Expand Up @@ -315,10 +316,8 @@ void QgsValueMapConfigDlg::loadFromCSVButtonPushed()
QTextStream s( &f );
s.setAutoDetectUnicode( true );

QRegExp re0( "^([^;]*);(.*)$" );
re0.setMinimal( true );
QRegExp re1( "^([^,]*),(.*)$" );
re1.setMinimal( true );
const thread_local QRegularExpression re0( "^([^;]*?);(.*?)$" );
const thread_local QRegularExpression re1( "^([^,]*?),(.*?)$" );

QList<QPair<QString, QVariant>> map;

Expand All @@ -329,18 +328,25 @@ void QgsValueMapConfigDlg::loadFromCSVButtonPushed()
QString key;
QString val;

if ( re0.indexIn( l ) >= 0 && re0.captureCount() == 2 )
const QRegularExpressionMatch re0match = re0.match( l );
if ( re0match.hasMatch() )
{
key = re0.cap( 1 ).trimmed();
val = re0.cap( 2 ).trimmed();
key = re0match.captured( 1 ).trimmed();
val = re0match.captured( 2 ).trimmed();
}
else if ( re1.indexIn( l ) >= 0 && re1.captureCount() == 2 )
else
{
key = re1.cap( 1 ).trimmed();
val = re1.cap( 2 ).trimmed();
const QRegularExpressionMatch re1match = re1.match( l );
if ( re1match.hasMatch() )
{
key = re1match.captured( 1 ).trimmed();
val = re1match.captured( 2 ).trimmed();
}
else
{
continue;
}
}
else
continue;

if ( ( key.startsWith( '\"' ) && key.endsWith( '\"' ) ) ||
( key.startsWith( '\'' ) && key.endsWith( '\'' ) ) )
Expand Down
5 changes: 3 additions & 2 deletions src/gui/ogr/qgsnewogrconnection.cpp
Expand Up @@ -15,7 +15,8 @@
* *
***************************************************************************/
#include <QMessageBox>
#include <QRegExpValidator>
#include <QRegularExpressionValidator>
#include <QRegularExpression>

#include "qgsnewogrconnection.h"
#include "qgslogger.h"
Expand Down Expand Up @@ -83,7 +84,7 @@ QgsNewOgrConnection::QgsNewOgrConnection( QWidget *parent, const QString &connTy
txtName->setEnabled( false );
cmbDatabaseTypes->setEnabled( false );
}
txtName->setValidator( new QRegExpValidator( QRegExp( "[^\\/]+" ), txtName ) );
txtName->setValidator( new QRegularExpressionValidator( QRegularExpression( "[^\\/]+" ), txtName ) );
mAuthSettingsDatabase->setDataprovider( QStringLiteral( "ogr" ) );
mAuthSettingsDatabase->showStoreCheckboxes( true );
}
Expand Down
5 changes: 3 additions & 2 deletions src/gui/qgsattributeformlegacyinterface.cpp
Expand Up @@ -20,7 +20,7 @@

#include <QString>
#include <QDateTime>
#include <QRegExp>
#include <QRegularExpression>

QgsAttributeFormLegacyInterface::QgsAttributeFormLegacyInterface( const QString &function, const QString &pyFormName, QgsAttributeForm *form )
: QgsAttributeFormInterface( form )
Expand All @@ -29,7 +29,8 @@ QgsAttributeFormLegacyInterface::QgsAttributeFormLegacyInterface( const QString
{
static int sLayerCounter = 0;
mPyLayerVarName = QStringLiteral( "_qgis_layer_%1_%2" ).arg( form->layer()->id() ).arg( sLayerCounter++ );
mPyLayerVarName.replace( QRegExp( "[^a-zA-Z0-9_]" ), QStringLiteral( "_" ) ); // clean identifier
const thread_local QRegularExpression reClean( QRegularExpression( "[^a-zA-Z0-9_]" ) );
mPyLayerVarName.replace( reClean, QStringLiteral( "_" ) ); // clean identifier

QString initLayer = QStringLiteral( "%1 = sip.wrapinstance( %2, qgis.core.QgsVectorLayer )" )
.arg( mPyLayerVarName )
Expand Down
5 changes: 3 additions & 2 deletions src/gui/qgscompoundcolorwidget.cpp
Expand Up @@ -31,6 +31,7 @@
#include <QScreen>
#include <QInputDialog>
#include <QVBoxLayout>
#include <QRegularExpression>

QgsCompoundColorWidget::QgsCompoundColorWidget( QWidget *parent, const QColor &color, Layout widgetLayout )
: QgsPanelWidget( parent )
Expand Down Expand Up @@ -444,9 +445,9 @@ QgsUserColorScheme *QgsCompoundColorWidget::createNewUserPalette( QWidget *paren
return nullptr;
}

//generate file name for new palette
//generate file name for new palette
QDir palettePath( gplFilePath() );
QRegExp badChars( "[,^@={}\\[\\]~!?:&*\"|#%<>$\"'();`' /\\\\]" );
const thread_local QRegularExpression badChars( "[,^@={}\\[\\]~!?:&*\"|#%<>$\"'();`' /\\\\]" );
QString filename = name.simplified().toLower().replace( badChars, QStringLiteral( "_" ) );
if ( filename.isEmpty() )
{
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsdoublevalidator.cpp
Expand Up @@ -18,7 +18,7 @@
***************************************************************************/

#include <limits>
#include <QRegExpValidator>
#include <QRegularExpressionValidator>
#include <QRegularExpression>
#include <QLocale>
#include "qgis_gui.h"
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsexpressiontreeview.cpp
Expand Up @@ -840,7 +840,7 @@ bool QgsExpressionItemSearchProxy::filterAcceptsRow( int source_row, const QMode
const QStringList tags = sourceModel()->data( index, QgsExpressionItem::SEARCH_TAGS_ROLE ).toStringList();
for ( const QString &tag : tags )
{
if ( tag.contains( filterRegExp() ) )
if ( tag.contains( filterRegularExpression() ) )
return true;
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/gui/qgsfieldvalidator.cpp
Expand Up @@ -20,7 +20,8 @@
#include "qgsfieldvalidator.h"

#include <QValidator>
#include <QRegExpValidator>
#include <QRegularExpression>
#include <QRegularExpressionValidator>
#include <QDate>
#include <QVariant>

Expand All @@ -43,7 +44,7 @@ QgsFieldValidator::QgsFieldValidator( QObject *parent, const QgsField &field, co
if ( mField.length() > 0 )
{
QString re = QStringLiteral( "-?\\d{0,%1}" ).arg( mField.length() );
mValidator = new QRegExpValidator( QRegExp( re ), parent );
mValidator = new QRegularExpressionValidator( QRegularExpression( re ), parent );
}
else
{
Expand All @@ -66,12 +67,12 @@ QgsFieldValidator::QgsFieldValidator( QObject *parent, const QgsField &field, co
{
re = QStringLiteral( "-?\\d{0,%1}([\\.,]\\d{0,%2})?" ).arg( mField.length() - mField.precision() ).arg( mField.precision() );
}
mValidator = new QRegExpValidator( QRegExp( re ), parent );
mValidator = new QRegularExpressionValidator( QRegularExpression( re ), parent );
}
else if ( mField.length() > 0 && mField.precision() == 0 )
{
QString re = QStringLiteral( "-?\\d{0,%1}" ).arg( mField.length() );
mValidator = new QRegExpValidator( QRegExp( re ), parent );
mValidator = new QRegularExpressionValidator( QRegularExpression( re ), parent );
}
else if ( mField.precision() > 0 )
{
Expand All @@ -85,7 +86,7 @@ QgsFieldValidator::QgsFieldValidator( QObject *parent, const QgsField &field, co
{
re = QStringLiteral( "-?\\d*([\\.]\\d{0,%1})?" ).arg( mField.precision() );
}
mValidator = new QRegExpValidator( QRegExp( re ), parent );
mValidator = new QRegularExpressionValidator( QRegularExpression( re ), parent );
}
else
{
Expand Down
6 changes: 4 additions & 2 deletions src/gui/qgsfilewidget.cpp
Expand Up @@ -84,12 +84,14 @@ QStringList QgsFileWidget::splitFilePaths( const QString &path )
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
const QStringList pathParts = path.split( QRegExp( "\"\\s+\"" ), QString::SkipEmptyParts );
#else
const QStringList pathParts = path.split( QRegExp( "\"\\s+\"" ), Qt::SkipEmptyParts );
const QStringList pathParts = path.split( QRegularExpression( "\"\\s+\"" ), Qt::SkipEmptyParts );
#endif

const thread_local QRegularExpression cleanRe( QStringLiteral( "(^\\s*\")|(\"\\s*)" ) );
for ( const auto &pathsPart : pathParts )
{
QString cleaned = pathsPart;
cleaned.remove( QRegExp( "(^\\s*\")|(\"\\s*)" ) );
cleaned.remove( cleanRe );
paths.append( cleaned );
}
return paths;
Expand Down
13 changes: 7 additions & 6 deletions src/gui/qgsnewhttpconnection.cpp
Expand Up @@ -24,8 +24,8 @@
#include <QMessageBox>
#include <QUrl>
#include <QPushButton>
#include <QRegExp>
#include <QRegExpValidator>
#include <QRegularExpression>
#include <QRegularExpressionValidator>
#include <QUrlQuery>

QgsNewHttpConnection::QgsNewHttpConnection( QWidget *parent, ConnectionTypes types, const QString &baseKey, const QString &connectionName, QgsNewHttpConnection::Flags flags, Qt::WindowFlags fl )
Expand All @@ -43,10 +43,11 @@ QgsNewHttpConnection::QgsNewHttpConnection( QWidget *parent, ConnectionTypes typ

connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsNewHttpConnection::showHelp );

QRegExp rx( "/connections-([^/]+)/" );
if ( rx.indexIn( baseKey ) != -1 )
const thread_local QRegularExpression rx( "/connections-([^/]+)/" );
const QRegularExpressionMatch rxMatch = rx.match( baseKey );
if ( rxMatch.hasMatch() )
{
QString connectionType( rx.cap( 1 ).toUpper() );
QString connectionType( rxMatch.captured( 1 ).toUpper() );
if ( connectionType == QLatin1String( "WMS" ) )
{
connectionType = QStringLiteral( "WMS/WMTS" );
Expand All @@ -61,7 +62,7 @@ QgsNewHttpConnection::QgsNewHttpConnection( QWidget *parent, ConnectionTypes typ
// using connection-wms and connection-wfs -> parse credential key from it.
mCredentialsBaseKey = mBaseKey.split( '-' ).last().toUpper();

txtName->setValidator( new QRegExpValidator( QRegExp( "[^\\/]+" ), txtName ) );
txtName->setValidator( new QRegularExpressionValidator( QRegularExpression( "[^\\/]+" ), txtName ) );

cmbDpiMode->clear();
cmbDpiMode->addItem( tr( "all" ) );
Expand Down
3 changes: 2 additions & 1 deletion src/gui/qgsowssourceselect.cpp
Expand Up @@ -53,6 +53,7 @@
#include <QValidator>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QRegularExpression>

QgsOWSSourceSelect::QgsOWSSourceSelect( const QString &service, QWidget *parent, Qt::WindowFlags fl, QgsProviderRegistry::WidgetMode theWidgetMode )
: QgsAbstractDataSourceWidget( parent, fl, theWidgetMode )
Expand Down Expand Up @@ -178,7 +179,7 @@ void QgsOWSSourceSelect::populateFormats()
{
QString format = layersFormats.value( i );
QgsDebugMsg( "server format = " + format );
QString simpleFormat = format.toLower().remove( QStringLiteral( "image/" ) ).remove( QRegExp( "_.*" ) );
QString simpleFormat = format.toLower().remove( QStringLiteral( "image/" ) ).remove( QRegularExpression( "_.*" ) );
QgsDebugMsg( "server simpleFormat = " + simpleFormat );
QString mimeFormat = "image/" + formatsMap.value( simpleFormat );
QgsDebugMsg( "server mimeFormat = " + mimeFormat );
Expand Down
8 changes: 5 additions & 3 deletions src/gui/qgsprojectionselectiontreewidget.cpp
Expand Up @@ -24,12 +24,14 @@
#include "qgscoordinatereferencesystemregistry.h"
#include "qgsdatums.h"
#include "qgsprojoperation.h"
#include "qgsstringutils.h"

//qt includes
#include <QFileInfo>
#include <QHeaderView>
#include <QResizeEvent>
#include <QMessageBox>
#include <QRegularExpression>

QgsProjectionSelectionTreeWidget::QgsProjectionSelectionTreeWidget( QWidget *parent )
: QWidget( parent )
Expand Down Expand Up @@ -782,9 +784,9 @@ void QgsProjectionSelectionTreeWidget::lstRecent_itemDoubleClicked( QTreeWidgetI

void QgsProjectionSelectionTreeWidget::updateFilter()
{
QString filterTxtCopy = QRegExp::escape( leSearch->text() );
filterTxtCopy.replace( QRegExp( "\\s+" ), QStringLiteral( ".*" ) );
QRegExp re( filterTxtCopy, Qt::CaseInsensitive );
QString filterTxtCopy = QgsStringUtils::qRegExpEscape( leSearch->text() );
filterTxtCopy.replace( QRegularExpression( "\\s+" ), QStringLiteral( ".*" ) );
const QRegularExpression re( filterTxtCopy, QRegularExpression::PatternOption::CaseInsensitiveOption );

const bool hideDeprecated = cbxHideDeprecated->isChecked();

Expand Down
8 changes: 5 additions & 3 deletions src/gui/qgsproviderguiregistry.cpp
Expand Up @@ -20,6 +20,7 @@
#include <QString>
#include <QDir>
#include <QLibrary>
#include <QRegularExpression>

#include "qgslogger.h"
#include "qgsgdalguiprovider.h"
Expand Down Expand Up @@ -127,7 +128,7 @@ void QgsProviderGuiRegistry::loadDynamicProviders( const QString &pluginPath )

// provider file regex pattern, only files matching the pattern are loaded if the variable is defined
QString filePattern = getenv( "QGIS_PROVIDER_FILE" );
QRegExp fileRegexp;
QRegularExpression fileRegexp;
if ( !filePattern.isEmpty() )
{
fileRegexp.setPattern( filePattern );
Expand All @@ -136,9 +137,10 @@ void QgsProviderGuiRegistry::loadDynamicProviders( const QString &pluginPath )
const auto constEntryInfoList = mLibraryDirectory.entryInfoList();
for ( const QFileInfo &fi : constEntryInfoList )
{
if ( !fileRegexp.isEmpty() )
if ( !fileRegexp.pattern().isEmpty() )
{
if ( fileRegexp.indexIn( fi.fileName() ) == -1 )
const QRegularExpressionMatch fileNameMatch = fileRegexp.match( fi.fileName() );
if ( !fileNameMatch.hasMatch() )
{
QgsDebugMsg( "provider " + fi.fileName() + " skipped because doesn't match pattern " + filePattern );
continue;
Expand Down
4 changes: 3 additions & 1 deletion src/gui/qgsrasterpyramidsoptionswidget.cpp
Expand Up @@ -27,6 +27,8 @@
#include <QMouseEvent>
#include <QMenu>
#include <QCheckBox>
#include <QRegularExpressionValidator>
#include <QRegularExpression>

QgsRasterPyramidsOptionsWidget::QgsRasterPyramidsOptionsWidget( QWidget *parent, const QString &provider )
: QWidget( parent )
Expand Down Expand Up @@ -71,7 +73,7 @@ void QgsRasterPyramidsOptionsWidget::updateUi()

// validate string, only space-separated positive integers are allowed
lePyramidsLevels->setEnabled( cbxPyramidsLevelsCustom->isChecked() );
lePyramidsLevels->setValidator( new QRegExpValidator( QRegExp( "(\\d*)(\\s\\d*)*" ), lePyramidsLevels ) );
lePyramidsLevels->setValidator( new QRegularExpressionValidator( QRegularExpression( "(\\d*)(\\s\\d*)*" ), lePyramidsLevels ) );
connect( lePyramidsLevels, &QLineEdit::textEdited,
this, &QgsRasterPyramidsOptionsWidget::setOverviewList );

Expand Down
3 changes: 2 additions & 1 deletion src/gui/qgsshortcutsmanager.cpp
Expand Up @@ -18,6 +18,7 @@
#include "qgssettings.h"

#include <QShortcut>
#include <QRegularExpression>

QgsShortcutsManager::QgsShortcutsManager( QObject *parent, const QString &settingsRoot )
: QObject( parent )
Expand Down Expand Up @@ -324,7 +325,7 @@ void QgsShortcutsManager::updateActionToolTip( QAction *action, const QString &s
{
QString current = action->toolTip();
// Remove the old shortcut.
QRegExp rx( "\\(.*\\)" );
const QRegularExpression rx( QStringLiteral( "\\(.*\\)" ) );
current.replace( rx, QString() );

if ( !sequence.isEmpty() )
Expand Down
5 changes: 3 additions & 2 deletions src/gui/qgstemporalcontrollerwidget.cpp
Expand Up @@ -31,6 +31,7 @@

#include <QAction>
#include <QMenu>
#include <QRegularExpression>

QgsTemporalControllerWidget::QgsTemporalControllerWidget( QWidget *parent )
: QgsPanelWidget( parent )
Expand Down Expand Up @@ -603,8 +604,8 @@ void QgsTemporalControllerWidget::setTimeStep( const QgsInterval &timeStep )
QgsUnitTypes::TemporalUnit unit = static_cast<QgsUnitTypes::TemporalUnit>( mTimeStepsComboBox->itemData( i ).toInt() );
double value = timeStep.seconds() * QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::TemporalSeconds, unit );
QString string = QString::number( value, 'f', precision );
string.remove( QRegExp( "0+$" ) ); //remove trailing zero
string.remove( QRegExp( "[.]+$" ) ); //remove last point if present
string.remove( QRegularExpression( "0+$" ) ); //remove trailing zero
string.remove( QRegularExpression( "[.]+$" ) ); //remove last point if present

if ( value >= 1
&& string.size() <= stringSize // less significant digit than currently selected
Expand Down

0 comments on commit 55608c2

Please sign in to comment.