QGIS API Documentation  2.11.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 {
86 
87  mLayer = layer;
88  mSymbol = symbol;
89  mVectorLayer = vl;
90 
91  setupUi( this );
92  // initialize the sub-widgets
93  // XXX Should this thing be here this way? Initialize all the widgets just for the sake of one layer?
94  // TODO Make this on demand creation
96 
97  // TODO Algorithm
98  //
99  // 3. populate the combo box with the supported layer type
100  // 4. set the present layer type
101  // 5. create the widget for the present layer type and set inn stacked widget
102  // 6. connect comboBox type changed to two things
103  // 1. emit signal that type has beed changed
104  // 2. remove the widget and place the new widget corresponding to the changed layer type
105  //
107  // update layer type combo box
108  int idx = cboLayerType->findData( mLayer->layerType() );
109  cboLayerType->setCurrentIndex( idx );
110  // set the corresponding widget
111  updateSymbolLayerWidget( layer );
112  connect( cboLayerType, SIGNAL( currentIndexChanged( int ) ), this, SLOT( layerTypeChanged() ) );
113 
114  connect( mEffectWidget, SIGNAL( changed() ), this, SLOT( emitSignalChanged() ) );
115  mEffectWidget->setPaintEffect( mLayer->paintEffect() );
116 }
117 
119 {
121 
122  for ( int i = 0; i < types.count(); i++ )
123  cboLayerType->addItem( QgsSymbolLayerV2Registry::instance()->symbolLayerMetadata( types[i] )->visibleName(), types[i] );
124 
125  if ( mSymbol->type() == QgsSymbolV2::Fill )
126  {
128  for ( int i = 0; i < typesLine.count(); i++ )
129  {
131  QString name = QString( tr( "Outline: %1" ) ).arg( visibleName );
132  cboLayerType->addItem( name, typesLine[i] );
133  }
134  }
135 
136 }
137 
139 {
140  if ( stackedWidget->currentWidget() != pageDummy )
141  {
142  // stop updating from the original widget
143  disconnect( stackedWidget->currentWidget(), SIGNAL( changed() ), this, SLOT( emitSignalChanged() ) );
144  stackedWidget->removeWidget( stackedWidget->currentWidget() );
145  }
146 
148 
149  QString layerType = layer->layerType();
150  QgsSymbolLayerV2AbstractMetadata* am = pReg->symbolLayerMetadata( layerType );
151  if ( am )
152  {
154  if ( w )
155  {
156  w->setSymbolLayer( layer );
157  stackedWidget->addWidget( w );
158  stackedWidget->setCurrentWidget( w );
159  // start receiving updates from widget
160  connect( w, SIGNAL( changed() ), this, SLOT( emitSignalChanged() ) );
161  return;
162  }
163  }
164  // When anything is not right
165  stackedWidget->setCurrentWidget( pageDummy );
166 }
167 
169 {
170  QgsSymbolLayerV2* layer = mLayer;
171  if ( !layer )
172  return;
173  QString newLayerType = cboLayerType->itemData( cboLayerType->currentIndex() ).toString();
174  if ( layer->layerType() == newLayerType )
175  return;
176 
177  // get creation function for new layer from registry
179  QgsSymbolLayerV2AbstractMetadata* am = pReg->symbolLayerMetadata( newLayerType );
180  if ( am == NULL ) // check whether the metadata is assigned
181  return;
182 
183  // change layer to a new (with different type)
184  // base new layer on existing layer's properties
185  QgsSymbolLayerV2* newLayer = am->createSymbolLayer( layer->properties() );
186  if ( newLayer == NULL )
187  return;
188 
189  updateSymbolLayerWidget( newLayer );
190  emit changeLayer( newLayer );
191 }
192 
194 {
195  emit changed();
196 
197  // also update paint effect preview
198  mEffectWidget->setPreviewPicture( QgsSymbolLayerV2Utils::symbolLayerPreviewPicture( mLayer, QgsSymbolV2::MM, QSize( 80, 80 ) ) );
199 }
virtual void setSymbolLayer(QgsSymbolLayerV2 *layer)=0
static QgsSymbolLayerV2Widget * create(const QgsVectorLayer *vl)
void setupUi(QWidget *widget)
SymbolType type() const
Definition: qgssymbolv2.h:86
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
QgsSymbolLayerV2AbstractMetadata * symbolLayerMetadata(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)
static QgsSymbolLayerV2Widget * create(const QgsVectorLayer *vl)
Stores metadata about one symbol layer class.
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)
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)
QgsSymbolLayerV2Widget *(* QgsSymbolLayerV2WidgetFunc)(const QgsVectorLayer *)
const char * name() const
int count(const T &value) const
static QgsSymbolLayerV2Widget * create(const QgsVectorLayer *vl)
void changeLayer(QgsSymbolLayerV2 *)
static bool _initWidgetFunction(QString name, QgsSymbolLayerV2WidgetFunc f)
virtual QgsSymbolLayerV2Widget * createSymbolLayerWidget(const QgsVectorLayer *)
Create widget for symbol layer of this type.
static QgsSymbolLayerV2Widget * create(const QgsVectorLayer *vl)
virtual QgsStringMap properties() const =0
static QgsSymbolLayerV2Widget * create(const QgsVectorLayer *vl)
static QgsSymbolLayerV2Widget * create(const QgsVectorLayer *vl)
virtual QString layerType() const =0
static QgsSymbolLayerV2Widget * create(const QgsVectorLayer *vl)
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.
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 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.
const QgsVectorLayer * mVectorLayer