Skip to content
Permalink
Browse files

Fix embedding larger files then can fit in a QLineEdit's contents

Fixes #20329
  • Loading branch information
nyalldawson committed Dec 5, 2018
1 parent 5ed9097 commit 32dc1020261a2a28491b680e7b8291b70fe6deb2
@@ -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 )
@@ -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 );
}
@@ -99,6 +106,7 @@ void QgsAbstractFileContentSourceLineEdit::selectFile()
{
return;
}
mBase64.clear();
mFileLineEdit->setText( file );
s.setValue( settingsKey(), fi.absolutePath() );
emit sourceChanged( mFileLineEdit->text() );
@@ -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() );
}
@@ -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()
@@ -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 );
@@ -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;
@@ -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()

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

@@ -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.
You can’t perform that action at this time.