QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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
18#include "qgsapplication.h"
19#include "qgsfileutils.h"
20#include "qgsfocuskeeper.h"
21#include "qgsmessagebar.h"
22#include "qgssettings.h"
24#include "qgsvectorlayer.h"
25
26#include <QAction>
27#include <QFileDialog>
28#include <QHBoxLayout>
29#include <QLabel>
30#include <QMenu>
31#include <QMessageBox>
32#include <QString>
33#include <QToolButton>
34#include <QUrl>
35
36#include "moc_qgsbinarywidgetwrapper.cpp"
37
38using namespace Qt::StringLiterals;
39
40QgsBinaryWidgetWrapper::QgsBinaryWidgetWrapper( QgsVectorLayer *layer, int fieldIdx, QWidget *editor, QWidget *parent, QgsMessageBar *messageBar )
41 : QgsEditorWidgetWrapper( layer, fieldIdx, editor, parent )
42 , mMessageBar( messageBar )
43{}
44
45
47{
48 return mValue.isEmpty() || mValue.isNull() ? QgsVariantUtils::createNullVariant( QMetaType::Type::QByteArray ) : mValue;
49}
50
52{
53 if ( mLabel )
54 mLabel->clear();
55}
56
58{
59 if ( mSetAction )
60 mSetAction->setEnabled( enabled );
61 if ( mClearAction )
62 mClearAction->setEnabled( enabled && !mValue.isEmpty() );
63}
64
65QWidget *QgsBinaryWidgetWrapper::createWidget( QWidget *parent )
66{
67 QWidget *container = new QWidget( parent );
68 QHBoxLayout *layout = new QHBoxLayout();
69 container->setLayout( layout );
70 layout->setContentsMargins( 0, 0, 0, 0 );
71
72 QLabel *label = new QLabel();
73 layout->addWidget( label, 1 );
74
75 QToolButton *button = new QToolButton();
76 button->setText( QChar( 0x2026 ) );
77 layout->addWidget( button, 0 );
78
79 container->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Minimum );
80 return container;
81}
82
84{
85 mLabel = editor->findChild<QLabel *>();
86 mButton = editor->findChild<QToolButton *>();
87
88 if ( mLabel )
89 {
90 QFont f = mLabel->font();
91 f.setItalic( true );
92 mLabel->setFont( f );
93 }
94
95 if ( mButton )
96 {
97 mButton->setPopupMode( QToolButton::InstantPopup );
98
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 );
111 }
112}
113
115{
116 return mLabel && mButton;
117}
118
119void QgsBinaryWidgetWrapper::updateValues( const QVariant &value, const QVariantList & )
120{
121 mValue = value.isValid() && !QgsVariantUtils::isNull( value ) && value.canConvert<QByteArray>() ? value.toByteArray() : QByteArray();
122 if ( mValue.length() == 0 )
123 mValue = QByteArray();
124
125 if ( mLabel )
126 {
127 if ( !mValue.isEmpty() )
128 {
129 mLabel->setText( tr( "Binary (%1)" ).arg( QgsFileUtils::representFileSize( mValue.size() ) ) );
130 }
131 else
132 {
133 mLabel->setText( QgsApplication::nullRepresentation() );
134 }
135 }
136 if ( mSaveAction )
137 mSaveAction->setEnabled( !mValue.isEmpty() );
138 if ( mClearAction )
139 mClearAction->setEnabled( !mValue.isEmpty() );
140}
141
142void QgsBinaryWidgetWrapper::saveContent()
143{
144 QgsSettings s;
145
146 QString file;
147 {
148 const QgsFocusKeeper focusKeeper;
149
150 file = QFileDialog::getSaveFileName( nullptr, tr( "Save Contents to File" ), defaultPath(), tr( "All files" ) + " (*.*)" );
151 }
152 if ( file.isEmpty() )
153 {
154 return;
155 }
156
157 const QFileInfo fi( file );
158 s.setValue( u"/UI/lastBinaryDir"_s, fi.absolutePath() );
159
160 QFile fileOut( file );
161 if ( fileOut.open( QIODevice::WriteOnly ) )
162 {
163 fileOut.write( mValue );
164 fileOut.close();
165
166 if ( mMessageBar )
167 mMessageBar->pushSuccess( QString(), tr( "Saved content to <a href=\"%1\">%2</a>" ).arg( QUrl::fromLocalFile( file ).toString(), QDir::toNativeSeparators( file ) ) );
168 }
169 else if ( mMessageBar )
170 {
171 mMessageBar->pushMessage( QString(), tr( "Error opening %1 for write" ).arg( QDir::toNativeSeparators( file ) ), Qgis::MessageLevel::Critical );
172 }
173}
174
175void QgsBinaryWidgetWrapper::setContent()
176{
177 QgsSettings s;
178
179 QString file;
180 {
181 const QgsFocusKeeper focusKeeper;
182
183 file = QFileDialog::getOpenFileName( nullptr, tr( "Embed File" ), defaultPath(), tr( "All files" ) + " (*.*)" );
184 }
185
186 const QFileInfo fi( file );
187 if ( file.isEmpty() || !fi.exists() )
188 {
189 return;
190 }
191
192 s.setValue( u"/UI/lastBinaryDir"_s, fi.absolutePath() );
193
194 QFile fileSource( file );
195 if ( !fileSource.open( QIODevice::ReadOnly ) )
196 {
197 return;
198 }
199
200 updateValues( fileSource.readAll() );
202}
203
204void QgsBinaryWidgetWrapper::clear()
205{
206 {
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 )
209 return;
210 }
211
212 updateValues( QByteArray() );
214}
215
216QString QgsBinaryWidgetWrapper::defaultPath()
217{
218 return QgsSettings().value( u"/UI/lastBinaryDir"_s, QDir::homePath() ).toString();
219}
@ Critical
Critical/error message.
Definition qgis.h:163
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.
int fieldIdx() const
Access the field index.
QgsEditorWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
Create a new widget wrapper.
void emitValueChanged()
Will call the value() method to determine the emitted value.
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.
QgsVectorLayer * layer() const
Returns the vector layer associated with the widget.