QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
qgsbinarywidgetwrapper.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsbinarywidgetwrapper.cpp
3 -------------------------
4 Date : November 2018
5 Copyright : (C) 2018 Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17#include "moc_qgsbinarywidgetwrapper.cpp"
18#include "qgsvectorlayer.h"
20#include "qgsfileutils.h"
21#include "qgsfocuskeeper.h"
22#include "qgssettings.h"
23#include "qgsmessagebar.h"
24#include "qgsapplication.h"
25#include <QHBoxLayout>
26#include <QFileDialog>
27#include <QLabel>
28#include <QToolButton>
29#include <QAction>
30#include <QMenu>
31#include <QMessageBox>
32#include <QUrl>
33
34QgsBinaryWidgetWrapper::QgsBinaryWidgetWrapper( QgsVectorLayer *layer, int fieldIdx, QWidget *editor, QWidget *parent, QgsMessageBar *messageBar )
35 : QgsEditorWidgetWrapper( layer, fieldIdx, editor, parent )
36 , mMessageBar( messageBar )
37{
38}
39
40
42{
43 return mValue.isEmpty() || mValue.isNull() ? QgsVariantUtils::createNullVariant( QMetaType::Type::QByteArray ) : mValue;
44}
45
47{
48 if ( mLabel )
49 mLabel->clear();
50}
51
53{
54 if ( mSetAction )
55 mSetAction->setEnabled( enabled );
56 if ( mClearAction )
57 mClearAction->setEnabled( enabled && !mValue.isEmpty() );
58}
59
60QWidget *QgsBinaryWidgetWrapper::createWidget( QWidget *parent )
61{
62 QWidget *container = new QWidget( parent );
63 QHBoxLayout *layout = new QHBoxLayout();
64 container->setLayout( layout );
65 layout->setContentsMargins( 0, 0, 0, 0 );
66
67 QLabel *label = new QLabel();
68 layout->addWidget( label, 1 );
69
70 QToolButton *button = new QToolButton();
71 button->setText( QChar( 0x2026 ) );
72 layout->addWidget( button, 0 );
73
74 container->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Minimum );
75 return container;
76}
77
79{
80 mLabel = editor->findChild<QLabel *>();
81 mButton = editor->findChild<QToolButton *>();
82
83 if ( mLabel )
84 {
85 QFont f = mLabel->font();
86 f.setItalic( true );
87 mLabel->setFont( f );
88 }
89
90 if ( mButton )
91 {
92 mButton->setPopupMode( QToolButton::InstantPopup );
93
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 );
106 }
107}
108
110{
111 return mLabel && mButton;
112}
113
114void QgsBinaryWidgetWrapper::updateValues( const QVariant &value, const QVariantList & )
115{
116 mValue = value.isValid() && !QgsVariantUtils::isNull( value ) && value.canConvert< QByteArray >() ? value.toByteArray() : QByteArray();
117 if ( mValue.length() == 0 )
118 mValue = QByteArray();
119
120 if ( mLabel )
121 {
122 if ( !mValue.isEmpty() )
123 {
124 mLabel->setText( tr( "Binary (%1)" ).arg( QgsFileUtils::representFileSize( mValue.size() ) ) );
125 }
126 else
127 {
128 mLabel->setText( QgsApplication::nullRepresentation() );
129 }
130 }
131 if ( mSaveAction )
132 mSaveAction->setEnabled( !mValue.isEmpty() );
133 if ( mClearAction )
134 mClearAction->setEnabled( !mValue.isEmpty() );
135}
136
137void QgsBinaryWidgetWrapper::saveContent()
138{
139 QgsSettings s;
140
141 QString file;
142 {
143 const QgsFocusKeeper focusKeeper;
144
145 file = QFileDialog::getSaveFileName( nullptr,
146 tr( "Save Contents to File" ),
147 defaultPath(),
148 tr( "All files" ) + " (*.*)" );
149 }
150 if ( file.isEmpty() )
151 {
152 return;
153 }
154
155 const QFileInfo fi( file );
156 s.setValue( QStringLiteral( "/UI/lastBinaryDir" ), fi.absolutePath() );
157
158 QFile fileOut( file );
159 fileOut.open( QIODevice::WriteOnly );
160 fileOut.write( mValue );
161 fileOut.close();
162
163 if ( mMessageBar )
164 mMessageBar->pushSuccess( QString(), tr( "Saved content to <a href=\"%1\">%2</a>" ).arg(
165 QUrl::fromLocalFile( file ).toString(), QDir::toNativeSeparators( file ) ) );
166}
167
168void QgsBinaryWidgetWrapper::setContent()
169{
170 QgsSettings s;
171
172 QString file;
173 {
174 const QgsFocusKeeper focusKeeper;
175
176 file = QFileDialog::getOpenFileName( nullptr,
177 tr( "Embed File" ),
178 defaultPath(),
179 tr( "All files" ) + " (*.*)" );
180 }
181
182 const QFileInfo fi( file );
183 if ( file.isEmpty() || !fi.exists() )
184 {
185 return;
186 }
187
188 s.setValue( QStringLiteral( "/UI/lastBinaryDir" ), fi.absolutePath() );
189
190 QFile fileSource( file );
191 if ( !fileSource.open( QIODevice::ReadOnly ) )
192 {
193 return;
194 }
195
196 updateValues( fileSource.readAll() );
198}
199
200void QgsBinaryWidgetWrapper::clear()
201{
202 {
203 const QgsFocusKeeper focusKeeper;
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 )
205 return;
206 }
207
208 updateValues( QByteArray() );
210}
211
212QString QgsBinaryWidgetWrapper::defaultPath()
213{
214 return QgsSettings().value( QStringLiteral( "/UI/lastBinaryDir" ), QDir::homePath() ).toString();
215}
216
static QString nullRepresentation()
Returns the string used to represent the value NULL throughout QGIS.
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
void setEnabled(bool enabled) override
Is used to enable or disable the edit functionality of the managed widget.
void showIndeterminateState() override
Sets the widget to display in an indeterminate "mixed value" state.
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
QVariant value() const override
Will be used to access the widget's value.
QgsBinaryWidgetWrapper(QgsVectorLayer *layer, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr, QgsMessageBar *messageBar=nullptr)
Constructor for QgsBinaryWidgetWrapper.
bool valid() const override
Returns true if the widget has been properly initialized.
Manages an editor widget Widget and wrapper share the same parent.
void emitValueChanged()
Will call the value() method to determine the emitted value.
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:
Definition qgssettings.h:64
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.