QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgstexteditwrapper.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgstexteditwrapper.cpp
3  --------------------------------------
4  Date : 5.1.2014
5  Copyright : (C) 2014 Matthias Kuhn
6  Email : matthias dot kuhn at gmx 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 "qgstexteditwrapper.h"
17 
18 #include "qgsfield.h"
19 #include "qgsfieldvalidator.h"
20 #include "qgsfilterlineedit.h"
21 
22 #include <QSettings>
23 
24 QgsTextEditWrapper::QgsTextEditWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* editor, QWidget* parent )
25  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
26 {
27 }
28 
30 {
31  QString v;
32 
33  if ( mTextEdit )
34  {
35  if ( config( "UseHtml" ).toBool() )
36  {
37  v = mTextEdit->toHtml();
38  }
39  else
40  {
41  v = mTextEdit->toPlainText();
42  }
43  }
44 
45  if ( mPlainTextEdit )
46  {
47  v = mPlainTextEdit->toPlainText();
48  }
49 
50  if ( mLineEdit )
51  {
52  v = mLineEdit->text();
53  }
54 
55  if (( v.isEmpty() && ( field().type() == QVariant::Int || field().type() == QVariant::Double || field().type() == QVariant::LongLong || field().type() == QVariant::Date ) ) ||
56  v == QSettings().value( "qgis/nullValue", "NULL" ).toString() )
57  return QVariant( field().type() );
58  else
59  return QVariant( v );
60 }
61 
62 QWidget* QgsTextEditWrapper::createWidget( QWidget* parent )
63 {
64  if ( config( "IsMultiline" ).toBool() )
65  {
66  if ( config( "UseHtml" ).toBool() )
67  {
68  return new QTextEdit( parent );
69  }
70  else
71  {
72  return new QPlainTextEdit( parent );
73  }
74  }
75  else
76  {
77  return new QgsFilterLineEdit( parent );
78  }
79 }
80 
81 void QgsTextEditWrapper::initWidget( QWidget* editor )
82 {
83  mTextEdit = qobject_cast<QTextEdit*>( editor );
84  mPlainTextEdit = qobject_cast<QPlainTextEdit*>( editor );
85  mLineEdit = qobject_cast<QLineEdit*>( editor );
86 
87  if ( mTextEdit )
88  connect( mTextEdit, SIGNAL( textChanged() ), this, SLOT( valueChanged() ) );
89 
90  if ( mPlainTextEdit )
91  connect( mPlainTextEdit, SIGNAL( textChanged() ), this, SLOT( valueChanged() ) );
92 
93  if ( mLineEdit )
94  {
95  mLineEdit->setValidator( new QgsFieldValidator( mLineEdit, field() ) );
96 
97  QgsFilterLineEdit *fle = qobject_cast<QgsFilterLineEdit*>( mLineEdit );
98  if ( fle && !( field().type() == QVariant::Int || field().type() == QVariant::Double || field().type() == QVariant::LongLong || field().type() == QVariant::Date ) )
99  {
100  fle->setNullValue( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
101  }
102 
103  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
104  }
105 }
106 
107 void QgsTextEditWrapper::setValue( const QVariant& value )
108 {
109  QString v;
110  if ( value.isNull() )
111  {
112  if ( !( field().type() == QVariant::Int || field().type() == QVariant::Double || field().type() == QVariant::LongLong || field().type() == QVariant::Date ) )
113  v = QSettings().value( "qgis/nullValue", "NULL" ).toString();
114  }
115  else
116  v = value.toString();
117 
118  if ( mTextEdit )
119  {
120  if ( config( "UseHtml" ).toBool() )
121  mTextEdit->setHtml( v );
122  else
123  mTextEdit->setPlainText( v );
124  }
125 
126  if ( mPlainTextEdit )
127  mPlainTextEdit->setPlainText( v );
128 
129  if ( mLineEdit )
130  mLineEdit->setText( v );
131 }
132 
133 void QgsTextEditWrapper::setEnabled( bool enabled )
134 {
135  if ( mTextEdit )
136  mTextEdit->setReadOnly( !enabled );
137 
138  if ( mPlainTextEdit )
139  mPlainTextEdit->setReadOnly( !enabled );
140 
141  if ( mLineEdit )
142  mLineEdit->setReadOnly( !enabled );
143 }
void valueChanged()
Will call the value() method to determine the emitted value.
Manages an editor widget Widget and wrapper share the same parent.
QgsField field()
Access the field.
QPlainTextEdit * mPlainTextEdit
const QgsEditorWidgetConfig config()
Returns the whole config.
Lineedit with builtin clear button.
void setNullValue(QString nullValue)
void setEnabled(bool enabled)
QWidget * createWidget(QWidget *parent)
This method should create a new widget with the provided parent.
void initWidget(QWidget *editor)
This method should initialize the editor widget with runtime data.
void setValue(const QVariant &value)
QgsTextEditWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor=0, QWidget *parent=0)
Represents a vector layer which manages a vector based data sets.
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
Definition: qgsfield.cpp:63
QVariant value()
Will be used to access the widget's value.