Skip to content

Commit ca6a225

Browse files
committedSep 5, 2011
Move symbol change slots to render widget parent class
1 parent 1490f58 commit ca6a225

7 files changed

+217
-146
lines changed
 

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

Lines changed: 0 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
#include "qgssymbolv2selectordialog.h"
1111

1212
#include "qgsvectorlayer.h"
13-
#include <QColorDialog>
14-
#include <QInputDialog>
1513
#include <QMenu>
1614
#include <QMessageBox>
1715
#include <QStandardItemModel>
@@ -406,131 +404,6 @@ void QgsCategorizedSymbolRendererV2Widget::sizeScaleFieldChanged( QString fldNam
406404
mRenderer->setSizeScaleField( fldName );
407405
}
408406

409-
void QgsCategorizedSymbolRendererV2Widget::contextMenuViewCategories( const QPoint& p )
410-
{
411-
QMenu contextMenu;
412-
contextMenu.addAction( tr( "Change color" ), this, SLOT( changeSymbolColor( ) ) );
413-
contextMenu.addAction( tr( "Change transparency" ), this, SLOT( changeSymbolTransparency() ) );
414-
contextMenu.addAction( tr( "Change output unit" ), this, SLOT( changeSymbolUnit() ) );
415-
416-
if ( mLayer && mLayer->geometryType() == QGis::Line )
417-
{
418-
contextMenu.addAction( tr( "Change width" ), this, SLOT( changeSymbolWidth() ) );
419-
}
420-
else if ( mLayer && mLayer->geometryType() == QGis::Point )
421-
{
422-
contextMenu.addAction( tr( "Change size" ), this, SLOT( changeSymbolSize() ) );
423-
}
424-
contextMenu.exec( viewCategories->mapToGlobal( p ) );
425-
}
426-
427-
void QgsCategorizedSymbolRendererV2Widget::changeSymbolColor()
428-
{
429-
QList<QgsSymbolV2*> symbolList = selectedSymbols();
430-
if ( symbolList.size() < 1 )
431-
{
432-
return;
433-
}
434-
435-
QColor color = QColorDialog::getColor( symbolList.at( 0 )->color(), this );
436-
if ( color.isValid() )
437-
{
438-
QList<QgsSymbolV2*>::iterator symbolIt = symbolList.begin();
439-
for ( ; symbolIt != symbolList.end(); ++symbolIt )
440-
{
441-
( *symbolIt )->setColor( color );
442-
}
443-
populateCategories();
444-
}
445-
}
446-
447-
void QgsCategorizedSymbolRendererV2Widget::changeSymbolTransparency()
448-
{
449-
QList<QgsSymbolV2*> symbolList = selectedSymbols();
450-
if ( symbolList.size() < 1 )
451-
{
452-
return;
453-
}
454-
455-
bool ok;
456-
double transparency = QInputDialog::getDouble( this, tr( "Transparency" ), tr( "Change symbol transparency" ), 1 - symbolList.at( 0 )->alpha(), 0.0, 1.0, 1, &ok );
457-
if ( ok )
458-
{
459-
QList<QgsSymbolV2*>::iterator symbolIt = symbolList.begin();
460-
for ( ; symbolIt != symbolList.end(); ++symbolIt )
461-
{
462-
( *symbolIt )->setAlpha( 1 - transparency );
463-
}
464-
populateCategories();
465-
}
466-
}
467-
468-
void QgsCategorizedSymbolRendererV2Widget::changeSymbolUnit()
469-
{
470-
QList<QgsSymbolV2*> symbolList = selectedSymbols();
471-
if ( symbolList.size() < 1 )
472-
{
473-
return;
474-
}
475-
476-
bool ok;
477-
int currentUnit = ( symbolList.at( 0 )->outputUnit() == QgsSymbolV2::MM ) ? 0 : 1;
478-
QString item = QInputDialog::getItem( this, tr( "Symbol unit" ), tr( "Select symbol unit" ), QStringList() << tr( "Millimeter" ) << tr( "Map unit" ), currentUnit, false, &ok );
479-
if ( ok )
480-
{
481-
QgsSymbolV2::OutputUnit unit = ( item.compare( tr( "Millimeter" ) ) == 0 ) ? QgsSymbolV2::MM : QgsSymbolV2::MapUnit;
482-
483-
QList<QgsSymbolV2*>::iterator symbolIt = symbolList.begin();
484-
for ( ; symbolIt != symbolList.end(); ++symbolIt )
485-
{
486-
( *symbolIt )->setOutputUnit( unit );
487-
}
488-
populateCategories();
489-
}
490-
}
491-
492-
void QgsCategorizedSymbolRendererV2Widget::changeSymbolWidth()
493-
{
494-
QList<QgsSymbolV2*> symbolList = selectedSymbols();
495-
if ( symbolList.size() < 1 )
496-
{
497-
return;
498-
}
499-
500-
bool ok;
501-
double width = QInputDialog::getDouble( this, tr( "Width" ), tr( "Change symbol width" ), dynamic_cast<QgsLineSymbolV2*>( symbolList.at( 0 ) )->width(), 0.0, 999999, 1, &ok );
502-
if ( ok )
503-
{
504-
QList<QgsSymbolV2*>::iterator symbolIt = symbolList.begin();
505-
for ( ; symbolIt != symbolList.end(); ++symbolIt )
506-
{
507-
dynamic_cast<QgsLineSymbolV2*>( *symbolIt )->setWidth( width );
508-
}
509-
populateCategories();
510-
}
511-
}
512-
513-
void QgsCategorizedSymbolRendererV2Widget::changeSymbolSize()
514-
{
515-
QList<QgsSymbolV2*> symbolList = selectedSymbols();
516-
if ( symbolList.size() < 1 )
517-
{
518-
return;
519-
}
520-
521-
bool ok;
522-
double size = QInputDialog::getDouble( this, tr( "Size" ), tr( "Change symbol size" ), dynamic_cast<QgsMarkerSymbolV2*>( symbolList.at( 0 ) )->size(), 0.0, 999999, 1, &ok );
523-
if ( ok )
524-
{
525-
QList<QgsSymbolV2*>::iterator symbolIt = symbolList.begin();
526-
for ( ; symbolIt != symbolList.end(); ++symbolIt )
527-
{
528-
dynamic_cast<QgsMarkerSymbolV2*>( *symbolIt )->setSize( size );
529-
}
530-
populateCategories();
531-
}
532-
}
533-
534407
QList<QgsSymbolV2*> QgsCategorizedSymbolRendererV2Widget::selectedSymbols()
535408
{
536409
QList<QgsSymbolV2*> selectedSymbols;

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

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,6 @@ class GUI_EXPORT QgsCategorizedSymbolRendererV2Widget : public QgsRendererV2Widg
3535
void rotationFieldChanged( QString fldName );
3636
void sizeScaleFieldChanged( QString fldName );
3737

38-
protected slots:
39-
void addCategory();
40-
void contextMenuViewCategories( const QPoint& p );
41-
/**Change color of selected symbols*/
42-
void changeSymbolColor();
43-
/**Change opacity of selected symbols*/
44-
void changeSymbolTransparency();
45-
/**Change units mm/map units of selected symbols*/
46-
void changeSymbolUnit();
47-
/**Change line widths of selected symbols*/
48-
void changeSymbolWidth();
49-
/**Change marker sizes of selected symbols*/
50-
void changeSymbolSize();
51-
5238
protected:
5339

5440
void updateUiFromRenderer();
@@ -74,6 +60,10 @@ class GUI_EXPORT QgsCategorizedSymbolRendererV2Widget : public QgsRendererV2Widg
7460
void changeCategorySymbol();
7561

7662
QList<QgsSymbolV2*> selectedSymbols();
63+
void refreshSymbolView() { populateCategories(); }
64+
65+
protected slots:
66+
void addCategory();
7767

7868
protected:
7969
QgsCategorizedSymbolRendererV2* mRenderer;

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

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
#include "qgsvectorlayer.h"
99

10-
#include "qgsgraduatedsymbolrendererv2.h"
11-
1210
#include "qgssymbolv2selectordialog.h"
1311

1412
#include "qgsludialog.h"
@@ -54,12 +52,14 @@ QgsGraduatedSymbolRendererV2Widget::QgsGraduatedSymbolRendererV2Widget( QgsVecto
5452
labels << tr( "Range" ) << tr( "Label" );
5553
mg->setHorizontalHeaderLabels( labels );
5654
viewGraduated->setModel( mg );
55+
viewGraduated->setSelectionMode( QAbstractItemView::ExtendedSelection );
5756

5857
mGraduatedSymbol = QgsSymbolV2::defaultSymbol( mLayer->geometryType() );
5958

6059
connect( cboGraduatedColumn, SIGNAL( currentIndexChanged( int ) ), this, SLOT( graduatedColumnChanged() ) );
6160
connect( viewGraduated, SIGNAL( doubleClicked( const QModelIndex & ) ), this, SLOT( rangesDoubleClicked( const QModelIndex & ) ) );
6261
connect( viewGraduated, SIGNAL( clicked( const QModelIndex & ) ), this, SLOT( rangesClicked( const QModelIndex & ) ) );
62+
connect( viewGraduated, SIGNAL( customContextMenuRequested( const QPoint& ) ), this, SLOT( contextMenuViewCategories( const QPoint& ) ) );
6363
connect( mg, SIGNAL( itemChanged( QStandardItem * ) ), this, SLOT( changeCurrentValue( QStandardItem * ) ) );
6464
connect( btnGraduatedClassify, SIGNAL( clicked() ), this, SLOT( classifyGraduated() ) );
6565
connect( btnChangeGraduatedSymbol, SIGNAL( clicked() ), this, SLOT( changeGraduatedSymbol() ) );
@@ -339,3 +339,57 @@ void QgsGraduatedSymbolRendererV2Widget::sizeScaleFieldChanged( QString fldName
339339
{
340340
mRenderer->setSizeScaleField( fldName );
341341
}
342+
343+
QList<QgsSymbolV2*> QgsGraduatedSymbolRendererV2Widget::selectedSymbols()
344+
{
345+
QList<QgsSymbolV2*> selectedSymbols;
346+
347+
QItemSelectionModel* m = viewGraduated->selectionModel();
348+
QModelIndexList selectedIndexes = m->selectedRows( 1 );
349+
if ( m && selectedIndexes.size() > 0 )
350+
{
351+
const QgsRangeList& ranges = mRenderer->ranges();
352+
QModelIndexList::const_iterator indexIt = selectedIndexes.constBegin();
353+
for ( ; indexIt != selectedIndexes.constEnd(); ++indexIt )
354+
{
355+
QStandardItem* currentItem = qobject_cast<const QStandardItemModel*>( m->model() )->itemFromIndex( *indexIt );
356+
if ( currentItem )
357+
{
358+
QStringList list = currentItem->data( 0 ).toString().split( " " );
359+
if ( list.size() < 3 )
360+
{
361+
continue;
362+
}
363+
364+
double lowerBound = list.at( 0 ).toDouble();
365+
double upperBound = list.at( 2 ).toDouble();
366+
QgsSymbolV2* s = findSymbolForRange( lowerBound, upperBound, ranges );
367+
if ( s )
368+
{
369+
selectedSymbols.append( s );
370+
}
371+
}
372+
}
373+
}
374+
return selectedSymbols;
375+
}
376+
377+
QgsSymbolV2* QgsGraduatedSymbolRendererV2Widget::findSymbolForRange( double lowerBound, double upperBound, const QgsRangeList& ranges ) const
378+
{
379+
for ( QgsRangeList::const_iterator it = ranges.begin(); it != ranges.end(); ++it )
380+
{
381+
//range string has been created with option 'f',4
382+
if ( doubleNear( lowerBound, it->lowerValue(), 0.0001 ) && doubleNear( upperBound, it->upperValue(), 0.0001 ) )
383+
{
384+
return it->symbol();
385+
}
386+
}
387+
return 0;
388+
}
389+
390+
void QgsGraduatedSymbolRendererV2Widget::refreshSymbolView()
391+
{
392+
populateRanges();
393+
}
394+
395+

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
#ifndef QGSGRADUATEDSYMBOLRENDERERV2WIDGET_H
22
#define QGSGRADUATEDSYMBOLRENDERERV2WIDGET_H
33

4+
#include "qgsgraduatedsymbolrendererv2.h"
45
#include "qgsrendererv2widget.h"
56
#include <QStandardItem>
67

7-
class QgsGraduatedSymbolRendererV2;
8-
98
#include "ui_qgsgraduatedsymbolrendererv2widget.h"
109

1110
class GUI_EXPORT QgsGraduatedSymbolRendererV2Widget : public QgsRendererV2Widget, private Ui::QgsGraduatedSymbolRendererV2Widget
@@ -53,7 +52,9 @@ class GUI_EXPORT QgsGraduatedSymbolRendererV2Widget : public QgsRendererV2Widget
5352
void changeRangeSymbol( int rangeIdx );
5453
void changeRange( int rangeIdx );
5554

56-
55+
QList<QgsSymbolV2*> selectedSymbols();
56+
QgsSymbolV2* findSymbolForRange( double lowerBound, double upperBound, const QgsRangeList& ranges ) const;
57+
void refreshSymbolView();
5758

5859

5960
protected:

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

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,142 @@
11
#include "qgsrendererv2widget.h"
2+
#include "qgssymbolv2.h"
3+
#include "qgsvectorlayer.h"
4+
#include <QColorDialog>
5+
#include <QInputDialog>
6+
#include <QMenu>
27

38

49
QgsRendererV2Widget::QgsRendererV2Widget( QgsVectorLayer* layer, QgsStyleV2* style )
510
: QWidget(), mLayer( layer ), mStyle( style )
611
{
712
}
813

14+
void QgsRendererV2Widget::contextMenuViewCategories( const QPoint& p )
15+
{
16+
QMenu contextMenu;
17+
contextMenu.addAction( tr( "Change color" ), this, SLOT( changeSymbolColor( ) ) );
18+
contextMenu.addAction( tr( "Change transparency" ), this, SLOT( changeSymbolTransparency() ) );
19+
contextMenu.addAction( tr( "Change output unit" ), this, SLOT( changeSymbolUnit() ) );
20+
21+
if ( mLayer && mLayer->geometryType() == QGis::Line )
22+
{
23+
contextMenu.addAction( tr( "Change width" ), this, SLOT( changeSymbolWidth() ) );
24+
}
25+
else if ( mLayer && mLayer->geometryType() == QGis::Point )
26+
{
27+
contextMenu.addAction( tr( "Change size" ), this, SLOT( changeSymbolSize() ) );
28+
}
29+
30+
contextMenu.exec( QCursor::pos() );
31+
}
32+
33+
void QgsRendererV2Widget::changeSymbolColor()
34+
{
35+
QList<QgsSymbolV2*> symbolList = selectedSymbols();
36+
if ( symbolList.size() < 1 )
37+
{
38+
return;
39+
}
40+
41+
QColor color = QColorDialog::getColor( symbolList.at( 0 )->color(), this );
42+
if ( color.isValid() )
43+
{
44+
QList<QgsSymbolV2*>::iterator symbolIt = symbolList.begin();
45+
for ( ; symbolIt != symbolList.end(); ++symbolIt )
46+
{
47+
( *symbolIt )->setColor( color );
48+
}
49+
//populateCategories();
50+
}
51+
}
52+
53+
void QgsRendererV2Widget::changeSymbolTransparency()
54+
{
55+
QList<QgsSymbolV2*> symbolList = selectedSymbols();
56+
if ( symbolList.size() < 1 )
57+
{
58+
return;
59+
}
60+
61+
bool ok;
62+
double transparency = QInputDialog::getDouble( this, tr( "Transparency" ), tr( "Change symbol transparency" ), 1 - symbolList.at( 0 )->alpha(), 0.0, 1.0, 1, &ok );
63+
if ( ok )
64+
{
65+
QList<QgsSymbolV2*>::iterator symbolIt = symbolList.begin();
66+
for ( ; symbolIt != symbolList.end(); ++symbolIt )
67+
{
68+
( *symbolIt )->setAlpha( 1 - transparency );
69+
}
70+
refreshSymbolView();
71+
}
72+
}
73+
74+
void QgsRendererV2Widget::changeSymbolUnit()
75+
{
76+
QList<QgsSymbolV2*> symbolList = selectedSymbols();
77+
if ( symbolList.size() < 1 )
78+
{
79+
return;
80+
}
81+
82+
bool ok;
83+
int currentUnit = ( symbolList.at( 0 )->outputUnit() == QgsSymbolV2::MM ) ? 0 : 1;
84+
QString item = QInputDialog::getItem( this, tr( "Symbol unit" ), tr( "Select symbol unit" ), QStringList() << tr( "Millimeter" ) << tr( "Map unit" ), currentUnit, false, &ok );
85+
if ( ok )
86+
{
87+
QgsSymbolV2::OutputUnit unit = ( item.compare( tr( "Millimeter" ) ) == 0 ) ? QgsSymbolV2::MM : QgsSymbolV2::MapUnit;
88+
89+
QList<QgsSymbolV2*>::iterator symbolIt = symbolList.begin();
90+
for ( ; symbolIt != symbolList.end(); ++symbolIt )
91+
{
92+
( *symbolIt )->setOutputUnit( unit );
93+
}
94+
refreshSymbolView();
95+
}
96+
}
97+
98+
void QgsRendererV2Widget::changeSymbolWidth()
99+
{
100+
QList<QgsSymbolV2*> symbolList = selectedSymbols();
101+
if ( symbolList.size() < 1 )
102+
{
103+
return;
104+
}
105+
106+
bool ok;
107+
double width = QInputDialog::getDouble( this, tr( "Width" ), tr( "Change symbol width" ), dynamic_cast<QgsLineSymbolV2*>( symbolList.at( 0 ) )->width(), 0.0, 999999, 1, &ok );
108+
if ( ok )
109+
{
110+
QList<QgsSymbolV2*>::iterator symbolIt = symbolList.begin();
111+
for ( ; symbolIt != symbolList.end(); ++symbolIt )
112+
{
113+
dynamic_cast<QgsLineSymbolV2*>( *symbolIt )->setWidth( width );
114+
}
115+
refreshSymbolView();
116+
}
117+
}
118+
119+
void QgsRendererV2Widget::changeSymbolSize()
120+
{
121+
QList<QgsSymbolV2*> symbolList = selectedSymbols();
122+
if ( symbolList.size() < 1 )
123+
{
124+
return;
125+
}
126+
127+
bool ok;
128+
double size = QInputDialog::getDouble( this, tr( "Size" ), tr( "Change symbol size" ), dynamic_cast<QgsMarkerSymbolV2*>( symbolList.at( 0 ) )->size(), 0.0, 999999, 1, &ok );
129+
if ( ok )
130+
{
131+
QList<QgsSymbolV2*>::iterator symbolIt = symbolList.begin();
132+
for ( ; symbolIt != symbolList.end(); ++symbolIt )
133+
{
134+
dynamic_cast<QgsMarkerSymbolV2*>( *symbolIt )->setSize( size );
135+
}
136+
refreshSymbolView();
137+
}
138+
}
139+
9140

10141

11142
////////////

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class QgsSymbolV2SelectorDialog;
2222
*/
2323
class GUI_EXPORT QgsRendererV2Widget : public QWidget
2424
{
25+
Q_OBJECT
2526
public:
2627
QgsRendererV2Widget( QgsVectorLayer* layer, QgsStyleV2* style );
2728

@@ -33,6 +34,24 @@ class GUI_EXPORT QgsRendererV2Widget : public QWidget
3334
protected:
3435
QgsVectorLayer* mLayer;
3536
QgsStyleV2* mStyle;
37+
38+
/**Subclasses may provide the capability of changing multiple symbols at once by implementing the following two methods
39+
and by connecting the slot contextMenuViewCategories(const QPoint&)*/
40+
virtual QList<QgsSymbolV2*> selectedSymbols() { return QList<QgsSymbolV2*>(); }
41+
virtual void refreshSymbolView() {}
42+
43+
protected slots:
44+
void contextMenuViewCategories( const QPoint& p );
45+
/**Change color of selected symbols*/
46+
void changeSymbolColor();
47+
/**Change opacity of selected symbols*/
48+
void changeSymbolTransparency();
49+
/**Change units mm/map units of selected symbols*/
50+
void changeSymbolUnit();
51+
/**Change line widths of selected symbols*/
52+
void changeSymbolWidth();
53+
/**Change marker sizes of selected symbols*/
54+
void changeSymbolSize();
3655
};
3756

3857

‎src/ui/qgsgraduatedsymbolrendererv2widget.ui

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@
153153
</item>
154154
<item>
155155
<widget class="QTreeView" name="viewGraduated">
156+
<property name="contextMenuPolicy">
157+
<enum>Qt::CustomContextMenu</enum>
158+
</property>
156159
<property name="rootIsDecorated">
157160
<bool>false</bool>
158161
</property>

0 commit comments

Comments
 (0)
Please sign in to comment.