Skip to content

Commit c65265d

Browse files
committedJan 27, 2012
Rule-based renderer: Add support to refine more then one rule at a time; decrease UI margins
1 parent ace2844 commit c65265d

File tree

5 files changed

+78
-42
lines changed

5 files changed

+78
-42
lines changed
 

‎src/gui/symbology-ng/qgsrendererv2widget.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,25 @@
1111
QgsRendererV2Widget::QgsRendererV2Widget( QgsVectorLayer* layer, QgsStyleV2* style )
1212
: QWidget(), mLayer( layer ), mStyle( style )
1313
{
14-
}
14+
contextMenu = new QMenu( "Renderer Options " );
1515

16-
void QgsRendererV2Widget::contextMenuViewCategories( const QPoint & )
17-
{
18-
QMenu contextMenu;
19-
contextMenu.addAction( tr( "Change color" ), this, SLOT( changeSymbolColor( ) ) );
20-
contextMenu.addAction( tr( "Change transparency" ), this, SLOT( changeSymbolTransparency() ) );
21-
contextMenu.addAction( tr( "Change output unit" ), this, SLOT( changeSymbolUnit() ) );
16+
contextMenu->addAction( tr( "Change color" ), this, SLOT( changeSymbolColor( ) ) );
17+
contextMenu->addAction( tr( "Change transparency" ), this, SLOT( changeSymbolTransparency() ) );
18+
contextMenu->addAction( tr( "Change output unit" ), this, SLOT( changeSymbolUnit() ) );
2219

2320
if ( mLayer && mLayer->geometryType() == QGis::Line )
2421
{
25-
contextMenu.addAction( tr( "Change width" ), this, SLOT( changeSymbolWidth() ) );
22+
contextMenu->addAction( tr( "Change width" ), this, SLOT( changeSymbolWidth() ) );
2623
}
2724
else if ( mLayer && mLayer->geometryType() == QGis::Point )
2825
{
29-
contextMenu.addAction( tr( "Change size" ), this, SLOT( changeSymbolSize() ) );
26+
contextMenu->addAction( tr( "Change size" ), this, SLOT( changeSymbolSize() ) );
3027
}
28+
}
3129

32-
contextMenu.exec( QCursor::pos() );
30+
void QgsRendererV2Widget::contextMenuViewCategories( const QPoint & )
31+
{
32+
contextMenu->exec( QCursor::pos() );
3333
}
3434

3535
void QgsRendererV2Widget::changeSymbolColor()

‎src/gui/symbology-ng/qgsrendererv2widget.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define QGSRENDERERV2WIDGET_H
33

44
#include <QWidget>
5+
#include <QMenu>
56

67
class QgsVectorLayer;
78
class QgsStyleV2;
@@ -37,14 +38,15 @@ class GUI_EXPORT QgsRendererV2Widget : public QWidget
3738
protected:
3839
QgsVectorLayer* mLayer;
3940
QgsStyleV2* mStyle;
41+
QMenu* contextMenu;
4042

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

4648
protected slots:
47-
void contextMenuViewCategories( const QPoint& p );
49+
void contextMenuViewCategories( const QPoint& p );
4850
/**Change color of selected symbols*/
4951
void changeSymbolColor();
5052
/**Change opacity of selected symbols*/

