QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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 <QToolButton>
33#include <QUrl>
34
35#include "moc_qgsbinarywidgetwrapper.cpp"
36
37QgsBinaryWidgetWrapper::QgsBinaryWidgetWrapper( QgsVectorLayer *layer, int fieldIdx, QWidget *editor, QWidget *parent, QgsMessageBar *messageBar )
38 : QgsEditorWidgetWrapper( layer, fieldIdx, editor, parent )
39 , mMessageBar( messageBar )
40{
41}
42
43
45{
46 return mValue.isEmpty() || mValue.isNull() ? QgsVariantUtils::createNullVariant( QMetaType::Type::QByteArray ) : mValue;
47}
48
50{
51 if ( mLabel )
52 mLabel->clear();
53}
54
56{
57 if ( mSetAction )
58 mSetAction->setEnabled( enabled );
59 if ( mClearAction )
60 mClearAction->setEnabled( enabled && !mValue.isEmpty() );
61}
62
63QWidget *QgsBinaryWidgetWrapper::createWidget( QWidget *parent )
64{
65 QWidget *container = new QWidget( parent );
66 QHBoxLayout *layout = new QHBoxLayout();
67 container->setLayout( layout );
68 layout->setContentsMargins( 0, 0, 0, 0 );
69
70 QLabel *label = new QLabel();
71 layout->addWidget( label, 1 );
72
73 QToolButton *button = new QToolButton();
74 button->setText( QChar( 0x2026 ) );
75 layout->addWidget( button, 0 );
76
77 container->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Minimum );
78 return container;
79}
80
82{
83 mLabel = editor->findChild<QLabel *>();
84 mButton = editor->findChild<QToolButton *>();
85
86 if ( mLabel )
87 {
88 QFont f = mLabel->font();
89 f.setItalic( true );
90 mLabel->setFont( f );
91 }
92
93 if ( mButton )
94 {
95 mButton->setPopupMode( QToolButton::InstantPopup );
96
97 mSetAction = new QAction( tr( "Embed File…" ), mButton );
98 connect( mSetAction, &QAction::triggered, this, &QgsBinaryWidgetWrapper::setContent );
99 mClearAction = new QAction( tr( "Clear Contents…" ), mButton );
100 connect( mClearAction, &QAction::triggered, this, &QgsBinaryWidgetWrapper::clear );
101 mSaveAction = new QAction( tr( "Save Contents to File…" ), mButton );
102 connect( mSaveAction, &QAction::triggered, this, &QgsBinaryWidgetWrapper::saveContent );
103 QMenu *menu = new QMenu( mButton );
104 menu->addAction( mSetAction );
105 menu->addAction( mClearAction );
106 menu->addSeparator();
107 menu->addAction( mSaveAction );
108 mButton->setMenu( menu );
109 }
110}
111
113{
114 return mLabel && mButton;
115}
116
117void QgsBinaryWidgetWrapper::updateValues( const QVariant &value, const QVariantList & )
118{
119 mValue = value.isValid() && !QgsVariantUtils::isNull( value ) && value.canConvert<QByteArray>() ? value.toByteArray() : QByteArray();
120 if ( mValue.length() == 0 )
121 mValue = QByteArray();
122
123 if ( mLabel )
124 {
125 if ( !mValue.isEmpty() )
126 {
127 mLabel->setText( tr( "Binary (%1)" ).arg( QgsFileUtils::representFileSize( mValue.size() ) ) );
128 }
129 else
130 {
131 mLabel->setText( QgsApplication::nullRepresentation() );
132 }
133 }
134 if ( mSaveAction )
135 mSaveAction->setEnabled( !mValue.isEmpty() );
136 if ( mClearAction )
137 mClearAction->setEnabled( !mValue.isEmpty() );
138}
139
140void QgsBinaryWidgetWrapper::saveContent()
141{
142 QgsSettings s;
143
144 QString file;
145 {
146 const QgsFocusKeeper focusKeeper;
147
148 file = QFileDialog::getSaveFileName( nullptr, tr( "Save Contents to File" ), defaultPath(), 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 if ( fileOut.open( QIODevice::WriteOnly ) )
160 {
161 fileOut.write( mValue );
162 fileOut.close();
163
164 if ( mMessageBar )
165 mMessageBar->pushSuccess( QString(), tr( "Saved content to <a href=\"%1\">%2</a>" ).arg( QUrl::fromLocalFile( file ).toString(), QDir::toNativeSeparators( file ) ) );
166 }
167 else if ( mMessageBar )
168 {
169 mMessageBar->pushMessage( QString(), tr( "Error opening %1 for write" ).arg( QDir::toNativeSeparators( file ) ), Qgis::MessageLevel::Critical );
170 }
171}
172
173void QgsBinaryWidgetWrapper::setContent()
174{
175 QgsSettings s;
176
177 QString file;
178 {
179 const QgsFocusKeeper focusKeeper;
180
181 file = QFileDialog::getOpenFileName( nullptr, tr( "Embed File" ), defaultPath(), tr( "All files" ) + " (*.*)" );
182 }
183
184 const QFileInfo fi( file );
185 if ( file.isEmpty() || !fi.exists() )
186 {
187 return;
188 }
189
190 s.setValue( QStringLiteral( "/UI/lastBinaryDir" ), fi.absolutePath() );
191
192 QFile fileSource( file );
193 if ( !fileSource.open( QIODevice::ReadOnly ) )
194 {
195 return;
196 }
197
198 updateValues( fileSource.readAll() );
200}
201
202void QgsBinaryWidgetWrapper::clear()
203{
204 {
205 const QgsFocusKeeper focusKeeper;
206 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 )
207 return;
208 }
209
210 updateValues( QByteArray() );
212}
213
214QString QgsBinaryWidgetWrapper::defaultPath()
215{
216 return QgsSettings().value( QStringLiteral( "/UI/lastBinaryDir" ), QDir::homePath() ).toString();
217}
@ Critical
Critical/error message.
Definition qgis.h:159
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.