QGIS API Documentation  2.8.2-Wien
 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  , mTextEdit( NULL )
27  , mPlainTextEdit( NULL )
28  , mLineEdit( NULL )
29 {
30 }
31 
33 {
34  QString v;
35 
36  if ( mTextEdit )
37  {
38  if ( config( "UseHtml" ).toBool() )
39  {
40  v = mTextEdit->toHtml();
41  }
42  else
43  {
44  v = mTextEdit->toPlainText();
45  }
46  }
47 
48  if ( mPlainTextEdit )
49  {
50  v = mPlainTextEdit->toPlainText();
51  }
52 
53  if ( mLineEdit )
54  {
55  v = mLineEdit->text();
56  }
57 
58  if (( v.isEmpty() && ( field().type() == QVariant::Int || field().type() == QVariant::Double || field().type() == QVariant::LongLong || field().type() == QVariant::Date ) ) ||
59  v == QSettings().value( "qgis/nullValue", "NULL" ).toString() )
60  return QVariant( field().type() );
61 
62  QVariant res( v );
63  if ( field().convertCompatible( res ) )
64  return res;
65  else
66  return QVariant( field().type() );
67 }
68 
69 QWidget* QgsTextEditWrapper::createWidget( QWidget* parent )
70 {
71  if ( config( "IsMultiline" ).toBool() )
72  {
73  if ( config( "UseHtml" ).toBool() )
74  {
75  return new QTextEdit( parent );
76  }
77  else
78  {
79  return new QPlainTextEdit( parent );
80  }
81  }
82  else
83  {
84  return new QgsFilterLineEdit( parent );
85  }
86 }
87 
88 void QgsTextEditWrapper::initWidget( QWidget* editor )
89 {
90  mTextEdit = qobject_cast<QTextEdit*>( editor );
91  mPlainTextEdit = qobject_cast<QPlainTextEdit*>( editor );
92  mLineEdit = qobject_cast<QLineEdit*>( editor );
93 
94  if ( mTextEdit )
95  connect( mTextEdit, SIGNAL( textChanged() ), this, SLOT( valueChanged() ) );
96 
97  if ( mPlainTextEdit )
98  connect( mPlainTextEdit, SIGNAL( textChanged() ), this, SLOT( valueChanged() ) );
99 
100  if ( mLineEdit )
101  {
102  mLineEdit->setValidator( new QgsFieldValidator( mLineEdit, field() ) );
103 
104  QgsFilterLineEdit *fle = qobject_cast<QgsFilterLineEdit*>( mLineEdit );
105  if ( field().type() == QVariant::Int || field().type() == QVariant::Double || field().type() == QVariant::LongLong || field().type() == QVariant::Date )
106  {
107  mLineEdit->setPlaceholderText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
108  }
109  else if ( fle )
110  {
111  fle->setNullValue( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
112  }
113 
114  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
115 
116  mWritablePalette = mLineEdit->palette();
117  mReadOnlyPalette = mLineEdit->palette();
118  mReadOnlyPalette.setColor( QPalette::Text, mWritablePalette.color( QPalette::Disabled, QPalette::Text ) );
119  }
120 }
121 
122 void QgsTextEditWrapper::setValue( const QVariant& value )
123 {
124  QString v;
125  if ( value.isNull() )
126  {
127  if ( !( field().type() == QVariant::Int || field().type() == QVariant::Double || field().type() == QVariant::LongLong || field().type() == QVariant::Date ) )
128  v = QSettings().value( "qgis/nullValue", "NULL" ).toString();
129  }
130  else
131  v = value.toString();
132 
133  if ( mTextEdit )
134  {
135  if ( config( "UseHtml" ).toBool() )
136  mTextEdit->setHtml( v );
137  else
138  mTextEdit->setPlainText( v );
139  }
140 
141  if ( mPlainTextEdit )
142  mPlainTextEdit->setPlainText( v );
143 
144  if ( mLineEdit )
145  mLineEdit->setText( v );
146 }
147 
148 void QgsTextEditWrapper::setEnabled( bool enabled )
149 {
150  if ( mTextEdit )
151  mTextEdit->setReadOnly( !enabled );
152 
153  if ( mPlainTextEdit )
154  mPlainTextEdit->setReadOnly( !enabled );
155 
156  if ( mLineEdit )
157  {
158  mLineEdit->setReadOnly( !enabled );
159  if ( enabled )
160  mLineEdit->setPalette( mWritablePalette );
161  else
162  mLineEdit->setPalette( mReadOnlyPalette );
163  }
164 }