22 #include <QToolButton> 23 #include <QHBoxLayout> 24 #include <QFileDialog> 25 #include <QInputDialog> 34 QHBoxLayout *layout =
new QHBoxLayout(
this );
35 layout->setContentsMargins( 0, 0, 0, 0 );
38 mFileToolButton =
new QToolButton(
this );
39 mFileToolButton->setText( QString( QChar( 0x2026 ) ) );
40 layout->addWidget( mFileLineEdit, 1 );
41 layout->addWidget( mFileToolButton );
44 QMenu *sourceMenu =
new QMenu( mFileToolButton );
46 QAction *selectFileAction =
new QAction( tr(
"Select File…" ), sourceMenu );
47 connect( selectFileAction, &QAction::triggered,
this, &QgsAbstractFileContentSourceLineEdit::selectFile );
48 sourceMenu->addAction( selectFileAction );
50 QAction *embedFileAction =
new QAction( tr(
"Embed File…" ), sourceMenu );
51 connect( embedFileAction, &QAction::triggered,
this, &QgsAbstractFileContentSourceLineEdit::embedFile );
52 sourceMenu->addAction( embedFileAction );
54 QAction *extractFileAction =
new QAction( tr(
"Extract Embedded File…" ), sourceMenu );
55 connect( extractFileAction, &QAction::triggered,
this, &QgsAbstractFileContentSourceLineEdit::extractFile );
56 sourceMenu->addAction( extractFileAction );
58 connect( sourceMenu, &QMenu::aboutToShow,
this, [
this, extractFileAction]
60 extractFileAction->setEnabled( mMode == ModeBase64 );
63 QAction *enterUrlAction =
new QAction( tr(
"From URL…" ), sourceMenu );
64 connect( enterUrlAction, &QAction::triggered,
this, &QgsAbstractFileContentSourceLineEdit::selectUrl );
65 sourceMenu->addAction( enterUrlAction );
67 mFileToolButton->setMenu( sourceMenu );
68 mFileToolButton->setPopupMode( QToolButton::MenuButtonPopup );
69 connect( mFileToolButton, &QToolButton::clicked,
this, &QgsAbstractFileContentSourceLineEdit::selectFile );
71 connect( mFileLineEdit, &QLineEdit::textEdited,
this, &QgsAbstractFileContentSourceLineEdit::mFileLineEdit_textEdited );
75 mFileLineEdit->setPlaceholderText( QString() );
87 return mFileLineEdit->text();
103 const bool isBase64 = source.startsWith( QLatin1String(
"base64:" ), Qt::CaseInsensitive );
105 if ( ( !isBase64 && source == mFileLineEdit->text() && mBase64.isEmpty() ) || ( isBase64 && source == mBase64 ) )
112 mFileLineEdit->clear();
113 mFileLineEdit->setPlaceholderText( tr(
"Embedded file" ) );
119 mFileLineEdit->setText( source );
120 mFileLineEdit->setPlaceholderText( QString() );
126 void QgsAbstractFileContentSourceLineEdit::selectFile()
129 QString file = QFileDialog::getOpenFileName(
nullptr,
133 QFileInfo fi( file );
134 if ( file.isEmpty() || !fi.exists() || file ==
source() )
140 mFileLineEdit->setText( file );
141 mFileLineEdit->setPlaceholderText( QString() );
142 s.
setValue( settingsKey(), fi.absolutePath() );
146 void QgsAbstractFileContentSourceLineEdit::selectUrl()
149 const QString path = QInputDialog::getText(
this, fileFromUrlTitle(), fileFromUrlText(), QLineEdit::Normal, mFileLineEdit->text(), &ok );
150 if ( ok && path !=
source() )
154 mFileLineEdit->setText( path );
155 mFileLineEdit->setPlaceholderText( QString() );
160 void QgsAbstractFileContentSourceLineEdit::embedFile()
163 QString file = QFileDialog::getOpenFileName(
nullptr,
167 QFileInfo fi( file );
168 if ( file.isEmpty() || !fi.exists() )
173 s.
setValue( settingsKey(), fi.absolutePath() );
176 QFile fileSource( file );
177 if ( !fileSource.open( QIODevice::ReadOnly ) )
182 QByteArray blob = fileSource.readAll();
183 QByteArray encoded = blob.toBase64();
185 QString path( encoded );
186 path.prepend( QLatin1String(
"base64:" ) );
193 mFileLineEdit->clear();
194 mFileLineEdit->setPlaceholderText( tr(
"Embedded file" ) );
199 void QgsAbstractFileContentSourceLineEdit::extractFile()
202 QString file = QFileDialog::getSaveFileName(
nullptr,
206 if ( file.isEmpty() )
211 QFileInfo fi( file );
212 s.
setValue( settingsKey(), fi.absolutePath() );
215 QByteArray base64 = mBase64.mid( 7 ).toLocal8Bit();
216 QByteArray decoded = QByteArray::fromBase64( base64, QByteArray::OmitTrailingEquals );
218 QFile fileOut( file );
219 fileOut.open( QIODevice::WriteOnly );
220 fileOut.write( decoded );
226 tr(
"Successfully extracted file to <a href=\"%1\">%2</a>" ).arg( QUrl::fromLocalFile( file ).toString(), QDir::toNativeSeparators( file ) ),
231 void QgsAbstractFileContentSourceLineEdit::mFileLineEdit_textEdited(
const QString &text )
233 mFileLineEdit->setPlaceholderText( QString() );
236 if ( !text.isEmpty() && !QFileInfo::exists( text ) )
239 if ( !url.isValid() )
247 QString QgsAbstractFileContentSourceLineEdit::defaultPath()
const 249 if ( QFileInfo::exists(
source() ) )
255 QString QgsAbstractFileContentSourceLineEdit::settingsKey()
const 257 return mLastPathKey.isEmpty() ? defaultSettingsKey() : mLastPathKey;
266 QString QgsSvgSourceLineEdit::fileFilter()
const 268 return tr(
"SVG files" ) +
" (*.svg)";
271 QString QgsSvgSourceLineEdit::selectFileTitle()
const 273 return tr(
"Select SVG File" );
276 QString QgsSvgSourceLineEdit::fileFromUrlTitle()
const 278 return tr(
"SVG From URL" );
281 QString QgsSvgSourceLineEdit::fileFromUrlText()
const 283 return tr(
"Enter SVG URL" );
286 QString QgsSvgSourceLineEdit::embedFileTitle()
const 288 return tr(
"Embed SVG File" );
291 QString QgsSvgSourceLineEdit::extractFileTitle()
const 293 return tr(
"Extract SVG File" );
296 QString QgsSvgSourceLineEdit::defaultSettingsKey()
const 298 return QStringLiteral(
"/UI/lastSVGDir" );
308 QString QgsImageSourceLineEdit::fileFilter()
const 310 return tr(
"All files" ) +
" (*.*)";
313 QString QgsImageSourceLineEdit::selectFileTitle()
const 315 return tr(
"Select Image File" );
318 QString QgsImageSourceLineEdit::fileFromUrlTitle()
const 320 return tr(
"Image From URL" );
323 QString QgsImageSourceLineEdit::fileFromUrlText()
const 325 return tr(
"Enter image URL" );
328 QString QgsImageSourceLineEdit::embedFileTitle()
const 330 return tr(
"Embed Image File" );
333 QString QgsImageSourceLineEdit::extractFileTitle()
const 335 return tr(
"Extract Image File" );
338 QString QgsImageSourceLineEdit::defaultSettingsKey()
const 340 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.