Skip to content

Commit fab96ed

Browse files
committed
add project defaults for layer symbology
1 parent 3a663fb commit fab96ed

7 files changed

+476
-15
lines changed

src/app/qgsprojectproperties.cpp

+149
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
#include "qgssnappingdialog.h"
3232
#include "qgsrasterlayer.h"
3333
#include "qgsgenericprojectionselector.h"
34+
#include "qgsstylev2.h"
35+
#include "qgssymbolv2.h"
36+
#include "qgsstylev2managerdialog.h"
37+
#include "qgsvectorcolorrampv2.h"
38+
#include "qgssymbolv2propertiesdialog.h"
3439

3540
//qt includes
3641
#include <QColorDialog>
@@ -276,6 +281,10 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas* mapCanvas, QWidget *pa
276281
twWFSLayers->setRowCount( j );
277282
twWFSLayers->verticalHeader()->setResizeMode( QHeaderView::ResizeToContents );
278283

284+
// Default Styles
285+
mStyle = QgsStyleV2::defaultStyle();
286+
populateStyles();
287+
279288
restoreState();
280289
}
281290

@@ -506,6 +515,13 @@ void QgsProjectProperties::apply()
506515
}
507516
QgsProject::instance()->writeEntry( "WFSLayers", "/", wfsLayerList );
508517

518+
// Default Styles
519+
QgsProject::instance()->writeEntry( "DefaultStyles", "/Marker", cboStyleMarker->currentText() );
520+
QgsProject::instance()->writeEntry( "DefaultStyles", "/Line", cboStyleLine->currentText() );
521+
QgsProject::instance()->writeEntry( "DefaultStyles", "/Fill", cboStyleFill->currentText() );
522+
QgsProject::instance()->writeEntry( "DefaultStyles", "/ColorRamp", cboStyleColorRamp->currentText() );
523+
QgsProject::instance()->writeEntry( "DefaultStyles", "/RandomColors", cbxStyleRandomColors->isChecked() );
524+
509525
//todo XXX set canvas color
510526
emit refresh();
511527
}
@@ -677,3 +693,136 @@ void QgsProjectProperties::on_pbnWMSSetUsedSRS_clicked()
677693
mWMSList->clear();
678694
mWMSList->addItems( crsList.values() );
679695
}
696+
697+
void QgsProjectProperties::populateStyles()
698+
{
699+
// Styles - taken from qgsstylev2managerdialog
700+
701+
// use QComboBox and QString lists for shorter code
702+
QStringList prefList;
703+
QList<QComboBox*> cboList;
704+
cboList << cboStyleMarker;
705+
prefList << QgsProject::instance()->readEntry( "DefaultStyles", "/Marker", "" );
706+
cboList << cboStyleLine;
707+
prefList << QgsProject::instance()->readEntry( "DefaultStyles", "/Line", "" );
708+
cboList << cboStyleFill;
709+
prefList << QgsProject::instance()->readEntry( "DefaultStyles", "/Fill", "" );
710+
cboList << cboStyleColorRamp;
711+
prefList << QgsProject::instance()->readEntry( "DefaultStyles", "/ColorRamp", "" );
712+
for ( int i = 0; i < cboList.count(); i++ )
713+
{
714+
cboList[i]->clear();
715+
cboList[i]->addItem( "" );
716+
}
717+
718+
// populate symbols
719+
QStringList symbolNames = mStyle->symbolNames();
720+
for ( int i = 0; i < symbolNames.count(); ++i )
721+
{
722+
QString name = symbolNames[i];
723+
QgsSymbolV2* symbol = mStyle->symbol( name );
724+
QComboBox* cbo = 0;
725+
switch ( symbol->type() )
726+
{
727+
case QgsSymbolV2::Marker :
728+
cbo = cboStyleMarker;
729+
break;
730+
case QgsSymbolV2::Line :
731+
cbo = cboStyleLine;
732+
break;
733+
case QgsSymbolV2::Fill :
734+
cbo = cboStyleFill;
735+
break;
736+
}
737+
if ( cbo )
738+
{
739+
QIcon icon = QgsSymbolLayerV2Utils::symbolPreviewIcon( symbol, cbo->iconSize() );
740+
cbo->addItem( icon, name );
741+
}
742+
delete symbol;
743+
}
744+
745+
// populate color ramps
746+
QStringList colorRamps = mStyle->colorRampNames();
747+
for ( int i = 0; i < colorRamps.count(); ++i )
748+
{
749+
QString name = colorRamps[i];
750+
QgsVectorColorRampV2* ramp = mStyle->colorRamp( name );
751+
QIcon icon = QgsSymbolLayerV2Utils::colorRampPreviewIcon( ramp, cboStyleColorRamp->iconSize() );
752+
cboStyleColorRamp->addItem( icon, name );
753+
delete ramp;
754+
}
755+
756+
// set current index if found
757+
for ( int i = 0; i < cboList.count(); i++ )
758+
{
759+
int index = cboList[i]->findText( prefList[i], Qt::MatchCaseSensitive );
760+
if ( index >= 0 )
761+
cboList[i]->setCurrentIndex( index );
762+
}
763+
764+
// random colors?
765+
cbxStyleRandomColors->setChecked( QgsProject::instance()->readBoolEntry( "DefaultStyles", "/RandomColors", true ) );
766+
767+
}
768+
769+
void QgsProjectProperties::on_pbtnStyleManager_clicked()
770+
{
771+
QgsStyleV2ManagerDialog dlg( mStyle, this );
772+
dlg.exec();
773+
populateStyles();
774+
}
775+
776+
void QgsProjectProperties::on_pbtnStyleMarker_clicked()
777+
{
778+
editSymbol( cboStyleMarker );
779+
}
780+
781+
void QgsProjectProperties::on_pbtnStyleLine_clicked()
782+
{
783+
editSymbol( cboStyleLine );
784+
}
785+
786+
void QgsProjectProperties::on_pbtnStyleFill_clicked()
787+
{
788+
editSymbol( cboStyleFill );
789+
}
790+
791+
void QgsProjectProperties::editSymbol( QComboBox* cbo )
792+
{
793+
QString symbolName = cbo->currentText();
794+
if ( symbolName == "" )
795+
{
796+
QMessageBox::information( this, "", tr( "Select a valid symbol" ) );
797+
return;
798+
}
799+
QgsSymbolV2* symbol = mStyle->symbol( symbolName );
800+
if ( ! symbol )
801+
{
802+
QMessageBox::warning( this, "", tr( "Invalid symbol : " ) + symbolName );
803+
return;
804+
}
805+
806+
// let the user edit the symbol and update list when done
807+
QgsSymbolV2PropertiesDialog dlg( symbol, 0, this );
808+
if ( dlg.exec() == 0 )
809+
{
810+
delete symbol;
811+
return;
812+
}
813+
814+
// by adding symbol to style with the same name the old effectively gets overwritten
815+
mStyle->addSymbol( symbolName, symbol );
816+
817+
// update icon
818+
QIcon icon = QgsSymbolLayerV2Utils::symbolPreviewIcon( symbol, cbo->iconSize() );
819+
cbo->setItemIcon( cbo->currentIndex(), icon );
820+
}
821+
822+
void QgsProjectProperties::on_pbtnStyleColorRamp_clicked()
823+
{
824+
// TODO for now just open style manager
825+
// code in QgsStyleV2ManagerDialog::editColorRamp()
826+
on_pbtnStyleManager_clicked();
827+
}
828+

