Skip to content
Permalink
Browse files

Rule-based renderer: Add support to refine more then one rule at a ti…

…me; decrease UI margins
  • Loading branch information
NathanW2 committed Jan 27, 2012
1 parent ace2844 commit c65265d841a9e2db0c190a64d6c13ffed47208e7
@@ -11,25 +11,25 @@
QgsRendererV2Widget::QgsRendererV2Widget( QgsVectorLayer* layer, QgsStyleV2* style )
: QWidget(), mLayer( layer ), mStyle( style )
{
}
contextMenu = new QMenu( "Renderer Options " );

void QgsRendererV2Widget::contextMenuViewCategories( const QPoint & )
{
QMenu contextMenu;
contextMenu.addAction( tr( "Change color" ), this, SLOT( changeSymbolColor( ) ) );
contextMenu.addAction( tr( "Change transparency" ), this, SLOT( changeSymbolTransparency() ) );
contextMenu.addAction( tr( "Change output unit" ), this, SLOT( changeSymbolUnit() ) );
contextMenu->addAction( tr( "Change color" ), this, SLOT( changeSymbolColor( ) ) );
contextMenu->addAction( tr( "Change transparency" ), this, SLOT( changeSymbolTransparency() ) );
contextMenu->addAction( tr( "Change output unit" ), this, SLOT( changeSymbolUnit() ) );

if ( mLayer && mLayer->geometryType() == QGis::Line )
{
contextMenu.addAction( tr( "Change width" ), this, SLOT( changeSymbolWidth() ) );
contextMenu->addAction( tr( "Change width" ), this, SLOT( changeSymbolWidth() ) );
}
else if ( mLayer && mLayer->geometryType() == QGis::Point )
{
contextMenu.addAction( tr( "Change size" ), this, SLOT( changeSymbolSize() ) );
contextMenu->addAction( tr( "Change size" ), this, SLOT( changeSymbolSize() ) );
}
}

contextMenu.exec( QCursor::pos() );
void QgsRendererV2Widget::contextMenuViewCategories( const QPoint & )
{
contextMenu->exec( QCursor::pos() );
}

void QgsRendererV2Widget::changeSymbolColor()
@@ -2,6 +2,7 @@
#define QGSRENDERERV2WIDGET_H

#include <QWidget>
#include <QMenu>

class QgsVectorLayer;
class QgsStyleV2;
@@ -37,14 +38,15 @@ class GUI_EXPORT QgsRendererV2Widget : public QWidget
protected:
QgsVectorLayer* mLayer;
QgsStyleV2* mStyle;
QMenu* contextMenu;

/**Subclasses may provide the capability of changing multiple symbols at once by implementing the following two methods
and by connecting the slot contextMenuViewCategories(const QPoint&)*/
virtual QList<QgsSymbolV2*> selectedSymbols() { return QList<QgsSymbolV2*>(); }
virtual void refreshSymbolView() {}

