QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
18#include <QUuid>
19
20#include "moc_qgsuuidwidgetwrapper.cpp"
21
23 : QgsEditorWidgetWrapper( layer, fieldIdx, editor, parent )
24
25{
26}
27
28QString QgsUuidWidgetWrapper::createUiid( int maxLength )
29{
30 QString uuid = QUuid::createUuid().toString();
31
32 if ( maxLength <= 0 || maxLength >= uuid.length() )
33 {
34 return uuid;
35 }
36 else
37 {
38 // trim left "{" and remove -'s... they are wasted characters given that we have a limited length!
39 return uuid.replace( '-', QString() ).mid( 1, maxLength );
40 }
41}
42
44{
45 QVariant v;
46
47 if ( mLineEdit )
48 v = mLineEdit->text();
49 if ( mLabel )
50 v = mLabel->text();
51
52 return v;
53}
54
55QWidget *QgsUuidWidgetWrapper::createWidget( QWidget *parent )
56{
57 return new QLineEdit( parent );
58}
59
60void QgsUuidWidgetWrapper::initWidget( QWidget *editor )
61{
62 mLineEdit = qobject_cast<QLineEdit *>( editor );
63 mLabel = qobject_cast<QLabel *>( editor );
64 if ( mLineEdit )
65 mLineEdit->setEnabled( false );
66}
67
69{
70 return mLineEdit || mLabel;
71}
72
73void QgsUuidWidgetWrapper::updateValues( const QVariant &value, const QVariantList & )
74{
76 {
77 int maxLength = 0;
78 if ( field().type() == QMetaType::Type::QString && field().length() > 0 )
79 {
80 maxLength = field().length();
81 }
82 const QString uuid = createUiid( maxLength );
83 if ( mLineEdit )
84 mLineEdit->setText( uuid );
85 if ( mLabel )
86 mLabel->setText( uuid );
87
89 }
90 else
91 {
92 if ( mLineEdit )
93 mLineEdit->setText( value.toString() );
94 if ( mLabel )
95 mLabel->setText( value.toString() );
96 }
97}
98
100{
101 Q_UNUSED( enabled )
102 // Do nothing... it is always disabled
103}
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.
QgsField field() const
Access the field.
int length
Definition qgsfield.h:59
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 dataset.
QgsVectorLayer * layer() const
Returns the vector layer associated with the widget.