Skip to content

Commit

Permalink
Moved label implementation to base class for consinstency with config
Browse files Browse the repository at this point in the history
  • Loading branch information
domi4484 committed May 4, 2021
1 parent fb1ac59 commit c773fd9
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 73 deletions.
19 changes: 17 additions & 2 deletions src/gui/qgsabstractrelationeditorwidget.cpp
Expand Up @@ -28,15 +28,25 @@
#include "qgsproject.h"
#include "qgstransactiongroup.h"
#include "qgsvectorlayerutils.h"
#include "qgscollapsiblegroupbox.h"

#include <QMessageBox>
#include <QPushButton>

#include <QVBoxLayout>

QgsAbstractRelationEditorWidget::QgsAbstractRelationEditorWidget( const QVariantMap &config, QWidget *parent )
: QWidget( parent )
{
Q_UNUSED( config );

QVBoxLayout *rootLayout = new QVBoxLayout( this );
rootLayout->setContentsMargins( 0, 0, 0, 0 );

mRootCollapsibleGroupBox = new QgsCollapsibleGroupBox( QString(), this );
rootLayout->addWidget( mRootCollapsibleGroupBox );

mTopVBoxLayout = new QVBoxLayout( mRootCollapsibleGroupBox );
mTopVBoxLayout->setContentsMargins( 0, 9, 0, 0 );
}

void QgsAbstractRelationEditorWidget::setRelationFeature( const QgsRelation &relation, const QgsFeature &feature )
Expand Down Expand Up @@ -607,7 +617,7 @@ void QgsAbstractRelationEditorWidget::updateUi()

void QgsAbstractRelationEditorWidget::setTitle( const QString &title )
{
Q_UNUSED( title )
mRootCollapsibleGroupBox->setTitle( title );
}

