QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 
23 #include "qgssymbollayerv2.h"
25 
26 #include "qgsapplication.h"
27 #include "qgslogger.h"
28 
29 #include "qgssymbollayerv2widget.h"
32 #include "qgssymbolv2.h" //for the unit
33 
34 static bool _initWidgetFunction( QString name, QgsSymbolLayerV2WidgetFunc f )
35 {
37 
38  QgsSymbolLayerV2AbstractMetadata* abstractMetadata = reg->symbolLayerMetadata( name );
39  if ( abstractMetadata == NULL )
40  {
41  QgsDebugMsg( "Failed to find symbol layer's entry in registry: " + name );
42  return false;
43  }
44  QgsSymbolLayerV2Metadata* metadata = dynamic_cast<QgsSymbolLayerV2Metadata*>( abstractMetadata );
45  if ( metadata == NULL )
46  {
47  QgsDebugMsg( "Failed to cast symbol layer's metadata: " + name );
48  return false;
49  }
50  metadata->setWidgetFunction( f );
51  return true;
52 }
53 
54 static void _initWidgetFunctions()
55 {
56  static bool initialized = false;
57  if ( initialized )
58  return;
59 
62 
68 
77 
78  initialized = true;
79 }
80 
81 
83  : QWidget( parent )
84 {
85 
86  mLayer = layer;
87  mSymbol = symbol;
88  mVectorLayer = vl;
89 
90  setupUi( this );
91  // initialize the sub-widgets
92  // XXX Should this thing be here this way? Initialize all the widgets just for the sake of one layer?
93  // TODO Make this on demand creation
95 
96  // TODO Algorithm
97  //
98  // 3. populate the combo box with the supported layer type
99  // 4. set the present layer type
100  // 5. create the widget for the present layer type and set inn stacked widget
101  // 6. connect comboBox type changed to two things
102  // 1. emit signal that type has beed changed
103  // 2. remove the widget and place the new widget corresponding to the changed layer type
104  //
106  // update layer type combo box
107  int idx = cboLayerType->findData( mLayer->layerType() );
108  cboLayerType->setCurrentIndex( idx );
109  // set the corresponding widget
110  updateSymbolLayerWidget( layer );
111  connect( cboLayerType, SIGNAL( currentIndexChanged( int ) ), this, SLOT( layerTypeChanged() ) );
112 }
113 
115 {
117 
118  for ( int i = 0; i < types.count(); i++ )
119  cboLayerType->addItem( QgsSymbolLayerV2Registry::instance()->symbolLayerMetadata( types[i] )->visibleName(), types[i] );
120 
121  if ( mSymbol->type() == QgsSymbolV2::Fill )
122  {
124  for ( int i = 0; i < typesLine.count(); i++ )
125  {
126  QString visibleName = QgsSymbolLayerV2Registry::instance()->symbolLayerMetadata( typesLine[i] )->visibleName();
127  QString name = QString( tr( "Outline: %1" ) ).arg( visibleName );
128  cboLayerType->addItem( name, typesLine[i] );
129  }
130  }
131 
132 }
133 
135 {
136  if ( stackedWidget->currentWidget() != pageDummy )
137  {
138  // stop updating from the original widget
139  disconnect( stackedWidget->currentWidget(), SIGNAL( changed() ), this, SLOT( emitSignalChanged() ) );
140  stackedWidget->removeWidget( stackedWidget->currentWidget() );
141  }
142 
144 
145  QString layerType = layer->layerType();
146  QgsSymbolLayerV2AbstractMetadata* am = pReg->symbolLayerMetadata( layerType );
147  if ( am )
148  {
150  if ( w )
151  {
152  w->setSymbolLayer( layer );
153  stackedWidget->addWidget( w );
154  stackedWidget->setCurrentWidget( w );
155  // start receiving updates from widget
156  connect( w, SIGNAL( changed() ), this, SLOT( emitSignalChanged() ) );
157  return;
158  }
159  }
160  // When anything is not right
161  stackedWidget->setCurrentWidget( pageDummy );
162 }
163 
165 {
166  QgsSymbolLayerV2* layer = mLayer;
167  if ( !layer )
168  return;
169  QString newLayerType = cboLayerType->itemData( cboLayerType->currentIndex() ).toString();
170  if ( layer->layerType() == newLayerType )
171  return;
172 
173  // get creation function for new layer from registry
175  QgsSymbolLayerV2AbstractMetadata* am = pReg->symbolLayerMetadata( newLayerType );
176  if ( am == NULL ) // check whether the metadata is assigned
177  return;
178 
179  // change layer to a new (with different type)
180  // base new layer on existing layer's properties
181  QgsSymbolLayerV2* newLayer = am->createSymbolLayer( layer->properties() );
182  if ( newLayer == NULL )
183  return;
184 
185  updateSymbolLayerWidget( newLayer );
186  emit changeLayer( newLayer );
187 }
188 
190 {
191  emit changed();
192 }
virtual void setSymbolLayer(QgsSymbolLayerV2 *layer)=0
static QgsSymbolLayerV2Widget * create(const QgsVectorLayer *vl)
SymbolType type() const
Definition: qgssymbolv2.h:79
#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)
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 *)
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)
void updateSymbolLayerWidget(QgsSymbolLayerV2 *layer)
void setWidgetFunction(QgsSymbolLayerV2WidgetFunc f)
not available in python bindings
Represents a vector layer which manages a vector based data sets.
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...
const QgsVectorLayer * mVectorLayer
#define tr(sourceText)