Skip to content
Permalink
Browse files

use a specific class for the widget (to be used as a custom widget), …

…rename old _widget to _wrapper
  • Loading branch information
3nids committed Aug 19, 2014
1 parent bc187ba commit 2a1a7944f7fea9fdd3519adfc6b8fc2c04b096c8
@@ -83,6 +83,7 @@ editorwidgets/qgsrangewidgetfactory.cpp
editorwidgets/qgsrelationwidgetwrapper.cpp
editorwidgets/qgsrelationreferencefactory.cpp
editorwidgets/qgsrelationreferencewidget.cpp
editorwidgets/qgsrelationreferencewidgetwrapper.cpp
editorwidgets/qgsrelreferenceconfigdlg.cpp
editorwidgets/qgstexteditconfigdlg.cpp
editorwidgets/qgstexteditwidget.cpp
@@ -299,6 +300,7 @@ editorwidgets/qgsphotowidget.h
editorwidgets/qgsrangeconfigdlg.h
editorwidgets/qgsrangewidget.h
editorwidgets/qgsrelationreferencewidget.h
editorwidgets/qgsrelationreferencewidgetwrapper.h
editorwidgets/qgsrelationwidgetwrapper.h
editorwidgets/qgsrelreferenceconfigdlg.h
editorwidgets/qgstexteditconfigdlg.h
@@ -528,6 +530,7 @@ editorwidgets/qgsrangewidget.h
editorwidgets/qgsrangewidgetfactory.h
editorwidgets/qgsrelationreferencefactory.h
editorwidgets/qgsrelationreferencewidget.h
editorwidgets/qgsrelationreferencewidgetwrapper.h
editorwidgets/qgsrelationwidgetwrapper.h
editorwidgets/qgsrelreferenceconfigdlg.h
editorwidgets/qgstexteditconfigdlg.h
@@ -15,7 +15,7 @@

#include "qgsrelationreferencefactory.h"

#include "qgsrelationreferencewidget.h"
#include "qgsrelationreferencewidgetwrapper.h"
#include "qgsrelreferenceconfigdlg.h"

QgsRelationReferenceFactory::QgsRelationReferenceFactory( QgsAttributeEditorContext context, QString name )
@@ -26,7 +26,7 @@ QgsRelationReferenceFactory::QgsRelationReferenceFactory( QgsAttributeEditorCont

QgsEditorWidgetWrapper* QgsRelationReferenceFactory::create( QgsVectorLayer* vl, int fieldIdx, QWidget* editor, QWidget* parent ) const
{
return new QgsRelationReferenceWidget( vl, fieldIdx, editor, mEditorContext, parent );
return new QgsRelationReferenceWidgetWrapper( vl, fieldIdx, editor, mEditorContext, parent );
}

QgsEditorConfigWidget* QgsRelationReferenceFactory::configWidget( QgsVectorLayer* vl, int fieldIdx, QWidget* parent ) const
@@ -17,67 +17,58 @@

#include <QPushButton>
#include <QDialog>
#include <QToolButton>

#include "qgsattributedialog.h"
#include "qgsapplication.h"
#include "qgscollapsiblegroupbox.h"
#include "qgseditorwidgetfactory.h"
#include "qgsexpression.h"
#include "qgsfield.h"
#include "qgsproject.h"
#include "qgsrelreferenceconfigdlg.h"
#include "qgsrelationmanager.h"
#include "qgsvectorlayer.h"

QgsRelationReferenceWidget::QgsRelationReferenceWidget( QgsVectorLayer* vl, int fieldIdx, QWidget* editor, QgsAttributeEditorContext context, QWidget* parent )
: QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
, mInitialValueAssigned( false )
, mComboBox( NULL )
, mAttributeEditorFrame( NULL )
, mAttributeEditorLayout( NULL )
, mAttributeEditorButton( NULL )

QgsRelationReferenceWidget::QgsRelationReferenceWidget( QWidget* parent )
: QWidget( parent )
, mReferencedLayer( NULL )
, mInitialValueAssigned( false )
, mAttributeDialog( NULL )
, mEditorContext( context )
, mEditorContext( QgsAttributeEditorContext() )
{
mLayout = new QGridLayout( this );
setLayout( mLayout );
mComboBox = new QComboBox( this );
mLayout->addWidget( mComboBox, 0, 0, 1, 1 );

// action button
QToolButton* attributeEditorButton = new QToolButton( this );
mShowFormAction = new QAction( QgsApplication::getThemeIcon( "/mActionToggleEditing.svg" ), tr( "Open Form" ), this );
attributeEditorButton->addAction( mShowFormAction );
attributeEditorButton->setDefaultAction( mShowFormAction );
connect( attributeEditorButton, SIGNAL( triggered( QAction* ) ), this, SLOT( buttonTriggered( QAction* ) ) );
mLayout->addWidget( attributeEditorButton, 0, 1, 1, 1 );

// embed form
mAttributeEditorFrame = new QgsCollapsibleGroupBox( this );
mAttributeEditorFrame->setCollapsed( true );
mAttributeEditorLayout = new QVBoxLayout( mAttributeEditorFrame );
mAttributeEditorFrame->setLayout( mAttributeEditorLayout );
mLayout->addWidget( mAttributeEditorFrame, 1, 0, 1, 3 );

mLayout->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding ), 0, 2, 1, 1 );
}

