23 #include <QHBoxLayout>    24 #include <QFileDialog>    26 #include <QToolButton>    29 #include <QMessageBox>    33   , mMessageBar( messageBar )
    40   return mValue.isEmpty() || mValue.isNull() ? QVariant( QVariant::ByteArray ) : mValue;
    52     mSetAction->setEnabled( enabled );
    54     mClearAction->setEnabled( enabled && !mValue.isEmpty() );
    59   QWidget *container = 
new QWidget( parent );
    60   QHBoxLayout *layout = 
new QHBoxLayout();
    61   container->setLayout( layout );
    62   layout->setMargin( 0 );
    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()
   138   QString file = QFileDialog::getSaveFileName( 
nullptr,
   139                  tr( 
"Save Contents to File" ),
   141                  tr( 
"All files" ) + 
" (*.*)" );
   142   if ( file.isEmpty() )
   147   QFileInfo fi( file );
   148   s.
setValue( QStringLiteral( 
"/UI/lastBinaryDir" ), fi.absolutePath() );
   150   QFile fileOut( file );
   151   fileOut.open( QIODevice::WriteOnly );
   152   fileOut.write( mValue );
   156     mMessageBar->
pushSuccess( QString(), tr( 
"Saved content to <a href=\"%1\">%2</a>" ).arg(
   157                                 QUrl::fromLocalFile( file ).toString(), QDir::toNativeSeparators( file ) ) );
   160 void QgsBinaryWidgetWrapper::setContent()
   163   QString file = QFileDialog::getOpenFileName( 
nullptr,
   166                  tr( 
"All files" ) + 
" (*.*)" );
   167   QFileInfo fi( file );
   168   if ( file.isEmpty() || !fi.exists() )
   173   s.
setValue( QStringLiteral( 
"/UI/lastBinaryDir" ), fi.absolutePath() );
   175   QFile fileSource( file );
   176   if ( !fileSource.open( QIODevice::ReadOnly ) )
   181   updateValues( fileSource.readAll() );
   185 void QgsBinaryWidgetWrapper::clear()
   187   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 )
   190   updateValues( QByteArray() );
   194 QString QgsBinaryWidgetWrapper::defaultPath()
   196   return QgsSettings().
value( QStringLiteral( 
"/UI/lastBinaryDir" ), QDir::homePath() ).toString();
 
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. 
 
A bar for displaying non-blocking messages to the user. 
 
void pushSuccess(const QString &title, const QString &message)
Pushes a success message with default timeout to the message bar. 
 
static QString representFileSize(qint64 bytes)
Returns the human size from bytes. 
 
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS. 
 
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value. 
 
Represents a vector layer which manages a vector based data sets.