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, tr(
"Save Contents to File" ), defaultPath(), tr(
"All files" ) +
" (*.*)" );
147 if ( file.isEmpty() )
152 const QFileInfo fi( file );
153 s.
setValue( QStringLiteral(
"/UI/lastBinaryDir" ), fi.absolutePath() );
155 QFile fileOut( file );
156 fileOut.open( QIODevice::WriteOnly );
157 fileOut.write( mValue );
161 mMessageBar->
pushSuccess( QString(), tr(
"Saved content to <a href=\"%1\">%2</a>" ).arg( QUrl::fromLocalFile( file ).toString(), QDir::toNativeSeparators( file ) ) );
164void QgsBinaryWidgetWrapper::setContent()
172 file = QFileDialog::getOpenFileName(
nullptr, tr(
"Embed File" ), defaultPath(), tr(
"All files" ) +
" (*.*)" );
175 const QFileInfo fi( file );
176 if ( file.isEmpty() || !fi.exists() )
181 s.
setValue( QStringLiteral(
"/UI/lastBinaryDir" ), fi.absolutePath() );
183 QFile fileSource( file );
184 if ( !fileSource.open( QIODevice::ReadOnly ) )
189 updateValues( fileSource.readAll() );
193void QgsBinaryWidgetWrapper::clear()
197 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 )
201 updateValues( QByteArray() );
205QString QgsBinaryWidgetWrapper::defaultPath()
207 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.