protected slots:
void contextMenuViewCategories( const QPoint& p );
void contextMenuViewCategories( const QPoint& p );
/**Change color of selected symbols*/
void changeSymbolColor();
/**Change opacity of selected symbols*/
@@ -64,11 +64,12 @@ QgsRuleBasedRendererV2Widget::QgsRuleBasedRendererV2Widget( QgsVectorLayer* laye
//new ModelTest( mModel, this ); // for model validity checking
viewRules->setModel( mModel );

mRefineMenu = new QMenu( btnRefineRule );
mRefineMenu->addAction( tr( "Add scales" ), this, SLOT( refineRuleScales() ) );
mRefineMenu->addAction( tr( "Add categories" ), this, SLOT( refineRuleCategories() ) );
mRefineMenu->addAction( tr( "Add ranges" ), this, SLOT( refineRuleRanges() ) );
mRefineMenu = new QMenu( "Refine current rule", btnRefineRule );
mRefineMenu->addAction( tr( "Add scales to rule" ), this, SLOT( refineRuleScales() ) );
mRefineMenu->addAction( tr( "Add categories to rule" ), this, SLOT( refineRuleCategories() ) );
mRefineMenu->addAction( tr( "Add ranges to rule" ), this, SLOT( refineRuleRanges() ) );
btnRefineRule->setMenu( mRefineMenu );
contextMenu->addMenu( mRefineMenu );

btnAddRule->setIcon( QIcon( QgsApplication::iconPath( "symbologyAdd.png" ) ) );
btnEditRule->setIcon( QIcon( QgsApplication::iconPath( "symbologyEdit.png" ) ) );
@@ -187,21 +188,23 @@ void QgsRuleBasedRendererV2Widget::currentRuleChanged( const QModelIndex& curren

void QgsRuleBasedRendererV2Widget::refineRule( int type )
{
QModelIndex index = viewRules->selectionModel()->currentIndex();
if ( !index.isValid() )
QModelIndexList indexlist = viewRules->selectionModel()->selectedRows();

if ( indexlist.isEmpty() )
return;


if ( type == 0 ) // categories
refineRuleCategoriesGui( index );
refineRuleCategoriesGui( indexlist );
else if ( type == 1 ) // ranges
refineRuleRangesGui( index );
refineRuleRangesGui( indexlist );
else // scales
refineRuleScalesGui( index );
refineRuleScalesGui( indexlist );

// TODO: set initial rule's symbol to NULL (?)

// show the newly added rules
foreach( QModelIndex index, indexlist )
viewRules->expand( index );
}

@@ -220,10 +223,8 @@ void QgsRuleBasedRendererV2Widget::refineRuleScales()
refineRule( 2 );
}

void QgsRuleBasedRendererV2Widget::refineRuleCategoriesGui( const QModelIndex& index )
void QgsRuleBasedRendererV2Widget::refineRuleCategoriesGui( const QModelIndexList& indexList )
{
QgsRuleBasedRendererV2::Rule* initialRule = mModel->ruleForIndex( index );

QDialog dlg;
dlg.setWindowTitle( tr( "Refine a rule to categories" ) );
QVBoxLayout* l = new QVBoxLayout();
@@ -240,15 +241,19 @@ void QgsRuleBasedRendererV2Widget::refineRuleCategoriesGui( const QModelIndex& i

// create new rules
QgsCategorizedSymbolRendererV2* r = static_cast<QgsCategorizedSymbolRendererV2*>( w->renderer() );
mModel->willAddRules( index, r->categories().count() );
QgsRuleBasedRendererV2::refineRuleCategories( initialRule, r );
foreach( QModelIndex index, indexList )
{
QgsRuleBasedRendererV2::Rule* initialRule = mModel->ruleForIndex( index );
mModel->willAddRules( index, r->categories().count() );
QgsRuleBasedRendererV2::refineRuleCategories( initialRule, r );
}
mModel->finishedAddingRules();
}


void QgsRuleBasedRendererV2Widget::refineRuleRangesGui( const QModelIndex& index )
void QgsRuleBasedRendererV2Widget::refineRuleRangesGui( const QModelIndexList& indexList )
{
QgsRuleBasedRendererV2::Rule* initialRule = mModel->ruleForIndex( index );


QDialog dlg;
dlg.setWindowTitle( tr( "Refine a rule to ranges" ) );
@@ -266,20 +271,27 @@ void QgsRuleBasedRendererV2Widget::refineRuleRangesGui( const QModelIndex& index

// create new rules
QgsGraduatedSymbolRendererV2* r = static_cast<QgsGraduatedSymbolRendererV2*>( w->renderer() );
mModel->willAddRules( index, r->ranges().count() );
QgsRuleBasedRendererV2::refineRuleRanges( initialRule, r );
foreach( QModelIndex index, indexList )
{
QgsRuleBasedRendererV2::Rule* initialRule = mModel->ruleForIndex( index );
mModel->willAddRules( index, r->ranges().count() );
QgsRuleBasedRendererV2::refineRuleRanges( initialRule, r );
}
mModel->finishedAddingRules();
}

void QgsRuleBasedRendererV2Widget::refineRuleScalesGui( const QModelIndex& index )
void QgsRuleBasedRendererV2Widget::refineRuleScalesGui( const QModelIndexList& indexList )
{
QgsRuleBasedRendererV2::Rule* initialRule = mModel->ruleForIndex( index );


if ( initialRule->symbol() == NULL )
foreach( QModelIndex index, indexList )
{
QMessageBox::warning( this, tr( "Scale refinement" ), tr( "Parent rule must have a symbol for this operation." ) );
return;
QgsRuleBasedRendererV2::Rule* initialRule = mModel->ruleForIndex( index );

// If any of the rules don't have a symbol let the user know and exit.
if ( initialRule->symbol() == NULL )
{
QMessageBox::warning( this, tr( "Scale refinement" ), tr( "Parent rule %1 must have a symbol for this operation." ).arg( initialRule->label() ) );
return;
}
}

QString txt = QInputDialog::getText( this,
@@ -299,8 +311,12 @@ void QgsRuleBasedRendererV2Widget::refineRuleScalesGui( const QModelIndex& index
QMessageBox::information( this, tr( "Error" ), QString( tr( "\"%1\" is not valid scale denominator, ignoring it." ) ).arg( item ) );
}

mModel->willAddRules( index, scales.count() + 1 );
QgsRuleBasedRendererV2::refineRuleScales( initialRule, scales );
foreach( QModelIndex index, indexList )
{
QgsRuleBasedRendererV2::Rule* initialRule = mModel->ruleForIndex( index );
mModel->willAddRules( index, scales.count() + 1 );
QgsRuleBasedRendererV2::refineRuleScales( initialRule, scales );
}
mModel->finishedAddingRules();
}

@@ -110,9 +110,9 @@ class GUI_EXPORT QgsRuleBasedRendererV2Widget : public QgsRendererV2Widget, priv
protected:

void refineRule( int type );
void refineRuleCategoriesGui( const QModelIndex& index );
void refineRuleRangesGui( const QModelIndex& index );
void refineRuleScalesGui( const QModelIndex& index );
void refineRuleCategoriesGui( const QModelIndexList& index );
void refineRuleRangesGui( const QModelIndexList& index );
void refineRuleScalesGui( const QModelIndexList& index );

QgsRuleBasedRendererV2::Rule* currentRule();

@@ -11,6 +11,9 @@
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="margin">
<number>0</number>
</property>
<item>
<widget class="QTreeView" name="viewRules">
<property name="contextMenuPolicy">
@@ -61,8 +64,23 @@
</item>
<item>
<widget class="QPushButton" name="btnRefineRule">
<property name="enabled">
<bool>true</bool>
</property>
<property name="text">
<string>Refine</string>
<string>Refine current rules</string>
</property>
<property name="checkable">
<bool>false</bool>
</property>
<property name="autoDefault">
<bool>false</bool>
</property>
<property name="default">
<bool>false</bool>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>

0 comments on commit c65265d

Please sign in to comment.
You can’t perform that action at this time.