Quantum GIS API Documentation  master-ce49b66
src/gui/symbology-ng/qgslayerpropertieswidget.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002     qgslayerpropertieswidget.cpp
00003     ----------------------------
00004     begin                : June 2012
00005     copyright            : (C) 2012 by Arunmozhi
00006     email                : aruntheguy at gmail.com
00007  ***************************************************************************
00008  *                                                                         *
00009  *   This program is free software; you can redistribute it and/or modify  *
00010  *   it under the terms of the GNU General Public License as published by  *
00011  *   the Free Software Foundation; either version 2 of the License, or     *
00012  *   (at your option) any later version.                                   *
00013  *                                                                         *
00014  ***************************************************************************/
00015 
00016 #include "qgslayerpropertieswidget.h"
00017 
00018 #include <QFile>
00019 #include <QStandardItem>
00020 #include <QKeyEvent>
00021 #include <QMessageBox>
00022 
00023 #include "qgssymbollayerv2.h"
00024 #include "qgssymbollayerv2registry.h"
00025 
00026 #include "qgsapplication.h"
00027 #include "qgslogger.h"
00028 
00029 #include "qgssymbollayerv2widget.h"
00030 #include "qgsellipsesymbollayerv2widget.h"
00031 #include "qgsvectorfieldsymbollayerwidget.h"
00032 #include "qgssymbolv2.h" //for the unit
00033 
00034 static bool _initWidgetFunction( QString name, QgsSymbolLayerV2WidgetFunc f )
00035 {
00036   QgsSymbolLayerV2Registry* reg = QgsSymbolLayerV2Registry::instance();
00037 
00038   QgsSymbolLayerV2AbstractMetadata* abstractMetadata = reg->symbolLayerMetadata( name );
00039   if ( abstractMetadata == NULL )
00040   {
00041     QgsDebugMsg( "Failed to find symbol layer's entry in registry: " + name );
00042     return false;
00043   }
00044   QgsSymbolLayerV2Metadata* metadata = dynamic_cast<QgsSymbolLayerV2Metadata*>( abstractMetadata );
00045   if ( metadata == NULL )
00046   {
00047     QgsDebugMsg( "Failed to cast symbol layer's metadata: " + name );
00048     return false;
00049   }
00050   metadata->setWidgetFunction( f );
00051   return true;
00052 }
00053 
00054 static void _initWidgetFunctions()
00055 {
00056   static bool initialized = false;
00057   if ( initialized )
00058     return;
00059 
00060   _initWidgetFunction( "SimpleLine", QgsSimpleLineSymbolLayerV2Widget::create );
00061   _initWidgetFunction( "MarkerLine", QgsMarkerLineSymbolLayerV2Widget::create );
00062   _initWidgetFunction( "LineDecoration", QgsLineDecorationSymbolLayerV2Widget::create );
00063 
00064   _initWidgetFunction( "SimpleMarker", QgsSimpleMarkerSymbolLayerV2Widget::create );
00065   _initWidgetFunction( "SvgMarker", QgsSvgMarkerSymbolLayerV2Widget::create );
00066   _initWidgetFunction( "FontMarker", QgsFontMarkerSymbolLayerV2Widget::create );
00067   _initWidgetFunction( "EllipseMarker", QgsEllipseSymbolLayerV2Widget::create );
00068   _initWidgetFunction( "VectorField", QgsVectorFieldSymbolLayerWidget::create );
00069 
00070   _initWidgetFunction( "SimpleFill", QgsSimpleFillSymbolLayerV2Widget::create );
00071   _initWidgetFunction( "SVGFill", QgsSVGFillSymbolLayerWidget::create );
00072   _initWidgetFunction( "CentroidFill", QgsCentroidFillSymbolLayerV2Widget::create );
00073   _initWidgetFunction( "LinePatternFill", QgsLinePatternFillSymbolLayerWidget::create );
00074   _initWidgetFunction( "PointPatternFill", QgsPointPatternFillSymbolLayerWidget::create );
00075 
00076   initialized = true;
00077 }
00078 
00079 
00080 QgsLayerPropertiesWidget::QgsLayerPropertiesWidget( QgsSymbolLayerV2* layer, const QgsSymbolV2* symbol, const QgsVectorLayer* vl, QWidget* parent )
00081     : QWidget( parent )
00082 {
00083 
00084   mLayer = layer;
00085   mSymbol = symbol;
00086   mVectorLayer = vl;
00087 
00088   setupUi( this );
00089   // initialize the sub-widgets
00090   // XXX Should this thing be here this way? Initialize all the widgets just for the sake of one layer?
00091   // TODO Make this on demand creation
00092   _initWidgetFunctions();
00093 
00094   // TODO Algorithm
00095   //
00096   // 3. populate the combo box with the supported layer type
00097   // 4. set the present layer type
00098   // 5. create the widget for the present layer type and set inn stacked widget
00099   // 6. connect comboBox type changed to two things
00100   //     1. emit signal that type has beed changed
00101   //     2. remove the widget and place the new widget corresponding to the changed layer type
00102   //
00103   populateLayerTypes();
00104   // update layer type combo box
00105   int idx = cboLayerType->findData( mLayer->layerType() );
00106   cboLayerType->setCurrentIndex( idx );
00107   // set the corresponding widget
00108   updateSymbolLayerWidget( layer );
00109   connect( cboLayerType, SIGNAL( currentIndexChanged( int ) ), this, SLOT( layerTypeChanged() ) );
00110 }
00111 
00112 void QgsLayerPropertiesWidget::populateLayerTypes()
00113 {
00114   QStringList types = QgsSymbolLayerV2Registry::instance()->symbolLayersForType( mSymbol->type() );
00115 
00116   for ( int i = 0; i < types.count(); i++ )
00117     cboLayerType->addItem( QgsSymbolLayerV2Registry::instance()->symbolLayerMetadata( types[i] )->visibleName(), types[i] );
00118 
00119   if ( mSymbol->type() == QgsSymbolV2::Fill )
00120   {
00121     QStringList typesLine = QgsSymbolLayerV2Registry::instance()->symbolLayersForType( QgsSymbolV2::Line );
00122     for ( int i = 0; i < typesLine.count(); i++ )
00123     {
00124       QString visibleName = QgsSymbolLayerV2Registry::instance()->symbolLayerMetadata( typesLine[i] )->visibleName();
00125       QString name = QString( tr( "Outline: %1" ) ).arg( visibleName );
00126       cboLayerType->addItem( name, typesLine[i] );
00127     }
00128   }
00129 
00130 }
00131 
00132 void QgsLayerPropertiesWidget::updateSymbolLayerWidget( QgsSymbolLayerV2* layer )
00133 {
00134   if ( stackedWidget->currentWidget() != pageDummy )
00135   {
00136     // stop updating from the original widget
00137     disconnect( stackedWidget->currentWidget(), SIGNAL( changed() ), this, SLOT( emitSignalChanged() ) );
00138     stackedWidget->removeWidget( stackedWidget->currentWidget() );
00139   }
00140 
00141   QgsSymbolLayerV2Registry* pReg = QgsSymbolLayerV2Registry::instance();
00142 
00143   QString layerType = layer->layerType();
00144   QgsSymbolLayerV2AbstractMetadata* am = pReg->symbolLayerMetadata( layerType );
00145   if ( am )
00146   {
00147     QgsSymbolLayerV2Widget* w = am->createSymbolLayerWidget( mVectorLayer );
00148     if ( w )
00149     {
00150       w->setSymbolLayer( layer );
00151       stackedWidget->addWidget( w );
00152       stackedWidget->setCurrentWidget( w );
00153       // start recieving updates from widget
00154       connect( w , SIGNAL( changed() ), this, SLOT( emitSignalChanged() ) );
00155       return;
00156     }
00157   }
00158   // When anything is not right
00159   stackedWidget->setCurrentWidget( pageDummy );
00160 }
00161 
00162 void QgsLayerPropertiesWidget::layerTypeChanged()
00163 {
00164   QgsSymbolLayerV2* layer = mLayer;
00165   if ( !layer )
00166     return;
00167   QString newLayerType = cboLayerType->itemData( cboLayerType->currentIndex() ).toString();
00168   if ( layer->layerType() == newLayerType )
00169     return;
00170 
00171   // get creation function for new layer from registry
00172   QgsSymbolLayerV2Registry* pReg = QgsSymbolLayerV2Registry::instance();
00173   QgsSymbolLayerV2AbstractMetadata* am = pReg->symbolLayerMetadata( newLayerType );
00174   if ( am == NULL ) // check whether the metadata is assigned
00175     return;
00176 
00177   // change layer to a new (with different type)
00178   QgsSymbolLayerV2* newLayer = am->createSymbolLayer( QgsStringMap() );
00179   if ( newLayer == NULL )
00180     return;
00181 
00182   updateSymbolLayerWidget( newLayer );
00183   emit changeLayer( newLayer );
00184 }
00185 
00186 void QgsLayerPropertiesWidget::emitSignalChanged()
00187 {
00188   emit changed();
00189 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines