36#include "moc_qgsbinarywidgetwrapper.cpp"
38using namespace Qt::StringLiterals;
42 , mMessageBar( messageBar )
60 mSetAction->setEnabled( enabled );
62 mClearAction->setEnabled( enabled && !mValue.isEmpty() );
67 QWidget *container =
new QWidget( parent );
68 QHBoxLayout *layout =
new QHBoxLayout();
69 container->setLayout( layout );
70 layout->setContentsMargins( 0, 0, 0, 0 );
72 QLabel *label =
new QLabel();
73 layout->addWidget( label, 1 );
75 QToolButton *button =
new QToolButton();
76 button->setText( QChar( 0x2026 ) );
77 layout->addWidget( button, 0 );
79 container->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Minimum );
85 mLabel = editor->findChild<QLabel *>();
86 mButton = editor->findChild<QToolButton *>();
90 QFont f = mLabel->font();
97 mButton->setPopupMode( QToolButton::InstantPopup );
99 mSetAction =
new QAction( tr(
"Embed File…" ), mButton );
100 connect( mSetAction, &QAction::triggered,
this, &QgsBinaryWidgetWrapper::setContent );
101 mClearAction =
new QAction( tr(
"Clear Contents…" ), mButton );
102 connect( mClearAction, &QAction::triggered,
this, &QgsBinaryWidgetWrapper::clear );
103 mSaveAction =
new QAction( tr(
"Save Contents to File…" ), mButton );
104 connect( mSaveAction, &QAction::triggered,
this, &QgsBinaryWidgetWrapper::saveContent );
105 QMenu *menu =
new QMenu( mButton );
106 menu->addAction( mSetAction );
107 menu->addAction( mClearAction );
108 menu->addSeparator();
109 menu->addAction( mSaveAction );
110 mButton->setMenu( menu );
116 return mLabel && mButton;
119void QgsBinaryWidgetWrapper::updateValues(
const QVariant &value,
const QVariantList & )
122 if ( mValue.length() == 0 )
123 mValue = QByteArray();
127 if ( !mValue.isEmpty() )
137 mSaveAction->setEnabled( !mValue.isEmpty() );
139 mClearAction->setEnabled( !mValue.isEmpty() );
142void QgsBinaryWidgetWrapper::saveContent()
148 const QgsFocusKeeper focusKeeper;
150 file = QFileDialog::getSaveFileName(
nullptr, tr(
"Save Contents to File" ), defaultPath(), tr(
"All files" ) +
" (*.*)" );
152 if ( file.isEmpty() )
157 const QFileInfo fi( file );
158 s.
setValue( u
"/UI/lastBinaryDir"_s, fi.absolutePath() );
160 QFile fileOut( file );
161 if ( fileOut.open( QIODevice::WriteOnly ) )
163 fileOut.write( mValue );
167 mMessageBar->pushSuccess( QString(), tr(
"Saved content to <a href=\"%1\">%2</a>" ).arg( QUrl::fromLocalFile( file ).toString(), QDir::toNativeSeparators( file ) ) );
169 else if ( mMessageBar )
171 mMessageBar->pushMessage( QString(), tr(
"Error opening %1 for write" ).arg( QDir::toNativeSeparators( file ) ),
Qgis::MessageLevel::Critical );
175void QgsBinaryWidgetWrapper::setContent()
181 const QgsFocusKeeper focusKeeper;
183 file = QFileDialog::getOpenFileName(
nullptr, tr(
"Embed File" ), defaultPath(), tr(
"All files" ) +
" (*.*)" );
186 const QFileInfo fi( file );
187 if ( file.isEmpty() || !fi.exists() )
192 s.
setValue( u
"/UI/lastBinaryDir"_s, fi.absolutePath() );
194 QFile fileSource( file );
195 if ( !fileSource.open( QIODevice::ReadOnly ) )
200 updateValues( fileSource.readAll() );
204void QgsBinaryWidgetWrapper::clear()
207 const QgsFocusKeeper focusKeeper;
208 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 )
212 updateValues( QByteArray() );
216QString QgsBinaryWidgetWrapper::defaultPath()
218 return QgsSettings().value( u
"/UI/lastBinaryDir"_s, 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.