QGIS API Documentation  2.13.0-Master
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 )
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 )
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 
80 
81  initialized = true;
82 }
83 
84 
86  : QWidget( parent )
87  , mPresetExpressionContext( nullptr )
88  , mMapCanvas( nullptr )
89 {
90 
91  mLayer = layer;
92  mSymbol = symbol;
93  mVectorLayer = vl;
94 
95  setupUi( this );
96  // initialize the sub-widgets
97  // XXX Should this thing be here this way? Initialize all the widgets just for the sake of one layer?
98  // TODO Make this on demand creation
100 
101  // TODO Algorithm
102  //
103  // 3. populate the combo box with the supported layer type
104  // 4. set the present layer type
105  // 5. create the widget for the present layer type and set inn stacked widget
106  // 6. connect comboBox type changed to two things
107  // 1. emit signal that type has beed changed
108  // 2. remove the widget and place the new widget corresponding to the changed layer type
109  //
111  // update layer type combo box
112  int idx = cboLayerType->findData( mLayer->layerType() );
113  cboLayerType->setCurrentIndex( idx );
114  // set the corresponding widget
115  updateSymbolLayerWidget( layer );
116  connect( cboLayerType, SIGNAL( currentIndexChanged( int ) ), this, SLOT( layerTypeChanged() ) );
117 
118  connect( mEffectWidget, SIGNAL( changed() ), this, SLOT( emitSignalChanged() ) );
119  mEffectWidget->setPaintEffect( mLayer->paintEffect() );
120 }
121 
123 {
124  mMapCanvas = canvas;
125  QgsSymbolLayerV2Widget* w = dynamic_cast< QgsSymbolLayerV2Widget* >( stackedWidget->currentWidget() );
126  if ( w )
127  w->setMapCanvas( mMapCanvas );
128 }
129 
131 {
132  mPresetExpressionContext = context;
133 
134  QgsSymbolLayerV2Widget* w = dynamic_cast< QgsSymbolLayerV2Widget* >( stackedWidget->currentWidget() );
135  if ( w )
136  w->setExpressionContext( mPresetExpressionContext );
137 }
138 
140 {
142 
143  Q_FOREACH ( const QString& symbolLayerId, symbolLayerIds )
144  cboLayerType->addItem( QgsSymbolLayerV2Registry::instance()->symbolLayerMetadata( symbolLayerId )->visibleName(), symbolLayerId );
145 
146  if ( mSymbol->type() == QgsSymbolV2::Fill )
147  {
149  Q_FOREACH ( const QString& lineLayerId, lineLayerIds )
150  {
152  if ( layerInfo->type() != QgsSymbolV2::Hybrid )
153  {
154  QString visibleName = layerInfo->visibleName();
155  QString name = QString( tr( "Outline: %1" ) ).arg( visibleName );
156  cboLayerType->addItem( name, lineLayerId );
157  }
158  }
159  }
160 }
161 
163 {
164  if ( stackedWidget->currentWidget() != pageDummy )
165  {
166  // stop updating from the original widget
167  disconnect( stackedWidget->currentWidget(), SIGNAL( changed() ), this, SLOT( emitSignalChanged() ) );
168  stackedWidget->removeWidget( stackedWidget->currentWidget() );
169  }
170 
172 
173  QString layerType = layer->layerType();
174  QgsSymbolLayerV2AbstractMetadata* am = pReg->symbolLayerMetadata( layerType );
175  if ( am )
176  {
178  if ( w )
179  {
180  w->setExpressionContext( mPresetExpressionContext );
181  if ( mMapCanvas )
182  w->setMapCanvas( mMapCanvas );
183  w->setSymbolLayer( layer );
184  stackedWidget->addWidget( w );
185  stackedWidget->setCurrentWidget( w );
186  // start receiving updates from widget
187  connect( w, SIGNAL( changed() ), this, SLOT( emitSignalChanged() ) );
188  connect( w, SIGNAL( symbolChanged() ), this, SLOT( reloadLayer() ) );
189  return;
190  }
191  }
192  // When anything is not right
193  stackedWidget->setCurrentWidget( pageDummy );
194 }
195 
197 {
198  QgsSymbolLayerV2* layer = mLayer;
199  if ( !layer )
200  return;
201  QString newLayerType = cboLayerType->itemData( cboLayerType->currentIndex() ).toString();
202  if ( layer->layerType() == newLayerType )
203  return;
204 
205  // get creation function for new layer from registry
207  QgsSymbolLayerV2AbstractMetadata* am = pReg->symbolLayerMetadata( newLayerType );
208  if ( !am ) // check whether the metadata is assigned
209  return;
210 
211  // change layer to a new (with different type)
212  // base new layer on existing layer's properties
213  QgsSymbolLayerV2* newLayer = am->createSymbolLayer( layer->properties() );
214  if ( !newLayer )
215  return;
216 
217  updateSymbolLayerWidget( newLayer );
218  emit changeLayer( newLayer );
219 }
220 
222 {
223  emit changed();
224 
225  // also update paint effect preview
226  mEffectWidget->setPreviewPicture( QgsSymbolLayerV2Utils::symbolLayerPreviewPicture( mLayer, QgsSymbolV2::MM, QSize( 80, 80 ) ) );
227 }
228 
229 void QgsLayerPropertiesWidget::reloadLayer()
230 {
231  emit changeLayer( mLayer );
232 }
virtual void setSymbolLayer(QgsSymbolLayerV2 *layer)=0
QgsSymbolV2::SymbolType type() const
static QgsSymbolLayerV2Widget * create(const QgsVectorLayer *vl)
void setupUi(QWidget *widget)
SymbolType type() const
Definition: qgssymbolv2.h:101
#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.
QgsLayerPropertiesWidget(QgsSymbolLayerV2 *layer, const QgsSymbolV2 *symbol, const QgsVectorLayer *vl, QWidget *parent=nullptr)
Line symbol.
Definition: qgssymbolv2.h:76
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:105
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:61
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
Should be reimplemented by subclasses to return a string map that contains the configuration informat...
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
Returns a string that represents this layer type.
static QgsSymbolLayerV2Widget * create(const QgsVectorLayer *vl)
QgsSymbolLayerV2Widget *(* QgsSymbolLayerV2WidgetFunc)(const QgsVectorLayer *)
Hybrid symbol.
Definition: qgssymbolv2.h:78
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:77
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)
static QgsSymbolLayerV2Widget * create(const QgsVectorLayer *vl)
Will be registered as factory.
void setWidgetFunction(QgsSymbolLayerV2WidgetFunc f)
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