Skip to content

Commit 4d5b195

Browse files
committed
Fix embedding larger files then can fit in a QLineEdit's contents
Fixes #20329 (cherry picked from commit 32dc102)
1 parent 6d61938 commit 4d5b195

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

src/gui/qgssvgsourcelineedit.cpp

+14-4
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ QgsSvgSourceLineEdit::QgsSvgSourceLineEdit( QWidget *parent )
6666

6767
QString QgsSvgSourceLineEdit::source() const
6868
{
69-
return mFileLineEdit->text();
69+
return mBase64.isEmpty() ? mFileLineEdit->text() : mBase64;
7070
}
7171

7272
void QgsSvgSourceLineEdit::setLastPathSettingsKey( const QString &key )
@@ -76,9 +76,16 @@ void QgsSvgSourceLineEdit::setLastPathSettingsKey( const QString &key )
7676

7777
void QgsSvgSourceLineEdit::setSource( const QString &source )
7878
{
79-
if ( source == mFileLineEdit->text() )
79+
const bool isBase64 = source.startsWith( QLatin1String( "base64:" ), Qt::CaseInsensitive );
80+
81+
if ( ( !isBase64 && source == mFileLineEdit->text() ) || ( isBase64 && source == mBase64 ) )
8082
return;
8183

84+
if ( isBase64 )
85+
mBase64 = source;
86+
else
87+
mBase64.clear();
88+
8289
mFileLineEdit->setText( source );
8390
emit sourceChanged( source );
8491
}
@@ -95,6 +102,7 @@ void QgsSvgSourceLineEdit::selectFile()
95102
{
96103
return;
97104
}
105+
mBase64.clear();
98106
mFileLineEdit->setText( file );
99107
s.setValue( settingsKey(), fi.absolutePath() );
100108
emit sourceChanged( mFileLineEdit->text() );
@@ -106,6 +114,7 @@ void QgsSvgSourceLineEdit::selectUrl()
106114
const QString path = QInputDialog::getText( this, tr( "SVG From URL" ), tr( "Enter SVG URL" ), QLineEdit::Normal, mFileLineEdit->text(), &ok );
107115
if ( ok && path != source() )
108116
{
117+
mBase64.clear();
109118
mFileLineEdit->setText( path );
110119
emit sourceChanged( mFileLineEdit->text() );
111120
}
@@ -141,8 +150,9 @@ void QgsSvgSourceLineEdit::embedFile()
141150
if ( path == source() )
142151
return;
143152

153+
mBase64 = path;
144154
mFileLineEdit->setText( path );
145-
emit sourceChanged( mFileLineEdit->text() );
155+
emit sourceChanged( path );
146156
}
147157

148158
void QgsSvgSourceLineEdit::extractFile()
@@ -164,7 +174,7 @@ void QgsSvgSourceLineEdit::extractFile()
164174
QString path = mFileLineEdit->text().trimmed();
165175
if ( path.startsWith( QLatin1String( "base64:" ), Qt::CaseInsensitive ) )
166176
{
167-
QByteArray base64 = path.mid( 7 ).toLocal8Bit(); // strip 'base64:' prefix
177+
QByteArray base64 = mBase64.mid( 7 ).toLocal8Bit(); // strip 'base64:' prefix
168178
QByteArray decoded = QByteArray::fromBase64( base64, QByteArray::OmitTrailingEquals );
169179

170180
QFile fileOut( file );

src/gui/qgssvgsourcelineedit.h

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class GUI_EXPORT QgsSvgSourceLineEdit : public QWidget
8383
QLineEdit *mFileLineEdit = nullptr;
8484
QToolButton *mFileToolButton = nullptr;
8585
QString mLastPathKey;
86+
QString mBase64;
8687

8788
QString defaultPath() const;
8889
QString settingsKey() const;

tests/src/python/test_qgssvgsourcelineedit.py

+33-1
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
__revision__ = '$Format:%H$'
1414

1515
import qgis # NOQA
16-
16+
import os
1717
from qgis.gui import QgsSvgSourceLineEdit
1818

1919
from qgis.PyQt.QtTest import QSignalSpy
2020
from qgis.testing import start_app, unittest
21+
from utilities import unitTestDataPath
2122

2223
start_app()
2324

@@ -44,6 +45,37 @@ def testGettersSetters(self):
4445
self.assertEqual(len(spy), 2)
4546
self.assertEqual(spy[1][0], 'another')
4647

48+
def testEmbedding(self):
49+
"""Test embedding large SVGs """
50+
w = QgsSvgSourceLineEdit()
51+
spy = QSignalSpy(w.sourceChanged)
52+
53+
w.setSource('source')
54+
self.assertEqual(w.source(), 'source')
55+
self.assertEqual(len(spy), 1)
56+
self.assertEqual(spy[0][0], 'source')
57+
58+
b64 = 'base64:' + ''.join(['x'] * 1000000)
59+
w.setSource(b64)
60+
self.assertEqual(w.source(), b64)
61+
self.assertEqual(len(spy), 2)
62+
self.assertEqual(spy[1][0], b64)
63+
64+
w.setSource(os.path.join(unitTestDataPath(), 'landsat.tif'))
65+
self.assertEqual(w.source(), os.path.join(unitTestDataPath(), 'landsat.tif'))
66+
self.assertEqual(len(spy), 3)
67+
self.assertEqual(spy[2][0], os.path.join(unitTestDataPath(), 'landsat.tif'))
68+
69+
w.setSource(b64)
70+
self.assertEqual(w.source(), b64)
71+
self.assertEqual(len(spy), 4)
72+
self.assertEqual(spy[3][0], b64)
73+
74+
w.setSource('')
75+
self.assertEqual(w.source(), '')
76+
self.assertEqual(len(spy), 5)
77+
self.assertEqual(spy[4][0], '')
78+
4779

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

0 commit comments

Comments
 (0)