QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgsuuidwidgetwrapper.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsuuidwidgetwrapper.cpp
3 --------------------------------------
4 Date : 5.1.2014
5 Copyright : (C) 2014 Matthias Kuhn
6 Email : matthias at opengis dot ch
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_qgsuuidwidgetwrapper.cpp"
18
19#include <QUuid>
20
21QgsUuidWidgetWrapper::QgsUuidWidgetWrapper( QgsVectorLayer *layer, int fieldIdx, QWidget *editor, QWidget *parent )
22 : QgsEditorWidgetWrapper( layer, fieldIdx, editor, parent )
23
24{
25}
26
27QString QgsUuidWidgetWrapper::createUiid( int maxLength )
28{
29 QString uuid = QUuid::createUuid().toString();
30
31 if ( maxLength <= 0 || maxLength >= uuid.length() )
32 {
33 return uuid;
34 }
35 else
36 {
37 // trim left "{" and remove -'s... they are wasted characters given that we have a limited length!
38 return uuid.replace( '-', QString() ).mid( 1, maxLength );
39 }
40}
41
43{
44 QVariant v;
45
46 if ( mLineEdit )
47 v = mLineEdit->text();
48 if ( mLabel )
49 v = mLabel->text();
50
51 return v;
52}
53
54QWidget *QgsUuidWidgetWrapper::createWidget( QWidget *parent )
55{
56 return new QLineEdit( parent );
57}
58
59void QgsUuidWidgetWrapper::initWidget( QWidget *editor )
60{
61 mLineEdit = qobject_cast<QLineEdit *>( editor );
62 mLabel = qobject_cast<QLabel *>( editor );
63 if ( mLineEdit )
64 mLineEdit->setEnabled( false );
65}
66
68{
69 return mLineEdit || mLabel;
70}
71
72void QgsUuidWidgetWrapper::updateValues( const QVariant &value, const QVariantList & )
73{
75 {
76 int maxLength = 0;
77 if ( field().type() == QMetaType::Type::QString && field().length() > 0 )
78 {
79 maxLength = field().length();
80 }
81 const QString uuid = createUiid( maxLength );
82 if ( mLineEdit )
83 mLineEdit->setText( uuid );
84 if ( mLabel )
85 mLabel->setText( uuid );
86
88 }
89 else
90 {
91 if ( mLineEdit )
92 mLineEdit->setText( value.toString() );
93 if ( mLabel )
94 mLabel->setText( value.toString() );
95 }
96}
97
99{
100 Q_UNUSED( enabled )
101 // Do nothing... it is always disabled
102}
Manages an editor widget Widget and wrapper share the same parent.
void emitValueChanged()
Will call the value() method to determine the emitted value.
QgsField field() const
Access the field.
int length
Definition qgsfield.h:58
void setEnabled(bool enabled) override
QVariant value() const override
Will be used to access the widget's value.
QgsUuidWidgetWrapper(QgsVectorLayer *layer, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
Constructor for QgsUuidWidgetWrapper.
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
static QString createUiid(int maxLength=0)
Creates a UUID value, respecting the specified maximum length.
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
bool valid() const override
Returns true if the widget has been properly initialized.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
Represents a vector layer which manages a vector based data sets.