24 #include <QHBoxLayout>
25 #include <QFileDialog>
27 #include <QToolButton>
30 #include <QMessageBox>
34 , mMessageBar( messageBar )
41 return mValue.isEmpty() || mValue.isNull() ? QVariant( QVariant::ByteArray ) : mValue;
53 mSetAction->setEnabled( enabled );
55 mClearAction->setEnabled( enabled && !mValue.isEmpty() );
60 QWidget *container =
new QWidget( parent );
61 QHBoxLayout *layout =
new QHBoxLayout();
62 container->setLayout( layout );
63 layout->setMargin( 0 );
64 layout->setContentsMargins( 0, 0, 0, 0 );
66 QLabel *label =
new QLabel();
67 layout->addWidget( label, 1 );
69 QToolButton *button =
new QToolButton();
70 button->setText( QChar( 0x2026 ) );
71 layout->addWidget( button, 0 );
73 container->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Minimum );
79 mLabel = editor->findChild<QLabel *>();
80 mButton = editor->findChild<QToolButton *>();
84 QFont f = mLabel->font();
91 mButton->setPopupMode( QToolButton::InstantPopup );
93 mSetAction =
new QAction( tr(
"Embed File…" ), mButton );
94 connect( mSetAction, &QAction::triggered,
this, &QgsBinaryWidgetWrapper::setContent );
95 mClearAction =
new QAction( tr(
"Clear Contents…" ), mButton );
96 connect( mClearAction, &QAction::triggered,
this, &QgsBinaryWidgetWrapper::clear );
97 mSaveAction =
new QAction( tr(
"Save Contents to File…" ), mButton );
98 connect( mSaveAction, &QAction::triggered,
this, &QgsBinaryWidgetWrapper::saveContent );
99 QMenu *menu =
new QMenu( mButton );
100 menu->addAction( mSetAction );
101 menu->addAction( mClearAction );
102 menu->addSeparator();
103 menu->addAction( mSaveAction );
104 mButton->setMenu( menu );
110 return mLabel && mButton;
113 void QgsBinaryWidgetWrapper::updateValues(
const QVariant &value,
const QVariantList & )
115 mValue =
value.isValid() && !
value.isNull() &&
value.canConvert< QByteArray >() ?
value.toByteArray() : QByteArray();
116 if ( mValue.length() == 0 )
117 mValue = QByteArray();
121 if ( !mValue.isEmpty() )
131 mSaveAction->setEnabled( !mValue.isEmpty() );
133 mClearAction->setEnabled( !mValue.isEmpty() );
136 void QgsBinaryWidgetWrapper::saveContent()
144 file = QFileDialog::getSaveFileName(
nullptr,
145 tr(
"Save Contents to File" ),
147 tr(
"All files" ) +
" (*.*)" );
149 if ( file.isEmpty() )
154 QFileInfo fi( file );
155 s.
setValue( QStringLiteral(
"/UI/lastBinaryDir" ), fi.absolutePath() );
157 QFile fileOut( file );
158 fileOut.open( QIODevice::WriteOnly );
159 fileOut.write( mValue );
163 mMessageBar->
pushSuccess( QString(), tr(
"Saved content to <a href=\"%1\">%2</a>" ).arg(
164 QUrl::fromLocalFile( file ).toString(), QDir::toNativeSeparators( file ) ) );
167 void QgsBinaryWidgetWrapper::setContent()
175 file = QFileDialog::getOpenFileName(
nullptr,
178 tr(
"All files" ) +
" (*.*)" );
181 QFileInfo fi( file );
182 if ( file.isEmpty() || !fi.exists() )
187 s.
setValue( QStringLiteral(
"/UI/lastBinaryDir" ), fi.absolutePath() );
189 QFile fileSource( file );
190 if ( !fileSource.open( QIODevice::ReadOnly ) )
195 updateValues( fileSource.readAll() );
199 void QgsBinaryWidgetWrapper::clear()
203 if ( QMessageBox::question(
nullptr, tr(
"Clear Contents" ), tr(
"Are you sure you want the clear this field's content?" ), QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
207 updateValues( QByteArray() );
211 QString QgsBinaryWidgetWrapper::defaultPath()
213 return QgsSettings().
value( QStringLiteral(
"/UI/lastBinaryDir" ), QDir::homePath() ).toString();