17#include "moc_qgsfilecontentsourcelineedit.cpp"
25#include <QImageReader>
26#include <QInputDialog>
40 QHBoxLayout *layout =
new QHBoxLayout(
this );
41 layout->setContentsMargins( 0, 0, 0, 0 );
44 mFileToolButton =
new QToolButton(
this );
45 mFileToolButton->setText( QString( QChar( 0x2026 ) ) );
47 layout->addWidget( mFileLineEdit, 1 );
48 layout->addWidget( mFileToolButton );
49 layout->addWidget( mPropertyOverrideButton );
52 QMenu *sourceMenu =
new QMenu( mFileToolButton );
54 QAction *selectFileAction =
new QAction( tr(
"Select File…" ), sourceMenu );
55 connect( selectFileAction, &QAction::triggered,
this, &QgsAbstractFileContentSourceLineEdit::selectFile );
56 sourceMenu->addAction( selectFileAction );
58 QAction *embedFileAction =
new QAction( tr(
"Embed File…" ), sourceMenu );
59 connect( embedFileAction, &QAction::triggered,
this, &QgsAbstractFileContentSourceLineEdit::embedFile );
60 sourceMenu->addAction( embedFileAction );
62 QAction *extractFileAction =
new QAction( tr(
"Extract Embedded File…" ), sourceMenu );
63 connect( extractFileAction, &QAction::triggered,
this, &QgsAbstractFileContentSourceLineEdit::extractFile );
64 sourceMenu->addAction( extractFileAction );
66 connect( sourceMenu, &QMenu::aboutToShow,
this, [
this, extractFileAction]
68 extractFileAction->setEnabled( mMode == ModeBase64 );
71 QAction *enterUrlAction =
new QAction( tr(
"From URL…" ), sourceMenu );
72 connect( enterUrlAction, &QAction::triggered,
this, &QgsAbstractFileContentSourceLineEdit::selectUrl );
73 sourceMenu->addAction( enterUrlAction );
75 mFileToolButton->setMenu( sourceMenu );
76 mFileToolButton->setPopupMode( QToolButton::MenuButtonPopup );
77 connect( mFileToolButton, &QToolButton::clicked,
this, &QgsAbstractFileContentSourceLineEdit::selectFile );
79 connect( mFileLineEdit, &QLineEdit::textEdited,
this, &QgsAbstractFileContentSourceLineEdit::mFileLineEdit_textEdited );
83 mFileLineEdit->setPlaceholderText( QString() );
88 mPropertyOverrideButton->setVisible( mPropertyOverrideButtonVisible );
97 return mFileLineEdit->text();
113 mPropertyOverrideButtonVisible = visible;
114 mPropertyOverrideButton->setVisible( visible );
119 const bool isBase64 =
source.startsWith( QLatin1String(
"base64:" ), Qt::CaseInsensitive );
121 if ( ( !isBase64 &&
source == mFileLineEdit->text() && mBase64.isEmpty() ) || ( isBase64 &&
source == mBase64 ) )
128 mFileLineEdit->clear();
129 mFileLineEdit->setPlaceholderText( tr(
"Embedded file" ) );
135 mFileLineEdit->setText(
source );
136 mFileLineEdit->setPlaceholderText( QString() );
142void QgsAbstractFileContentSourceLineEdit::selectFile()
145 const QString file = QFileDialog::getOpenFileName(
nullptr,
149 const QFileInfo fi( file );
150 if ( file.isEmpty() || !fi.exists() || file ==
source() )
156 mFileLineEdit->setText( file );
157 mFileLineEdit->setPlaceholderText( QString() );
158 s.
setValue( settingsKey(), fi.absolutePath() );
162void QgsAbstractFileContentSourceLineEdit::selectUrl()
165 const QString path = QInputDialog::getText(
this, fileFromUrlTitle(), fileFromUrlText(), QLineEdit::Normal, mFileLineEdit->text(), &ok );
166 if ( ok && path !=
source() )
170 mFileLineEdit->setText( path );
171 mFileLineEdit->setPlaceholderText( QString() );
176void QgsAbstractFileContentSourceLineEdit::embedFile()
179 const QString file = QFileDialog::getOpenFileName(
nullptr,
183 const QFileInfo fi( file );
184 if ( file.isEmpty() || !fi.exists() )
189 s.
setValue( settingsKey(), fi.absolutePath() );
192 QFile fileSource( file );
193 if ( !fileSource.open( QIODevice::ReadOnly ) )
198 const QByteArray blob = fileSource.readAll();
199 const QByteArray encoded = blob.toBase64();
201 QString path( encoded );
202 path.prepend( QLatin1String(
"base64:" ) );
209 mFileLineEdit->clear();
210 mFileLineEdit->setPlaceholderText( tr(
"Embedded file" ) );
215void QgsAbstractFileContentSourceLineEdit::extractFile()
218 const QString file = QFileDialog::getSaveFileName(
nullptr,
225 if ( file.isEmpty() )
230 const QFileInfo fi( file );
231 s.
setValue( settingsKey(), fi.absolutePath() );
234 const QByteArray base64 = mBase64.mid( 7 ).toLocal8Bit();
235 const QByteArray decoded = QByteArray::fromBase64( base64, QByteArray::OmitTrailingEquals );
237 QFile fileOut( file );
238 fileOut.open( QIODevice::WriteOnly );
239 fileOut.write( decoded );
245 tr(
"Successfully extracted file to <a href=\"%1\">%2</a>" ).arg( QUrl::fromLocalFile( file ).toString(), QDir::toNativeSeparators( file ) ),
250void QgsAbstractFileContentSourceLineEdit::mFileLineEdit_textEdited(
const QString &text )
252 mFileLineEdit->setPlaceholderText( QString() );
255 if ( !text.isEmpty() && !QFileInfo::exists( text ) )
257 const QUrl url( text );
258 if ( !url.isValid() )
266QString QgsAbstractFileContentSourceLineEdit::defaultPath()
const
268 if ( QFileInfo::exists(
source() ) )
274QString QgsAbstractFileContentSourceLineEdit::settingsKey()
const
276 return mLastPathKey.isEmpty() ? defaultSettingsKey() : mLastPathKey;
298QString QgsPictureSourceLineEditBase::fileFilter()
const
303 return tr(
"SVG files" ) +
" (*.svg)";
306 QStringList formatsFilter;
307 const QByteArrayList supportedFormats = QImageReader::supportedImageFormats();
308 for (
const auto &format : supportedFormats )
310 formatsFilter.append( QString( QStringLiteral(
"*.%1" ) ).arg( QString( format ) ) );
312 return QString(
"%1 (%2);;%3 (*.*)" ).arg( tr(
"Images" ), formatsFilter.join( QLatin1Char(
' ' ) ), tr(
"All files" ) );
317 QStringList formatsFilter;
318 const QByteArrayList supportedFormats = QMovie::supportedFormats();
319 for (
const auto &format : supportedFormats )
321 formatsFilter.append( QString( QStringLiteral(
"*.%1" ) ).arg( QString( format ) ) );
323 return QString(
"%1 (%2);;%3 (*.*)" ).arg( tr(
"Animated Images" ), formatsFilter.join( QLatin1Char(
' ' ) ), tr(
"All files" ) );
329QString QgsPictureSourceLineEditBase::selectFileTitle()
const
334 return tr(
"Select SVG File" );
336 return tr(
"Select Image File" );
338 return tr(
"Select Animated Image File" );
343QString QgsPictureSourceLineEditBase::fileFromUrlTitle()
const
348 return tr(
"SVG From URL" );
350 return tr(
"Image From URL" );
352 return tr(
"Animated Image From URL" );
357QString QgsPictureSourceLineEditBase::fileFromUrlText()
const
362 return tr(
"Enter SVG URL" );
364 return tr(
"Enter image URL" );
366 return tr(
"Enter animated image URL" );
371QString QgsPictureSourceLineEditBase::embedFileTitle()
const
376 return tr(
"Embed SVG File" );
378 return tr(
"Embed Image File" );
380 return tr(
"Embed Animated Image File" );
385QString QgsPictureSourceLineEditBase::extractFileTitle()
const
390 return tr(
"Extract SVG File" );
392 return tr(
"Extract Image File" );
394 return tr(
"Extract Animated Image File" );
399QString QgsPictureSourceLineEditBase::defaultSettingsKey()
const
404 return QStringLiteral(
"/UI/lastSVGDir" );
406 return QStringLiteral(
"/UI/lastImageDir" );
408 return QStringLiteral(
"/UI/lastAnimatedImageDir" );
@ Success
Used for reporting a successful operation.
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.
void setMessageBar(QgsMessageBar *bar)
Sets the message bar associated with the widget.
void setPropertyOverrideToolButtonVisible(bool visible)
Sets the visibility of the property override tool button.
QgsMessageBar * messageBar() const
Returns the message bar associated with the widget.
void setSource(const QString &source)
Sets a new source to show in the widget.
QgsAbstractFileContentSourceLineEdit(QWidget *parent=nullptr)
Constructor for QgsAbstractFileContentSourceLineEdit, with the specified parent widget.
QLineEdit subclass with built in support for clearing the widget's value and handling custom null val...
void setShowClearButton(bool visible)
Sets whether the widget's clear button is visible.
void cleared()
Emitted when the widget is cleared.
A bar for displaying non-blocking messages to the user.
void pushMessage(const QString &text, Qgis::MessageLevel level=Qgis::MessageLevel::Info, int duration=-1)
A convenience method for pushing a message with the specified text to the bar.
@ AnimatedImage
Animated image.
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 setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
#define BUILTIN_UNREACHABLE