22 #include <QToolButton> 23 #include <QHBoxLayout> 24 #include <QFileDialog> 25 #include <QInputDialog> 34 QHBoxLayout *layout =
new QHBoxLayout(
this );
37 mFileToolButton =
new QToolButton(
this );
38 mFileToolButton->setText( QStringLiteral(
"…" ) );
39 layout->addWidget( mFileLineEdit, 1 );
40 layout->addWidget( mFileToolButton );
43 QMenu *sourceMenu =
new QMenu( mFileToolButton );
45 QAction *selectFileAction =
new QAction( tr(
"Select File…" ), sourceMenu );
46 connect( selectFileAction, &QAction::triggered,
this, &QgsAbstractFileContentSourceLineEdit::selectFile );
47 sourceMenu->addAction( selectFileAction );
49 QAction *embedFileAction =
new QAction( tr(
"Embed File…" ), sourceMenu );
50 connect( embedFileAction, &QAction::triggered,
this, &QgsAbstractFileContentSourceLineEdit::embedFile );
51 sourceMenu->addAction( embedFileAction );
53 QAction *extractFileAction =
new QAction( tr(
"Extract Embedded File…" ), sourceMenu );
54 connect( extractFileAction, &QAction::triggered,
this, &QgsAbstractFileContentSourceLineEdit::extractFile );
55 sourceMenu->addAction( extractFileAction );
57 connect( sourceMenu, &QMenu::aboutToShow,
this, [
this, extractFileAction]
59 extractFileAction->setEnabled( mMode == ModeBase64 );
62 QAction *enterUrlAction =
new QAction( tr(
"From URL…" ), sourceMenu );
63 connect( enterUrlAction, &QAction::triggered,
this, &QgsAbstractFileContentSourceLineEdit::selectUrl );
64 sourceMenu->addAction( enterUrlAction );
66 mFileToolButton->setMenu( sourceMenu );
67 mFileToolButton->setPopupMode( QToolButton::MenuButtonPopup );
68 connect( mFileToolButton, &QToolButton::clicked,
this, &QgsAbstractFileContentSourceLineEdit::selectFile );
70 connect( mFileLineEdit, &QLineEdit::textEdited,
this, &QgsAbstractFileContentSourceLineEdit::mFileLineEdit_textEdited );
74 mFileLineEdit->setPlaceholderText( QString() );
86 return mFileLineEdit->text();
102 const bool isBase64 = source.startsWith( QLatin1String(
"base64:" ), Qt::CaseInsensitive );
104 if ( ( !isBase64 && source == mFileLineEdit->text() && mBase64.isEmpty() ) || ( isBase64 && source == mBase64 ) )
111 mFileLineEdit->clear();
112 mFileLineEdit->setPlaceholderText( tr(
"Embedded file" ) );
118 mFileLineEdit->setText( source );
119 mFileLineEdit->setPlaceholderText( QString() );
125 void QgsAbstractFileContentSourceLineEdit::selectFile()
128 QString file = QFileDialog::getOpenFileName(
nullptr,
132 QFileInfo fi( file );
133 if ( file.isEmpty() || !fi.exists() || file ==
source() )
139 mFileLineEdit->setText( file );
140 mFileLineEdit->setPlaceholderText( QString() );
141 s.
setValue( settingsKey(), fi.absolutePath() );
145 void QgsAbstractFileContentSourceLineEdit::selectUrl()
148 const QString path = QInputDialog::getText(
this, fileFromUrlTitle(), fileFromUrlText(), QLineEdit::Normal, mFileLineEdit->text(), &ok );
149 if ( ok && path !=
source() )
153 mFileLineEdit->setText( path );
154 mFileLineEdit->setPlaceholderText( QString() );
159 void QgsAbstractFileContentSourceLineEdit::embedFile()
162 QString file = QFileDialog::getOpenFileName(
nullptr,
166 QFileInfo fi( file );
167 if ( file.isEmpty() || !fi.exists() )
172 s.
setValue( settingsKey(), fi.absolutePath() );
175 QFile fileSource( file );
176 if ( !fileSource.open( QIODevice::ReadOnly ) )
181 QByteArray blob = fileSource.readAll();
182 QByteArray encoded = blob.toBase64();
184 QString path( encoded );
185 path.prepend( QLatin1String(
"base64:" ) );
192 mFileLineEdit->clear();
193 mFileLineEdit->setPlaceholderText( tr(
"Embedded file" ) );
198 void QgsAbstractFileContentSourceLineEdit::extractFile()
201 QString file = QFileDialog::getSaveFileName(
nullptr,
205 if ( file.isEmpty() )
210 QFileInfo fi( file );
211 s.
setValue( settingsKey(), fi.absolutePath() );
214 QByteArray base64 = mBase64.mid( 7 ).toLocal8Bit();
215 QByteArray decoded = QByteArray::fromBase64( base64, QByteArray::OmitTrailingEquals );
217 QFile fileOut( file );
218 fileOut.open( QIODevice::WriteOnly );
219 fileOut.write( decoded );
225 tr(
"Successfully extracted file to <a href=\"%1\">%2</a>" ).arg( QUrl::fromLocalFile( file ).toString(), QDir::toNativeSeparators( file ) ),
230 void QgsAbstractFileContentSourceLineEdit::mFileLineEdit_textEdited(
const QString &text )
232 mFileLineEdit->setPlaceholderText( QString() );
235 if ( !text.isEmpty() && !QFileInfo::exists( text ) )
238 if ( !url.isValid() )
246 QString QgsAbstractFileContentSourceLineEdit::defaultPath()
const 248 if ( QFileInfo::exists(
source() ) )
254 QString QgsAbstractFileContentSourceLineEdit::settingsKey()
const 256 return mLastPathKey.isEmpty() ? defaultSettingsKey() : mLastPathKey;
265 QString QgsSvgSourceLineEdit::fileFilter()
const 267 return tr(
"SVG files" ) +
" (*.svg)";
270 QString QgsSvgSourceLineEdit::selectFileTitle()
const 272 return tr(
"Select SVG File" );
275 QString QgsSvgSourceLineEdit::fileFromUrlTitle()
const 277 return tr(
"SVG From URL" );
280 QString QgsSvgSourceLineEdit::fileFromUrlText()
const 282 return tr(
"Enter SVG URL" );
285 QString QgsSvgSourceLineEdit::embedFileTitle()
const 287 return tr(
"Embed SVG File" );
290 QString QgsSvgSourceLineEdit::extractFileTitle()
const 292 return tr(
"Extract SVG File" );
295 QString QgsSvgSourceLineEdit::defaultSettingsKey()
const 297 return QStringLiteral(
"/UI/lastSVGDir" );
307 QString QgsImageSourceLineEdit::fileFilter()
const 309 return tr(
"All files" ) +
" (*.*)";
312 QString QgsImageSourceLineEdit::selectFileTitle()
const 314 return tr(
"Select Image File" );
317 QString QgsImageSourceLineEdit::fileFromUrlTitle()
const 319 return tr(
"Image From URL" );
322 QString QgsImageSourceLineEdit::fileFromUrlText()
const 324 return tr(
"Enter image URL" );
327 QString QgsImageSourceLineEdit::embedFileTitle()
const 329 return tr(
"Embed Image File" );
332 QString QgsImageSourceLineEdit::extractFileTitle()
const 334 return tr(
"Extract Image File" );
337 QString QgsImageSourceLineEdit::defaultSettingsKey()
const 339 return QStringLiteral(
"/UI/lastImageDir" );
QgsAbstractFileContentSourceLineEdit(QWidget *parent=nullptr)
Constructor for QgsAbstractFileContentSourceLineEdit, with the specified parent widget.
This class is a composition of two QSettings instances:
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setSource(const QString &source)
Sets a new source to show in the widget.
void setLastPathSettingsKey(const QString &key)
Sets a specific settings key to use when storing the last used path for the file source.
void sourceChanged(const QString &source)
Emitted whenever the file source is changed in the widget.
A bar for displaying non-blocking messages to the user.
QgsMessageBar * messageBar() const
Returns the message bar associated with the widget.
QLineEdit subclass with built in support for clearing the widget's value and handling custom null val...
void pushMessage(const QString &text, Qgis::MessageLevel level=Qgis::Info, int duration=5)
convenience method for pushing a message to the bar
void setMessageBar(QgsMessageBar *bar)
Sets the message bar associated with the widget.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
void cleared()
Emitted when the widget is cleared.
void setShowClearButton(bool visible)
Sets whether the widget's clear button is visible.
QString source() const
Returns the current file source.