Skip to content

Commit

Permalink
Port base class for item configuration widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 25, 2017
1 parent a3e2678 commit 72bf292
Show file tree
Hide file tree
Showing 5 changed files with 415 additions and 0 deletions.
1 change: 1 addition & 0 deletions python/gui/gui_auto.sip
Expand Up @@ -281,6 +281,7 @@
%Include layertree/qgslayertreeviewdefaultactions.sip
%Include layout/qgslayoutdesignerinterface.sip
%Include layout/qgslayoutitemguiregistry.sip
%Include layout/qgslayoutitemwidget.sip
%Include layout/qgslayoutnewitempropertiesdialog.sip
%Include layout/qgslayoutruler.sip
%Include layout/qgslayoutunitscombobox.sip
Expand Down
106 changes: 106 additions & 0 deletions python/gui/layout/qgslayoutitemwidget.sip
@@ -0,0 +1,106 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/layout/qgslayoutitemwidget.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/






class QgsLayoutConfigObject: QObject
{
%Docstring

An object for property widgets for layout items. All layout config type widgets should contain
this object.

If you are creating a new QgsLayoutItem configuration widget, you should instead
inherit from QgsLayoutItemBaseWidget (rather then directly working with QgsLayoutConfigObject).

.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgslayoutitemwidget.h"
%End
public:

QgsLayoutConfigObject( QWidget *parent /TransferThis/, QgsLayoutObject *layoutObject );
%Docstring
Constructor for QgsLayoutConfigObject, linked with the specified ``layoutObject``.
%End

void initializeDataDefinedButton( QgsPropertyOverrideButton *button, QgsLayoutObject::DataDefinedProperty key );
%Docstring
Registers a data defined ``button``, setting up its initial value, connections and description.
The corresponding property ``key`` must be specified.
%End

void updateDataDefinedButton( QgsPropertyOverrideButton *button );
%Docstring
Updates a data defined button to reflect the item's current properties.
%End

QgsVectorLayer *coverageLayer() const;
%Docstring
Returns the current layout context coverage layer (if set).
:rtype: QgsVectorLayer
%End


};

class QgsLayoutItemBaseWidget: QgsPanelWidget
{
%Docstring

A base class for property widgets for layout items. All layout item widgets should inherit from
this base class.


.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgslayoutitemwidget.h"
%End
public:

QgsLayoutItemBaseWidget( QWidget *parent /TransferThis/, QgsLayoutObject *layoutObject );
%Docstring
Constructor for QgsLayoutItemBaseWidget, linked with the specified ``layoutObject``.
%End

protected:

void registerDataDefinedButton( QgsPropertyOverrideButton *button, QgsLayoutObject::DataDefinedProperty property );
%Docstring
Registers a data defined ``button``, setting up its initial value, connections and description.
The corresponding property ``key`` must be specified.
%End

void updateDataDefinedButton( QgsPropertyOverrideButton *button );
%Docstring
Updates a previously registered data defined button to reflect the item's current properties.
%End

QgsVectorLayer *coverageLayer() const;
%Docstring
Returns the current layout context coverage layer (if set).
:rtype: QgsVectorLayer
%End


};

