Skip to content

Commit

Permalink
[FEATURE] Add options under categorised renderer advanced menu to
Browse files Browse the repository at this point in the history
set categories to symbols with a matching name from the style
library or an XML style file.

Thanks Lene for the great hackfest!
  • Loading branch information
nyalldawson committed May 22, 2015
1 parent 2004f22 commit 9031e98
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
26 changes: 26 additions & 0 deletions python/gui/symbology-ng/qgscategorizedsymbolrendererv2widget.sip
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ class QgsCategorizedSymbolRendererV2Widget : QgsRendererV2Widget

virtual QgsFeatureRendererV2* renderer();

/** Replaces category symbols with the symbols from a style that have a matching
* name.
* @param style style containing symbols to match with
* @return number of symbols matched
* @see matchToSymbolsFromLibrary
* @see matchToSymbolsFromXml
* @note added in QGIS 2.9
*/
int matchToSymbols( QgsStyleV2* style );

public slots:
void changeCategorizedSymbol();
void categoryColumnChanged( QString field );
Expand All @@ -30,6 +40,22 @@ class QgsCategorizedSymbolRendererV2Widget : QgsRendererV2Widget

void rowsMoved();

/** Replaces category symbols with the symbols from the users' symbol library that have a
* matching name.
* @see matchToSymbolsFromXml
* @see matchToSymbols
* @note added in QGIS 2.9
*/
void matchToSymbolsFromLibrary();

/** Prompts for selection of an xml file, then replaces category symbols with the symbols
* from the XML file with a matching name.
* @see matchToSymbolsFromLibrary
* @see matchToSymbols
* @note added in QGIS 2.9
*/
void matchToSymbolsFromXml();

protected:

void updateUiFromRenderer();
Expand Down
77 changes: 77 additions & 0 deletions src/gui/symbology-ng/qgscategorizedsymbolrendererv2widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <QStandardItem>
#include <QPen>
#include <QPainter>
#include <QFileDialog>

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

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

mDataDefinedMenus = new QgsRendererV2DataDefinedMenus( advMenu, mLayer,
Expand Down Expand Up @@ -882,6 +885,80 @@ void QgsCategorizedSymbolRendererV2Widget::rowsMoved()
viewCategories->selectionModel()->clear();
}

void QgsCategorizedSymbolRendererV2Widget::matchToSymbolsFromLibrary()
{
int matched = matchToSymbols( QgsStyleV2::defaultStyle() );
if ( matched > 0 )
{
QMessageBox::information( this, tr( "Matched symbols" ),
tr( "Matched %1 categories to symbols." ).arg( matched ) );
}
else
{
QMessageBox::warning( this, tr( "Matched symbols" ),
tr( "No categories could be matched to symbols in library." ) );
}
}

int QgsCategorizedSymbolRendererV2Widget::matchToSymbols( QgsStyleV2* style )
{
if ( !mLayer || !style )
return 0;

int matched = 0;
for ( int catIdx = 0; catIdx < mRenderer->categories().count(); ++catIdx )
{
QString val = mRenderer->categories().at( catIdx ).value().toString();
QgsSymbolV2* symbol = style->symbol( val );
if ( symbol &&
(( symbol->type() == QgsSymbolV2::Marker && mLayer->geometryType() == QGis::Point )
|| ( symbol->type() == QgsSymbolV2::Line && mLayer->geometryType() == QGis::Line )
|| ( symbol->type() == QgsSymbolV2::Fill && mLayer->geometryType() == QGis::Polygon ) ) )
{
matched++;
mRenderer->updateCategorySymbol( catIdx, symbol->clone() );
}
}
mModel->updateSymbology();
return matched;
}

void QgsCategorizedSymbolRendererV2Widget::matchToSymbolsFromXml()
{
QSettings settings;
QString openFileDir = settings.value( "UI/lastMatchToSymbolsDir", "" ).toString();

QString fileName = QFileDialog::getOpenFileName( this, tr( "Match to symbols from file" ), openFileDir,
tr( "XML files (*.xml *XML)" ) );
if ( fileName.isEmpty() )
{
return;
}

QFileInfo openFileInfo( fileName );
settings.setValue( "UI/lastMatchToSymbolsDir", openFileInfo.absolutePath() );

QgsStyleV2 importedStyle;
if ( !importedStyle.importXML( fileName ) )
{
QMessageBox::warning( this, tr( "Matching error" ),
tr( "An error occured reading file:\n%1" ).arg( importedStyle.errorString() ) );
return;
}

int matched = matchToSymbols( &importedStyle );
if ( matched > 0 )
{
QMessageBox::information( this, tr( "Matched symbols" ),
tr( "Matched %1 categories to symbols from file." ).arg( matched ) );
}
else
{
QMessageBox::warning( this, tr( "Matched symbols" ),
tr( "No categories could be matched to symbols in file." ) );
}
}

void QgsCategorizedSymbolRendererV2Widget::keyPressEvent( QKeyEvent* event )
{
if ( !event )
Expand Down
26 changes: 26 additions & 0 deletions src/gui/symbology-ng/qgscategorizedsymbolrendererv2widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ class GUI_EXPORT QgsCategorizedSymbolRendererV2Widget : public QgsRendererV2Widg

virtual QgsFeatureRendererV2* renderer() override;

/** Replaces category symbols with the symbols from a style that have a matching
* name.
* @param style style containing symbols to match with
* @return number of symbols matched
* @see matchToSymbolsFromLibrary
* @see matchToSymbolsFromXml
* @note added in QGIS 2.9
*/
int matchToSymbols( QgsStyleV2* style );

public slots:
void changeCategorizedSymbol();
void categoryColumnChanged( QString field );
Expand All @@ -98,6 +108,22 @@ class GUI_EXPORT QgsCategorizedSymbolRendererV2Widget : public QgsRendererV2Widg

void rowsMoved();

/** Replaces category symbols with the symbols from the users' symbol library that have a
* matching name.
* @see matchToSymbolsFromXml
* @see matchToSymbols
* @note added in QGIS 2.9
*/
void matchToSymbolsFromLibrary();

/** Prompts for selection of an xml file, then replaces category symbols with the symbols
* from the XML file with a matching name.
* @see matchToSymbolsFromLibrary
* @see matchToSymbols
* @note added in QGIS 2.9
*/
void matchToSymbolsFromXml();

protected:

void updateUiFromRenderer();
Expand Down

0 comments on commit 9031e98

Please sign in to comment.