Truncate filter from middle of string

This commit is contained in:
Nyall Dawson 2021-07-19 14:56:33 +10:00
parent 73f9020399
commit 1436a81b31
5 changed files with 53 additions and 2 deletions

View File

@ -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

View File

@ -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() );

View File

@ -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 )

View File

@ -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

View File

@ -220,6 +220,23 @@ class PyQgsStringUtils(unittest.TestCase):
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()