Skip to content

Commit

Permalink
Removed remaining usage of QRegExp
Browse files Browse the repository at this point in the history
Replaced with QRegularExpression, except for one case where wildcard
matching is done, since QRegularExpression::wildcardToRegularExpression
only exists since Qt 5.15.
  • Loading branch information
bjorn committed Oct 12, 2020
1 parent 35d21cd commit dccb1fa
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/tiled/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
#include <QLabel>
#include <QMessageBox>
#include <QMimeData>
#include <QRegExp>
#include <QRegularExpression>
#include <QSessionManager>
#include <QShortcut>
#include <QStatusBar>
Expand Down Expand Up @@ -149,9 +149,9 @@ ExportDetails<Format> chooseExportDetails(const QString &fileName,
QFileInfo baseNameInfo = QFileInfo(fileName);
QString baseName = baseNameInfo.baseName();

QRegExp extensionFinder(QLatin1String("\\(\\*\\.([^\\)\\s]*)"));
extensionFinder.indexIn(selectedFilter);
const QString extension = extensionFinder.cap(1);
QRegularExpression extensionFinder(QLatin1String("\\(\\*\\.([^\\)\\s]*)"));
QRegularExpressionMatch match = extensionFinder.match(selectedFilter);
const QString extension = match.captured(1);

QString lastExportedFilePath = pref->lastPath(Preferences::ExportedFile);

Expand Down
26 changes: 22 additions & 4 deletions src/tiled/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@
#include <QMainWindow>
#include <QMenu>
#include <QProcess>
#include <QRegularExpression>
#if QT_VERSION < QT_VERSION_CHECK(5,15,0)
#include <QRegExp>
#endif
#include <QScreen>

#include "qtcompat_p.h"
Expand Down Expand Up @@ -92,13 +95,18 @@ QStringList cleanFilterList(const QString &filter)
const char filterRegExp[] =
"^(.*)\\(([a-zA-Z0-9_.,*? +;#\\-\\[\\]@\\{\\}/!<>\\$%&=^~:\\|]*)\\)$";

QRegExp regexp(QString::fromLatin1(filterRegExp));
QRegularExpression regexp(QString::fromLatin1(filterRegExp));
Q_ASSERT(regexp.isValid());
QString f = filter;
int i = regexp.indexIn(f);
if (i >= 0)
f = regexp.cap(2);
QRegularExpressionMatch match;
filter.indexOf(regexp, 0, &match);
if (match.hasMatch())
f = match.captured(2);
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
return f.split(QLatin1Char(' '), QString::SkipEmptyParts);
#else
return f.split(QLatin1Char(' '), Qt::SkipEmptyParts);
#endif
}

/**
Expand All @@ -108,14 +116,24 @@ QStringList cleanFilterList(const QString &filter)
bool fileNameMatchesNameFilter(const QString &fileName,
const QString &nameFilter)
{
#if QT_VERSION < QT_VERSION_CHECK(5,15,0)
QRegExp rx;
rx.setCaseSensitivity(Qt::CaseInsensitive);
rx.setPatternSyntax(QRegExp::Wildcard);
#else
QRegularExpression rx;
rx.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
#endif

const QStringList filterList = cleanFilterList(nameFilter);
for (const QString &filter : filterList) {
#if QT_VERSION < QT_VERSION_CHECK(5,15,0)
rx.setPattern(filter);
if (rx.exactMatch(fileName))
#else
rx.setPattern(QRegularExpression::wildcardToRegularExpression(filter));
if (rx.match(fileName).hasMatch())
#endif
return true;
}
return false;
Expand Down
9 changes: 4 additions & 5 deletions src/tiled/zoomable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ void Zoomable::setComboBox(QComboBox *comboBox)
this, &Zoomable::comboEdited);

if (!mComboValidator)
mComboValidator = new QRegExpValidator(mComboRegExp, this);
mComboValidator = new QRegularExpressionValidator(mComboRegExp, this);
mComboBox->setValidator(mComboValidator);
}
}
Expand All @@ -210,12 +210,11 @@ void Zoomable::comboActivated(int index)

void Zoomable::comboEdited()
{
int pos = mComboRegExp.indexIn(mComboBox->currentText());
Q_ASSERT(pos != -1);
Q_UNUSED(pos)
const QRegularExpressionMatch match = mComboRegExp.match(mComboBox->currentText());
Q_ASSERT(match.hasMatch());

qreal scale = qBound(mZoomFactors.first(),
qreal(mComboRegExp.cap(1).toDouble() / 100.f),
qreal(match.captured(1).toDouble() / 100.f),
mZoomFactors.last());

setScale(scale);
Expand Down
8 changes: 4 additions & 4 deletions src/tiled/zoomable.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
#pragma once

#include <QObject>
#include <QRegExp>
#include <QRegularExpression>
#include <QVector>

class QComboBox;
class QPinchGesture;
class QRegExpValidator;
class QRegularExpressionValidator;

namespace Tiled {

Expand Down Expand Up @@ -93,8 +93,8 @@ public slots:
qreal mGestureStartScale;
QVector<qreal> mZoomFactors;
QComboBox *mComboBox;
QRegExp mComboRegExp;
QRegExpValidator *mComboValidator;
QRegularExpression mComboRegExp;
QRegularExpressionValidator *mComboValidator;
};

} // namespace Tiled

0 comments on commit dccb1fa

Please sign in to comment.