36#include "moc_qgsbinarywidgetwrapper.cpp"
38using namespace Qt::StringLiterals;
42 , mMessageBar( messageBar )
61 mSetAction->setEnabled( enabled );
63 mClearAction->setEnabled( enabled && !mValue.isEmpty() );
68 QWidget *container =
new QWidget( parent );
69 QHBoxLayout *layout =
new QHBoxLayout();
70 container->setLayout( layout );
71 layout->setContentsMargins( 0, 0, 0, 0 );
73 QLabel *label =
new QLabel();
74 layout->addWidget( label, 1 );
76 QToolButton *button =
new QToolButton();
77 button->setText( QChar( 0x2026 ) );
78 layout->addWidget( button, 0 );
80 container->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Minimum );
86 mLabel = editor->findChild<QLabel *>();
87 mButton = editor->findChild<QToolButton *>();
91 QFont f = mLabel->font();
98 mButton->setPopupMode( QToolButton::InstantPopup );
100 mSetAction =
new QAction( tr(
"Embed File…" ), mButton );
101 connect( mSetAction, &QAction::triggered,
this, &QgsBinaryWidgetWrapper::setContent );
102 mClearAction =
new QAction( tr(
"Clear Contents…" ), mButton );
103 connect( mClearAction, &QAction::triggered,
this, &QgsBinaryWidgetWrapper::clear );
104 mSaveAction =
new QAction( tr(
"Save Contents to File…" ), mButton );
105 connect( mSaveAction, &QAction::triggered,
this, &QgsBinaryWidgetWrapper::saveContent );
106 QMenu *menu =
new QMenu( mButton );
107 menu->addAction( mSetAction );
108 menu->addAction( mClearAction );
109 menu->addSeparator();
110 menu->addAction( mSaveAction );
111 mButton->setMenu( menu );
117 return mLabel && mButton;
120void QgsBinaryWidgetWrapper::updateValues(
const QVariant &value,
const QVariantList & )
123 if ( mValue.length() == 0 )
124 mValue = QByteArray();
128 if ( !mValue.isEmpty() )
138 mSaveAction->setEnabled( !mValue.isEmpty() );
140 mClearAction->setEnabled( !mValue.isEmpty() );
143void QgsBinaryWidgetWrapper::saveContent()
149 const QgsFocusKeeper focusKeeper;
151 file = QFileDialog::getSaveFileName(
nullptr, tr(
"Save Contents to File" ), defaultPath(), tr(
"All files" ) +
" (*.*)" );
153 if ( file.isEmpty() )
158 const QFileInfo fi( file );
159 s.
setValue( u
"/UI/lastBinaryDir"_s, fi.absolutePath() );
161 QFile fileOut( file );
162 if ( fileOut.open( QIODevice::WriteOnly ) )
164 fileOut.write( mValue );
168 mMessageBar->pushSuccess( QString(), tr(
"Saved content to <a href=\"%1\">%2</a>" ).arg( QUrl::fromLocalFile( file ).toString(), QDir::toNativeSeparators( file ) ) );
170 else if ( mMessageBar )
172 mMessageBar->pushMessage( QString(), tr(
"Error opening %1 for write" ).arg( QDir::toNativeSeparators( file ) ),
Qgis::MessageLevel::Critical );
176void QgsBinaryWidgetWrapper::setContent()
182 const QgsFocusKeeper focusKeeper;
184 file = QFileDialog::getOpenFileName(
nullptr, tr(
"Embed File" ), defaultPath(), tr(
"All files" ) +
" (*.*)" );
187 const QFileInfo fi( file );
188 if ( file.isEmpty() || !fi.exists() )
193 s.
setValue( u
"/UI/lastBinaryDir"_s, fi.absolutePath() );
195 QFile fileSource( file );
196 if ( !fileSource.open( QIODevice::ReadOnly ) )
201 updateValues( fileSource.readAll() );
205void QgsBinaryWidgetWrapper::clear()
208 const QgsFocusKeeper focusKeeper;
209 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 )
213 updateValues( QByteArray() );
217QString QgsBinaryWidgetWrapper::defaultPath()
219 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.