QGIS API Documentation 3.27.0-Master (0a97e3138f)
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
20QgsUuidWidgetWrapper::QgsUuidWidgetWrapper( QgsVectorLayer *layer, int fieldIdx, QWidget *editor, QWidget *parent )
21 : QgsEditorWidgetWrapper( layer, fieldIdx, editor, parent )
22
23{
24}
25
26QString QgsUuidWidgetWrapper::createUiid( int maxLength )
27{
28 QString uuid = QUuid::createUuid().toString();
29
30 if ( maxLength <= 0 || maxLength >= uuid.length() )
31 {
32 return uuid;
33 }
34 else
35 {
36 // trim left "{" and remove -'s... they are wasted characters given that we have a limited length!
37 return uuid.replace( '-', QString() ).mid( 1, maxLength );
38 }
39}
40
42{
43 QVariant v;
44
45 if ( mLineEdit )
46 v = mLineEdit->text();
47 if ( mLabel )
48 v = mLabel->text();
49
50 return v;
51}
52
53QWidget *QgsUuidWidgetWrapper::createWidget( QWidget *parent )
54{
55 return new QLineEdit( parent );
56}
57
58void QgsUuidWidgetWrapper::initWidget( QWidget *editor )
59{
60 mLineEdit = qobject_cast<QLineEdit *>( editor );
61 mLabel = qobject_cast<QLabel *>( editor );
62 if ( mLineEdit )
63 mLineEdit->setEnabled( false );
64}
65
67{
68 return mLineEdit || mLabel;
69}
70
71void QgsUuidWidgetWrapper::updateValues( const QVariant &value, const QVariantList & )
72{
73 if ( value.isNull() )
74 {
75 int maxLength = 0;
76 if ( field().type() == QVariant::String && field().length() > 0 )
77 {
78 maxLength = field().length();
79 }
80 const QString uuid = createUiid( maxLength );
81 if ( mLineEdit )
82 mLineEdit->setText( uuid );
83 if ( mLabel )
84 mLabel->setText( uuid );
85
87 }
88 else
89 {
90 if ( mLineEdit )
91 mLineEdit->setText( value.toString() );
92 if ( mLabel )
93 mLabel->setText( value.toString() );
94 }
95}
96
98{
99 Q_UNUSED( enabled )
100 // Do nothing... it is always disabled
101}
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:56
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.
Represents a vector layer which manages a vector based data sets.