QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 ( field().type() == QVariant::Int || field().type() == QVariant::Double || field().type() == QVariant::LongLong || field().type() == QVariant::Date )
99  {
100  mLineEdit->setPlaceholderText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
101  }
102  else if ( fle )
103  {
104  fle->setNullValue( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
105  }
106 
107  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
108 
109  mWritablePalette = mLineEdit->palette();
110  mReadOnlyPalette = mLineEdit->palette();
111  mReadOnlyPalette.setColor( QPalette::Text, mWritablePalette.color( QPalette::Disabled, QPalette::Text ) );
112  }
113 }
114 
115 void QgsTextEditWrapper::setValue( const QVariant& value )
116 {
117  QString v;
118  if ( value.isNull() )
119  {
120  if ( !( field().type() == QVariant::Int || field().type() == QVariant::Double || field().type() == QVariant::LongLong || field().type() == QVariant::Date ) )
121  v = QSettings().value( "qgis/nullValue", "NULL" ).toString();
122  }
123  else
124  v = value.toString();
125 
126  if ( mTextEdit )
127  {
128  if ( config( "UseHtml" ).toBool() )
129  mTextEdit->setHtml( v );
130  else
131  mTextEdit->setPlainText( v );
132  }
133 
134  if ( mPlainTextEdit )
135  mPlainTextEdit->setPlainText( v );
136 
137  if ( mLineEdit )
138  mLineEdit->setText( v );
139 }
140 
141 void QgsTextEditWrapper::setEnabled( bool enabled )
142 {
143  if ( mTextEdit )
144  mTextEdit->setReadOnly( !enabled );
145 
146  if ( mPlainTextEdit )
147  mPlainTextEdit->setReadOnly( !enabled );
148 
149  if ( mLineEdit )
150  {
151  mLineEdit->setReadOnly( !enabled );
152  if ( enabled )
153  mLineEdit->setPalette( mWritablePalette );
154  else
155  mLineEdit->setPalette( mReadOnlyPalette );
156  }
157 }
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.
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:64
QVariant value()
Will be used to access the widget's value.