20 #include <QToolButton> 21 #include <QHBoxLayout> 22 #include <QFileDialog> 23 #include <QInputDialog> 28 QHBoxLayout *layout =
new QHBoxLayout(
this );
29 mFileLineEdit =
new QLineEdit(
this );
30 mFileToolButton =
new QToolButton(
this );
31 mFileToolButton->setText( tr(
"…" ) );
32 layout->addWidget( mFileLineEdit, 1 );
33 layout->addWidget( mFileToolButton );
36 QMenu *sourceMenu =
new QMenu( mFileToolButton );
38 QAction *selectFileAction =
new QAction( tr(
"Select File…" ), sourceMenu );
39 connect( selectFileAction, &QAction::triggered,
this, &QgsSvgSourceLineEdit::selectFile );
40 sourceMenu->addAction( selectFileAction );
42 QAction *embedFileAction =
new QAction( tr(
"Embed File…" ), sourceMenu );
43 connect( embedFileAction, &QAction::triggered,
this, &QgsSvgSourceLineEdit::embedFile );
44 sourceMenu->addAction( embedFileAction );
46 QAction *extractFileAction =
new QAction( tr(
"Extract Embedded File…" ), sourceMenu );
47 connect( extractFileAction, &QAction::triggered,
this, &QgsSvgSourceLineEdit::extractFile );
48 sourceMenu->addAction( extractFileAction );
50 connect( sourceMenu, &QMenu::aboutToShow,
this, [
this, extractFileAction]
52 extractFileAction->setEnabled( mFileLineEdit->text().startsWith( QLatin1String(
"base64:" ), Qt::CaseInsensitive ) );
55 QAction *enterUrlAction =
new QAction( tr(
"From URL…" ), sourceMenu );
56 connect( enterUrlAction, &QAction::triggered,
this, &QgsSvgSourceLineEdit::selectUrl );
57 sourceMenu->addAction( enterUrlAction );
59 mFileToolButton->setMenu( sourceMenu );
60 mFileToolButton->setPopupMode( QToolButton::MenuButtonPopup );
61 connect( mFileToolButton, &QToolButton::clicked,
this, &QgsSvgSourceLineEdit::selectFile );
63 connect( mFileLineEdit, &QLineEdit::textEdited,
this, &QgsSvgSourceLineEdit::mFileLineEdit_textEdited );
64 connect( mFileLineEdit, &QLineEdit::editingFinished,
this, &QgsSvgSourceLineEdit::mFileLineEdit_editingFinished );
69 return mBase64.isEmpty() ? mFileLineEdit->text() : mBase64;
79 const bool isBase64 = source.startsWith( QLatin1String(
"base64:" ), Qt::CaseInsensitive );
81 if ( ( !isBase64 && source == mFileLineEdit->text() ) || ( isBase64 && source == mBase64 ) )
89 mFileLineEdit->setText( source );
93 void QgsSvgSourceLineEdit::selectFile()
96 QString file = QFileDialog::getOpenFileName(
nullptr,
97 tr(
"Select SVG file" ),
99 tr(
"SVG files" ) +
" (*.svg)" );
100 QFileInfo fi( file );
101 if ( file.isEmpty() || !fi.exists() || file ==
source() )
106 mFileLineEdit->setText( file );
107 s.
setValue( settingsKey(), fi.absolutePath() );
111 void QgsSvgSourceLineEdit::selectUrl()
114 const QString path = QInputDialog::getText(
this, tr(
"SVG From URL" ), tr(
"Enter SVG URL" ), QLineEdit::Normal, mFileLineEdit->text(), &ok );
115 if ( ok && path !=
source() )
118 mFileLineEdit->setText( path );
123 void QgsSvgSourceLineEdit::embedFile()
126 QString file = QFileDialog::getOpenFileName(
nullptr,
127 tr(
"Embed SVG File" ),
129 tr(
"SVG files" ) +
" (*.svg)" );
130 QFileInfo fi( file );
131 if ( file.isEmpty() || !fi.exists() )
136 s.
setValue( settingsKey(), fi.absolutePath() );
139 QFile fileSource( file );
140 if ( !fileSource.open( QIODevice::ReadOnly ) )
145 QByteArray blob = fileSource.readAll();
146 QByteArray encoded = blob.toBase64();
148 QString path( encoded );
149 path.prepend( QLatin1String(
"base64:" ) );
154 mFileLineEdit->setText( path );
158 void QgsSvgSourceLineEdit::extractFile()
161 QString file = QFileDialog::getSaveFileName(
nullptr,
162 tr(
"Extract SVG File" ),
164 tr(
"SVG files" ) +
" (*.svg)" );
165 if ( file.isEmpty() )
170 QFileInfo fi( file );
171 s.
setValue( settingsKey(), fi.absolutePath() );
174 QString path = mFileLineEdit->text().trimmed();
175 if ( path.startsWith( QLatin1String(
"base64:" ), Qt::CaseInsensitive ) )
177 QByteArray base64 = mBase64.mid( 7 ).toLocal8Bit();
178 QByteArray decoded = QByteArray::fromBase64( base64, QByteArray::OmitTrailingEquals );
180 QFile fileOut( file );
181 fileOut.open( QIODevice::WriteOnly );
182 fileOut.write( decoded );
187 void QgsSvgSourceLineEdit::mFileLineEdit_textEdited(
const QString &text )
189 if ( !QFileInfo::exists( text ) )
196 void QgsSvgSourceLineEdit::mFileLineEdit_editingFinished()
198 if ( !QFileInfo::exists( mFileLineEdit->text() ) )
200 QUrl url( mFileLineEdit->text() );
201 if ( !url.isValid() )
210 QString QgsSvgSourceLineEdit::defaultPath()
const 212 if ( QFileInfo::exists(
source() ) )
218 QString QgsSvgSourceLineEdit::settingsKey()
const 220 return mLastPathKey.isEmpty() ? QStringLiteral(
"/UI/lastSVGDir" ) : mLastPathKey;
This class is a composition of two QSettings instances:
QString source() const
Returns the current SVG source.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
QgsSvgSourceLineEdit(QWidget *parent=nullptr)
Constructor for QgsSvgSourceLineEdit, with the specified parent widget.
void setSource(const QString &source)
Sets a new source to show in the widget.
void sourceChanged(const QString &source)
Emitted whenever the SVG source is changed in the widget.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
void setLastPathSettingsKey(const QString &key)
Sets a specific settings key to use when storing the last used path for the SVG source.