QWidget* QgsRelationReferenceWidget::createWidget( QWidget* parent )
void QgsRelationReferenceWidget::displayEmbedForm( bool display )
{
return new QWidget( parent );
mAttributeEditorFrame->setVisible( display );
}

void QgsRelationReferenceWidget::initWidget( QWidget* editor )
void QgsRelationReferenceWidget::setRelation( QgsRelation relation, bool allowNullValue )
{
QGridLayout* layout = new QGridLayout( editor );
editor->setLayout( layout );

mComboBox = new QComboBox( editor );
layout->addWidget( mComboBox, 0, 0, 1, 1 );

if ( config( "ShowForm", true ).toBool() )
{
mAttributeEditorFrame = new QgsCollapsibleGroupBox( editor );
mAttributeEditorLayout = new QVBoxLayout( mAttributeEditorFrame );
mAttributeEditorFrame->setLayout( mAttributeEditorLayout );

layout->addWidget( mAttributeEditorFrame, 1, 0, 1, 3 );
}
else
{
mAttributeEditorButton = new QPushButton( tr( "Open Form" ) );

layout->addWidget( mAttributeEditorButton, 0, 1, 1, 1 );

connect( mAttributeEditorButton, SIGNAL( clicked() ), this, SLOT( openForm() ) );
}

layout->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding ), 0, 2, 1, 1 );

QgsRelation relation = QgsProject::instance()->relationManager()->relation( config( "Relation" ).toString() );

if ( relation.isValid() )
{
if ( config( "AllowNULL" ).toBool() )
if ( allowNullValue )
{
mComboBox->addItem( tr( "(no selection)" ), QVariant( field().type() ) );
}
@@ -110,43 +101,20 @@ void QgsRelationReferenceWidget::initWidget( QWidget* editor )
font.setItalic( true );
lbl->setStyleSheet( "QLabel { color: red; } " );
lbl->setFont( font );
layout->addWidget( lbl, 1, 0, 1, 3 );
mLayout->addWidget( lbl, 1, 0, 1, 3 );
}
}

QVariant QgsRelationReferenceWidget::value()
void QgsRelationReferenceWidget::setRelationEditable( bool editable )
{
QVariant varFid = mComboBox->itemData( mComboBox->currentIndex() );
if ( varFid.isNull() )
{
return QVariant( field().type() );
}
else
{
return mFidFkMap.value( varFid.value<QgsFeatureId>() );
}
mComboBox->setEnabled( editable );
}

void QgsRelationReferenceWidget::setValue( const QVariant& value )
void QgsRelationReferenceWidget::setRelatedFeature( const QVariant& value )
{
QgsFeatureId fid = mFidFkMap.key( value );
int oldIdx = mComboBox->currentIndex();

if ( value.isNull() )
{
if ( config( "AllowNULL" ).toBool() )
{
mComboBox->setCurrentIndex( 0 );
}
else
{
mComboBox->setCurrentIndex( -1 );
}
}
else
{
QgsFeatureId fid = mFidFkMap.key( value );
mComboBox->setCurrentIndex( mComboBox->findData( fid ) );
}
mComboBox->setCurrentIndex( mComboBox->findData( fid ) );

if ( !mInitialValueAssigned )
{
@@ -159,16 +127,57 @@ void QgsRelationReferenceWidget::setValue( const QVariant& value )
}
}

void QgsRelationReferenceWidget::setEnabled( bool enabled )
QVariant QgsRelationReferenceWidget::relatedFeature()
{
QVariant varFid = mComboBox->itemData( mComboBox->currentIndex() );
if ( varFid.isNull() )
{
return QVariant( field.type() );
}
else
{
return mFidFkMap.value( varFid.value<QgsFeatureId>() );
}
}

void QgsRelationReferenceWidget::setEditorContext( QgsAttributeEditorContext context )
{
mEditorContext = context;
}

