QGIS API Documentation 3.99.0-Master (d270888f95f)
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
46
48{
49 return mValue.isEmpty() || mValue.isNull() ? QgsVariantUtils::createNullVariant( QMetaType::Type::QByteArray ) : mValue;
50}
51
53{
54 if ( mLabel )
55 mLabel->clear();
56}
57
59{
60 if ( mSetAction )
61 mSetAction->setEnabled( enabled );
62 if ( mClearAction )
63 mClearAction->setEnabled( enabled && !mValue.isEmpty() );
64}
65
66QWidget *QgsBinaryWidgetWrapper::createWidget( QWidget *parent )
67{
68 QWidget *container = new QWidget( parent );
69 QHBoxLayout *layout = new QHBoxLayout();
70 container->setLayout( layout );
71 layout->setContentsMargins( 0, 0, 0, 0 );
72
73 QLabel *label = new QLabel();
74 layout->addWidget( label, 1 );
75
76 QToolButton *button = new QToolButton();
77 button->setText( QChar( 0x2026 ) );
78 layout->addWidget( button, 0 );
79
80 container->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Minimum );
81 return container;
82}
83
85{
86 mLabel = editor->findChild<QLabel *>();
87 mButton = editor->findChild<QToolButton *>();
88
89 if ( mLabel )
90 {
91 QFont f = mLabel->font();
92 f.setItalic( true );
93 mLabel->setFont( f );
94 }
95
96 if ( mButton )
97 {
98 mButton->setPopupMode( QToolButton::InstantPopup );
99
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 );
112 }
113}
114
116{
117 return mLabel && mButton;
118}
119
120void QgsBinaryWidgetWrapper::updateValues( const QVariant &value, const QVariantList & )
121{
122 mValue = value.isValid() && !QgsVariantUtils::isNull( value ) && value.canConvert<QByteArray>() ? value.toByteArray() : QByteArray();
123 if ( mValue.length() == 0 )
124 mValue = QByteArray();
125
126 if ( mLabel )
127 {
128 if ( !mValue.isEmpty() )
129 {
130 mLabel->setText( tr( "Binary (%1)" ).arg( QgsFileUtils::representFileSize( mValue.size() ) ) );
131 }
132 else
133 {
134 mLabel->setText( QgsApplication::nullRepresentation() );
135 }
136 }
137 if ( mSaveAction )
138 mSaveAction->setEnabled( !mValue.isEmpty() );
139 if ( mClearAction )
140 mClearAction->setEnabled( !mValue.isEmpty() );
141}
142
143void QgsBinaryWidgetWrapper::saveContent()
144{
145 QgsSettings s;
146
147 QString file;
148 {
149 const QgsFocusKeeper focusKeeper;
150
151 file = QFileDialog::getSaveFileName( nullptr, tr( "Save Contents to File" ), defaultPath(), tr( "All files" ) + " (*.*)" );
152 }
153 if ( file.isEmpty() )
154 {
155 return;
156 }
157
158 const QFileInfo fi( file );
159 s.setValue( u"/UI/lastBinaryDir"_s, fi.absolutePath() );
160
161 QFile fileOut( file );
162 if ( fileOut.open( QIODevice::WriteOnly ) )
163 {
164 fileOut.write( mValue );
165 fileOut.close();
166
167 if ( mMessageBar )
168 mMessageBar->pushSuccess( QString(), tr( "Saved content to <a href=\"%1\">%2</a>" ).arg( QUrl::fromLocalFile( file ).toString(), QDir::toNativeSeparators( file ) ) );
169 }
170 else if ( mMessageBar )
171 {
172 mMessageBar->pushMessage( QString(), tr( "Error opening %1 for write" ).arg( QDir::toNativeSeparators( file ) ), Qgis::MessageLevel::Critical );
173 }
174}
175
176void QgsBinaryWidgetWrapper::setContent()
177{
178 QgsSettings s;
179
180 QString file;
181 {
182 const QgsFocusKeeper focusKeeper;
183
184 file = QFileDialog::getOpenFileName( nullptr, tr( "Embed File" ), defaultPath(), tr( "All files" ) + " (*.*)" );
185 }
186
187 const QFileInfo fi( file );
188 if ( file.isEmpty() || !fi.exists() )
189 {
190 return;
191 }
192
193 s.setValue( u"/UI/lastBinaryDir"_s, fi.absolutePath() );
194
195 QFile fileSource( file );
196 if ( !fileSource.open( QIODevice::ReadOnly ) )
197 {
198 return;
199 }
200
201 updateValues( fileSource.readAll() );
203}
204
205void QgsBinaryWidgetWrapper::clear()
206{
207 {
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 )
210 return;
211 }
212
213 updateValues( QByteArray() );
215}
216
217QString QgsBinaryWidgetWrapper::defaultPath()
218{
219 return QgsSettings().value( u"/UI/lastBinaryDir"_s, QDir::homePath() ).toString();
220}
@ Critical
Critical/error message.
Definition qgis.h:162
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.