QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
16 #include "qgsuuidwidgetwrapper.h"
17 
18 #include <QUuid>
19 
20 QgsUuidWidgetWrapper::QgsUuidWidgetWrapper( QgsVectorLayer *layer, int fieldIdx, QWidget *editor, QWidget *parent )
21  : QgsEditorWidgetWrapper( layer, fieldIdx, editor, parent )
22 
23 {
24 }
25 
26 QString 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 
53 QWidget *QgsUuidWidgetWrapper::createWidget( QWidget *parent )
54 {
55  return new QLineEdit( parent );
56 }
57 
58 void 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 
71 void 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 }
QgsField::length
int length
Definition: qgsfield.h:56
QgsEditorWidgetWrapper
Manages an editor widget Widget and wrapper share the same parent.
Definition: qgseditorwidgetwrapper.h:47
QgsUuidWidgetWrapper::value
QVariant value() const override
Will be used to access the widget's value.
Definition: qgsuuidwidgetwrapper.cpp:41
QgsUuidWidgetWrapper::createWidget
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
Definition: qgsuuidwidgetwrapper.cpp:53
QgsUuidWidgetWrapper::valid
bool valid() const override
Returns true if the widget has been properly initialized.
Definition: qgsuuidwidgetwrapper.cpp:66
QgsEditorWidgetWrapper::field
QgsField field() const
Access the field.
Definition: qgseditorwidgetwrapper.cpp:39
QgsEditorWidgetWrapper::emitValueChanged
void emitValueChanged()
Will call the value() method to determine the emitted value.
Definition: qgseditorwidgetwrapper.cpp:91
QgsUuidWidgetWrapper::createUiid
static QString createUiid(int maxLength=0)
Creates a UUID value, respecting the specified maximum length.
Definition: qgsuuidwidgetwrapper.cpp:26
QgsUuidWidgetWrapper::QgsUuidWidgetWrapper
QgsUuidWidgetWrapper(QgsVectorLayer *layer, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
Constructor for QgsUuidWidgetWrapper.
Definition: qgsuuidwidgetwrapper.cpp:20
qgsuuidwidgetwrapper.h
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsUuidWidgetWrapper::setEnabled
void setEnabled(bool enabled) override
Definition: qgsuuidwidgetwrapper.cpp:97
QgsUuidWidgetWrapper::initWidget
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
Definition: qgsuuidwidgetwrapper.cpp:58