2
#include "qgssymbolv2propertiesdialog.h"
5
#include <QStandardItem>
8
#include "qgssymbollayerv2.h"
9
#include "qgssymbolv2.h"
10
#include "qgssymbollayerv2registry.h"
12
#include "qgsapplication.h"
14
#include "qgssymbollayerv2widget.h"
16
static const int SymbolLayerItemType = QStandardItem::UserType + 1;
18
class SymbolLayerItem : public QStandardItem
21
SymbolLayerItem( QgsSymbolLayerV2* layer )
26
void setLayer( QgsSymbolLayerV2* layer )
34
QIcon icon = QgsSymbolLayerV2Utils::symbolLayerPreviewIcon( mLayer, QSize( 16, 16 ) );
38
int type() const { return SymbolLayerItemType; }
40
QVariant data( int role ) const
42
if ( role == Qt::DisplayRole )
43
return QVariant( mLayer->layerType() );
44
if ( role == Qt::SizeHintRole )
45
return QVariant( QSize( 32, 32 ) );
46
if ( role == Qt::CheckStateRole )
47
return QVariant(); // could be true/false
48
return QStandardItem::data( role );
52
QgsSymbolLayerV2* mLayer;
57
static QString iconPath( QString iconFile )
60
QString path = QgsApplication::activeThemePath();
61
if ( QFile::exists( path + iconFile ) )
62
return path + iconFile;
65
return QgsApplication::defaultThemePath() + iconFile;
70
QgsSymbolV2PropertiesDialog::QgsSymbolV2PropertiesDialog( QgsSymbolV2* symbol, QWidget* parent )
71
: QDialog( parent ), mSymbol( symbol )
76
btnAddLayer->setIcon( QIcon( iconPath( "symbologyAdd.png" ) ) );
77
btnRemoveLayer->setIcon( QIcon( iconPath( "symbologyRemove.png" ) ) );
78
btnLock->setIcon( QIcon( iconPath( "symbologyLock.png" ) ) );
79
btnUp->setIcon( QIcon( iconPath( "symbologyUp.png" ) ) );
80
btnDown->setIcon( QIcon( iconPath( "symbologyDown.png" ) ) );
82
// set widget functions
83
// (should be probably moved somewhere else)
84
QgsSymbolLayerV2Registry::instance()->setLayerTypeWidgetFunction( "SimpleLine", QgsSimpleLineSymbolLayerV2Widget::create );
85
QgsSymbolLayerV2Registry::instance()->setLayerTypeWidgetFunction( "MarkerLine", QgsMarkerLineSymbolLayerV2Widget::create );
86
QgsSymbolLayerV2Registry::instance()->setLayerTypeWidgetFunction( "LineDecoration", QgsLineDecorationSymbolLayerV2Widget::create );
88
QgsSymbolLayerV2Registry::instance()->setLayerTypeWidgetFunction( "SimpleMarker", QgsSimpleMarkerSymbolLayerV2Widget::create );
89
QgsSymbolLayerV2Registry::instance()->setLayerTypeWidgetFunction( "SvgMarker", QgsSvgMarkerSymbolLayerV2Widget::create );
91
QgsSymbolLayerV2Registry::instance()->setLayerTypeWidgetFunction( "SimpleFill", QgsSimpleFillSymbolLayerV2Widget::create );
95
connect( btnUp, SIGNAL( clicked() ), this, SLOT( moveLayerUp() ) );
96
connect( btnDown, SIGNAL( clicked() ), this, SLOT( moveLayerDown() ) );
97
connect( btnAddLayer, SIGNAL( clicked() ), this, SLOT( addLayer() ) );
98
connect( btnRemoveLayer, SIGNAL( clicked() ), this, SLOT( removeLayer() ) );
99
connect( btnLock, SIGNAL( clicked() ), this, SLOT( lockLayer() ) );
101
populateLayerTypes();
102
connect( cboLayerType, SIGNAL( currentIndexChanged( int ) ), this, SLOT( layerTypeChanged() ) );
104
loadPropertyWidgets();
108
// set first layer as active
109
QModelIndex newIndex = listLayers->model()->index( 0, 0 );
110
listLayers->setCurrentIndex( newIndex );
114
void QgsSymbolV2PropertiesDialog::loadSymbol()
116
QStandardItemModel* model = new QStandardItemModel( this );
117
listLayers->setModel( model );
119
QItemSelectionModel* selModel = listLayers->selectionModel();
120
connect( selModel, SIGNAL( currentChanged( const QModelIndex&, const QModelIndex& ) ), this, SLOT( layerChanged() ) );
122
int count = mSymbol->symbolLayerCount();
123
for ( int i = count - 1; i >= 0; i-- )
125
model->appendRow( new SymbolLayerItem( mSymbol->symbolLayer( i ) ) );
132
void QgsSymbolV2PropertiesDialog::populateLayerTypes()
134
QStringList types = QgsSymbolLayerV2Registry::instance()->symbolLayersForType( mSymbol->type() );
136
cboLayerType->clear();
137
for ( int i = 0; i < types.count(); i++ )
138
cboLayerType->addItem( types[i] );
142
void QgsSymbolV2PropertiesDialog::updateUi()
144
int row = currentRowIndex();
145
btnUp->setEnabled( row > 0 );
146
btnDown->setEnabled( row < listLayers->model()->rowCount() - 1 && row != -1 );
147
btnRemoveLayer->setEnabled( row != -1 );
150
void QgsSymbolV2PropertiesDialog::updatePreview()
152
QImage preview = mSymbol->bigSymbolPreviewImage();
153
lblPreview->setPixmap( QPixmap::fromImage( preview ) );
156
void QgsSymbolV2PropertiesDialog::updateLayerPreview()
158
// get current layer item and update its icon
159
SymbolLayerItem* item = currentLayerItem();
161
item->updatePreview();
163
// update also preview of the whole symbol
167
void QgsSymbolV2PropertiesDialog::updateSymbolLayerWidget( QgsSymbolLayerV2* layer )
169
QString layerType = layer->layerType();
171
// stop updating from the original widget
172
if ( stackedWidget->currentWidget() != pageDummy )
173
disconnect( stackedWidget->currentWidget(), SIGNAL( changed() ), this, SLOT( updateLayerPreview() ) );
175
// update active properties widget
176
if ( mWidgets.contains( layerType ) )
178
stackedWidget->setCurrentWidget( mWidgets[layerType] );
179
mWidgets[layerType]->setSymbolLayer( layer );
181
// start recieving updates from widget
182
connect( mWidgets[layerType], SIGNAL( changed() ), this, SLOT( updateLayerPreview() ) );
186
// use dummy widget instead
187
stackedWidget->setCurrentWidget( pageDummy );
191
void QgsSymbolV2PropertiesDialog::loadPropertyWidgets()
193
QgsSymbolLayerV2Registry* pReg = QgsSymbolLayerV2Registry::instance();
195
QStringList layerTypes = pReg->symbolLayersForType( mSymbol->type() );
197
for ( int i = 0; i < layerTypes.count(); i++ )
199
QString layerType = layerTypes[i];
200
QgsSymbolLayerV2WidgetFunc f = pReg->symbolLayerMetadata( layerType ).widgetFunction();
201
if ( f == NULL ) // check whether the function is assigned
204
QgsSymbolLayerV2Widget* w = f();
205
if ( w == NULL ) // check whether the function returns correct widget
208
mWidgets[layerType] = w;
209
stackedWidget->addWidget( w );
213
int QgsSymbolV2PropertiesDialog::currentRowIndex()
215
QModelIndex idx = listLayers->selectionModel()->currentIndex();
216
if ( !idx.isValid() )
221
int QgsSymbolV2PropertiesDialog::currentLayerIndex()
223
return listLayers->model()->rowCount() - currentRowIndex() - 1;
226
SymbolLayerItem* QgsSymbolV2PropertiesDialog::currentLayerItem()
228
int index = currentRowIndex();
232
QStandardItemModel* model = qobject_cast<QStandardItemModel*>( listLayers->model() );
235
QStandardItem* item = model->item( index );
236
if ( item->type() != SymbolLayerItemType )
238
return static_cast<SymbolLayerItem*>( item );
241
QgsSymbolLayerV2* QgsSymbolV2PropertiesDialog::currentLayer()
243
int idx = currentLayerIndex();
247
return mSymbol->symbolLayer( idx );
251
void QgsSymbolV2PropertiesDialog::layerChanged()
256
QgsSymbolLayerV2* layer = currentLayer();
260
// update layer type combo box
261
int idx = cboLayerType->findText( layer->layerType() );
262
cboLayerType->setCurrentIndex( idx );
264
updateSymbolLayerWidget( layer );
270
void QgsSymbolV2PropertiesDialog::updateLockButton()
272
QgsSymbolLayerV2* layer = currentLayer();
273
if ( layer == NULL ) return;
275
btnLock->setChecked( layer->isLocked() );
279
void QgsSymbolV2PropertiesDialog::layerTypeChanged()
281
QgsSymbolLayerV2* layer = currentLayer();
282
if ( layer == NULL ) return;
284
QString newLayerType = cboLayerType->currentText();
285
if ( layer->layerType() == newLayerType )
288
// get creation function for new layer from registry
289
QgsSymbolLayerV2Registry* pReg = QgsSymbolLayerV2Registry::instance();
290
QgsSymbolLayerV2CreateFunc f = pReg->symbolLayerMetadata( newLayerType ).createFunction();
291
if ( f == NULL ) // check whether the function is assigned
294
// change layer to a new (with different type)
295
QgsSymbolLayerV2* newLayer = f( QgsStringMap() );
296
mSymbol->changeSymbolLayer( currentLayerIndex(), newLayer );
298
updateSymbolLayerWidget( newLayer );
300
// update symbol layer item
301
SymbolLayerItem* item = currentLayerItem();
302
item->setLayer( newLayer );
303
item->updatePreview();
309
void QgsSymbolV2PropertiesDialog::addLayer()
311
QgsSymbolLayerV2* newLayer = QgsSymbolLayerV2Registry::instance()->defaultSymbolLayer( mSymbol->type() );
313
mSymbol->appendSymbolLayer( newLayer );
317
QModelIndex newIndex = listLayers->model()->index( 0, 0 );
318
listLayers->setCurrentIndex( newIndex );
324
void QgsSymbolV2PropertiesDialog::removeLayer()
326
int idx = currentLayerIndex();
327
if ( idx < 0 ) return;
328
mSymbol->deleteSymbolLayer( idx );
336
void QgsSymbolV2PropertiesDialog::moveLayerDown()
338
moveLayerByOffset( +1 );
341
void QgsSymbolV2PropertiesDialog::moveLayerUp()
343
moveLayerByOffset( -1 );
346
void QgsSymbolV2PropertiesDialog::moveLayerByOffset( int offset )
348
int rowIdx = currentRowIndex();
349
int layerIdx = currentLayerIndex();
352
QgsSymbolLayerV2* tmpLayer = mSymbol->takeSymbolLayer( layerIdx );
353
mSymbol->insertSymbolLayer( layerIdx - offset, tmpLayer );
357
QModelIndex newIndex = listLayers->model()->index( rowIdx + offset, 0 );
358
listLayers->setCurrentIndex( newIndex );
364
void QgsSymbolV2PropertiesDialog::lockLayer()
366
QgsSymbolLayerV2* layer = currentLayer();
367
if ( layer == NULL ) return;
369
layer->setLocked( btnLock->isChecked() );
372
#include "qgslogger.h"
374
void QgsSymbolV2PropertiesDialog::keyPressEvent( QKeyEvent * e )
376
// Ignore the ESC key to avoid close the dialog without the properties window
377
if ( !isWindow() && e->key() == Qt::Key_Escape )
383
QDialog::keyPressEvent( e );