Skip to content

Commit

Permalink
Cleaner approach to base64 embedded widget handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 10, 2018
1 parent 2cca68a commit f5f4f53
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/core/mesh/qgsmeshdataprovider.cpp
Expand Up @@ -256,6 +256,7 @@ int QgsMeshDataBlock::count() const
case Vector2DDouble:
return static_cast<int>( mDoubleBuffer.size() / 2.0 );
}
return 0; // no warnings
}

bool QgsMeshDataBlock::isValid() const
Expand All @@ -277,6 +278,7 @@ QgsMeshDatasetValue QgsMeshDataBlock::value( int index ) const
mDoubleBuffer[2 * index + 1]
);
}
return QgsMeshDatasetValue(); // no warnings
}

bool QgsMeshDataBlock::active( int index ) const
Expand Down
46 changes: 29 additions & 17 deletions src/gui/qgsfilecontentsourcelineedit.cpp
Expand Up @@ -56,7 +56,7 @@ QgsAbstractFileContentSourceLineEdit::QgsAbstractFileContentSourceLineEdit( QWid

connect( sourceMenu, &QMenu::aboutToShow, this, [this, extractFileAction]
{
extractFileAction->setEnabled( mFileLineEdit->text().startsWith( QLatin1String( "base64:" ), Qt::CaseInsensitive ) );
extractFileAction->setEnabled( mMode == ModeBase64 );
} );

QAction *enterUrlAction = new QAction( tr( "From URL…" ), sourceMenu );
Expand All @@ -70,6 +70,7 @@ QgsAbstractFileContentSourceLineEdit::QgsAbstractFileContentSourceLineEdit( QWid
connect( mFileLineEdit, &QLineEdit::textEdited, this, &QgsAbstractFileContentSourceLineEdit::mFileLineEdit_textEdited );
connect( mFileLineEdit, &QgsFilterLineEdit::cleared, this, [ = ]
{
mMode = ModeFile;
mFileLineEdit->setPlaceholderText( QString() );
mBase64.clear();
emit sourceChanged( QString() );
Expand All @@ -79,7 +80,16 @@ QgsAbstractFileContentSourceLineEdit::QgsAbstractFileContentSourceLineEdit( QWid

QString QgsAbstractFileContentSourceLineEdit::source() const
{
return mBase64.isEmpty() ? mFileLineEdit->text() : mBase64;
switch ( mMode )
{
case ModeFile:
return mFileLineEdit->text();

case ModeBase64:
return mBase64;
}

return QString();
}

void QgsAbstractFileContentSourceLineEdit::setLastPathSettingsKey( const QString &key )
Expand All @@ -96,12 +106,14 @@ void QgsAbstractFileContentSourceLineEdit::setSource( const QString &source )

if ( isBase64 )
{
mMode = ModeBase64;
mBase64 = source;
mFileLineEdit->clear();
mFileLineEdit->setPlaceholderText( tr( "Embedded file" ) );
}
else
{
mMode = ModeFile;
mBase64.clear();
mFileLineEdit->setText( source );
mFileLineEdit->setPlaceholderText( QString() );
Expand All @@ -122,6 +134,7 @@ void QgsAbstractFileContentSourceLineEdit::selectFile()
{
return;
}
mMode = ModeFile;
mBase64.clear();
mFileLineEdit->setText( file );
mFileLineEdit->setPlaceholderText( QString() );
Expand All @@ -135,6 +148,7 @@ void QgsAbstractFileContentSourceLineEdit::selectUrl()
const QString path = QInputDialog::getText( this, fileFromUrlTitle(), fileFromUrlText(), QLineEdit::Normal, mFileLineEdit->text(), &ok );
if ( ok && path != source() )
{
mMode = ModeFile;
mBase64.clear();
mFileLineEdit->setText( path );
mFileLineEdit->setPlaceholderText( QString() );
Expand Down Expand Up @@ -173,6 +187,7 @@ void QgsAbstractFileContentSourceLineEdit::embedFile()
return;

mBase64 = path;
mMode = ModeBase64;

mFileLineEdit->clear();
mFileLineEdit->setPlaceholderText( tr( "Embedded file" ) );
Expand All @@ -196,30 +211,27 @@ void QgsAbstractFileContentSourceLineEdit::extractFile()
s.setValue( settingsKey(), fi.absolutePath() );

// decode current base64 embedded file
QString path = mFileLineEdit->text().trimmed();
if ( path.startsWith( QLatin1String( "base64:" ), Qt::CaseInsensitive ) )
{
QByteArray base64 = mBase64.mid( 7 ).toLocal8Bit(); // strip 'base64:' prefix
QByteArray decoded = QByteArray::fromBase64( base64, QByteArray::OmitTrailingEquals );
QByteArray base64 = mBase64.mid( 7 ).toLocal8Bit(); // strip 'base64:' prefix
QByteArray decoded = QByteArray::fromBase64( base64, QByteArray::OmitTrailingEquals );

QFile fileOut( file );
fileOut.open( QIODevice::WriteOnly );
fileOut.write( decoded );
fileOut.close();
QFile fileOut( file );
fileOut.open( QIODevice::WriteOnly );
fileOut.write( decoded );
fileOut.close();

if ( mMessageBar )
{
mMessageBar->pushMessage( extractFileTitle(),
tr( "Successfully extracted file to <a href=\"%1\">%2</a>" ).arg( QUrl::fromLocalFile( file ).toString(), QDir::toNativeSeparators( file ) ),
Qgis::Success, 0 );
}
if ( mMessageBar )
{
mMessageBar->pushMessage( extractFileTitle(),
tr( "Successfully extracted file to <a href=\"%1\">%2</a>" ).arg( QUrl::fromLocalFile( file ).toString(), QDir::toNativeSeparators( file ) ),
Qgis::Success, 0 );
}
}

void QgsAbstractFileContentSourceLineEdit::mFileLineEdit_textEdited( const QString &text )
{
mFileLineEdit->setPlaceholderText( QString() );
mBase64.clear();
mMode = ModeFile;
if ( !text.isEmpty() && !QFileInfo::exists( text ) )
{
QUrl url( text );
Expand Down
8 changes: 8 additions & 0 deletions src/gui/qgsfilecontentsourcelineedit.h
Expand Up @@ -147,6 +147,14 @@ class GUI_EXPORT QgsAbstractFileContentSourceLineEdit : public QWidget SIP_ABSTR

private:

enum Mode
{
ModeFile,
ModeBase64,
};

Mode mMode = ModeFile;

QgsFilterLineEdit *mFileLineEdit = nullptr;
QToolButton *mFileToolButton = nullptr;
QString mLastPathKey;
Expand Down

0 comments on commit f5f4f53

Please sign in to comment.