23 #include <QToolButton>
24 #include <QHBoxLayout>
25 #include <QFileDialog>
26 #include <QInputDialog>
35 QHBoxLayout *layout =
new QHBoxLayout(
this );
36 layout->setContentsMargins( 0, 0, 0, 0 );
39 mFileToolButton =
new QToolButton(
this );
40 mFileToolButton->setText( QString( QChar( 0x2026 ) ) );
42 layout->addWidget( mFileLineEdit, 1 );
43 layout->addWidget( mFileToolButton );
44 layout->addWidget( mPropertyOverrideButton );
47 QMenu *sourceMenu =
new QMenu( mFileToolButton );
49 QAction *selectFileAction =
new QAction( tr(
"Select File…" ), sourceMenu );
50 connect( selectFileAction, &QAction::triggered,
this, &QgsAbstractFileContentSourceLineEdit::selectFile );
51 sourceMenu->addAction( selectFileAction );
53 QAction *embedFileAction =
new QAction( tr(
"Embed File…" ), sourceMenu );
54 connect( embedFileAction, &QAction::triggered,
this, &QgsAbstractFileContentSourceLineEdit::embedFile );
55 sourceMenu->addAction( embedFileAction );
57 QAction *extractFileAction =
new QAction( tr(
"Extract Embedded File…" ), sourceMenu );
58 connect( extractFileAction, &QAction::triggered,
this, &QgsAbstractFileContentSourceLineEdit::extractFile );
59 sourceMenu->addAction( extractFileAction );
61 connect( sourceMenu, &QMenu::aboutToShow,
this, [
this, extractFileAction]
63 extractFileAction->setEnabled( mMode == ModeBase64 );
66 QAction *enterUrlAction =
new QAction( tr(
"From URL…" ), sourceMenu );
67 connect( enterUrlAction, &QAction::triggered,
this, &QgsAbstractFileContentSourceLineEdit::selectUrl );
68 sourceMenu->addAction( enterUrlAction );
70 mFileToolButton->setMenu( sourceMenu );
71 mFileToolButton->setPopupMode( QToolButton::MenuButtonPopup );
72 connect( mFileToolButton, &QToolButton::clicked,
this, &QgsAbstractFileContentSourceLineEdit::selectFile );
74 connect( mFileLineEdit, &QLineEdit::textEdited,
this, &QgsAbstractFileContentSourceLineEdit::mFileLineEdit_textEdited );
78 mFileLineEdit->setPlaceholderText( QString() );
83 mPropertyOverrideButton->setVisible( mPropertyOverrideButtonVisible );
92 return mFileLineEdit->text();
108 mPropertyOverrideButtonVisible = visible;
109 mPropertyOverrideButton->setVisible( visible );
114 const bool isBase64 =
source.startsWith( QLatin1String(
"base64:" ), Qt::CaseInsensitive );
116 if ( ( !isBase64 &&
source == mFileLineEdit->text() && mBase64.isEmpty() ) || ( isBase64 &&
source == mBase64 ) )
123 mFileLineEdit->clear();
124 mFileLineEdit->setPlaceholderText( tr(
"Embedded file" ) );
130 mFileLineEdit->setText(
source );
131 mFileLineEdit->setPlaceholderText( QString() );
137 void QgsAbstractFileContentSourceLineEdit::selectFile()
140 QString file = QFileDialog::getOpenFileName(
nullptr,
144 QFileInfo fi( file );
145 if ( file.isEmpty() || !fi.exists() || file ==
source() )
151 mFileLineEdit->setText( file );
152 mFileLineEdit->setPlaceholderText( QString() );
153 s.
setValue( settingsKey(), fi.absolutePath() );
157 void QgsAbstractFileContentSourceLineEdit::selectUrl()
160 const QString path = QInputDialog::getText(
this, fileFromUrlTitle(), fileFromUrlText(), QLineEdit::Normal, mFileLineEdit->text(), &ok );
161 if ( ok && path !=
source() )
165 mFileLineEdit->setText( path );
166 mFileLineEdit->setPlaceholderText( QString() );
171 void QgsAbstractFileContentSourceLineEdit::embedFile()
174 QString file = QFileDialog::getOpenFileName(
nullptr,
178 QFileInfo fi( file );
179 if ( file.isEmpty() || !fi.exists() )
184 s.
setValue( settingsKey(), fi.absolutePath() );
187 QFile fileSource( file );
188 if ( !fileSource.open( QIODevice::ReadOnly ) )
193 QByteArray blob = fileSource.readAll();
194 QByteArray encoded = blob.toBase64();
196 QString path( encoded );
197 path.prepend( QLatin1String(
"base64:" ) );
204 mFileLineEdit->clear();
205 mFileLineEdit->setPlaceholderText( tr(
"Embedded file" ) );
210 void QgsAbstractFileContentSourceLineEdit::extractFile()
213 QString file = QFileDialog::getSaveFileName(
nullptr,
217 if ( file.isEmpty() )
222 QFileInfo fi( file );
223 s.
setValue( settingsKey(), fi.absolutePath() );
226 QByteArray base64 = mBase64.mid( 7 ).toLocal8Bit();
227 QByteArray decoded = QByteArray::fromBase64( base64, QByteArray::OmitTrailingEquals );
229 QFile fileOut( file );
230 fileOut.open( QIODevice::WriteOnly );
231 fileOut.write( decoded );
237 tr(
"Successfully extracted file to <a href=\"%1\">%2</a>" ).arg( QUrl::fromLocalFile( file ).toString(), QDir::toNativeSeparators( file ) ),
242 void QgsAbstractFileContentSourceLineEdit::mFileLineEdit_textEdited(
const QString &text )
244 mFileLineEdit->setPlaceholderText( QString() );
247 if ( !text.isEmpty() && !QFileInfo::exists( text ) )
250 if ( !url.isValid() )
258 QString QgsAbstractFileContentSourceLineEdit::defaultPath()
const
260 if ( QFileInfo::exists(
source() ) )
266 QString QgsAbstractFileContentSourceLineEdit::settingsKey()
const
268 return mLastPathKey.isEmpty() ? defaultSettingsKey() : mLastPathKey;
277 QString QgsSvgSourceLineEdit::fileFilter()
const
279 return tr(
"SVG files" ) +
" (*.svg)";
282 QString QgsSvgSourceLineEdit::selectFileTitle()
const
284 return tr(
"Select SVG File" );
287 QString QgsSvgSourceLineEdit::fileFromUrlTitle()
const
289 return tr(
"SVG From URL" );
292 QString QgsSvgSourceLineEdit::fileFromUrlText()
const
294 return tr(
"Enter SVG URL" );
297 QString QgsSvgSourceLineEdit::embedFileTitle()
const
299 return tr(
"Embed SVG File" );
302 QString QgsSvgSourceLineEdit::extractFileTitle()
const
304 return tr(
"Extract SVG File" );
307 QString QgsSvgSourceLineEdit::defaultSettingsKey()
const
309 return QStringLiteral(
"/UI/lastSVGDir" );
319 QString QgsImageSourceLineEdit::fileFilter()
const
321 return tr(
"All files" ) +
" (*.*)";
324 QString QgsImageSourceLineEdit::selectFileTitle()
const
326 return tr(
"Select Image File" );
329 QString QgsImageSourceLineEdit::fileFromUrlTitle()
const
331 return tr(
"Image From URL" );
334 QString QgsImageSourceLineEdit::fileFromUrlText()
const
336 return tr(
"Enter image URL" );
339 QString QgsImageSourceLineEdit::embedFileTitle()
const
341 return tr(
"Embed Image File" );
344 QString QgsImageSourceLineEdit::extractFileTitle()
const
346 return tr(
"Extract Image File" );
349 QString QgsImageSourceLineEdit::defaultSettingsKey()
const
351 return QStringLiteral(
"/UI/lastImageDir" );