Skip to content

Commit 9031e98

Browse files
committed
[FEATURE] Add options under categorised renderer advanced menu to
set categories to symbols with a matching name from the style library or an XML style file. Thanks Lene for the great hackfest!
1 parent 2004f22 commit 9031e98

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

python/gui/symbology-ng/qgscategorizedsymbolrendererv2widget.sip

+26
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ class QgsCategorizedSymbolRendererV2Widget : QgsRendererV2Widget
1212

1313
virtual QgsFeatureRendererV2* renderer();
1414

15+
/** Replaces category symbols with the symbols from a style that have a matching
16+
* name.
17+
* @param style style containing symbols to match with
18+
* @return number of symbols matched
19+
* @see matchToSymbolsFromLibrary
20+
* @see matchToSymbolsFromXml
21+
* @note added in QGIS 2.9
22+
*/
23+
int matchToSymbols( QgsStyleV2* style );
24+
1525
public slots:
1626
void changeCategorizedSymbol();
1727
void categoryColumnChanged( QString field );
@@ -30,6 +40,22 @@ class QgsCategorizedSymbolRendererV2Widget : QgsRendererV2Widget
3040

3141
void rowsMoved();
3242

43+
/** Replaces category symbols with the symbols from the users' symbol library that have a
44+
* matching name.
45+
* @see matchToSymbolsFromXml
46+
* @see matchToSymbols
47+
* @note added in QGIS 2.9
48+
*/
49+
void matchToSymbolsFromLibrary();
50+
51+
/** Prompts for selection of an xml file, then replaces category symbols with the symbols
52+
* from the XML file with a matching name.
53+
* @see matchToSymbolsFromLibrary
54+
* @see matchToSymbols
55+
* @note added in QGIS 2.9
56+
*/
57+
void matchToSymbolsFromXml();
58+
3359
protected:
3460

3561
void updateUiFromRenderer();

src/gui/symbology-ng/qgscategorizedsymbolrendererv2widget.cpp

+77
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <QStandardItem>
3838
#include <QPen>
3939
#include <QPainter>
40+
#include <QFileDialog>
4041

