/
qgsfilecontentsourcelineedit.cpp
352 lines (292 loc) · 9.32 KB
/
qgsfilecontentsourcelineedit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/***************************************************************************
qgsfilecontentsourcelineedit.cpp
-----------------------
begin : July 2018
copyright : (C) 2018 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsfilecontentsourcelineedit.h"
#include "qgssettings.h"
#include "qgsmessagebar.h"
#include "qgsfilterlineedit.h"
#include <QMenu>
#include <QLineEdit>
#include <QToolButton>
#include <QHBoxLayout>
#include <QFileDialog>
#include <QInputDialog>
//
// QgsAbstractFileContentSourceLineEdit
//
QgsAbstractFileContentSourceLineEdit::QgsAbstractFileContentSourceLineEdit( QWidget *parent )
: QWidget( parent )
{
QHBoxLayout *layout = new QHBoxLayout( this );
mFileLineEdit = new QgsFilterLineEdit( this );
mFileLineEdit->setShowClearButton( true );
mFileToolButton = new QToolButton( this );
mFileToolButton->setText( tr( "…" ) );
layout->addWidget( mFileLineEdit, 1 );
layout->addWidget( mFileToolButton );
setLayout( layout );
QMenu *sourceMenu = new QMenu( mFileToolButton );
QAction *selectFileAction = new QAction( tr( "Select File…" ), sourceMenu );
connect( selectFileAction, &QAction::triggered, this, &QgsAbstractFileContentSourceLineEdit::selectFile );
sourceMenu->addAction( selectFileAction );
QAction *embedFileAction = new QAction( tr( "Embed File…" ), sourceMenu );
connect( embedFileAction, &QAction::triggered, this, &QgsAbstractFileContentSourceLineEdit::embedFile );
sourceMenu->addAction( embedFileAction );
QAction *extractFileAction = new QAction( tr( "Extract Embedded File…" ), sourceMenu );
connect( extractFileAction, &QAction::triggered, this, &QgsAbstractFileContentSourceLineEdit::extractFile );
sourceMenu->addAction( extractFileAction );
connect( sourceMenu, &QMenu::aboutToShow, this, [this, extractFileAction]
{
extractFileAction->setEnabled( mMode == ModeBase64 );
} );
QAction *enterUrlAction = new QAction( tr( "From URL…" ), sourceMenu );
connect( enterUrlAction, &QAction::triggered, this, &QgsAbstractFileContentSourceLineEdit::selectUrl );
sourceMenu->addAction( enterUrlAction );
mFileToolButton->setMenu( sourceMenu );
mFileToolButton->setPopupMode( QToolButton::MenuButtonPopup );
connect( mFileToolButton, &QToolButton::clicked, this, &QgsAbstractFileContentSourceLineEdit::selectFile );
connect( mFileLineEdit, &QLineEdit::textEdited, this, &QgsAbstractFileContentSourceLineEdit::mFileLineEdit_textEdited );
connect( mFileLineEdit, &QgsFilterLineEdit::cleared, this, [ = ]
{
mMode = ModeFile;
mFileLineEdit->setPlaceholderText( QString() );
mBase64.clear();
emit sourceChanged( QString() );
} );
}
QString QgsAbstractFileContentSourceLineEdit::source() const
{
switch ( mMode )
{
case ModeFile:
return mFileLineEdit->text();
case ModeBase64:
return mBase64;
}
return QString();
}
void QgsAbstractFileContentSourceLineEdit::setLastPathSettingsKey( const QString &key )
{
mLastPathKey = key;
}
void QgsAbstractFileContentSourceLineEdit::setSource( const QString &source )
{
const bool isBase64 = source.startsWith( QLatin1String( "base64:" ), Qt::CaseInsensitive );
if ( ( !isBase64 && source == mFileLineEdit->text() && mBase64.isEmpty() ) || ( isBase64 && source == mBase64 ) )
return;
if ( isBase64 )
{
mMode = ModeBase64;
mBase64 = source;
mFileLineEdit->clear();
mFileLineEdit->setPlaceholderText( tr( "Embedded file" ) );
}
else
{
mMode = ModeFile;
mBase64.clear();
mFileLineEdit->setText( source );
mFileLineEdit->setPlaceholderText( QString() );
}
emit sourceChanged( source );
}
void QgsAbstractFileContentSourceLineEdit::selectFile()
{
QgsSettings s;
QString file = QFileDialog::getOpenFileName( nullptr,
selectFileTitle(),
defaultPath(),
fileFilter() );
QFileInfo fi( file );
if ( file.isEmpty() || !fi.exists() || file == source() )
{
return;
}
mMode = ModeFile;
mBase64.clear();
mFileLineEdit->setText( file );
mFileLineEdit->setPlaceholderText( QString() );
s.setValue( settingsKey(), fi.absolutePath() );
emit sourceChanged( mFileLineEdit->text() );
}
void QgsAbstractFileContentSourceLineEdit::selectUrl()
{
bool ok = false;
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() );
emit sourceChanged( mFileLineEdit->text() );
}
}
void QgsAbstractFileContentSourceLineEdit::embedFile()
{
QgsSettings s;
QString file = QFileDialog::getOpenFileName( nullptr,
embedFileTitle(),
defaultPath(),
fileFilter() );
QFileInfo fi( file );
if ( file.isEmpty() || !fi.exists() )
{
return;
}
s.setValue( settingsKey(), fi.absolutePath() );
// encode file as base64
QFile fileSource( file );
if ( !fileSource.open( QIODevice::ReadOnly ) )
{
return;
}
QByteArray blob = fileSource.readAll();
QByteArray encoded = blob.toBase64();
QString path( encoded );
path.prepend( QLatin1String( "base64:" ) );
if ( path == source() )
return;
mBase64 = path;
mMode = ModeBase64;
mFileLineEdit->clear();
mFileLineEdit->setPlaceholderText( tr( "Embedded file" ) );
emit sourceChanged( path );
}
void QgsAbstractFileContentSourceLineEdit::extractFile()
{
QgsSettings s;
QString file = QFileDialog::getSaveFileName( nullptr,
extractFileTitle(),
defaultPath(),
fileFilter() );
if ( file.isEmpty() )
{
return;
}
QFileInfo fi( file );
s.setValue( settingsKey(), fi.absolutePath() );
// decode current base64 embedded file
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();
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 );
if ( !url.isValid() )
{
return;
}
}
emit sourceChanged( text );
}
QString QgsAbstractFileContentSourceLineEdit::defaultPath() const
{
if ( QFileInfo::exists( source() ) )
return source();
return QgsSettings().value( settingsKey(), QDir::homePath() ).toString();
}
QString QgsAbstractFileContentSourceLineEdit::settingsKey() const
{
return mLastPathKey.isEmpty() ? defaultSettingsKey() : mLastPathKey;
}
//
// QgsSvgSourceLineEdit
//
///@cond PRIVATE
QString QgsSvgSourceLineEdit::fileFilter() const
{
return tr( "SVG files" ) + " (*.svg)";
}
QString QgsSvgSourceLineEdit::selectFileTitle() const
{
return tr( "Select SVG File" );
}
QString QgsSvgSourceLineEdit::fileFromUrlTitle() const
{
return tr( "SVG From URL" );
}
QString QgsSvgSourceLineEdit::fileFromUrlText() const
{
return tr( "Enter SVG URL" );
}
QString QgsSvgSourceLineEdit::embedFileTitle() const
{
return tr( "Embed SVG File" );
}
QString QgsSvgSourceLineEdit::extractFileTitle() const
{
return tr( "Extract SVG File" );
}
QString QgsSvgSourceLineEdit::defaultSettingsKey() const
{
return QStringLiteral( "/UI/lastSVGDir" );
}
///@endcond
//
// QgsImageSourceLineEdit
//
///@cond PRIVATE
QString QgsImageSourceLineEdit::fileFilter() const
{
return tr( "All files" ) + " (*.*)";
}
QString QgsImageSourceLineEdit::selectFileTitle() const
{
return tr( "Select Image File" );
}
QString QgsImageSourceLineEdit::fileFromUrlTitle() const
{
return tr( "Image From URL" );
}
QString QgsImageSourceLineEdit::fileFromUrlText() const
{
return tr( "Enter image URL" );
}
QString QgsImageSourceLineEdit::embedFileTitle() const
{
return tr( "Embed Image File" );
}
QString QgsImageSourceLineEdit::extractFileTitle() const
{
return tr( "Extract Image File" );
}
QString QgsImageSourceLineEdit::defaultSettingsKey() const
{
return QStringLiteral( "/UI/lastImageDir" );
}
///@endcond
void QgsAbstractFileContentSourceLineEdit::setMessageBar( QgsMessageBar *bar )
{
mMessageBar = bar;
}
QgsMessageBar *QgsAbstractFileContentSourceLineEdit::messageBar() const
{
return mMessageBar;
}