‎src/gui/symbology-ng/qgsrulebasedrendererv2widget.cpp

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,12 @@ QgsRuleBasedRendererV2Widget::QgsRuleBasedRendererV2Widget( QgsVectorLayer* laye
6464
//new ModelTest( mModel, this ); // for model validity checking
6565
viewRules->setModel( mModel );
6666

67-
mRefineMenu = new QMenu( btnRefineRule );
68-
mRefineMenu->addAction( tr( "Add scales" ), this, SLOT( refineRuleScales() ) );
69-
mRefineMenu->addAction( tr( "Add categories" ), this, SLOT( refineRuleCategories() ) );
70-
mRefineMenu->addAction( tr( "Add ranges" ), this, SLOT( refineRuleRanges() ) );
67+
mRefineMenu = new QMenu( "Refine current rule", btnRefineRule );
68+
mRefineMenu->addAction( tr( "Add scales to rule" ), this, SLOT( refineRuleScales() ) );
69+
mRefineMenu->addAction( tr( "Add categories to rule" ), this, SLOT( refineRuleCategories() ) );
70+
mRefineMenu->addAction( tr( "Add ranges to rule" ), this, SLOT( refineRuleRanges() ) );
7171
btnRefineRule->setMenu( mRefineMenu );
72+
contextMenu->addMenu( mRefineMenu );
7273

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

188189
void QgsRuleBasedRendererV2Widget::refineRule( int type )
189190
{
190-
QModelIndex index = viewRules->selectionModel()->currentIndex();
191-
if ( !index.isValid() )
191+
QModelIndexList indexlist = viewRules->selectionModel()->selectedRows();
192+
193+
if ( indexlist.isEmpty() )
192194
return;
193195

194196

195197
if ( type == 0 ) // categories
196-
refineRuleCategoriesGui( index );
198+
refineRuleCategoriesGui( indexlist );
197199
else if ( type == 1 ) // ranges
198-
refineRuleRangesGui( index );
200+
refineRuleRangesGui( indexlist );
199201
else // scales
200-
refineRuleScalesGui( index );
202+
refineRuleScalesGui( indexlist );
201203

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

204206
// show the newly added rules
207+
foreach( QModelIndex index, indexlist )
205208
viewRules->expand( index );
206209
}
207210

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

223-
void QgsRuleBasedRendererV2Widget::refineRuleCategoriesGui( const QModelIndex& index )
226+
void QgsRuleBasedRendererV2Widget::refineRuleCategoriesGui( const QModelIndexList& indexList )
224227
{
225-
QgsRuleBasedRendererV2::Rule* initialRule = mModel->ruleForIndex( index );
226-
227228
QDialog dlg;
228229
dlg.setWindowTitle( tr( "Refine a rule to categories" ) );
229230
QVBoxLayout* l = new QVBoxLayout();
@@ -240,15 +241,19 @@ void QgsRuleBasedRendererV2Widget::refineRuleCategoriesGui( const QModelIndex& i
240241

241242
// create new rules
242243
QgsCategorizedSymbolRendererV2* r = static_cast<QgsCategorizedSymbolRendererV2*>( w->renderer() );
243-
mModel->willAddRules( index, r->categories().count() );
244-
QgsRuleBasedRendererV2::refineRuleCategories( initialRule, r );
244+
foreach( QModelIndex index, indexList )
245+
{
246+
QgsRuleBasedRendererV2::Rule* initialRule = mModel->ruleForIndex( index );
247+
mModel->willAddRules( index, r->categories().count() );
248+
QgsRuleBasedRendererV2::refineRuleCategories( initialRule, r );
249+
}
245250
mModel->finishedAddingRules();
246251
}
247252

248253

249-
void QgsRuleBasedRendererV2Widget::refineRuleRangesGui( const QModelIndex& index )
254+
void QgsRuleBasedRendererV2Widget::refineRuleRangesGui( const QModelIndexList& indexList )
250255
{
251-
QgsRuleBasedRendererV2::Rule* initialRule = mModel->ruleForIndex( index );
256+
252257

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

267272
// create new rules
268273
QgsGraduatedSymbolRendererV2* r = static_cast<QgsGraduatedSymbolRendererV2*>( w->renderer() );
269-
mModel->willAddRules( index, r->ranges().count() );
270-
QgsRuleBasedRendererV2::refineRuleRanges( initialRule, r );
274+
foreach( QModelIndex index, indexList )
275+
{
276+
QgsRuleBasedRendererV2::Rule* initialRule = mModel->ruleForIndex( index );
277+
mModel->willAddRules( index, r->ranges().count() );
278+
QgsRuleBasedRendererV2::refineRuleRanges( initialRule, r );
279+
}
271280
mModel->finishedAddingRules();
272281
}
273282

274-
void QgsRuleBasedRendererV2Widget::refineRuleScalesGui( const QModelIndex& index )
283+
void QgsRuleBasedRendererV2Widget::refineRuleScalesGui( const QModelIndexList& indexList )
275284
{
276-
QgsRuleBasedRendererV2::Rule* initialRule = mModel->ruleForIndex( index );
277-
278-
279-
if ( initialRule->symbol() == NULL )
285+
foreach( QModelIndex index, indexList )
280286
{
281-
QMessageBox::warning( this, tr( "Scale refinement" ), tr( "Parent rule must have a symbol for this operation." ) );
282-
return;
287+
QgsRuleBasedRendererV2::Rule* initialRule = mModel->ruleForIndex( index );
288+
289+
// If any of the rules don't have a symbol let the user know and exit.
290+
if ( initialRule->symbol() == NULL )
291+
{
292+
QMessageBox::warning( this, tr( "Scale refinement" ), tr( "Parent rule %1 must have a symbol for this operation." ).arg( initialRule->label() ) );
293+
return;
294+
}
283295
}
284296

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

302-
mModel->willAddRules( index, scales.count() + 1 );
303-
QgsRuleBasedRendererV2::refineRuleScales( initialRule, scales );
314+
foreach( QModelIndex index, indexList )
315+
{
316+
QgsRuleBasedRendererV2::Rule* initialRule = mModel->ruleForIndex( index );
317+
mModel->willAddRules( index, scales.count() + 1 );
318+
QgsRuleBasedRendererV2::refineRuleScales( initialRule, scales );
319+
}
304320
mModel->finishedAddingRules();
305321
}
306322

‎src/gui/symbology-ng/qgsrulebasedrendererv2widget.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ class GUI_EXPORT QgsRuleBasedRendererV2Widget : public QgsRendererV2Widget, priv
110110
protected:
111111

112112
void refineRule( int type );
113-
void refineRuleCategoriesGui( const QModelIndex& index );
114-
void refineRuleRangesGui( const QModelIndex& index );
115-
void refineRuleScalesGui( const QModelIndex& index );
113+
void refineRuleCategoriesGui( const QModelIndexList& index );
114+
void refineRuleRangesGui( const QModelIndexList& index );
115+
void refineRuleScalesGui( const QModelIndexList& index );
116116

117117
QgsRuleBasedRendererV2::Rule* currentRule();
118118

‎src/ui/qgsrulebasedrendererv2widget.ui

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
</rect>
1212
</property>
1313
<layout class="QVBoxLayout" name="verticalLayout">
14+
<property name="margin">
15+
<number>0</number>
16+
</property>
1417
<item>
1518
<widget class="QTreeView" name="viewRules">
1619
<property name="contextMenuPolicy">
@@ -61,8 +64,23 @@
6164
</item>
6265
<item>
6366
<widget class="QPushButton" name="btnRefineRule">
67+
<property name="enabled">
68+
<bool>true</bool>
69+
</property>
6470
<property name="text">
65-
<string>Refine</string>
71+
<string>Refine current rules</string>
72+
</property>
73+
<property name="checkable">
74+
<bool>false</bool>
75+
</property>
76+
<property name="autoDefault">
77+
<bool>false</bool>
78+
</property>
79+
<property name="default">
80+
<bool>false</bool>
81+
</property>
82+
<property name="flat">
83+
<bool>false</bool>
6684
</property>
6785
</widget>
6886
</item>

0 commit comments

Comments
 (0)
Please sign in to comment.