35#include "moc_qgsbinarywidgetwrapper.cpp"
39 , mMessageBar( messageBar )
58 mSetAction->setEnabled( enabled );
60 mClearAction->setEnabled( enabled && !mValue.isEmpty() );
65 QWidget *container =
new QWidget( parent );
66 QHBoxLayout *layout =
new QHBoxLayout();
67 container->setLayout( layout );
68 layout->setContentsMargins( 0, 0, 0, 0 );
70 QLabel *label =
new QLabel();
71 layout->addWidget( label, 1 );
73 QToolButton *button =
new QToolButton();
74 button->setText( QChar( 0x2026 ) );
75 layout->addWidget( button, 0 );
77 container->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Minimum );
83 mLabel = editor->findChild<QLabel *>();
84 mButton = editor->findChild<QToolButton *>();
88 QFont f = mLabel->font();
95 mButton->setPopupMode( QToolButton::InstantPopup );
97 mSetAction =
new QAction( tr(
"Embed File…" ), mButton );
98 connect( mSetAction, &QAction::triggered,
this, &QgsBinaryWidgetWrapper::setContent );
99 mClearAction =
new QAction( tr(
"Clear Contents…" ), mButton );
100 connect( mClearAction, &QAction::triggered,
this, &QgsBinaryWidgetWrapper::clear );
101 mSaveAction =
new QAction( tr(
"Save Contents to File…" ), mButton );
102 connect( mSaveAction, &QAction::triggered,
this, &QgsBinaryWidgetWrapper::saveContent );
103 QMenu *menu =
new QMenu( mButton );
104 menu->addAction( mSetAction );
105 menu->addAction( mClearAction );
106 menu->addSeparator();
107 menu->addAction( mSaveAction );
108 mButton->setMenu( menu );
114 return mLabel && mButton;
117void QgsBinaryWidgetWrapper::updateValues(
const QVariant &value,
const QVariantList & )
120 if ( mValue.length() == 0 )
121 mValue = QByteArray();
125 if ( !mValue.isEmpty() )
135 mSaveAction->setEnabled( !mValue.isEmpty() );
137 mClearAction->setEnabled( !mValue.isEmpty() );
140void QgsBinaryWidgetWrapper::saveContent()
146 const QgsFocusKeeper focusKeeper;
148 file = QFileDialog::getSaveFileName(
nullptr, tr(
"Save Contents to File" ), defaultPath(), 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 if ( fileOut.open( QIODevice::WriteOnly ) )
161 fileOut.write( mValue );
165 mMessageBar->pushSuccess( QString(), tr(
"Saved content to <a href=\"%1\">%2</a>" ).arg( QUrl::fromLocalFile( file ).toString(), QDir::toNativeSeparators( file ) ) );
167 else if ( mMessageBar )
169 mMessageBar->pushMessage( QString(), tr(
"Error opening %1 for write" ).arg( QDir::toNativeSeparators( file ) ),
Qgis::MessageLevel::Critical );
173void QgsBinaryWidgetWrapper::setContent()
179 const QgsFocusKeeper focusKeeper;
181 file = QFileDialog::getOpenFileName(
nullptr, tr(
"Embed File" ), defaultPath(), tr(
"All files" ) +
" (*.*)" );
184 const QFileInfo fi( file );
185 if ( file.isEmpty() || !fi.exists() )
190 s.
setValue( QStringLiteral(
"/UI/lastBinaryDir" ), fi.absolutePath() );
192 QFile fileSource( file );
193 if ( !fileSource.open( QIODevice::ReadOnly ) )
198 updateValues( fileSource.readAll() );
202void QgsBinaryWidgetWrapper::clear()
205 const QgsFocusKeeper focusKeeper;
206 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 )
210 updateValues( QByteArray() );
214QString QgsBinaryWidgetWrapper::defaultPath()
216 return QgsSettings().value( QStringLiteral(
"/UI/lastBinaryDir" ), QDir::homePath() ).toString();
@ Critical
Critical/error message.
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.
A bar for displaying non-blocking messages to the user.
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 dataset.