void QgsRelationReferenceWidget::buttonTriggered( QAction* action )
{
mComboBox->setEnabled( enabled );
if ( action == mShowFormAction )
{
openForm();
}
}

void QgsRelationReferenceWidget::openForm()
{
QgsFeatureId fid = mComboBox->itemData( mComboBox->currentIndex() ).value<QgsFeatureId>();

QgsFeature feat;

if ( !mReferencedLayer )
return;

mReferencedLayer->getFeatures( QgsFeatureRequest().setFilterFid( fid ) ).nextFeature( feat );

if ( !feat.isValid() )
return;

// TODO: Get a proper QgsDistanceArea thingie
mAttributeDialog = new QgsAttributeDialog( mReferencedLayer, new QgsFeature( feat ), true, this, true, mEditorContext );
mAttributeDialog->exec();
delete mAttributeDialog;
}

void QgsRelationReferenceWidget::referenceChanged( int index )
{
QgsFeatureId fid = mComboBox->itemData( index ).value<QgsFeatureId>();

emit valueChanged( mFidFkMap.value( fid ) );
emit relatedFeatureChanged( mFidFkMap.value( fid ) );

// Check if we're running with an embedded frame we need to update
if ( mAttributeEditorFrame )
@@ -198,20 +207,3 @@ void QgsRelationReferenceWidget::referenceChanged( int index )
}
}
}

void QgsRelationReferenceWidget::openForm()
{
QgsFeatureId fid = mComboBox->itemData( mComboBox->currentIndex() ).value<QgsFeatureId>();

QgsFeature feat;

mReferencedLayer->getFeatures( QgsFeatureRequest().setFilterFid( fid ) ).nextFeature( feat );

if ( !feat.isValid() )
return;

// TODO: Get a proper QgsDistanceArea thingie
mAttributeDialog = new QgsAttributeDialog( mReferencedLayer, new QgsFeature( feat ), true, widget(), true, mEditorContext );
mAttributeDialog->exec();
delete mAttributeDialog;
}
@@ -17,47 +17,55 @@
#define QGSRELATIONREFERENCEWIDGET_H

#include "qgsattributeeditorcontext.h"
#include "qgseditorwidgetwrapper.h"
#include "qgscollapsiblegroupbox.h"
#include "qgsfeature.h"

#include <QComboBox>
#include <QPushButton>
#include <QVBoxLayout>

class QgsAttributeDialog;
class QgsVectorLayerTools;

class GUI_EXPORT QgsRelationReferenceWidget : public QgsEditorWidgetWrapper
class GUI_EXPORT QgsRelationReferenceWidget : public QWidget
{
Q_OBJECT
public:
explicit QgsRelationReferenceWidget( QgsVectorLayer* vl, int fieldIdx, QWidget* editor, QgsAttributeEditorContext context, QWidget* parent = 0 );
virtual QWidget* createWidget( QWidget* parent );
virtual void initWidget( QWidget* editor );
virtual QVariant value();
explicit QgsRelationReferenceWidget( QWidget* parent );

signals:
void valueChanged( const QVariant& value );
void displayEmbedForm( bool display );

void setRelation( QgsRelation relation , bool allowNullValue );

void setRelationEditable( bool editable );

void setRelatedFeature( const QVariant &value );

public slots:
virtual void setValue( const QVariant& value );
virtual void setEnabled( bool enabled );
QVariant relatedFeature();

void setEditorContext( QgsAttributeEditorContext context );

signals:
void relatedFeatureChanged( QVariant );

private slots:
void buttonTriggered( QAction* action );
void referenceChanged( int index );
void openForm();

private:
bool mInitialValueAssigned;
QComboBox* mComboBox;
QWidget* mAttributeEditorFrame;
QVBoxLayout* mAttributeEditorLayout;
QPushButton* mAttributeEditorButton;
QgsVectorLayer* mReferencedLayer;
QVariant mCurrentValue;
bool mInitialValueAssigned;
QgsAttributeDialog* mAttributeDialog;
QGridLayout* mLayout;
QHash<QgsFeatureId, QVariant> mFidFkMap; // Mapping from feature id => foreign key
QAction* mShowFormAction;
QComboBox* mComboBox;
QgsCollapsibleGroupBox* mAttributeEditorFrame;
QVBoxLayout* mAttributeEditorLayout;
QgsAttributeEditorContext mEditorContext;
};




#endif // QGSRELATIONREFERENCEWIDGET_H

0 comments on commit 2a1a794

Please sign in to comment.
You can’t perform that action at this time.