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