194 changes: 88 additions & 106 deletions src/gui/symbology-ng/qgssymbolv2propertiesdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

static const int SymbolLayerItemType = QStandardItem::UserType + 1;

// Hybrid iitem which may represent a symbol or a layer
// Hybrid item which may represent a symbol or a layer
// Check using item->isLayer()
class SymbolLayerItem : public QStandardItem
{
Expand Down Expand Up @@ -86,10 +86,16 @@ class SymbolLayerItem : public QStandardItem
{
if ( mIsLayer )
return NULL;

return mSymbol;
}

QgsSymbolLayerV2* layer()
{
if ( mIsLayer )
return mLayer;
return NULL;
}

QVariant data( int role ) const
{
if ( role == Qt::DisplayRole )
Expand All @@ -100,9 +106,9 @@ class SymbolLayerItem : public QStandardItem
{
switch( mSymbol->type() )
{
case QgsSymbolV2::Marker : return "Marker Symbol";
case QgsSymbolV2::Fill : return "Fill Symbol";
case QgsSymbolV2::Line : return "Line Symbol";
case QgsSymbolV2::Marker : return "Symbol: Marker";
case QgsSymbolV2::Fill : return "Symbol: Fill";
case QgsSymbolV2::Line : return "Symbol: Line";
default: return "Symbol";
}
}
Expand Down Expand Up @@ -190,8 +196,8 @@ QgsSymbolV2PropertiesDialog::QgsSymbolV2PropertiesDialog( QgsSymbolV2* symbol, c
// (should be probably moved somewhere else)
_initWidgetFunctions();

model = new QStandardItemModel();
// Set the symbol
QStandardItemModel* model = new QStandardItemModel( this );
layersTree->setModel( model );
layersTree->setHeaderHidden( true );

Expand All @@ -207,14 +213,9 @@ QgsSymbolV2PropertiesDialog::QgsSymbolV2PropertiesDialog( QgsSymbolV2* symbol, c
connect( btnRemoveLayer, SIGNAL( clicked() ), this, SLOT( removeLayer() ) );
connect( btnLock, SIGNAL( clicked() ), this, SLOT( lockLayer() ) );

populateLayerTypes();
connect( cboLayerType, SIGNAL( currentIndexChanged( int ) ), this, SLOT( layerTypeChanged() ) );

loadPropertyWidgets();

updateUi();

// set first layer as active
// set symbol as active item in the tree
QModelIndex newIndex = layersTree->model()->index( 0, 0 );
layersTree->setCurrentIndex( newIndex );
}
Expand Down Expand Up @@ -242,15 +243,14 @@ void QgsSymbolV2PropertiesDialog::loadSymbol( QgsSymbolV2* symbol, SymbolLayerIt

void QgsSymbolV2PropertiesDialog::loadSymbol()
{
QStandardItemModel *model = qobject_cast<QStandardItemModel*>( layersTree->model() );
model->clear();
loadSymbol( mSymbol, static_cast<SymbolLayerItem*>( model->invisibleRootItem() ) );
}


void QgsSymbolV2PropertiesDialog::populateLayerTypes()
void QgsSymbolV2PropertiesDialog::populateLayerTypes( QgsSymbolV2 *symbol )
{
QStringList types = QgsSymbolLayerV2Registry::instance()->symbolLayersForType( mSymbol->type() );
QStringList types = QgsSymbolLayerV2Registry::instance()->symbolLayersForType( symbol->type() );

cboLayerType->clear();
for ( int i = 0; i < types.count(); i++ )
Expand All @@ -271,7 +271,6 @@ void QgsSymbolV2PropertiesDialog::populateLayerTypes()

void QgsSymbolV2PropertiesDialog::updateUi()
{
QStandardItemModel *model = qobject_cast<QStandardItemModel*>( layersTree->model() );
QModelIndex currentIdx = layersTree->currentIndex();
if ( !currentIdx.isValid() )
return;
Expand Down Expand Up @@ -307,113 +306,97 @@ void QgsSymbolV2PropertiesDialog::updateLayerPreview()
SymbolLayerItem* item = currentLayerItem();
if ( item )
item->updatePreview();

// update also preview of the whole symbol
updatePreview();
}

void QgsSymbolV2PropertiesDialog::updateSymbolLayerWidget( QgsSymbolLayerV2* layer )
{
QString layerType = layer->layerType();

// stop updating from the original widget
if ( stackedWidget->currentWidget() != pageDummy )
disconnect( stackedWidget->currentWidget(), SIGNAL( changed() ), this, SLOT( updateLayerPreview() ) );

// update active properties widget
if ( mWidgets.contains( layerType ) )
{
stackedWidget->setCurrentWidget( mWidgets[layerType] );
mWidgets[layerType]->setSymbolLayer( layer );

// start recieving updates from widget
connect( mWidgets[layerType], SIGNAL( changed() ), this, SLOT( updateLayerPreview() ) );
}
else
{
// use dummy widget instead
stackedWidget->setCurrentWidget( pageDummy );
// stop updating from the original widget
disconnect( stackedWidget->currentWidget(), SIGNAL( changed() ), this, SLOT( updateLayerPreview() ) );
stackedWidget->removeWidget( stackedWidget->currentWidget() );
}
}

void QgsSymbolV2PropertiesDialog::loadPropertyWidgets()
{
QgsSymbolLayerV2Registry* pReg = QgsSymbolLayerV2Registry::instance();

QStringList layerTypes = pReg->symbolLayersForType( mSymbol->type() );

// also load line symbol layers for fill symbols
if ( mSymbol->type() == QgsSymbolV2::Fill )
layerTypes += pReg->symbolLayersForType( QgsSymbolV2::Line );
QString layerType = layer->layerType();

for ( int i = 0; i < layerTypes.count(); i++ )
QgsSymbolLayerV2AbstractMetadata* am = pReg->symbolLayerMetadata( layerType );
if ( am )
{
QString layerType = layerTypes[i];
QgsSymbolLayerV2AbstractMetadata* am = pReg->symbolLayerMetadata( layerType );
if ( am == NULL ) // check whether the metadata is assigned
continue;

QgsSymbolLayerV2Widget* w = am->createSymbolLayerWidget( mVectorLayer );
if ( w == NULL ) // check whether the function returns correct widget
continue;

mWidgets[layerType] = w;
stackedWidget->addWidget( w );
if ( w )
{
w->setSymbolLayer( layer );
stackedWidget->addWidget( w );
stackedWidget->setCurrentWidget( w );
// start recieving updates from widget
connect( w , SIGNAL( changed() ), this, SLOT( updateLayerPreview() ) );
return;
}
}
}

int QgsSymbolV2PropertiesDialog::currentRowIndex()
{
QModelIndex idx = layersTree->selectionModel()->currentIndex();
if ( !idx.isValid() )
return -1;
return idx.row();
}

int QgsSymbolV2PropertiesDialog::currentLayerIndex()
{
return layersTree->model()->rowCount() - currentRowIndex() - 1;
// When anything is not right
stackedWidget->setCurrentWidget( pageDummy );
}

SymbolLayerItem* QgsSymbolV2PropertiesDialog::currentLayerItem()
{
int index = currentRowIndex();
if ( index < 0 )
QModelIndex idx = layersTree->currentIndex();
if ( !idx.isValid() )
return NULL;

QStandardItemModel* model = qobject_cast<QStandardItemModel*>( layersTree->model() );
if ( model == NULL )
return NULL;
QStandardItem* item = model->item( index );
if ( item->type() != SymbolLayerItemType )
SymbolLayerItem *item = static_cast<SymbolLayerItem*>( model->itemFromIndex( idx ) );
if ( !item->isLayer() )
return NULL;
return static_cast<SymbolLayerItem*>( item );

return item;
}

QgsSymbolLayerV2* QgsSymbolV2PropertiesDialog::currentLayer()
{
int idx = currentLayerIndex();
if ( idx < 0 )
QModelIndex idx = layersTree->currentIndex();
if ( !idx.isValid() )
return NULL;

return mSymbol->symbolLayer( idx );
SymbolLayerItem *item = static_cast<SymbolLayerItem*>( model->itemFromIndex( idx ) );
if ( item->isLayer() )
return item->layer();

return NULL;
}


void QgsSymbolV2PropertiesDialog::layerChanged()
{
// We donot want slot to fire while we load compatible layertypes
disconnect( cboLayerType, SIGNAL( currentIndexChanged( int ) ), this, SLOT( layerTypeChanged() ) );
updateUi();

// get layer info
QgsSymbolLayerV2* layer = currentLayer();
if ( !layer )
SymbolLayerItem *currentItem = static_cast<SymbolLayerItem*>( model->itemFromIndex( layersTree->currentIndex() ) );
if ( currentItem == NULL )
return;

// FIXME get the current symbol and populate the saved symbols of that type in the widget area
if ( !currentItem->isLayer() )
{
cboLayerType->clear();
stackedWidget->removeWidget( stackedWidget->currentWidget() );
return;
}

SymbolLayerItem *symbolItem = static_cast<SymbolLayerItem*>( currentItem->parent() );
populateLayerTypes( symbolItem->symbol() );

QgsSymbolLayerV2 *layer = currentItem->layer();
// update layer type combo box
int idx = cboLayerType->findData( layer->layerType() );
cboLayerType->setCurrentIndex( idx );

updateSymbolLayerWidget( layer );
connect( cboLayerType, SIGNAL( currentIndexChanged( int ) ), this, SLOT( layerTypeChanged() ) );

updateLockButton();
}
Expand Down Expand Up @@ -447,30 +430,39 @@ void QgsSymbolV2PropertiesDialog::layerTypeChanged()
QgsSymbolLayerV2* newLayer = am->createSymbolLayer( QgsStringMap() );
if ( newLayer == NULL )
return;
mSymbol->changeSymbolLayer( currentLayerIndex(), newLayer );

updateSymbolLayerWidget( newLayer );

SymbolLayerItem *item = currentLayerItem();
// remove previos childs if any
if ( layer->subSymbol() )
{
item->removeRow( 0 );
}
// update symbol layer item
SymbolLayerItem* item = currentLayerItem();
item->setLayer( newLayer );
// When it is a marker symbol
if ( newLayer->subSymbol() )
{
SymbolLayerItem *subsymbol = new SymbolLayerItem( newLayer->subSymbol()->symbolLayer( 0 ) );
SymbolLayerItem *subsymbol = new SymbolLayerItem( newLayer->subSymbol() );
SymbolLayerItem *sublayer = new SymbolLayerItem( newLayer->subSymbol()->symbolLayer( 0 ) );
subsymbol->appendRow( sublayer );
item->appendRow( subsymbol );
}
item->updatePreview();

// Change the symbol at last to avoid deleting item's layer
QgsSymbolV2* symbol = static_cast<SymbolLayerItem*>( item->parent() )->symbol();
int layerIdx = item->parent()->rowCount() - item->row() - 1;
symbol->changeSymbolLayer( layerIdx, newLayer );

updateSymbolLayerWidget( newLayer );

item->updatePreview();
updatePreview();
}


void QgsSymbolV2PropertiesDialog::addLayer()
{
QModelIndex idx = layersTree->currentIndex();
QStandardItemModel *model = qobject_cast<QStandardItemModel*>( layersTree->model() );

if ( !idx.isValid() )
return;

Expand All @@ -491,20 +483,17 @@ void QgsSymbolV2PropertiesDialog::addLayer()

layersTree->setCurrentIndex( model->indexFromItem( newLayerItem ) );
updateUi();
updatePreview();
}


void QgsSymbolV2PropertiesDialog::removeLayer()
{
QModelIndex idx = layersTree->currentIndex();
int row = idx.row();

QStandardItemModel *model = qobject_cast<QStandardItemModel*>( layersTree->model() );
SymbolLayerItem *item = static_cast<SymbolLayerItem*>( model->itemFromIndex( idx ) );

SymbolLayerItem *item = currentLayerItem();
int row = item->row();
SymbolLayerItem *parent = static_cast<SymbolLayerItem*>( item->parent() );
int layerIdx = parent->rowCount() - row - 1; // IMPORTANT

int layerIdx = parent->rowCount() - row - 1; // IMPORTANT
QgsSymbolV2* parentSymbol = parent->symbol();
QgsSymbolLayerV2 *tmpLayer = parentSymbol->takeSymbolLayer( layerIdx );

Expand All @@ -515,11 +504,11 @@ void QgsSymbolV2PropertiesDialog::removeLayer()
layersTree->setCurrentIndex( newIdx );

updateUi();
updatePreview();
//finally delete the removed layer pointer
delete tmpLayer;
}


void QgsSymbolV2PropertiesDialog::moveLayerDown()
{
moveLayerByOffset( + 1 );
Expand All @@ -532,18 +521,12 @@ void QgsSymbolV2PropertiesDialog::moveLayerUp()

void QgsSymbolV2PropertiesDialog::moveLayerByOffset( int offset )
{

QModelIndex idx = layersTree->currentIndex();
int row = idx.row();

QStandardItemModel *model = qobject_cast<QStandardItemModel*>( layersTree->model() );
SymbolLayerItem *item = static_cast<SymbolLayerItem*>( model->itemFromIndex( idx ) );

if( !item->isLayer() )
SymbolLayerItem *item = currentLayerItem();
if( item == NULL )
return;
int row = item->row();

SymbolLayerItem *parent = static_cast<SymbolLayerItem*>( item->parent() );

QgsSymbolV2* parentSymbol = parent->symbol();

int layerIdx = parent->rowCount() - row - 1;
Expand All @@ -558,10 +541,10 @@ void QgsSymbolV2PropertiesDialog::moveLayerByOffset( int offset )
QModelIndex newIdx = rowItems[ 0 ]->index();
layersTree->setCurrentIndex( newIdx );

updatePreview();
updateUi();
}


void QgsSymbolV2PropertiesDialog::lockLayer()
{
QgsSymbolLayerV2* layer = currentLayer();
Expand All @@ -570,7 +553,6 @@ void QgsSymbolV2PropertiesDialog::lockLayer()
layer->setLocked( btnLock->isChecked() );
}


void QgsSymbolV2PropertiesDialog::keyPressEvent( QKeyEvent * e )
{
// Ignore the ESC key to avoid close the dialog without the properties window
Expand Down
9 changes: 5 additions & 4 deletions src/gui/symbology-ng/qgssymbolv2propertiesdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class QgsVectorLayer;
class SymbolLayerItem;

#include <QMap>
#include <QStandardItemModel>


class GUI_EXPORT QgsSymbolV2PropertiesDialog : public QDialog, private Ui::DlgSymbolV2Properties
Expand Down Expand Up @@ -60,17 +61,15 @@ class GUI_EXPORT QgsSymbolV2PropertiesDialog : public QDialog, private Ui::DlgSy
void loadSymbol( QgsSymbolV2* symbol, SymbolLayerItem* parent );
void loadSymbol();

void populateLayerTypes();
void populateLayerTypes( QgsSymbolV2* symbol );

void updateUi();

void loadPropertyWidgets();
//void loadPropertyWidgets();

void updateSymbolLayerWidget( QgsSymbolLayerV2* layer );
void updateLockButton();

int currentRowIndex();
int currentLayerIndex();
SymbolLayerItem* currentLayerItem();
QgsSymbolLayerV2* currentLayer();

Expand All @@ -82,6 +81,8 @@ class GUI_EXPORT QgsSymbolV2PropertiesDialog : public QDialog, private Ui::DlgSy
QMap<QString, QgsSymbolLayerV2Widget*> mWidgets;

const QgsVectorLayer* mVectorLayer;

QStandardItemModel* model;
};

#endif
6 changes: 5 additions & 1 deletion src/ui/qgssymbolv2propertiesdialogbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,11 @@
</widget>
</item>
<item row="1" column="0">
<widget class="QTreeView" name="layersTree"/>
<widget class="QTreeView" name="layersTree">
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
</widget>
</item>
</layout>
</widget>
Expand Down
55 changes: 12 additions & 43 deletions src/ui/symbollayer/widget_markerline.ui
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,14 @@
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Marker</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QPushButton" name="btnChangeMarker">
<property name="text">
<string>Change</string>
</property>
</widget>
</item>
<item row="0" column="2">
<spacer>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Preferred</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>109</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="0" colspan="2">
<item row="0" column="0" colspan="2">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Marker placement</string>
</property>
</widget>
</item>
<item row="2" column="0">
<item row="1" column="0">
<widget class="QRadioButton" name="radInterval">
<property name="text">
<string>with interval</string>
Expand All @@ -61,7 +31,7 @@
</property>
</widget>
</item>
<item row="2" column="1">
<item row="1" column="1">
<widget class="QDoubleSpinBox" name="spinInterval">
<property name="decimals">
<number>2</number>
Expand All @@ -74,42 +44,42 @@
</property>
</widget>
</item>
<item row="3" column="0" colspan="2">
<item row="2" column="0" colspan="2">
<widget class="QRadioButton" name="radVertex">
<property name="text">
<string>on every vertex</string>
</property>
</widget>
</item>
<item row="4" column="0" colspan="2">
<item row="3" column="0" colspan="2">
<widget class="QRadioButton" name="radVertexLast">
<property name="text">
<string>on last vertex only</string>
</property>
</widget>
</item>
<item row="5" column="0" colspan="2">
<item row="4" column="0" colspan="2">
<widget class="QRadioButton" name="radVertexFirst">
<property name="text">
<string>on first vertex only</string>
</property>
</widget>
</item>
<item row="8" column="0" colspan="2">
<item row="7" column="0" colspan="2">
<widget class="QCheckBox" name="chkRotateMarker">
<property name="text">
<string>Rotate marker</string>
</property>
</widget>
</item>
<item row="9" column="0">
<item row="8" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Line offset</string>
</property>
</widget>
</item>
<item row="9" column="1">
<item row="8" column="1">
<widget class="QDoubleSpinBox" name="spinOffset">
<property name="decimals">
<number>5</number>
Expand All @@ -122,7 +92,7 @@
</property>
</widget>
</item>
<item row="10" column="0" colspan="3">
<item row="9" column="0" colspan="3">
<spacer>
<property name="orientation">
<enum>Qt::Vertical</enum>
Expand All @@ -135,14 +105,14 @@
</property>
</spacer>
</item>
<item row="7" column="0" colspan="3">
<item row="6" column="0" colspan="3">
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="6" column="0">
<item row="5" column="0">
<widget class="QRadioButton" name="radCentralPoint">
<property name="text">
<string>on central point</string>
Expand All @@ -152,7 +122,6 @@
</layout>
</widget>
<tabstops>
<tabstop>btnChangeMarker</tabstop>
<tabstop>radInterval</tabstop>
<tabstop>spinInterval</tabstop>
<tabstop>radVertex</tabstop>
Expand Down