Skip to content

Commit 32dc102

Browse files
committed
Fix embedding larger files then can fit in a QLineEdit's contents
Fixes #20329
1 parent 5ed9097 commit 32dc102

File tree

4 files changed

+81
-6
lines changed

4 files changed

+81
-6
lines changed

src/gui/qgsfilecontentsourcelineedit.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ QgsAbstractFileContentSourceLineEdit::QgsAbstractFileContentSourceLineEdit( QWid
7070

7171
QString QgsAbstractFileContentSourceLineEdit::source() const
7272
{
73-
return mFileLineEdit->text();
73+
return mBase64.isEmpty() ? mFileLineEdit->text() : mBase64;
7474
}
7575

7676
void QgsAbstractFileContentSourceLineEdit::setLastPathSettingsKey( const QString &key )
@@ -80,9 +80,16 @@ void QgsAbstractFileContentSourceLineEdit::setLastPathSettingsKey( const QString
8080

8181
void QgsAbstractFileContentSourceLineEdit::setSource( const QString &source )
8282
{
83-
if ( source == mFileLineEdit->text() )
83+
const bool isBase64 = source.startsWith( QLatin1String( "base64:" ), Qt::CaseInsensitive );
84+
85+
if ( ( !isBase64 && source == mFileLineEdit->text() ) || ( isBase64 && source == mBase64 ) )
8486
return;
8587

88+
if ( isBase64 )
89+
mBase64 = source;
90+
else
91+
mBase64.clear();
92+
8693
mFileLineEdit->setText( source );
8794
emit sourceChanged( source );
8895
}
@@ -99,6 +106,7 @@ void QgsAbstractFileContentSourceLineEdit::selectFile()
99106
{
100107
return;
101108
}
109+
mBase64.clear();
102110
mFileLineEdit->setText( file );
103111
s.setValue( settingsKey(), fi.absolutePath() );
104112
emit sourceChanged( mFileLineEdit->text() );
@@ -110,6 +118,7 @@ void QgsAbstractFileContentSourceLineEdit::selectUrl()
110118
const QString path = QInputDialog::getText( this, fileFromUrlTitle(), fileFromUrlText(), QLineEdit::Normal, mFileLineEdit->text(), &ok );
111119
if ( ok && path != source() )
112120
{
121+
mBase64.clear();
113122
mFileLineEdit->setText( path );
114123
emit sourceChanged( mFileLineEdit->text() );
115124
}
@@ -145,8 +154,9 @@ void QgsAbstractFileContentSourceLineEdit::embedFile()
145154
if ( path == source() )
146155
return;
147156

157+
mBase64 = path;
148158
mFileLineEdit->setText( path );
149-
emit sourceChanged( mFileLineEdit->text() );
159+
emit sourceChanged( path );
150160
}
151161

152162
void QgsAbstractFileContentSourceLineEdit::extractFile()
@@ -168,7 +178,7 @@ void QgsAbstractFileContentSourceLineEdit::extractFile()
168178
QString path = mFileLineEdit->text().trimmed();
169179
if ( path.startsWith( QLatin1String( "base64:" ), Qt::CaseInsensitive ) )
170180
{
171-
QByteArray base64 = path.mid( 7 ).toLocal8Bit(); // strip 'base64:' prefix
181+
QByteArray base64 = mBase64.mid( 7 ).toLocal8Bit(); // strip 'base64:' prefix
172182
QByteArray decoded = QByteArray::fromBase64( base64, QByteArray::OmitTrailingEquals );
173183

174184
QFile fileOut( file );

src/gui/qgsfilecontentsourcelineedit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ class GUI_EXPORT QgsAbstractFileContentSourceLineEdit : public QWidget SIP_ABSTR
137137
QLineEdit *mFileLineEdit = nullptr;
138138
QToolButton *mFileToolButton = nullptr;
139139
QString mLastPathKey;
140+
QString mBase64;
140141

141142
QString defaultPath() const;
142143
QString settingsKey() const;

tests/src/python/test_qgsimagesourcelineedit.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
__revision__ = '$Format:%H$'
1414

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

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

2224
start_app()
2325

@@ -44,6 +46,36 @@ def testGettersSetters(self):
4446
self.assertEqual(len(spy), 2)
4547
self.assertEqual(spy[1][0], 'another')
4648

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

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

tests/src/python/test_qgssvgsourcelineedit.py

Lines changed: 33 additions & 1 deletion
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)