Skip to content

Commit f5f4f53

Browse files
committedDec 10, 2018
Cleaner approach to base64 embedded widget handling
1 parent 2cca68a commit f5f4f53

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed
 

‎src/core/mesh/qgsmeshdataprovider.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ int QgsMeshDataBlock::count() const
256256
case Vector2DDouble:
257257
return static_cast<int>( mDoubleBuffer.size() / 2.0 );
258258
}
259+
return 0; // no warnings
259260
}
260261

261262
bool QgsMeshDataBlock::isValid() const
@@ -277,6 +278,7 @@ QgsMeshDatasetValue QgsMeshDataBlock::value( int index ) const
277278
mDoubleBuffer[2 * index + 1]
278279
);
279280
}
281+
return QgsMeshDatasetValue(); // no warnings
280282
}
281283

282284
bool QgsMeshDataBlock::active( int index ) const

‎src/gui/qgsfilecontentsourcelineedit.cpp

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ QgsAbstractFileContentSourceLineEdit::QgsAbstractFileContentSourceLineEdit( QWid
5656

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

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

8081
QString QgsAbstractFileContentSourceLineEdit::source() const
8182
{
82-
return mBase64.isEmpty() ? mFileLineEdit->text() : mBase64;
83+
switch ( mMode )
84+
{
85+
case ModeFile:
86+
return mFileLineEdit->text();
87+
88+
case ModeBase64:
89+
return mBase64;
90+
}
91+
92+
return QString();
8393
}
8494

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

97107
if ( isBase64 )
98108
{
109+
mMode = ModeBase64;
99110
mBase64 = source;
100111
mFileLineEdit->clear();
101112
mFileLineEdit->setPlaceholderText( tr( "Embedded file" ) );
102113
}
103114
else
104115
{
116+
mMode = ModeFile;
105117
mBase64.clear();
106118
mFileLineEdit->setText( source );
107119
mFileLineEdit->setPlaceholderText( QString() );
@@ -122,6 +134,7 @@ void QgsAbstractFileContentSourceLineEdit::selectFile()
122134
{
123135
return;
124136
}
137+
mMode = ModeFile;
125138
mBase64.clear();
126139
mFileLineEdit->setText( file );
127140
mFileLineEdit->setPlaceholderText( QString() );
@@ -135,6 +148,7 @@ void QgsAbstractFileContentSourceLineEdit::selectUrl()
135148
const QString path = QInputDialog::getText( this, fileFromUrlTitle(), fileFromUrlText(), QLineEdit::Normal, mFileLineEdit->text(), &ok );
136149
if ( ok && path != source() )
137150
{
151+
mMode = ModeFile;
138152
mBase64.clear();
139153
mFileLineEdit->setText( path );
140154
mFileLineEdit->setPlaceholderText( QString() );
@@ -173,6 +187,7 @@ void QgsAbstractFileContentSourceLineEdit::embedFile()
173187
return;
174188

175189
mBase64 = path;
190+
mMode = ModeBase64;
176191

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

198213
// decode current base64 embedded file
199-
QString path = mFileLineEdit->text().trimmed();
200-
if ( path.startsWith( QLatin1String( "base64:" ), Qt::CaseInsensitive ) )
201-
{
202-
QByteArray base64 = mBase64.mid( 7 ).toLocal8Bit(); // strip 'base64:' prefix
203-
QByteArray decoded = QByteArray::fromBase64( base64, QByteArray::OmitTrailingEquals );
214+
QByteArray base64 = mBase64.mid( 7 ).toLocal8Bit(); // strip 'base64:' prefix
215+
QByteArray decoded = QByteArray::fromBase64( base64, QByteArray::OmitTrailingEquals );
204216

205-
QFile fileOut( file );
206-
fileOut.open( QIODevice::WriteOnly );
207-
fileOut.write( decoded );
208-
fileOut.close();
217+
QFile fileOut( file );
218+
fileOut.open( QIODevice::WriteOnly );
219+
fileOut.write( decoded );
220+
fileOut.close();
209221

210-
if ( mMessageBar )
211-
{
212-
mMessageBar->pushMessage( extractFileTitle(),
213-
tr( "Successfully extracted file to <a href=\"%1\">%2</a>" ).arg( QUrl::fromLocalFile( file ).toString(), QDir::toNativeSeparators( file ) ),
214-
Qgis::Success, 0 );
215-
}
222+
if ( mMessageBar )
223+
{
224+
mMessageBar->pushMessage( extractFileTitle(),
225+
tr( "Successfully extracted file to <a href=\"%1\">%2</a>" ).arg( QUrl::fromLocalFile( file ).toString(), QDir::toNativeSeparators( file ) ),
226+
Qgis::Success, 0 );
216227
}
217228
}
218229

219230
void QgsAbstractFileContentSourceLineEdit::mFileLineEdit_textEdited( const QString &text )
220231
{
221232
mFileLineEdit->setPlaceholderText( QString() );
222233
mBase64.clear();
234+
mMode = ModeFile;
223235
if ( !text.isEmpty() && !QFileInfo::exists( text ) )
224236
{
225237
QUrl url( text );

‎src/gui/qgsfilecontentsourcelineedit.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,14 @@ class GUI_EXPORT QgsAbstractFileContentSourceLineEdit : public QWidget SIP_ABSTR
147147

148148
private:
149149

150+
enum Mode
151+
{
152+
ModeFile,
153+
ModeBase64,
154+
};
155+
156+
Mode mMode = ModeFile;
157+
150158
QgsFilterLineEdit *mFileLineEdit = nullptr;
151159
QToolButton *mFileToolButton = nullptr;
152160
QString mLastPathKey;

0 commit comments

Comments
 (0)
Please sign in to comment.