Skip to content

Commit

Permalink
Fix embedding larger files then can fit in a QLineEdit's contents
Browse files Browse the repository at this point in the history
Fixes #20329
  • Loading branch information
nyalldawson committed Dec 5, 2018
1 parent 5ed9097 commit 32dc102
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 6 deletions.
18 changes: 14 additions & 4 deletions src/gui/qgsfilecontentsourcelineedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ QgsAbstractFileContentSourceLineEdit::QgsAbstractFileContentSourceLineEdit( QWid

QString QgsAbstractFileContentSourceLineEdit::source() const
{
return mFileLineEdit->text();
return mBase64.isEmpty() ? mFileLineEdit->text() : mBase64;
}

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

void QgsAbstractFileContentSourceLineEdit::setSource( const QString &source )
{
if ( source == mFileLineEdit->text() )
const bool isBase64 = source.startsWith( QLatin1String( "base64:" ), Qt::CaseInsensitive );

if ( ( !isBase64 && source == mFileLineEdit->text() ) || ( isBase64 && source == mBase64 ) )
return;

if ( isBase64 )
mBase64 = source;
else
mBase64.clear();

mFileLineEdit->setText( source );
emit sourceChanged( source );
}
Expand All @@ -99,6 +106,7 @@ void QgsAbstractFileContentSourceLineEdit::selectFile()
{
return;
}
mBase64.clear();
mFileLineEdit->setText( file );
s.setValue( settingsKey(), fi.absolutePath() );
emit sourceChanged( mFileLineEdit->text() );
Expand All @@ -110,6 +118,7 @@ void QgsAbstractFileContentSourceLineEdit::selectUrl()
const QString path = QInputDialog::getText( this, fileFromUrlTitle(), fileFromUrlText(), QLineEdit::Normal, mFileLineEdit->text(), &ok );
if ( ok && path != source() )
{
mBase64.clear();
mFileLineEdit->setText( path );
emit sourceChanged( mFileLineEdit->text() );
}
Expand Down Expand Up @@ -145,8 +154,9 @@ void QgsAbstractFileContentSourceLineEdit::embedFile()
if ( path == source() )
return;

mBase64 = path;
mFileLineEdit->setText( path );
emit sourceChanged( mFileLineEdit->text() );
emit sourceChanged( path );
}

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

QFile fileOut( file );
Expand Down
1 change: 1 addition & 0 deletions src/gui/qgsfilecontentsourcelineedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class GUI_EXPORT QgsAbstractFileContentSourceLineEdit : public QWidget SIP_ABSTR
QLineEdit *mFileLineEdit = nullptr;
QToolButton *mFileToolButton = nullptr;
QString mLastPathKey;
QString mBase64;

QString defaultPath() const;
QString settingsKey() const;
Expand Down
34 changes: 33 additions & 1 deletion tests/src/python/test_qgsimagesourcelineedit.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
__revision__ = '$Format:%H$'

import qgis # NOQA

import os
from qgis.gui import QgsImageSourceLineEdit

from qgis.PyQt.QtTest import QSignalSpy
from qgis.testing import start_app, unittest
from utilities import unitTestDataPath


start_app()

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

def testEmbedding(self):
"""Test embedding large SVGs """
w = QgsImageSourceLineEdit()
spy = QSignalSpy(w.sourceChanged)

w.setSource('source')
self.assertEqual(w.source(), 'source')
self.assertEqual(len(spy), 1)
self.assertEqual(spy[0][0], 'source')

b64 = 'base64:' + ''.join(['x'] * 1000000)
w.setSource(b64)
self.assertEqual(w.source(), b64)
self.assertEqual(len(spy), 2)
self.assertEqual(spy[1][0], b64)

w.setSource(os.path.join(unitTestDataPath(), 'landsat.tif'))
self.assertEqual(w.source(), os.path.join(unitTestDataPath(), 'landsat.tif'))
self.assertEqual(len(spy), 3)
self.assertEqual(spy[2][0], os.path.join(unitTestDataPath(), 'landsat.tif'))

w.setSource(b64)
self.assertEqual(w.source(), b64)
self.assertEqual(len(spy), 4)
self.assertEqual(spy[3][0], b64)

w.setSource('')
self.assertEqual(w.source(), '')
self.assertEqual(len(spy), 5)
self.assertEqual(spy[4][0], '')

if __name__ == '__main__':
unittest.main()
34 changes: 33 additions & 1 deletion tests/src/python/test_qgssvgsourcelineedit.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
__revision__ = '$Format:%H$'

import qgis # NOQA

import os
from qgis.gui import QgsSvgSourceLineEdit

from qgis.PyQt.QtTest import QSignalSpy
from qgis.testing import start_app, unittest
from utilities import unitTestDataPath

start_app()

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

def testEmbedding(self):
"""Test embedding large SVGs """
w = QgsSvgSourceLineEdit()
spy = QSignalSpy(w.sourceChanged)

w.setSource('source')
self.assertEqual(w.source(), 'source')
self.assertEqual(len(spy), 1)
self.assertEqual(spy[0][0], 'source')

b64 = 'base64:' + ''.join(['x'] * 1000000)
w.setSource(b64)
self.assertEqual(w.source(), b64)
self.assertEqual(len(spy), 2)
self.assertEqual(spy[1][0], b64)

w.setSource(os.path.join(unitTestDataPath(), 'landsat.tif'))
self.assertEqual(w.source(), os.path.join(unitTestDataPath(), 'landsat.tif'))
self.assertEqual(len(spy), 3)
self.assertEqual(spy[2][0], os.path.join(unitTestDataPath(), 'landsat.tif'))

w.setSource(b64)
self.assertEqual(w.source(), b64)
self.assertEqual(len(spy), 4)
self.assertEqual(spy[3][0], b64)

w.setSource('')
self.assertEqual(w.source(), '')
self.assertEqual(len(spy), 5)
self.assertEqual(spy[4][0], '')


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

0 comments on commit 32dc102

Please sign in to comment.