Skip to content

Commit

Permalink
Attribute Editor: Synchronize editable QComboBox for LineEdit's and s…
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Nov 8, 2012
1 parent fce38c5 commit 7db8601
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/gui/qgsattributeeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,13 @@ QWidget *QgsAttributeEditor::createAttributeEditor( QWidget *parent, QWidget *ed
if ( !vl )
return 0;

bool synchronized = false;
QWidget *myWidget = 0;
QgsVectorLayer::EditType editType = vl->editType( idx );
const QgsField &field = vl->pendingFields()[idx];
QVariant::Type myFieldType = field.type();

bool synchronized = false;

switch ( editType )
{
case QgsVectorLayer::UniqueValues:
Expand Down Expand Up @@ -438,12 +439,14 @@ QWidget *QgsAttributeEditor::createAttributeEditor( QWidget *parent, QWidget *ed
QLineEdit *le = 0;
QTextEdit *te = 0;
QPlainTextEdit *pte = 0;
QComboBox * cb = 0;

if ( editor )
{
le = qobject_cast<QLineEdit *>( editor );
te = qobject_cast<QTextEdit *>( editor );
pte = qobject_cast<QPlainTextEdit *>( editor );
cb = qobject_cast<QComboBox *>( editor );
}
else if ( editType == QgsVectorLayer::TextEdit )
{
Expand Down Expand Up @@ -491,15 +494,42 @@ QWidget *QgsAttributeEditor::createAttributeEditor( QWidget *parent, QWidget *ed
myWidget = pte;
}

if ( cb )
{
myWidget = cb;
}

if ( myWidget )
{
myWidget->setDisabled( editType == QgsVectorLayer::Immutable );

QgsStringRelay* relay = NULL;

QMap<int, QWidget*>::const_iterator it = proxyWidgets.find( idx );
if ( it != proxyWidgets.end() )
{
synchronized = connect( *it, SIGNAL( textChanged( QString ) ), myWidget, SLOT( setText( QString ) ) );
synchronized &= connect( myWidget, SIGNAL( textChanged( QString ) ), *it, SLOT( setText( QString ) ) );
QObject* obj = qvariant_cast<QObject*>( (*it)->property( "QgisAttrEditProxy" ) );
relay = qobject_cast<QgsStringRelay*>( obj );
}
else
{
relay = new QgsStringRelay( myWidget );
}

if ( cb && cb->isEditable() )
{
synchronized = connect( relay, SIGNAL( textChanged( QString ) ), myWidget, SLOT( setEditText( QString ) ) );
synchronized &= connect( myWidget, SIGNAL( editTextChanged( QString ) ), relay, SLOT( changeText( QString ) ) );
}
else
{
synchronized = connect( relay, SIGNAL( textChanged( QString ) ), myWidget, SLOT( setText( QString ) ) );
synchronized &= connect( myWidget, SIGNAL( textChanged( QString ) ), relay, SLOT( changeText( QString ) ) );
}

if ( !cb || cb->isEditable() )
{
myWidget->setProperty( "QgisAttrEditProxy", QVariant( QMetaType::QObjectStar, &relay ) );
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/gui/qgsattributeeditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define QGSATTRIBUTEEDITOR_H

#include <QVariant>
#include <QMetaType>

#include "qgsfeature.h"

Expand Down Expand Up @@ -79,5 +80,30 @@ class GUI_EXPORT QgsAttributeEditor : public QObject
void selectDate();
};

class QgsStringRelay : public QObject
{
Q_OBJECT

public:
QgsStringRelay()
: QObject( NULL ) {}

QgsStringRelay( QObject* parent )
: QObject( parent ) {}

QgsStringRelay( const QgsStringRelay& sr )
: QObject( sr.parent() ) {}

public slots:
void changeText( QString str )
{
emit textChanged( str );
}

signals:
void textChanged( QString );
};

Q_DECLARE_METATYPE( QgsStringRelay )

#endif

0 comments on commit 7db8601

Please sign in to comment.