17#include "moc_qgsbinarywidgetwrapper.cpp"
36 , mMessageBar( messageBar )
55 mSetAction->setEnabled( enabled );
57 mClearAction->setEnabled( enabled && !mValue.isEmpty() );
62 QWidget *container =
new QWidget( parent );
63 QHBoxLayout *layout =
new QHBoxLayout();
64 container->setLayout( layout );
65 layout->setContentsMargins( 0, 0, 0, 0 );
67 QLabel *label =
new QLabel();
68 layout->addWidget( label, 1 );
70 QToolButton *button =
new QToolButton();
71 button->setText( QChar( 0x2026 ) );
72 layout->addWidget( button, 0 );
74 container->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Minimum );
80 mLabel = editor->findChild<QLabel *>();
81 mButton = editor->findChild<QToolButton *>();
85 QFont f = mLabel->font();
92 mButton->setPopupMode( QToolButton::InstantPopup );
94 mSetAction =
new QAction( tr(
"Embed File…" ), mButton );
95 connect( mSetAction, &QAction::triggered,
this, &QgsBinaryWidgetWrapper::setContent );
96 mClearAction =
new QAction( tr(
"Clear Contents…" ), mButton );
97 connect( mClearAction, &QAction::triggered,
this, &QgsBinaryWidgetWrapper::clear );
98 mSaveAction =
new QAction( tr(
"Save Contents to File…" ), mButton );
99 connect( mSaveAction, &QAction::triggered,
this, &QgsBinaryWidgetWrapper::saveContent );
100 QMenu *menu =
new QMenu( mButton );
101 menu->addAction( mSetAction );
102 menu->addAction( mClearAction );
103 menu->addSeparator();
104 menu->addAction( mSaveAction );
105 mButton->setMenu( menu );
111 return mLabel && mButton;
114void QgsBinaryWidgetWrapper::updateValues(
const QVariant &value,
const QVariantList & )
117 if ( mValue.length() == 0 )
118 mValue = QByteArray();
122 if ( !mValue.isEmpty() )
132 mSaveAction->setEnabled( !mValue.isEmpty() );
134 mClearAction->setEnabled( !mValue.isEmpty() );
137void QgsBinaryWidgetWrapper::saveContent()
145 file = QFileDialog::getSaveFileName(
nullptr,
146 tr(
"Save Contents to File" ),
148 tr(
"All files" ) +
" (*.*)" );
150 if ( file.isEmpty() )
155 const QFileInfo fi( file );
156 s.
setValue( QStringLiteral(
"/UI/lastBinaryDir" ), fi.absolutePath() );
158 QFile fileOut( file );
159 fileOut.open( QIODevice::WriteOnly );
160 fileOut.write( mValue );
164 mMessageBar->
pushSuccess( QString(), tr(
"Saved content to <a href=\"%1\">%2</a>" ).arg(
165 QUrl::fromLocalFile( file ).toString(), QDir::toNativeSeparators( file ) ) );
168void QgsBinaryWidgetWrapper::setContent()
176 file = QFileDialog::getOpenFileName(
nullptr,
179 tr(
"All files" ) +
" (*.*)" );
182 const QFileInfo fi( file );
183 if ( file.isEmpty() || !fi.exists() )
188 s.
setValue( QStringLiteral(
"/UI/lastBinaryDir" ), fi.absolutePath() );
190 QFile fileSource( file );
191 if ( !fileSource.open( QIODevice::ReadOnly ) )
196 updateValues( fileSource.readAll() );
200void QgsBinaryWidgetWrapper::clear()
204 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 )
208 updateValues( QByteArray() );
212QString QgsBinaryWidgetWrapper::defaultPath()
214 return QgsSettings().
value( QStringLiteral(
"/UI/lastBinaryDir" ), QDir::homePath() ).toString();
static QString nullRepresentation()
Returns the string used to represent the value NULL throughout QGIS.
static QString representFileSize(qint64 bytes)
Returns the human size from bytes.
Trick to keep a widget focused and avoid QT crashes.
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.
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.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
static QVariant createNullVariant(QMetaType::Type metaType)
Helper method to properly create a null QVariant from a metaType Returns the created QVariant.
Represents a vector layer which manages a vector based data sets.