|
| 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 | + |
| 16 | +#include "qgslayerpropertieswidget.h" |
| 17 | + |
| 18 | +#include <QFile> |
| 19 | +#include <QStandardItem> |
| 20 | +#include <QKeyEvent> |
| 21 | +#include <QMessageBox> |
| 22 | + |
| 23 | +#include "qgssymbollayerv2.h" |
| 24 | +#include "qgssymbollayerv2registry.h" |
| 25 | + |
| 26 | +#include "qgsapplication.h" |
| 27 | +#include "qgslogger.h" |
| 28 | + |
| 29 | +#include "qgssymbollayerv2widget.h" |
| 30 | +#include "qgsellipsesymbollayerv2widget.h" |
| 31 | +#include "qgsvectorfieldsymbollayerwidget.h" |
| 32 | +#include "qgssymbolv2.h" //for the unit |
| 33 | + |
| 34 | +static bool _initWidgetFunction( QString name, QgsSymbolLayerV2WidgetFunc f ) |
| 35 | +{ |
| 36 | + QgsSymbolLayerV2Registry* reg = QgsSymbolLayerV2Registry::instance(); |
| 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 | + |
| 60 | + _initWidgetFunction( "SimpleLine", QgsSimpleLineSymbolLayerV2Widget::create ); |
| 61 | + _initWidgetFunction( "MarkerLine", QgsMarkerLineSymbolLayerV2Widget::create ); |
| 62 | + _initWidgetFunction( "LineDecoration", QgsLineDecorationSymbolLayerV2Widget::create ); |
| 63 | + |
| 64 | + _initWidgetFunction( "SimpleMarker", QgsSimpleMarkerSymbolLayerV2Widget::create ); |
| 65 | + _initWidgetFunction( "SvgMarker", QgsSvgMarkerSymbolLayerV2Widget::create ); |
| 66 | + _initWidgetFunction( "FontMarker", QgsFontMarkerSymbolLayerV2Widget::create ); |
| 67 | + _initWidgetFunction( "EllipseMarker", QgsEllipseSymbolLayerV2Widget::create ); |
| 68 | + _initWidgetFunction( "VectorField", QgsVectorFieldSymbolLayerWidget::create ); |
| 69 | + |
| 70 | + _initWidgetFunction( "SimpleFill", QgsSimpleFillSymbolLayerV2Widget::create ); |
| 71 | + _initWidgetFunction( "SVGFill", QgsSVGFillSymbolLayerWidget::create ); |
| 72 | + _initWidgetFunction( "CentroidFill", QgsCentroidFillSymbolLayerV2Widget::create ); |
| 73 | + _initWidgetFunction( "LinePatternFill", QgsLinePatternFillSymbolLayerWidget::create ); |
| 74 | + _initWidgetFunction( "PointPatternFill", QgsPointPatternFillSymbolLayerWidget::create ); |
| 75 | + |
| 76 | + initialized = true; |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | +QgsLayerPropertiesWidget::QgsLayerPropertiesWidget( QgsSymbolLayerV2* layer, const QgsSymbolV2* symbol, const QgsVectorLayer* vl, QWidget* parent ) |
| 81 | + : QWidget( parent ) |
| 82 | +{ |
| 83 | + |
| 84 | + mLayer = layer; |
| 85 | + mSymbol = symbol; |
| 86 | + mVectorLayer = vl; |
| 87 | + |
| 88 | + setupUi( this ); |
| 89 | + // initalize the sub-widgets |
| 90 | + // XXX Should this thing be here this way? Initalize all th widgets just for the sake of one layer? |
| 91 | + // TODO Make this on demand creation |
| 92 | + _initWidgetFunctions(); |
| 93 | + |
| 94 | + // TODO Algorithm |
| 95 | + // |
| 96 | + // 3. populate the combo box with the supported layer type |
| 97 | + // 4. set the present layer type |
| 98 | + // 5. create the widget for the present layer type and set inn stacked widget |
| 99 | + // 6. connect comboBox type changed to two things |
| 100 | + // 1. emit signal that type has beed changed |
| 101 | + // 2. remove the widget and place the new widget corresponding to the changed layer type |
| 102 | + // |
| 103 | + populateLayerTypes(); |
| 104 | + // update layer type combo box |
| 105 | + int idx = cboLayerType->findData( mLayer->layerType() ); |
| 106 | + cboLayerType->setCurrentIndex( idx ); |
| 107 | + // set the corresponding widget |
| 108 | + updateSymbolLayerWidget( layer ); |
| 109 | + connect( cboLayerType, SIGNAL( currentIndexChanged( int ) ), this, SLOT( layerTypeChanged() ) ); |
| 110 | +} |
| 111 | + |
| 112 | +void QgsLayerPropertiesWidget::populateLayerTypes() |
| 113 | +{ |
| 114 | + QStringList types = QgsSymbolLayerV2Registry::instance()->symbolLayersForType( mSymbol->type() ); |
| 115 | + |
| 116 | + for ( int i = 0; i < types.count(); i++ ) |
| 117 | + cboLayerType->addItem( QgsSymbolLayerV2Registry::instance()->symbolLayerMetadata( types[i] )->visibleName(), types[i] ); |
| 118 | + |
| 119 | + if ( mSymbol->type() == QgsSymbolV2::Fill ) |
| 120 | + { |
| 121 | + QStringList typesLine = QgsSymbolLayerV2Registry::instance()->symbolLayersForType( QgsSymbolV2::Line ); |
| 122 | + for ( int i = 0; i < typesLine.count(); i++ ) |
| 123 | + { |
| 124 | + QString visibleName = QgsSymbolLayerV2Registry::instance()->symbolLayerMetadata( typesLine[i] )->visibleName(); |
| 125 | + QString name = QString( tr( "Outline: %1" ) ).arg( visibleName ); |
| 126 | + cboLayerType->addItem( name, typesLine[i] ); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | +} |
| 131 | + |
| 132 | +void QgsLayerPropertiesWidget::updateSymbolLayerWidget( QgsSymbolLayerV2* layer ) |
| 133 | +{ |
| 134 | + if ( stackedWidget->currentWidget() != pageDummy ) |
| 135 | + { |
| 136 | + // stop updating from the original widget |
| 137 | + disconnect( stackedWidget->currentWidget(), SIGNAL( changed() ), this, SLOT( emitSignalChanged() ) ); |
| 138 | + stackedWidget->removeWidget( stackedWidget->currentWidget() ); |
| 139 | + } |
| 140 | + |
| 141 | + QgsSymbolLayerV2Registry* pReg = QgsSymbolLayerV2Registry::instance(); |
| 142 | + |
| 143 | + QString layerType = layer->layerType(); |
| 144 | + QgsSymbolLayerV2AbstractMetadata* am = pReg->symbolLayerMetadata( layerType ); |
| 145 | + if ( am ) |
| 146 | + { |
| 147 | + QgsSymbolLayerV2Widget* w = am->createSymbolLayerWidget( mVectorLayer ); |
| 148 | + if ( w ) |
| 149 | + { |
| 150 | + w->setSymbolLayer( layer ); |
| 151 | + stackedWidget->addWidget( w ); |
| 152 | + stackedWidget->setCurrentWidget( w ); |
| 153 | + // start recieving updates from widget |
| 154 | + connect( w , SIGNAL( changed() ), this, SLOT( emitSignalChanged() ) ); |
| 155 | + return; |
| 156 | + } |
| 157 | + } |
| 158 | + // When anything is not right |
| 159 | + stackedWidget->setCurrentWidget( pageDummy ); |
| 160 | +} |
| 161 | + |
| 162 | +void QgsLayerPropertiesWidget::layerTypeChanged() |
| 163 | +{ |
| 164 | + QgsSymbolLayerV2* layer = mLayer; |
| 165 | + if ( !layer ) |
| 166 | + return; |
| 167 | + QString newLayerType = cboLayerType->itemData( cboLayerType->currentIndex() ).toString(); |
| 168 | + if ( layer->layerType() == newLayerType ) |
| 169 | + return; |
| 170 | + |
| 171 | + // get creation function for new layer from registry |
| 172 | + QgsSymbolLayerV2Registry* pReg = QgsSymbolLayerV2Registry::instance(); |
| 173 | + QgsSymbolLayerV2AbstractMetadata* am = pReg->symbolLayerMetadata( newLayerType ); |
| 174 | + if ( am == NULL ) // check whether the metadata is assigned |
| 175 | + return; |
| 176 | + |
| 177 | + // change layer to a new (with different type) |
| 178 | + QgsSymbolLayerV2* newLayer = am->createSymbolLayer( QgsStringMap() ); |
| 179 | + if ( newLayer == NULL ) |
| 180 | + return; |
| 181 | + |
| 182 | + updateSymbolLayerWidget( newLayer ); |
| 183 | + emit changeLayer( newLayer ); |
| 184 | +} |
| 185 | + |
| 186 | +void QgsLayerPropertiesWidget::emitSignalChanged() |
| 187 | +{ |
| 188 | + emit changed(); |
| 189 | +} |
0 commit comments