src/app/qgsprojectproperties.h

+14-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include "qgscontexthelp.h"
2424

2525
class QgsMapCanvas;
26-
26+
class QgsStyleV2;
2727

2828
/*! Dialog to set project level properties
2929
@@ -91,6 +91,15 @@ class QgsProjectProperties : public QDialog, private Ui::QgsProjectPropertiesBas
9191
void on_pbnWMSRemoveSRS_clicked();
9292
void on_pbnWMSSetUsedSRS_clicked();
9393

94+
/*!
95+
* Slots for Styles
96+
*/
97+
void on_pbtnStyleManager_clicked();
98+
void on_pbtnStyleMarker_clicked();
99+
void on_pbtnStyleLine_clicked();
100+
void on_pbtnStyleFill_clicked();
101+
void on_pbtnStyleColorRamp_clicked();
102+
94103
/*!
95104
* Slot to show the context help for this dialog
96105
*/
@@ -113,6 +122,10 @@ class QgsProjectProperties : public QDialog, private Ui::QgsProjectPropertiesBas
113122

114123
private:
115124
QgsMapCanvas* mMapCanvas;
125+
QgsStyleV2* mStyle;
126+
127+
void populateStyles();
128+
void editSymbol( QComboBox* cbo );
116129

117130
/*!
118131
* Function to save dialog window state

src/core/qgsvectorlayer.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
#include "qgssymbollayerv2.h"
7171
#include "qgssinglesymbolrendererv2.h"
7272
#include "qgsdiagramrendererv2.h"
73+
#include "qgsstylev2.h"
7374

7475
#ifdef TESTPROVIDERLIB
7576
#include <dlfcn.h>

src/core/symbology-ng/qgssymbolv2.cpp

+38-7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
#include "qgslogger.h"
2525
#include "qgsrendercontext.h" // for bigSymbolPreview
2626

27+
#include "qgsproject.h"
28+
#include "qgsstylev2.h"
29+
2730
#include <QColor>
2831
#include <QImage>
2932
#include <QPainter>
@@ -60,20 +63,48 @@ QgsSymbolV2::~QgsSymbolV2()
6063

6164
QgsSymbolV2* QgsSymbolV2::defaultSymbol( QGis::GeometryType geomType )
6265
{
63-
QgsSymbolV2* s;
66+
QgsSymbolV2* s = 0;
67+
68+
// override global default if project has a default for this type
69+
QString defaultSymbol;
6470
switch ( geomType )
6571
{
66-
case QGis::Point: s = new QgsMarkerSymbolV2(); break;
67-
case QGis::Line: s = new QgsLineSymbolV2(); break;
68-
case QGis::Polygon: s = new QgsFillSymbolV2(); break;
69-
default: QgsDebugMsg( "unknown layer's geometry type" ); return NULL;
72+
case QGis::Point :
73+
defaultSymbol = QgsProject::instance()->readEntry( "DefaultStyles", "/Marker", "" );
74+
break;
75+
case QGis::Line :
76+
defaultSymbol = QgsProject::instance()->readEntry( "DefaultStyles", "/Line", "" );
77+
break;
78+
case QGis::Polygon :
79+
defaultSymbol = QgsProject::instance()->readEntry( "DefaultStyles", "/Fill", "" );
80+
break;
81+
default: defaultSymbol = ""; break;
82+
}
83+
if ( defaultSymbol != "" )
84+
s = QgsStyleV2::defaultStyle()->symbol( defaultSymbol );
85+
86+
// if no default found for this type, get global default (as previously)
87+
if ( ! s )
88+
{
89+
switch ( geomType )
90+
{
91+
case QGis::Point: s = new QgsMarkerSymbolV2(); break;
92+
case QGis::Line: s = new QgsLineSymbolV2(); break;
93+
case QGis::Polygon: s = new QgsFillSymbolV2(); break;
94+
default: QgsDebugMsg( "unknown layer's geometry type" ); return NULL;
95+
}
96+
}
97+
98+
// set random color, it project prefs allow
99+
if ( defaultSymbol == "" ||
100+
QgsProject::instance()->readBoolEntry( "DefaultStyles", "/RandomColors", true ) )
101+
{
102+
s->setColor( QColor::fromHsv( rand() % 360, 64 + rand() % 192, 128 + rand() % 128 ) );
70103
}
71104

72-
s->setColor( QColor::fromHsv( rand() % 360, 64 + rand() % 192, 128 + rand() % 128 ) );
73105
return s;
74106
}
75107

76-
77108
QgsSymbolLayerV2* QgsSymbolV2::symbolLayer( int layer )
78109
{
79110
if ( layer < 0 || layer >= mLayers.count() )

src/gui/symbology-ng/qgscategorizedsymbolrendererv2widget.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
#include "qgssymbolv2selectordialog.h"
2525

2626
#include "qgsvectorlayer.h"
27+
28+
#include "qgsproject.h"
29+
2730
#include <QMenu>
2831
#include <QMessageBox>
2932
#include <QStandardItemModel>
@@ -63,6 +66,15 @@ QgsCategorizedSymbolRendererV2Widget::QgsCategorizedSymbolRendererV2Widget( QgsV
6366

6467
cboCategorizedColorRamp->populate( mStyle );
6568

69+
// set project default color ramp
70+
QString defaultColorRamp = QgsProject::instance()->readEntry( "DefaultStyles", "/ColorRamp", "" );
71+
if ( defaultColorRamp != "" )
72+
{
73+
int index = cboCategorizedColorRamp->findText( defaultColorRamp, Qt::MatchCaseSensitive );
74+
if ( index >= 0 )
75+
cboCategorizedColorRamp->setCurrentIndex( index );
76+
}
77+
6678
QStandardItemModel* m = new QStandardItemModel( this );
6779
QStringList labels;
6880
labels << tr( "Symbol" ) << tr( "Value" ) << tr( "Label" );

src/gui/symbology-ng/qgsgraduatedsymbolrendererv2widget.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
#include "qgsludialog.h"
2727

28+
#include "qgsproject.h"
29+
2830
#include <QMenu>
2931
#include <QMessageBox>
3032
#include <QStandardItemModel>
@@ -61,6 +63,15 @@ QgsGraduatedSymbolRendererV2Widget::QgsGraduatedSymbolRendererV2Widget( QgsVecto
6163

6264
cboGraduatedColorRamp->populate( mStyle );
6365

66+
// set project default color ramp
67+
QString defaultColorRamp = QgsProject::instance()->readEntry( "DefaultStyles", "/ColorRamp", "" );
68+
if ( defaultColorRamp != "" )
69+
{
70+
int index = cboGraduatedColorRamp->findText( defaultColorRamp, Qt::MatchCaseSensitive );
71+
if ( index >= 0 )
72+
cboGraduatedColorRamp->setCurrentIndex( index );
73+
}
74+
6475
QStandardItemModel* mg = new QStandardItemModel( this );
6576
QStringList labels;
6677
labels << tr( "Range" ) << tr( "Label" );

0 commit comments

Comments
 (0)