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->setContentsMargins( 0, 0, 0, 0 );
65 QLabel *label =
new QLabel();
66 layout->addWidget( label, 1 );
68 QToolButton *button =
new QToolButton();
69 button->setText( QChar( 0x2026 ) );
70 layout->addWidget( button, 0 );
72 container->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Minimum );
78 mLabel = editor->findChild<QLabel *>();
79 mButton = editor->findChild<QToolButton *>();
83 QFont f = mLabel->font();
90 mButton->setPopupMode( QToolButton::InstantPopup );
92 mSetAction =
new QAction( tr(
"Embed File…" ), mButton );
93 connect( mSetAction, &QAction::triggered,
this, &QgsBinaryWidgetWrapper::setContent );
94 mClearAction =
new QAction( tr(
"Clear Contents…" ), mButton );
95 connect( mClearAction, &QAction::triggered,
this, &QgsBinaryWidgetWrapper::clear );
96 mSaveAction =
new QAction( tr(
"Save Contents to File…" ), mButton );
97 connect( mSaveAction, &QAction::triggered,
this, &QgsBinaryWidgetWrapper::saveContent );
98 QMenu *menu =
new QMenu( mButton );
99 menu->addAction( mSetAction );
100 menu->addAction( mClearAction );
101 menu->addSeparator();
102 menu->addAction( mSaveAction );
103 mButton->setMenu( menu );
109 return mLabel && mButton;
112 void QgsBinaryWidgetWrapper::updateValues(
const QVariant &value,
const QVariantList & )
114 mValue =
value.isValid() && !
value.isNull() &&
value.canConvert< QByteArray >() ?
value.toByteArray() : QByteArray();
115 if ( mValue.length() == 0 )
116 mValue = QByteArray();
120 if ( !mValue.isEmpty() )
130 mSaveAction->setEnabled( !mValue.isEmpty() );
132 mClearAction->setEnabled( !mValue.isEmpty() );
135 void QgsBinaryWidgetWrapper::saveContent()
143 file = QFileDialog::getSaveFileName(
nullptr,
144 tr(
"Save Contents to File" ),
146 tr(
"All files" ) +
" (*.*)" );
148 if ( file.isEmpty() )
153 QFileInfo fi( file );
154 s.
setValue( QStringLiteral(
"/UI/lastBinaryDir" ), fi.absolutePath() );
156 QFile fileOut( file );
157 fileOut.open( QIODevice::WriteOnly );
158 fileOut.write( mValue );
162 mMessageBar->
pushSuccess( QString(), tr(
"Saved content to <a href=\"%1\">%2</a>" ).arg(
163 QUrl::fromLocalFile( file ).toString(), QDir::toNativeSeparators( file ) ) );
166 void QgsBinaryWidgetWrapper::setContent()
174 file = QFileDialog::getOpenFileName(
nullptr,
177 tr(
"All files" ) +
" (*.*)" );
180 QFileInfo fi( file );
181 if ( file.isEmpty() || !fi.exists() )
186 s.
setValue( QStringLiteral(
"/UI/lastBinaryDir" ), fi.absolutePath() );
188 QFile fileSource( file );
189 if ( !fileSource.open( QIODevice::ReadOnly ) )
194 updateValues( fileSource.readAll() );
198 void QgsBinaryWidgetWrapper::clear()
202 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 )
206 updateValues( QByteArray() );
210 QString QgsBinaryWidgetWrapper::defaultPath()
212 return QgsSettings().
value( QStringLiteral(
"/UI/lastBinaryDir" ), QDir::homePath() ).toString();