[kstars] kstars/tools: deps-- to Qt 5.1: use a lambda wrapper to replace a Qt5.5 method

Akarsh Simha akarsh at kde.org
Tue Aug 30 08:34:53 UTC 2016


Git commit a79f4667b59a25a3f35f2b3da3e210d7de545f47 by Akarsh Simha.
Committed on 30/08/2016 at 08:31.
Pushed by asimha into branch 'master'.

deps-- to Qt 5.1: use a lambda wrapper to replace a Qt5.5 method

QString::indexOf( ... ) calls that were removed seem to require Qt
5.5. In order to encourage functional building of KStars on older
Ubuntu versions, we avoid using this call, and instead use a C++
lambda to wrap around. If the call becomes deprecated, we can easily
modify the wrapper to use the new syntax.

Also: move helper method AddDeepSkyObject::countNonOverlappingMatches
into a lambda.

CCMAIL: kstars-devel at kde.org

M  +28   -19   kstars/tools/adddeepskyobject.cpp
M  +0    -2    kstars/tools/adddeepskyobject.h

http://commits.kde.org/kstars/a79f4667b59a25a3f35f2b3da3e210d7de545f47

diff --git a/kstars/tools/adddeepskyobject.cpp b/kstars/tools/adddeepskyobject.cpp
index b81ef31..cedb54c 100644
--- a/kstars/tools/adddeepskyobject.cpp
+++ b/kstars/tools/adddeepskyobject.cpp
@@ -90,22 +90,45 @@ void AddDeepSkyObject::fillFromText( const QString &text ) {
     float minorAxis = NaN::f;
     float positionAngle = 0;
     QString name;
+
+    // Note: The following method is a proxy to support older versions of Qt.
+    // In Qt 5.5 and above, the QString::indexOf(const QRegularExpression &re, int from, QRegularExpressionMatch *rmatch) method obviates the need for the following.
+    auto indexOf = []( const QString &s, const QRegularExpression &regExp, int from, QRegularExpressionMatch *m ) -> int {
+        *m = regExp.match( s, from );
+        return m->capturedStart( 0 );
+    };
+
+    auto countNonOverlappingMatches = [ indexOf ]( const QString &string, const QRegularExpression &regExp, QStringList *list = 0 ) -> int {
+        int count = 0;
+        int matchIndex = -1;
+        int lastMatchLength = 1;
+        QRegularExpressionMatch rmatch;
+        while ( ( matchIndex = indexOf( string, regExp, matchIndex + lastMatchLength, &rmatch ) ) >= 0 ) {
+            ++count;
+            lastMatchLength = rmatch.captured( 0 ).length();
+         if ( list )
+             list->append( rmatch.captured( 0 ) );
+        }
+        return count;
+    };
+
     QRegularExpressionMatch rmatch;
-    if ( countNonOverlappingMatches( text, matchCoords ) == 2 ) {
+    int nonOverlappingMatchCount;
+    if ( ( nonOverlappingMatchCount = countNonOverlappingMatches( text, matchCoords ) ) == 2 ) {
         coordText = text;
     }
-    else if ( countNonOverlappingMatches( text, matchCoords ) > 2 ) {
+    else if ( nonOverlappingMatchCount > 2 ) {
         qDebug() << "Found more than 2 coordinate matches. Trying to match J2000 line.";
-        if ( text.indexOf( matchJ2000Line, 0, &rmatch ) >= 0 ) {
+        if ( indexOf( text, matchJ2000Line, 0, &rmatch ) >= 0 ) {
             coordText = rmatch.captured( 1 ) + rmatch.captured( 2 );
             qDebug() << "Found a J2000 line match: " << coordText;
         }
     }
     if ( !coordText.isEmpty() ) {
-        int coord1 = coordText.indexOf( matchCoords, 0, &rmatch );
+        int coord1 = indexOf( coordText, matchCoords, 0, &rmatch );
         Q_ASSERT( coord1 >= 0 );
         RA = dms( rmatch.captured( 1 ) + ' ' + rmatch.captured( 2 ) + ' ' + rmatch.captured( 3 ), false );
-        int coord2 = coordText.indexOf( matchCoords, coord1 + rmatch.captured( 0 ).length(), &rmatch );
+        int coord2 = indexOf( coordText, matchCoords, coord1 + rmatch.captured( 0 ).length(), &rmatch );
         Q_ASSERT( coord2 >= 0 );
         Dec = dms( rmatch.captured( 1 ) + ' ' + rmatch.captured( 2 ) + ' ' + rmatch.captured( 3 ), true );
         qDebug() << "Extracted coordinates: " << RA.toHMSString() << " " << Dec.toDMSString();
@@ -287,17 +310,3 @@ void AddDeepSkyObject::slotFillFromText() {
     if ( ok )
         fillFromText( text );
 }
-
-int AddDeepSkyObject::countNonOverlappingMatches( const QString &string, const QRegularExpression &regExp, QStringList *list ) {
-     int count = 0;
-     int matchIndex = -1;
-     int lastMatchLength = 1;
-     QRegularExpressionMatch rmatch;
-     while ( ( matchIndex = string.indexOf( regExp, matchIndex + lastMatchLength, &rmatch ) ) >= 0 ) {
-         ++count;
-         lastMatchLength = rmatch.captured( 0 ).length();
-         if ( list )
-             list->append( rmatch.captured( 0 ) );
-     }
-     return count;
-}
diff --git a/kstars/tools/adddeepskyobject.h b/kstars/tools/adddeepskyobject.h
index fa0ceec..df68c8e 100644
--- a/kstars/tools/adddeepskyobject.h
+++ b/kstars/tools/adddeepskyobject.h
@@ -71,8 +71,6 @@ class AddDeepSkyObject : public QDialog, public Ui::AddDeepSkyObject {
 
  private:
 
-    int countNonOverlappingMatches( const QString &string, const QRegularExpression &regExp, QStringList *list = 0 );
-
     SyncedCatalogComponent *m_catalog;
     Ui::AddDeepSkyObject *ui;
 };


More information about the Kstars-devel mailing list