void QgsAbstractRelationEditorWidget::beforeSetRelationFeature( const QgsRelation &newRelation, const QgsFeature &newFeature )
Expand All @@ -628,6 +638,11 @@ void QgsAbstractRelationEditorWidget::beforeSetRelations( const QgsRelation &new
void QgsAbstractRelationEditorWidget::afterSetRelations()
{}

QVBoxLayout *QgsAbstractRelationEditorWidget::rootTopVBoxLayout() const
{
return mTopVBoxLayout;
}

void QgsAbstractRelationEditorWidget::duplicateFeature( const QgsFeatureId &fid )
{
duplicateFeatures( QgsFeatureIds() << fid );
Expand Down
11 changes: 10 additions & 1 deletion src/gui/qgsabstractrelationeditorwidget.h
Expand Up @@ -33,8 +33,10 @@
% End
#endif

class QVBoxLayout;
class QgsFeature;
class QgsVectorLayer;
class QgsCollapsibleGroupBox;

/**
* Base class to build new relation widgets.
Expand Down Expand Up @@ -234,6 +236,9 @@ class GUI_EXPORT QgsAbstractRelationEditorWidget : public QWidget
bool mForceSuppressFormPopup = false;
QString mLabel;

QgsCollapsibleGroupBox *mRootCollapsibleGroupBox = nullptr;
QVBoxLayout *mTopVBoxLayout = nullptr;

/**
* Updates the title contents to reflect the current state of the widget
*/
Expand Down Expand Up @@ -261,7 +266,6 @@ class GUI_EXPORT QgsAbstractRelationEditorWidget : public QWidget

/**
* Sets the title of the widget, if it is wrapped within a QgsCollapsibleGroupBox
* Check QgsRealationEditorWidget as an example.
*/
virtual void setTitle( const QString &title );

Expand All @@ -288,6 +292,11 @@ class GUI_EXPORT QgsAbstractRelationEditorWidget : public QWidget
* Check QgsRealationEditorWidget as an example.
*/
virtual void afterSetRelations();

/**
* The root collapsible group box to insert content into
*/
QVBoxLayout *rootTopVBoxLayout() const;
};


Expand Down
86 changes: 25 additions & 61 deletions src/gui/qgsrelationeditorwidget.cpp
Expand Up @@ -35,6 +35,7 @@
#include "qgsexpressioncontextutils.h"
#include "qgsmessagebar.h"
#include "qgsmessagebaritem.h"
#include "qgscollapsiblegroupbox.h"

#include <QHBoxLayout>
#include <QLabel>
Expand Down Expand Up @@ -97,15 +98,6 @@ QgsRelationEditorWidget::QgsRelationEditorWidget( const QVariantMap &config, QWi
: QgsAbstractRelationEditorWidget( config, parent )
, mButtonsVisibility( qgsFlagKeysToValue( config.value( QStringLiteral( "buttons" ) ).toString(), QgsRelationEditorWidget::Button::AllButtons ) )
{
QVBoxLayout *rootLayout = new QVBoxLayout( this );
rootLayout->setContentsMargins( 0, 0, 0, 0 );

mRootCollapsibleGroupBox = new QgsCollapsibleGroupBox( QString(), this );
rootLayout->addWidget( mRootCollapsibleGroupBox );

QVBoxLayout *topLayout = new QVBoxLayout( mRootCollapsibleGroupBox );
topLayout->setContentsMargins( 0, 9, 0, 0 );

// buttons
QHBoxLayout *buttonLayout = new QHBoxLayout();
buttonLayout->setContentsMargins( 0, 0, 0, 0 );
Expand Down Expand Up @@ -195,13 +187,12 @@ QgsRelationEditorWidget::QgsRelationEditorWidget( const QVariantMap &config, QWi
mViewModeButtonGroup->addButton( mTableViewButton, QgsDualView::AttributeTable );

// add buttons layout
topLayout->addLayout( buttonLayout );
rootTopVBoxLayout()->addLayout( buttonLayout );

mRelationLayout = new QGridLayout();
mRelationLayout->setContentsMargins( 0, 0, 0, 0 );
topLayout->addLayout( mRelationLayout );
rootTopVBoxLayout()->addLayout( mRelationLayout );

connect( mRootCollapsibleGroupBox, &QgsCollapsibleGroupBoxBasic::collapsedStateChanged, this, &QgsRelationEditorWidget::onCollapsedStateChanged );
connect( mViewModeButtonGroup, static_cast<void ( QButtonGroup::* )( int )>( &QButtonGroup::buttonClicked ),
this, static_cast<void ( QgsRelationEditorWidget::* )( int )>( &QgsRelationEditorWidget::setViewMode ) );
connect( mToggleEditingButton, &QAbstractButton::clicked, this, &QgsRelationEditorWidget::toggleEditing );
Expand Down Expand Up @@ -366,48 +357,33 @@ void QgsRelationEditorWidget::toggleEditing( bool state )
updateButtons();
}

void QgsRelationEditorWidget::onCollapsedStateChanged( bool collapsed )
{
if ( !collapsed )
{
if ( !mVisible )
{
mVisible = true;
updateUi();
}
}
}

void QgsRelationEditorWidget::updateUi()
{
// If not yet initialized, it is not (yet) visible, so we don't load it to be faster (lazy loading)
// If it is already initialized, it has been set visible before and the currently shown feature is changing
// and the widget needs updating
if ( mVisible && mRelation.isValid() && mFeature.isValid() )
{
QgsFeatureRequest request = mRelation.getRelatedFeaturesRequest( mFeature );

if ( mNmRelation.isValid() )
{
QgsFeatureIterator it = mRelation.referencingLayer()->getFeatures( request );
QgsFeature fet;
QStringList filters;
if ( !mRelation.isValid() || !mFeature.isValid() )
return;

while ( it.nextFeature( fet ) )
{
QString filter = mNmRelation.getReferencedFeatureRequest( fet ).filterExpression()->expression();
filters << filter.prepend( '(' ).append( ')' );
}
QgsFeatureRequest request = mRelation.getRelatedFeaturesRequest( mFeature );

QgsFeatureRequest nmRequest;
nmRequest.setFilterExpression( filters.join( QLatin1String( " OR " ) ) );
if ( mNmRelation.isValid() )
{
QgsFeatureIterator it = mRelation.referencingLayer()->getFeatures( request );
QgsFeature fet;
QStringList filters;

initDualView( mNmRelation.referencedLayer(), nmRequest );
}
else if ( mRelation.referencingLayer() )
while ( it.nextFeature( fet ) )
{
initDualView( mRelation.referencingLayer(), request );
QString filter = mNmRelation.getReferencedFeatureRequest( fet ).filterExpression()->expression();
filters << filter.prepend( '(' ).append( ')' );
}

QgsFeatureRequest nmRequest;
nmRequest.setFilterExpression( filters.join( QLatin1String( " OR " ) ) );

initDualView( mNmRelation.referencedLayer(), nmRequest );
}
else if ( mRelation.referencingLayer() )
{
initDualView( mRelation.referencingLayer(), request );
}
}

Expand Down Expand Up @@ -510,11 +486,6 @@ void QgsRelationEditorWidget::setConfig( const QVariantMap &config )
updateButtons();
}

void QgsRelationEditorWidget::setTitle( const QString &title )
{
mRootCollapsibleGroupBox->setTitle( title );
}

void QgsRelationEditorWidget::beforeSetRelationFeature( const QgsRelation &newRelation, const QgsFeature &newFeature )
{
Q_UNUSED( newRelation );
Expand Down Expand Up @@ -549,15 +520,8 @@ void QgsRelationEditorWidget::afterSetRelationFeature()
mToggleEditingButton->setEnabled( false );
}

// If not yet initialized, it is not (yet) visible, so we don't load it to be faster (lazy loading)
// If it is already initialized, it has been set visible before and the currently shown feature is changing
// and the widget needs updating

if ( mVisible )
{
QgsFeatureRequest myRequest = mRelation.getRelatedFeaturesRequest( mFeature );
initDualView( mRelation.referencingLayer(), myRequest );
}
QgsFeatureRequest myRequest = mRelation.getRelatedFeaturesRequest( mFeature );
initDualView( mRelation.referencingLayer(), myRequest );
}

void QgsRelationEditorWidget::beforeSetRelations( const QgsRelation &newRelation, const QgsRelation &newNmRelation )
Expand Down
9 changes: 0 additions & 9 deletions src/gui/qgsrelationeditorwidget.h
Expand Up @@ -27,7 +27,6 @@
#include "qgsabstractrelationeditorwidget.h"
#include "qobjectuniqueptr.h"
#include "qgsattributeeditorcontext.h"
#include "qgscollapsiblegroupbox.h"
#include "qgsdualview.h"
#include "qgsrelation.h"
#include "qgsvectorlayerselectionmanager.h"
Expand Down Expand Up @@ -185,11 +184,6 @@ class GUI_EXPORT QgsRelationEditorWidget : public QgsAbstractRelationEditorWidge
*/
void setConfig( const QVariantMap &config ) override;

/**
* Sets the title of the root groupbox
*/
void setTitle( const QString &title ) override;

public slots:
void parentFormValueChanged( const QString &attribute, const QVariant &newValue ) override;

Expand All @@ -206,7 +200,6 @@ class GUI_EXPORT QgsRelationEditorWidget : public QgsAbstractRelationEditorWidge

void addFeatureGeometry();
void toggleEditing( bool state );
void onCollapsedStateChanged( bool collapsed );
void showContextMenu( QgsActionMenu *menu, QgsFeatureId fid );
void mapToolDeactivated();
void onKeyPressed( QKeyEvent *e );
Expand All @@ -217,7 +210,6 @@ class GUI_EXPORT QgsRelationEditorWidget : public QgsAbstractRelationEditorWidge
void setMapTool( QgsMapTool *mapTool );
void unsetMapTool();

QgsCollapsibleGroupBox *mRootCollapsibleGroupBox = nullptr;
QgsDualView *mDualView = nullptr;
QPointer<QgsMessageBarItem> mMessageBarItem;
QgsDualView::ViewMode mViewMode = QgsDualView::AttributeEditor;
Expand All @@ -239,7 +231,6 @@ class GUI_EXPORT QgsRelationEditorWidget : public QgsAbstractRelationEditorWidge
QgsVectorLayerSelectionManager *mFeatureSelectionMgr = nullptr;

Buttons mButtonsVisibility = Button::AllButtons;
bool mVisible = true;
};


Expand Down
Expand Up @@ -29,6 +29,9 @@
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>If left empty the relation name is used.</string>
</property>
</widget>
</item>
<item row="1" column="0">
Expand Down

0 comments on commit c773fd9

Please sign in to comment.