Merge bitcoin-core/gui#620: Replace QRegExp with QRegularExpression

67364ebe4c test, qt: Add tests for `GUIUtil::extractFirstSuffixFromFilter` (w0xlt)
ace9af5688 qt: Replace `QRegExp` with `QRegularExpression` (w0xlt)
c378535e28 qt: Add a function that extracts the suffix from a filter (w0xlt)

Pull request description:

  Picking up https://github.com/bitcoin-core/gui/pull/606 (labeled "Up for grabs") and applying https://github.com/bitcoin-core/gui/pull/606#pullrequestreview-984607067 and https://github.com/bitcoin-core/gui/pull/606#issuecomment-1137149907.

  Replaces occurrences of `QRegExp` usage with `QRegularExpression` as part of the roadmap for Qt6 integration.

  Fixes https://github.com/bitcoin-core/gui/issues/578

ACKs for top commit:
  laanwj:
    Code review and lightly tested ACK 67364ebe4c
  hebasto:
    ACK 67364ebe4c

Tree-SHA512: 4a17d83e557bc635cbd1a15776856e9edb7162b23a369ccbd2ac59c68b8a1ea663baaa7d5ad98e419dc03b91ef3315c768eeadc01c0b29162de109493161e814
This commit is contained in:
laanwj 2022-06-22 06:53:03 +02:00
commit 58b9d6cf9e
No known key found for this signature in database
GPG key ID: 1E4AED62986CD25D
5 changed files with 38 additions and 19 deletions

View file

@ -53,6 +53,7 @@
#include <QMouseEvent>
#include <QPluginLoader>
#include <QProgressDialog>
#include <QRegularExpression>
#include <QScreen>
#include <QSettings>
#include <QShortcut>
@ -289,6 +290,17 @@ QString getDefaultDataDirectory()
return PathToQString(GetDefaultDataDir());
}
QString ExtractFirstSuffixFromFilter(const QString& filter)
{
QRegularExpression filter_re(QStringLiteral(".* \\(\\*\\.(.*)[ \\)]"), QRegularExpression::InvertedGreedinessOption);
QString suffix;
QRegularExpressionMatch m = filter_re.match(filter);
if (m.hasMatch()) {
suffix = m.captured(1);
}
return suffix;
}
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir,
const QString &filter,
QString *selectedSuffixOut)
@ -306,13 +318,7 @@ QString getSaveFileName(QWidget *parent, const QString &caption, const QString &
/* Directly convert path to native OS path separators */
QString result = QDir::toNativeSeparators(QFileDialog::getSaveFileName(parent, caption, myDir, filter, &selectedFilter));
/* Extract first suffix from filter pattern "Description (*.foo)" or "Description (*.foo *.bar ...) */
QRegExp filter_re(".* \\(\\*\\.(.*)[ \\)]");
QString selectedSuffix;
if(filter_re.exactMatch(selectedFilter))
{
selectedSuffix = filter_re.cap(1);
}
QString selectedSuffix = ExtractFirstSuffixFromFilter(selectedFilter);
/* Add suffix if needed */
QFileInfo info(result);
@ -354,14 +360,8 @@ QString getOpenFileName(QWidget *parent, const QString &caption, const QString &
if(selectedSuffixOut)
{
/* Extract first suffix from filter pattern "Description (*.foo)" or "Description (*.foo *.bar ...) */
QRegExp filter_re(".* \\(\\*\\.(.*)[ \\)]");
QString selectedSuffix;
if(filter_re.exactMatch(selectedFilter))
{
selectedSuffix = filter_re.cap(1);
}
*selectedSuffixOut = selectedSuffix;
*selectedSuffixOut = ExtractFirstSuffixFromFilter(selectedFilter);
;
}
return result;
}

View file

@ -123,6 +123,14 @@ namespace GUIUtil
*/
QString getDefaultDataDirectory();
/**
* Extract first suffix from filter pattern "Description (*.foo)" or "Description (*.foo *.bar ...).
*
* @param[in] filter Filter specification such as "Comma Separated Files (*.csv)"
* @return QString
*/
QString ExtractFirstSuffixFromFilter(const QString& filter);
/** Get save filename, mimics QFileDialog::getSaveFileName, except that it appends a default suffix
when no suffix is provided by the user.

View file

@ -4,6 +4,7 @@
#include <init.h>
#include <qt/bitcoin.h>
#include <qt/guiutil.h>
#include <qt/test/optiontests.h>
#include <test/util/setup_common.h>
#include <util/system.h>
@ -122,3 +123,12 @@ void OptionTests::parametersInteraction()
QVERIFY(!settings.contains("fListen"));
gArgs.ClearPathCache();
}
void OptionTests::extractFilter()
{
QString filter = QString("Partially Signed Transaction (Binary) (*.psbt)");
QCOMPARE(GUIUtil::ExtractFirstSuffixFromFilter(filter), "psbt");
filter = QString("Image (*.png *.jpg)");
QCOMPARE(GUIUtil::ExtractFirstSuffixFromFilter(filter), "png");
}

View file

@ -22,6 +22,7 @@ private Q_SLOTS:
void migrateSettings();
void integerGetArgBug();
void parametersInteraction();
void extractFilter();
private:
interfaces::Node& m_node;

View file

@ -22,7 +22,8 @@
#include <QCloseEvent>
#include <QLabel>
#include <QMainWindow>
#include <QRegExp>
#include <QRegularExpression>
#include <QString>
#include <QTextCursor>
#include <QTextTable>
#include <QVBoxLayout>
@ -44,9 +45,8 @@ HelpMessageDialog::HelpMessageDialog(QWidget *parent, bool about) :
/// HTML-format the license message from the core
QString licenseInfoHTML = QString::fromStdString(LicenseInfo());
// Make URLs clickable
QRegExp uri("<(.*)>", Qt::CaseSensitive, QRegExp::RegExp2);
uri.setMinimal(true); // use non-greedy matching
licenseInfoHTML.replace(uri, "<a href=\"\\1\">\\1</a>");
QRegularExpression uri(QStringLiteral("<(.*)>"), QRegularExpression::InvertedGreedinessOption);
licenseInfoHTML.replace(uri, QStringLiteral("<a href=\"\\1\">\\1</a>"));
// Replace newlines with HTML breaks
licenseInfoHTML.replace("\n", "<br>");