Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Jun 2, 2016
1 parent ea2395d commit c780949
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 151 deletions.
3 changes: 2 additions & 1 deletion src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ SET(QGIS_GUI_SRCS
layertree/qgscustomlayerorderwidget.cpp
layertree/qgslayertreeembeddedconfigwidget.cpp
layertree/qgslayertreeembeddedwidgetregistry.cpp
layertree/qgslayertreeembeddedwidgetsimpl.cpp
layertree/qgslayertreemapcanvasbridge.cpp
layertree/qgslayertreeview.cpp
layertree/qgslayertreeviewdefaultactions.cpp
Expand Down Expand Up @@ -577,7 +578,7 @@ SET(QGIS_GUI_MOC_HDRS

layertree/qgscustomlayerorderwidget.h
layertree/qgslayertreeembeddedconfigwidget.h
layertree/qgslayertreeembeddedwidgetregistry.h
layertree/qgslayertreeembeddedwidgetsimpl.h
layertree/qgslayertreemapcanvasbridge.h
layertree/qgslayertreeview.h
layertree/qgslayertreeviewdefaultactions.h
Expand Down
47 changes: 27 additions & 20 deletions src/gui/layertree/qgslayertreeembeddedconfigwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,68 @@
#include "qgslayertreeembeddedwidgetregistry.h"

#include <QStringListModel>
#include <QStandardItemModel>

QgsLayerTreeEmbeddedConfigWidget::QgsLayerTreeEmbeddedConfigWidget( QgsLayerTreeLayer* nodeLayer, QWidget* parent )
: QWidget( parent )
, mNodeLayer( nodeLayer )
{
setupUi( this );

connect( mBtnAdd, SIGNAL(clicked(bool)), this, SLOT(onAddClicked()));
connect( mBtnRemove, SIGNAL(clicked(bool)), this, SLOT(onRemoveClicked()));
connect( mBtnAdd, SIGNAL( clicked( bool ) ), this, SLOT( onAddClicked() ) );
connect( mBtnRemove, SIGNAL( clicked( bool ) ), this, SLOT( onRemoveClicked() ) );

QStandardItemModel* modelAvailable = new QStandardItemModel( this );
QStandardItemModel* modelUsed = new QStandardItemModel( this );

// populate available
QStringList lst;
Q_FOREACH ( const QString& providerId, QgsLayerTreeEmbeddedWidgetRegistry::instance()->providers() )
{
QgsLayerTreeEmbeddedWidgetProvider* provider = QgsLayerTreeEmbeddedWidgetRegistry::instance()->provider( providerId );
lst << provider->id();
QStandardItem* item = new QStandardItem( provider->name() );
item->setData( provider->id(), Qt::UserRole + 1 );
modelAvailable->appendRow( item );
}
mListAvailable->setModel( new QStringListModel( lst, this ) );
mListAvailable->setModel( modelAvailable );

// populate used
QStringList lstUsed;
int widgetsCount = nodeLayer->customProperty( "embeddedWidgets/count", 0 ).toInt();
for ( int i = 0; i < widgetsCount; ++i )
{
QString providerId = nodeLayer->customProperty( QString( "embeddedWidgets/%1/id" ).arg( i ) ).toString();
if ( /*QgsLayerTreeEmbeddedWidgetProvider* provider =*/ QgsLayerTreeEmbeddedWidgetRegistry::instance()->provider( providerId ) )
if ( QgsLayerTreeEmbeddedWidgetProvider* provider = QgsLayerTreeEmbeddedWidgetRegistry::instance()->provider( providerId ) )
{
lstUsed << providerId;
QStandardItem* item = new QStandardItem( provider->name() );
item->setData( provider->id(), Qt::UserRole + 1 );
modelUsed->appendRow( item );
}
}
mListUsed->setModel( new QStringListModel( lstUsed, this ) );
mListUsed->setModel( modelUsed );
}

void QgsLayerTreeEmbeddedConfigWidget::onAddClicked()
{
if (!mListAvailable->currentIndex().isValid())
if ( !mListAvailable->currentIndex().isValid() )
return;

QString providerId = mListAvailable->model()->data( mListAvailable->currentIndex(), Qt::UserRole + 1 ).toString();
QgsLayerTreeEmbeddedWidgetProvider* provider = QgsLayerTreeEmbeddedWidgetRegistry::instance()->provider( providerId );
if ( !provider )
return;

QString providerId = mListAvailable->model()->data( mListAvailable->currentIndex() ).toString();
if (QStringListModel* model = qobject_cast<QStringListModel*>(mListUsed->model()))
if ( QStandardItemModel* model = qobject_cast<QStandardItemModel*>( mListUsed->model() ) )
{
int row = model->rowCount();
model->insertRow( row );
model->setData( model->index(row, 0), providerId );
//QStringList lst = model->stringList();
//lst << providerId;
//model->setStringList( lst );
QStandardItem* item = new QStandardItem( provider->name() );
item->setData( provider->id(), Qt::UserRole + 1 );
model->appendRow( item );
}

updateCustomProperties();
}

void QgsLayerTreeEmbeddedConfigWidget::onRemoveClicked()
{
if (!mListUsed->currentIndex().isValid())
if ( !mListUsed->currentIndex().isValid() )
return;

int row = mListUsed->currentIndex().row();
Expand All @@ -81,7 +88,7 @@ void QgsLayerTreeEmbeddedConfigWidget::updateCustomProperties()
mNodeLayer->setCustomProperty( "embeddedWidgets/count", newCount );
for ( int i = 0; i < newCount; ++i )
{
QString providerId = mListUsed->model()->data( mListUsed->model()->index(i, 0) ).toString();
QString providerId = mListUsed->model()->data( mListUsed->model()->index( i, 0 ), Qt::UserRole + 1 ).toString();
mNodeLayer->setCustomProperty( QString( "embeddedWidgets/%1/id" ).arg( i ), providerId );
}
}
105 changes: 9 additions & 96 deletions src/gui/layertree/qgslayertreeembeddedwidgetregistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,22 @@

#include "qgslayertreeembeddedwidgetregistry.h"

#include "qgslayertreeembeddedwidgetsimpl.h"

#include "qgsmaplayer.h"


class TransparencyWidgetProvider : public QgsLayerTreeEmbeddedWidgetProvider
{
public:
virtual QString id() const override
{
return "transparency";
}

virtual TransparencyWidget* createWidget( QgsMapLayer* layer, QMap<QString, QString> properties ) override
{
Q_UNUSED( properties );
return new TransparencyWidget( layer );
}

virtual bool supportsLayer( QgsMapLayer *layer ) override
{
return layer->type() == QgsMapLayer::VectorLayer || layer->type() == QgsMapLayer::RasterLayer;
}
};


/////


QgsLayerTreeEmbeddedWidgetRegistry*QgsLayerTreeEmbeddedWidgetRegistry::instance()
QgsLayerTreeEmbeddedWidgetRegistry* QgsLayerTreeEmbeddedWidgetRegistry::instance()
{
static QgsLayerTreeEmbeddedWidgetRegistry* sInstance( new QgsLayerTreeEmbeddedWidgetRegistry() );
return sInstance;
}

QgsLayerTreeEmbeddedWidgetRegistry::QgsLayerTreeEmbeddedWidgetRegistry()
{
// populate with default implementations
addProvider( new QgsLayerTreeTransparencyWidget::Provider() );
}

QgsLayerTreeEmbeddedWidgetRegistry::~QgsLayerTreeEmbeddedWidgetRegistry()
{
Q_FOREACH ( QgsLayerTreeEmbeddedWidgetProvider* provider, mProviders )
Expand All @@ -62,7 +44,7 @@ QStringList QgsLayerTreeEmbeddedWidgetRegistry::providers() const
return mProviders.keys();
}

QgsLayerTreeEmbeddedWidgetProvider*QgsLayerTreeEmbeddedWidgetRegistry::provider( const QString& providerId ) const
QgsLayerTreeEmbeddedWidgetProvider* QgsLayerTreeEmbeddedWidgetRegistry::provider( const QString& providerId ) const
{
return mProviders.value( providerId );
}
Expand All @@ -84,72 +66,3 @@ bool QgsLayerTreeEmbeddedWidgetRegistry::removeProvider( const QString& provider
delete mProviders.take( providerId );
return true;
}

QgsLayerTreeEmbeddedWidgetRegistry::QgsLayerTreeEmbeddedWidgetRegistry()
{
addProvider( new TransparencyWidgetProvider() );
}


/////

#include <QLabel>
#include <QSlider>
#include <QHBoxLayout>
#include "qgsrasterlayer.h"
#include "qgsrasterrenderer.h"
#include "qgsvectorlayer.h"

TransparencyWidget::TransparencyWidget( QgsMapLayer* layer )
: mLayer( layer )
{
setAutoFillBackground( true ); // override the content from model
QLabel* l = new QLabel( "Transparency", this );
mSlider = new QSlider( Qt::Horizontal, this );
mSlider->setRange( 0, 100 );
QHBoxLayout* lay = new QHBoxLayout();
lay->addWidget( l );
lay->addWidget( mSlider );
setLayout( lay );

connect( mSlider, SIGNAL( valueChanged( int ) ), this, SLOT( sliderValueChanged( int ) ) );

// init from layer
if ( mLayer->type() == QgsMapLayer::VectorLayer )
{
mSlider->setValue( qobject_cast<QgsVectorLayer*>( mLayer )->layerTransparency() );
connect( mLayer, SIGNAL( layerTransparencyChanged( int ) ), this, SLOT( layerTrChanged() ) );
}
else if ( mLayer->type() == QgsMapLayer::RasterLayer )
{
mSlider->setValue( 100 - qobject_cast<QgsRasterLayer*>( mLayer )->renderer()->opacity() * 100 );
// TODO: there is no signal for raster layers
}
}

QSize TransparencyWidget::sizeHint() const
{
return QWidget::sizeHint();
//return QSize(200,200); // horizontal seems ignored, vertical is used for spacing
}

void TransparencyWidget::sliderValueChanged( int value )
{
if ( mLayer->type() == QgsMapLayer::VectorLayer )
{
qobject_cast<QgsVectorLayer*>( mLayer )->setLayerTransparency( value );
}
else if ( mLayer->type() == QgsMapLayer::RasterLayer )
{
qobject_cast<QgsRasterLayer*>( mLayer )->renderer()->setOpacity( 1 - value / 100. );
}

mLayer->triggerRepaint();
}

void TransparencyWidget::layerTrChanged()
{
mSlider->blockSignals( true );
mSlider->setValue( qobject_cast<QgsVectorLayer*>( mLayer )->layerTransparency() );
mSlider->blockSignals( false );
}
37 changes: 3 additions & 34 deletions src/gui/layertree/qgslayertreeembeddedwidgetregistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@
#include <QMap>
#include <QWidget>

#if 0
/** Base class for embedded widgets */
class QgsLayerTreeEmbeddedWidget : public QWidget
{
public:

};
#endif

class QgsMapLayer;

Expand All @@ -39,6 +31,9 @@ class GUI_EXPORT QgsLayerTreeEmbeddedWidgetProvider
//! unique name of the provider (among other providers)
virtual QString id() const = 0;

//! human readable name - may be translatable with tr()
virtual QString name() const = 0;

//! factory to create widgets
virtual QWidget* createWidget( QgsMapLayer* layer, QMap<QString, QString> properties ) = 0;

Expand Down Expand Up @@ -79,30 +74,4 @@ class GUI_EXPORT QgsLayerTreeEmbeddedWidgetRegistry
};


////


// TMP
class QSlider;
class QgsMapLayer;
#include <QWidget>
class TransparencyWidget : public QWidget
{
Q_OBJECT
public:
TransparencyWidget( QgsMapLayer* layer );

virtual QSize sizeHint() const override;

public slots:
void sliderValueChanged( int value );
void layerTrChanged();

private:
QgsMapLayer* mLayer;
QSlider* mSlider;
};



#endif // QGSLAYERTREEEMBEDDEDWIDGETREGISTRY_H
Loading

0 comments on commit c780949

Please sign in to comment.