QGIS API Documentation  2.15.0-Master (af20121)
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"
34 #include "qgssymbolv2.h" //for the unit
35 
37 {
39 
40  QgsSymbolLayerV2AbstractMetadata* abstractMetadata = reg->symbolLayerMetadata( name );
41  if ( !abstractMetadata )
42  {
43  QgsDebugMsg( "Failed to find symbol layer's entry in registry: " + name );
44  return false;
45  }
46  QgsSymbolLayerV2Metadata* metadata = dynamic_cast<QgsSymbolLayerV2Metadata*>( abstractMetadata );
47  if ( !metadata )
48  {
49  QgsDebugMsg( "Failed to cast symbol layer's metadata: " + name );
50  return false;
51  }
52  metadata->setWidgetFunction( f );
53  return true;
54 }
55 
56 static void _initWidgetFunctions()
57 {
58  static bool initialized = false;
59  if ( initialized )
60  return;
61 
65 
72 
81 
83 
84  initialized = true;
85 }
86 
87 
89  : QWidget( parent )
90  , mPresetExpressionContext( nullptr )
91  , mMapCanvas( nullptr )
92 {
93 
94  mLayer = layer;
95  mSymbol = symbol;
96  mVectorLayer = vl;
97 
98  setupUi( this );
99  // initialize the sub-widgets
100  // XXX Should this thing be here this way? Initialize all the widgets just for the sake of one layer?
101  // TODO Make this on demand creation
103 
104  // TODO Algorithm
105  //
106  // 3. populate the combo box with the supported layer type
107  // 4. set the present layer type
108  // 5. create the widget for the present layer type and set inn stacked widget
109  // 6. connect comboBox type changed to two things
110  // 1. emit signal that type has beed changed
111  // 2. remove the widget and place the new widget corresponding to the changed layer type
112  //
114  // update layer type combo box
115  int idx = cboLayerType->findData( mLayer->layerType() );
116  cboLayerType->setCurrentIndex( idx );
117  // set the corresponding widget
118  updateSymbolLayerWidget( layer );
119  connect( cboLayerType, SIGNAL( currentIndexChanged( int ) ), this, SLOT( layerTypeChanged() ) );
120 
121  connect( mEffectWidget, SIGNAL( changed() ), this, SLOT( emitSignalChanged() ) );
122  mEffectWidget->setPaintEffect( mLayer->paintEffect() );
123 }
124 
126 {
127  mMapCanvas = canvas;
128  QgsSymbolLayerV2Widget* w = dynamic_cast< QgsSymbolLayerV2Widget* >( stackedWidget->currentWidget() );
129  if ( w )
130  w->setMapCanvas( mMapCanvas );
131 }
132 
134 {
135  mPresetExpressionContext = context;
136 
137  QgsSymbolLayerV2Widget* w = dynamic_cast< QgsSymbolLayerV2Widget* >( stackedWidget->currentWidget() );
138  if ( w )
139  w->setExpressionContext( mPresetExpressionContext );
140 }
141 
143 {
145 
146  Q_FOREACH ( const QString& symbolLayerId, symbolLayerIds )
147  cboLayerType->addItem( QgsSymbolLayerV2Registry::instance()->symbolLayerMetadata( symbolLayerId )->visibleName(), symbolLayerId );
148 
149  if ( mSymbol->type() == QgsSymbolV2::Fill )
150  {
152  Q_FOREACH ( const QString& lineLayerId, lineLayerIds )
153  {
155  if ( layerInfo->type() != QgsSymbolV2::Hybrid )
156  {
157  QString visibleName = layerInfo->visibleName();
158  QString name = QString( tr( "Outline: %1" ) ).arg( visibleName );
159  cboLayerType->addItem( name, lineLayerId );
160  }
161  }
162  }
163 }
164 
166 {
167  if ( stackedWidget->currentWidget() != pageDummy )
168  {
169  // stop updating from the original widget
170  disconnect( stackedWidget->currentWidget(), SIGNAL( changed() ), this, SLOT( emitSignalChanged() ) );
171  stackedWidget->removeWidget( stackedWidget->currentWidget() );
172  }
173 
175 
176  QString layerType = layer->layerType();
177  QgsSymbolLayerV2AbstractMetadata* am = pReg->symbolLayerMetadata( layerType );
178  if ( am )
179  {
181  if ( w )
182  {
183  w->setExpressionContext( mPresetExpressionContext );
184  if ( mMapCanvas )
185  w->setMapCanvas( mMapCanvas );
186  w->setSymbolLayer( layer );
187  stackedWidget->addWidget( w );
188  stackedWidget->setCurrentWidget( w );
189  // start receiving updates from widget
190  connect( w, SIGNAL( changed() ), this, SLOT( emitSignalChanged() ) );
191  connect( w, SIGNAL( symbolChanged() ), this, SLOT( reloadLayer() ) );
192  return;
193  }
194  }
195  // When anything is not right
196  stackedWidget->setCurrentWidget( pageDummy );
197 }
198 
200 {
201  QgsSymbolLayerV2* layer = mLayer;
202  if ( !layer )
203  return;
204  QString newLayerType = cboLayerType->itemData( cboLayerType->currentIndex() ).toString();
205  if ( layer->layerType() == newLayerType )
206  return;
207 
208  // get creation function for new layer from registry
210  QgsSymbolLayerV2AbstractMetadata* am = pReg->symbolLayerMetadata( newLayerType );
211  if ( !am ) // check whether the metadata is assigned
212  return;
213 
214  // change layer to a new (with different type)
215  // base new layer on existing layer's properties
216  QgsSymbolLayerV2* newLayer = am->createSymbolLayer( layer->properties() );
217  if ( !newLayer )
218  return;
219 
220  updateSymbolLayerWidget( newLayer );
221  emit changeLayer( newLayer );
222 }
223 
225 {
226  emit changed();
227 
228  // also update paint effect preview
229  mEffectWidget->setPreviewPicture( QgsSymbolLayerV2Utils::symbolLayerPreviewPicture( mLayer, QgsSymbolV2::MM, QSize( 80, 80 ) ) );
230 }
231 
232 void QgsLayerPropertiesWidget::reloadLayer()
233 {
234  emit changeLayer( mLayer );
235 }
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:104
#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:79
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:108
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:64
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:81
QStringList symbolLayersForType(QgsSymbolV2::SymbolType type)
return a list of available symbol layers for a specified symbol type
static QgsSymbolLayerV2Widget * create(const QgsVectorLayer *layer)
Static creation method.
virtual QgsSymbolLayerV2 * createSymbolLayer(const QgsStringMap &map)=0
Create a symbol layer of this type given the map of properties.
Fill symbol.
Definition: qgssymbolv2.h:80
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.
static QgsSymbolLayerV2Widget * create(const QgsVectorLayer *vl)
Creates a new QgsFilledMarkerSymbolLayerWidget.
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