|
37 | 37 | #include <QStandardItem>
|
38 | 38 | #include <QPen>
|
39 | 39 | #include <QPainter>
|
| 40 | +#include <QFileDialog> |
40 | 41 |
|
41 | 42 | QgsCategorizedSymbolRendererV2Model::QgsCategorizedSymbolRendererV2Model( QObject * parent ) : QAbstractItemModel( parent )
|
42 | 43 | , mRenderer( 0 )
|
@@ -443,6 +444,8 @@ QgsCategorizedSymbolRendererV2Widget::QgsCategorizedSymbolRendererV2Widget( QgsV
|
443 | 444 | // menus for data-defined rotation/size
|
444 | 445 | QMenu* advMenu = new QMenu;
|
445 | 446 |
|
| 447 | + advMenu->addAction( tr( "Match to saved symbols" ), this, SLOT( matchToSymbolsFromLibrary() ) ); |
| 448 | + advMenu->addAction( tr( "Match to symbols from file..." ), this, SLOT( matchToSymbolsFromXml() ) ); |
446 | 449 | advMenu->addAction( tr( "Symbol levels..." ), this, SLOT( showSymbolLevels() ) );
|
447 | 450 |
|
448 | 451 | mDataDefinedMenus = new QgsRendererV2DataDefinedMenus( advMenu, mLayer,
|
@@ -882,6 +885,80 @@ void QgsCategorizedSymbolRendererV2Widget::rowsMoved()
|
882 | 885 | viewCategories->selectionModel()->clear();
|
883 | 886 | }
|
884 | 887 |
|
| 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 | + |
885 | 962 | void QgsCategorizedSymbolRendererV2Widget::keyPressEvent( QKeyEvent* event )
|
886 | 963 | {
|
887 | 964 | if ( !event )
|
|
0 commit comments