4142
QgsCategorizedSymbolRendererV2Model::QgsCategorizedSymbolRendererV2Model( QObject * parent ) : QAbstractItemModel( parent )
4243
, mRenderer( 0 )
@@ -443,6 +444,8 @@ QgsCategorizedSymbolRendererV2Widget::QgsCategorizedSymbolRendererV2Widget( QgsV
443444
// menus for data-defined rotation/size
444445
QMenu* advMenu = new QMenu;
445446

447+
advMenu->addAction( tr( "Match to saved symbols" ), this, SLOT( matchToSymbolsFromLibrary() ) );
448+
advMenu->addAction( tr( "Match to symbols from file..." ), this, SLOT( matchToSymbolsFromXml() ) );
446449
advMenu->addAction( tr( "Symbol levels..." ), this, SLOT( showSymbolLevels() ) );
447450

448451
mDataDefinedMenus = new QgsRendererV2DataDefinedMenus( advMenu, mLayer,
@@ -882,6 +885,80 @@ void QgsCategorizedSymbolRendererV2Widget::rowsMoved()
882885
viewCategories->selectionModel()->clear();
883886
}
884887

888+
void QgsCategorizedSymbolRendererV2Widget::matchToSymbolsFromLibrary()
889+
{
890+
int matched = matchToSymbols( QgsStyleV2::defaultStyle() );
891+
if ( matched > 0 )
892+
{
893+
QMessageBox::information( this, tr( "Matched symbols" ),
894+
tr( "Matched %1 categories to symbols." ).arg( matched ) );
895+
}
896+
else
897+
{
898+
QMessageBox::warning( this, tr( "Matched symbols" ),
899+
tr( "No categories could be matched to symbols in library." ) );
900+
}
901+
}
902+
903+
int QgsCategorizedSymbolRendererV2Widget::matchToSymbols( QgsStyleV2* style )
904+
{
905+
if ( !mLayer || !style )
906+
return 0;
907+
908+
int matched = 0;
909+
for ( int catIdx = 0; catIdx < mRenderer->categories().count(); ++catIdx )
910+
{
911+
QString val = mRenderer->categories().at( catIdx ).value().toString();
912+
QgsSymbolV2* symbol = style->symbol( val );
913+
if ( symbol &&
914+
(( symbol->type() == QgsSymbolV2::Marker && mLayer->geometryType() == QGis::Point )
915+
|| ( symbol->type() == QgsSymbolV2::Line && mLayer->geometryType() == QGis::Line )
916+
|| ( symbol->type() == QgsSymbolV2::Fill && mLayer->geometryType() == QGis::Polygon ) ) )
917+
{
918+
matched++;
919+
mRenderer->updateCategorySymbol( catIdx, symbol->clone() );
920+
}
921+
}
922+
mModel->updateSymbology();
923+
return matched;
924+
}
925+
926+
void QgsCategorizedSymbolRendererV2Widget::matchToSymbolsFromXml()
927+
{
928+
QSettings settings;
929+
QString openFileDir = settings.value( "UI/lastMatchToSymbolsDir", "" ).toString();
930+
931+
QString fileName = QFileDialog::getOpenFileName( this, tr( "Match to symbols from file" ), openFileDir,
932+
tr( "XML files (*.xml *XML)" ) );
933+
if ( fileName.isEmpty() )
934+
{
935+
return;
936+
}
937+
938+
QFileInfo openFileInfo( fileName );
939+
settings.setValue( "UI/lastMatchToSymbolsDir", openFileInfo.absolutePath() );
940+
941+
QgsStyleV2 importedStyle;
942+
if ( !importedStyle.importXML( fileName ) )
943+
{
944+
QMessageBox::warning( this, tr( "Matching error" ),
945+
tr( "An error occured reading file:\n%1" ).arg( importedStyle.errorString() ) );
946+
return;
947+
}
948+
949+
int matched = matchToSymbols( &importedStyle );
950+
if ( matched > 0 )
951+
{
952+
QMessageBox::information( this, tr( "Matched symbols" ),
953+
tr( "Matched %1 categories to symbols from file." ).arg( matched ) );
954+
}
955+
else
956+
{
957+
QMessageBox::warning( this, tr( "Matched symbols" ),
958+
tr( "No categories could be matched to symbols in file." ) );
959+
}
960+
}
961+
885962
void QgsCategorizedSymbolRendererV2Widget::keyPressEvent( QKeyEvent* event )
886963
{
887964
if ( !event )

src/gui/symbology-ng/qgscategorizedsymbolrendererv2widget.h

+26
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ class GUI_EXPORT QgsCategorizedSymbolRendererV2Widget : public QgsRendererV2Widg
8080

8181
virtual QgsFeatureRendererV2* renderer() override;
8282

83+
/** Replaces category symbols with the symbols from a style that have a matching
84+
* name.
85+
* @param style style containing symbols to match with
86+
* @return number of symbols matched
87+
* @see matchToSymbolsFromLibrary
88+
* @see matchToSymbolsFromXml
89+
* @note added in QGIS 2.9
90+
*/
91+
int matchToSymbols( QgsStyleV2* style );
92+
8393
public slots:
8494
void changeCategorizedSymbol();
8595
void categoryColumnChanged( QString field );
@@ -98,6 +108,22 @@ class GUI_EXPORT QgsCategorizedSymbolRendererV2Widget : public QgsRendererV2Widg
98108

99109
void rowsMoved();
100110

111+
/** Replaces category symbols with the symbols from the users' symbol library that have a
112+
* matching name.
113+
* @see matchToSymbolsFromXml
114+
* @see matchToSymbols
115+
* @note added in QGIS 2.9
116+
*/
117+
void matchToSymbolsFromLibrary();
118+
119+
/** Prompts for selection of an xml file, then replaces category symbols with the symbols
120+
* from the XML file with a matching name.
121+
* @see matchToSymbolsFromLibrary
122+
* @see matchToSymbols
123+
* @note added in QGIS 2.9
124+
*/
125+
void matchToSymbolsFromXml();
126+
101127
protected:
102128

103129
void updateUiFromRenderer();

0 commit comments

Comments
 (0)