/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/layout/qgslayoutitemwidget.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -159,6 +159,7 @@ SET(QGIS_GUI_SRCS
layertree/qgslayertreeviewdefaultactions.cpp

layout/qgslayoutitemguiregistry.cpp
layout/qgslayoutitemwidget.cpp
layout/qgslayoutnewitempropertiesdialog.cpp
layout/qgslayoutruler.cpp
layout/qgslayoutunitscombobox.cpp
Expand Down Expand Up @@ -655,6 +656,7 @@ SET(QGIS_GUI_MOC_HDRS

layout/qgslayoutdesignerinterface.h
layout/qgslayoutitemguiregistry.h
layout/qgslayoutitemwidget.h
layout/qgslayoutnewitempropertiesdialog.h
layout/qgslayoutruler.h
layout/qgslayoutunitscombobox.h
Expand Down
156 changes: 156 additions & 0 deletions src/gui/layout/qgslayoutitemwidget.cpp
@@ -0,0 +1,156 @@
/***************************************************************************
qgslayoutitemwidget.cpp
------------------------
Date : July 2017
Copyright : (C) 2017 Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgslayoutitemwidget.h"
#include "qgspropertyoverridebutton.h"
#include "qgslayout.h"


//
// QgsLayoutConfigObject
//

QgsLayoutConfigObject::QgsLayoutConfigObject( QWidget *parent, QgsLayoutObject *layoutObject )
: QObject( parent )
, mLayoutObject( layoutObject )
{
#if 0 //TODO
connect( atlasComposition(), &QgsAtlasComposition::coverageLayerChanged,
this, [ = ] { updateDataDefinedButtons(); } );
connect( atlasComposition(), &QgsAtlasComposition::toggled, this, &QgsComposerConfigObject::updateDataDefinedButtons );
#endif
}

void QgsLayoutConfigObject::updateDataDefinedProperty()
{
//match data defined button to item's data defined property
QgsPropertyOverrideButton *ddButton = qobject_cast<QgsPropertyOverrideButton *>( sender() );
if ( !ddButton )
{
return;
}
QgsLayoutObject::DataDefinedProperty key = QgsLayoutObject::NoProperty;

if ( ddButton->propertyKey() >= 0 )
key = static_cast< QgsLayoutObject::DataDefinedProperty >( ddButton->propertyKey() );

if ( key == QgsLayoutObject::NoProperty )
{
return;
}

//set the data defined property and refresh the item
if ( mLayoutObject )
{
mLayoutObject->dataDefinedProperties().setProperty( key, ddButton->toProperty() );
mLayoutObject->refresh();
}
}

void QgsLayoutConfigObject::updateDataDefinedButtons()
{
#if 0 //TODO
Q_FOREACH ( QgsPropertyOverrideButton *button, findChildren< QgsPropertyOverrideButton * >() )
{
button->setVectorLayer( atlasCoverageLayer() );
}
#endif
}

void QgsLayoutConfigObject::initializeDataDefinedButton( QgsPropertyOverrideButton *button, QgsLayoutObject::DataDefinedProperty key )
{
button->blockSignals( true );
button->init( key, mLayoutObject->dataDefinedProperties(), QgsLayoutObject::propertyDefinitions(), coverageLayer() );
connect( button, &QgsPropertyOverrideButton::changed, this, &QgsLayoutConfigObject::updateDataDefinedProperty );
button->registerExpressionContextGenerator( mLayoutObject );
button->blockSignals( false );
}

void QgsLayoutConfigObject::updateDataDefinedButton( QgsPropertyOverrideButton *button )
{
if ( !button )
return;

if ( button->propertyKey() < 0 || !mLayoutObject )
return;

QgsLayoutObject::DataDefinedProperty key = static_cast< QgsLayoutObject::DataDefinedProperty >( button->propertyKey() );
whileBlocking( button )->setToProperty( mLayoutObject->dataDefinedProperties().property( key ) );
}

#if 0 // TODO
QgsAtlasComposition *QgsLayoutConfigObject::atlasComposition() const
{
if ( !mLayoutObject )
{
return nullptr;
}

QgsComposition *composition = mComposerObject->composition();

if ( !composition )
{
return nullptr;
}

return &composition->atlasComposition();
}
#endif

QgsVectorLayer *QgsLayoutConfigObject::coverageLayer() const
{
if ( !mLayoutObject )
return nullptr;

QgsLayout *layout = mLayoutObject->layout();
if ( !layout )
return nullptr;

return layout->context().layer();
}


//
// QgsLayoutItemBaseWidget
//

QgsLayoutItemBaseWidget::QgsLayoutItemBaseWidget( QWidget *parent, QgsLayoutObject *layoutObject )
: QgsPanelWidget( parent )
, mConfigObject( new QgsLayoutConfigObject( this, layoutObject ) )
{

}

void QgsLayoutItemBaseWidget::registerDataDefinedButton( QgsPropertyOverrideButton *button, QgsLayoutObject::DataDefinedProperty property )
{
mConfigObject->initializeDataDefinedButton( button, property );
}

void QgsLayoutItemBaseWidget::updateDataDefinedButton( QgsPropertyOverrideButton *button )
{
mConfigObject->updateDataDefinedButton( button );
}

QgsVectorLayer *QgsLayoutItemBaseWidget::coverageLayer() const
{
return mConfigObject->coverageLayer();
}

#if 0 //TODO
QgsAtlasComposition *QgsLayoutItemBaseWidget::atlasComposition() const
{
return mConfigObject->atlasComposition();
}
#endif

0 comments on commit 72bf292

Please sign in to comment.