QGIS API Documentation  2.12.0-Lyon
qgslayerpropertieswidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayerpropertieswidget.cpp
3  ----------------------------
4  begin : June 2012
5  copyright : (C) 2012 by Arunmozhi
6  email : aruntheguy at gmail.com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
17 
18 #include <QFile>
19 #include <QStandardItem>
20 #include <QKeyEvent>
21 #include <QMessageBox>
22 #include <QPicture>
23 
24 #include "qgssymbollayerv2.h"
26 
27 #include "qgsapplication.h"
28 #include "qgslogger.h"
29 
30 #include "qgssymbollayerv2widget.h"
33 #include "qgssymbolv2.h" //for the unit
34 
36 {
38 
39  QgsSymbolLayerV2AbstractMetadata* abstractMetadata = reg->symbolLayerMetadata( name );
40  if ( abstractMetadata == NULL )
41  {
42  QgsDebugMsg( "Failed to find symbol layer's entry in registry: " + name );
43  return false;
44  }
45  QgsSymbolLayerV2Metadata* metadata = dynamic_cast<QgsSymbolLayerV2Metadata*>( abstractMetadata );
46  if ( metadata == NULL )
47  {
48  QgsDebugMsg( "Failed to cast symbol layer's metadata: " + name );
49  return false;
50  }
51  metadata->setWidgetFunction( f );
52  return true;
53 }
54 
55 static void _initWidgetFunctions()
56 {
57  static bool initialized = false;
58  if ( initialized )
59  return;
60 
63 
69 
78 
79  initialized = true;
80 }
81 
82 
84  : QWidget( parent )
85  , mPresetExpressionContext( 0 )
86  , mMapCanvas( 0 )
87 {
88 
89  mLayer = layer;
90  mSymbol = symbol;
91  mVectorLayer = vl;
92 
93  setupUi( this );
94  // initialize the sub-widgets
95  // XXX Should this thing be here this way? Initialize all the widgets just for the sake of one layer?
96  // TODO Make this on demand creation
98 
99  // TODO Algorithm
100  //
101  // 3. populate the combo box with the supported layer type
102  // 4. set the present layer type
103  // 5. create the widget for the present layer type and set inn stacked widget
104  // 6. connect comboBox type changed to two things
105  // 1. emit signal that type has beed changed
106  // 2. remove the widget and place the new widget corresponding to the changed layer type
107  //
109  // update layer type combo box
110  int idx = cboLayerType->findData( mLayer->layerType() );
111  cboLayerType->setCurrentIndex( idx );
112  // set the corresponding widget
113  updateSymbolLayerWidget( layer );
114  connect( cboLayerType, SIGNAL( currentIndexChanged( int ) ), this, SLOT( layerTypeChanged() ) );
115 
116  connect( mEffectWidget, SIGNAL( changed() ), this, SLOT( emitSignalChanged() ) );
117  mEffectWidget->setPaintEffect( mLayer->paintEffect() );
118 }
119 
121 {
122  mMapCanvas = canvas;
123  QgsSymbolLayerV2Widget* w = dynamic_cast< QgsSymbolLayerV2Widget* >( stackedWidget->currentWidget() );
124  if ( w )
125  w->setMapCanvas( mMapCanvas );
126 }
127 
129 {
130  mPresetExpressionContext = context;
131 
132  QgsSymbolLayerV2Widget* w = dynamic_cast< QgsSymbolLayerV2Widget* >( stackedWidget->currentWidget() );
133  if ( w )
134  w->setExpressionContext( mPresetExpressionContext );
135 }
136 
138 {
140 
141  for ( int i = 0; i < types.count(); i++ )
142  cboLayerType->addItem( QgsSymbolLayerV2Registry::instance()->symbolLayerMetadata( types[i] )->visibleName(), types[i] );
143 
144  if ( mSymbol->type() == QgsSymbolV2::Fill )
145  {
147  for ( int i = 0; i < typesLine.count(); i++ )
148  {
150  QString name = QString( tr( "Outline: %1" ) ).arg( visibleName );
151  cboLayerType->addItem( name, typesLine[i] );
152  }
153  }
154 
155 }
156 
158 {
159  if ( stackedWidget->currentWidget() != pageDummy )
160  {
161  // stop updating from the original widget
162  disconnect( stackedWidget->currentWidget(), SIGNAL( changed() ), this, SLOT( emitSignalChanged() ) );
163  stackedWidget->removeWidget( stackedWidget->currentWidget() );
164  }
165 
167 
168  QString layerType = layer->layerType();
169  QgsSymbolLayerV2AbstractMetadata* am = pReg->symbolLayerMetadata( layerType );
170  if ( am )
171  {
173  if ( w )
174  {
175  w->setSymbolLayer( layer );
176  w->setExpressionContext( mPresetExpressionContext );
177  if ( mMapCanvas )
178  w->setMapCanvas( mMapCanvas );
179  stackedWidget->addWidget( w );
180  stackedWidget->setCurrentWidget( w );
181  // start receiving updates from widget
182  connect( w, SIGNAL( changed() ), this, SLOT( emitSignalChanged() ) );
183  return;
184  }
185  }
186  // When anything is not right
187  stackedWidget->setCurrentWidget( pageDummy );
188 }
189 
191 {
192  QgsSymbolLayerV2* layer = mLayer;
193  if ( !layer )
194  return;
195  QString newLayerType = cboLayerType->itemData( cboLayerType->currentIndex() ).toString();
196  if ( layer->layerType() == newLayerType )
197  return;
198 
199  // get creation function for new layer from registry
201  QgsSymbolLayerV2AbstractMetadata* am = pReg->symbolLayerMetadata( newLayerType );
202  if ( am == NULL ) // check whether the metadata is assigned
203  return;
204 
205  // change layer to a new (with different type)
206  // base new layer on existing layer's properties
207  QgsSymbolLayerV2* newLayer = am->createSymbolLayer( layer->properties() );
208  if ( newLayer == NULL )
209  return;
210 
211  updateSymbolLayerWidget( newLayer );
212  emit changeLayer( newLayer );
213 }
214 
216 {
217  emit changed();
218 
219  // also update paint effect preview
220  mEffectWidget->setPreviewPicture( QgsSymbolLayerV2Utils::symbolLayerPreviewPicture( mLayer, QgsSymbolV2::MM, QSize( 80, 80 ) ) );
221 }
virtual void setSymbolLayer(QgsSymbolLayerV2 *layer)=0
static QgsSymbolLayerV2Widget * create(const QgsVectorLayer *vl)
void setupUi(QWidget *widget)
SymbolType type() const
Definition: qgssymbolv2.h:95
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
static QgsSymbolLayerV2Widget * create(const QgsVectorLayer *vl)
static QgsSymbolLayerV2Widget * create(const QgsVectorLayer *vl)
static QgsSymbolLayerV2Widget * create(const QgsVectorLayer *vl)
Stores metadata about one symbol layer class.
Line symbol.
Definition: qgssymbolv2.h:71
static QgsSymbolLayerV2Registry * instance()
return the single instance of this class (instantiate it if not exists)
static QgsSymbolLayerV2Widget * create(const QgsVectorLayer *vl)
static QgsSymbolLayerV2Widget * create(const QgsVectorLayer *vl)
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
QString tr(const char *sourceText, const char *disambiguation, int n)
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:107
QgsLayerPropertiesWidget(QgsSymbolLayerV2 *layer, const QgsSymbolV2 *symbol, const QgsVectorLayer *vl, QWidget *parent=NULL)
static QgsSymbolLayerV2Widget * create(const QgsVectorLayer *vl)
static void _initWidgetFunctions()
static QgsSymbolLayerV2Widget * create(const QgsVectorLayer *vl)
virtual void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
const char * name() const
The output shall be in millimeters.
Definition: qgssymbolv2.h:57
int count(const T &value) const
static QgsSymbolLayerV2Widget * create(const QgsVectorLayer *vl)
void changeLayer(QgsSymbolLayerV2 *)
void setExpressionContext(QgsExpressionContext *context)
Sets the optional expression context used for the widget.
virtual QgsSymbolLayerV2Widget * createSymbolLayerWidget(const QgsVectorLayer *)
Create widget for symbol layer of this type.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
static QgsSymbolLayerV2Widget * create(const QgsVectorLayer *vl)
virtual void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
virtual QgsStringMap properties() const =0
QgsSymbolLayerV2AbstractMetadata * symbolLayerMetadata(const QString &name) const
return metadata for specified symbol layer. Returns NULL if not found
static QgsSymbolLayerV2Widget * create(const QgsVectorLayer *vl)
static QgsSymbolLayerV2Widget * create(const QgsVectorLayer *vl)
virtual QString layerType() const =0
static QgsSymbolLayerV2Widget * create(const QgsVectorLayer *vl)
QgsSymbolLayerV2Widget *(* QgsSymbolLayerV2WidgetFunc)(const QgsVectorLayer *)
QStringList symbolLayersForType(QgsSymbolV2::SymbolType type)
return a list of available symbol layers for a specified symbol type
virtual QgsSymbolLayerV2 * createSymbolLayer(const QgsStringMap &map)=0
Create a symbol layer of this type given the map of properties.
Fill symbol.
Definition: qgssymbolv2.h:72
static QgsSymbolLayerV2Widget * create(const QgsVectorLayer *vl)
static QPicture symbolLayerPreviewPicture(QgsSymbolLayerV2 *layer, QgsSymbolV2::OutputUnit units, QSize size, const QgsMapUnitScale &scale=QgsMapUnitScale())
Draws a symbol layer preview to a QPicture.
void updateSymbolLayerWidget(QgsSymbolLayerV2 *layer)
void setWidgetFunction(QgsSymbolLayerV2WidgetFunc f)
not available in python bindings
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Represents a vector layer which manages a vector based data sets.
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
Registry of available symbol layer classes.
static bool _initWidgetFunction(const QString &name, QgsSymbolLayerV2WidgetFunc f)
static QgsSymbolLayerV2Widget * create(const QgsVectorLayer *vl)
Convenience metadata class that uses static functions to create symbol layer and its widget...
QgsPaintEffect * paintEffect() const
Returns the current paint effect for the layer.
void setExpressionContext(QgsExpressionContext *context)
Sets the optional expression context used for the widget.
const QgsVectorLayer * mVectorLayer