Skip to content
Permalink
Browse files
Truncate filter from middle of string
  • Loading branch information
nyalldawson committed Jul 19, 2021
1 parent 73f9020 commit 1436a81b31a35b9e782af1cf5521973c246a60e3
@@ -325,6 +325,17 @@ Returns an escaped string matching the behavior of QRegExp.escape.

:return: Escaped string

.. versionadded:: 3.22
%End

static QString truncateMiddleOfString( const QString &string, int maxLength );
%Docstring
Truncates a ``string`` to the specified maximum character length.

If the ``string`` exceeds the maximum character length, then the string
will be truncated by removing characters from the middle of the string
and replacing them with a horizontal ellipsis character.

.. versionadded:: 3.22
%End

@@ -23,7 +23,7 @@
#include "qgsvectorlayer.h"
#include "qgsrasterlayer.h"
#include "qgisapp.h"

#include "qgsstringutils.h"

QgsLayerTreeViewFilterIndicatorProvider::QgsLayerTreeViewFilterIndicatorProvider( QgsLayerTreeView *view )
: QgsLayerTreeViewIndicatorProvider( view )
@@ -63,7 +63,7 @@ QString QgsLayerTreeViewFilterIndicatorProvider::tooltipText( QgsMapLayer *layer

if ( filter.size() > 1024 )
{
filter = filter.left( 1023 ) + QString( QChar( 0x2026 ) );
filter = QgsStringUtils::truncateMiddleOfString( filter, 1024 );
}

return QStringLiteral( "<b>%1:</b><br>%2" ).arg( tr( "Filter" ), filter.toHtmlEscaped() );
@@ -721,6 +721,18 @@ QString QgsStringUtils::qRegExpEscape( const QString &string )
return escaped;
}

QString QgsStringUtils::truncateMiddleOfString( const QString &string, int maxLength )
{
const int charactersToTruncate = string.length() - maxLength;
if ( charactersToTruncate <= 0 )
return string;

// note we actually truncate an extra character, as we'll be replacing it with the ... character
const int truncateFrom = string.length() / 2 - ( charactersToTruncate + 1 ) / 2;

return string.leftRef( truncateFrom ) + QString( QChar( 0x2026 ) ) + string.midRef( truncateFrom + charactersToTruncate + 1 );
}

QgsStringReplacement::QgsStringReplacement( const QString &match, const QString &replacement, bool caseSensitive, bool wholeWordOnly )
: mMatch( match )
, mReplacement( replacement )
@@ -315,6 +315,17 @@ class CORE_EXPORT QgsStringUtils
*/
static QString qRegExpEscape( const QString &string );

/**
* Truncates a \a string to the specified maximum character length.
*
* If the \a string exceeds the maximum character length, then the string
* will be truncated by removing characters from the middle of the string
* and replacing them with a horizontal ellipsis character.
*
* \since QGIS 3.22
*/
static QString truncateMiddleOfString( const QString &string, int maxLength );

};

#endif //QGSSTRINGUTILS_H
@@ -220,6 +220,23 @@ def testfuzzyScore(self):
QgsStringUtils.fuzzyScore('abcd efg hig', 'abcd e hi')
)

def test_truncate_from_middle(self):
"""
Test QgsStringUtils.truncateMiddleOfString
"""
self.assertEqual(QgsStringUtils.truncateMiddleOfString('', 0), '')
self.assertEqual(QgsStringUtils.truncateMiddleOfString('', 10), '')
self.assertEqual(QgsStringUtils.truncateMiddleOfString('abcdef', 10), 'abcdef')
self.assertEqual(QgsStringUtils.truncateMiddleOfString('abcdefghij', 10), 'abcdefghij')
self.assertEqual(QgsStringUtils.truncateMiddleOfString('abcdefghijk', 10), 'abcd…ghijk')
self.assertEqual(QgsStringUtils.truncateMiddleOfString('abcdefghijkl', 10), 'abcde…ijkl')
self.assertEqual(QgsStringUtils.truncateMiddleOfString('abcdefghijklmnop', 10), 'abcde…mnop')
self.assertEqual(QgsStringUtils.truncateMiddleOfString('this is a test', 11), 'this … test')
self.assertEqual(QgsStringUtils.truncateMiddleOfString('this is a test', 1), '…')
self.assertEqual(QgsStringUtils.truncateMiddleOfString('this is a test', 2), 't…')
self.assertEqual(QgsStringUtils.truncateMiddleOfString('this is a test', 3), 't…t')
self.assertEqual(QgsStringUtils.truncateMiddleOfString('this is a test', 0), '…')


if __name__ == '__main__':
unittest.main()

0 comments on commit 1436a81

Please sign in to comment.