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