Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add project defaults for layer symbology
  • Loading branch information
etiennesky committed Jul 17, 2012
1 parent 3a663fb commit fab96ed
Show file tree
Hide file tree
Showing 7 changed files with 476 additions and 15 deletions.
149 changes: 149 additions & 0 deletions src/app/qgsprojectproperties.cpp
Expand Up @@ -31,6 +31,11 @@
#include "qgssnappingdialog.h"
#include "qgsrasterlayer.h"
#include "qgsgenericprojectionselector.h"
#include "qgsstylev2.h"
#include "qgssymbolv2.h"
#include "qgsstylev2managerdialog.h"
#include "qgsvectorcolorrampv2.h"
#include "qgssymbolv2propertiesdialog.h"

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

// Default Styles
mStyle = QgsStyleV2::defaultStyle();
populateStyles();

restoreState();
}

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

// Default Styles
QgsProject::instance()->writeEntry( "DefaultStyles", "/Marker", cboStyleMarker->currentText() );
QgsProject::instance()->writeEntry( "DefaultStyles", "/Line", cboStyleLine->currentText() );
QgsProject::instance()->writeEntry( "DefaultStyles", "/Fill", cboStyleFill->currentText() );
QgsProject::instance()->writeEntry( "DefaultStyles", "/ColorRamp", cboStyleColorRamp->currentText() );
QgsProject::instance()->writeEntry( "DefaultStyles", "/RandomColors", cbxStyleRandomColors->isChecked() );

//todo XXX set canvas color
emit refresh();
}
Expand Down Expand Up @@ -677,3 +693,136 @@ void QgsProjectProperties::on_pbnWMSSetUsedSRS_clicked()
mWMSList->clear();
mWMSList->addItems( crsList.values() );
}

void QgsProjectProperties::populateStyles()
{
// Styles - taken from qgsstylev2managerdialog

// use QComboBox and QString lists for shorter code
QStringList prefList;
QList<QComboBox*> cboList;
cboList << cboStyleMarker;
prefList << QgsProject::instance()->readEntry( "DefaultStyles", "/Marker", "" );
cboList << cboStyleLine;
prefList << QgsProject::instance()->readEntry( "DefaultStyles", "/Line", "" );
cboList << cboStyleFill;
prefList << QgsProject::instance()->readEntry( "DefaultStyles", "/Fill", "" );
cboList << cboStyleColorRamp;
prefList << QgsProject::instance()->readEntry( "DefaultStyles", "/ColorRamp", "" );
for ( int i = 0; i < cboList.count(); i++ )
{
cboList[i]->clear();
cboList[i]->addItem( "" );
}

// populate symbols
QStringList symbolNames = mStyle->symbolNames();
for ( int i = 0; i < symbolNames.count(); ++i )
{
QString name = symbolNames[i];
QgsSymbolV2* symbol = mStyle->symbol( name );
QComboBox* cbo = 0;
switch ( symbol->type() )
{
case QgsSymbolV2::Marker :
cbo = cboStyleMarker;
break;
case QgsSymbolV2::Line :
cbo = cboStyleLine;
break;
case QgsSymbolV2::Fill :
cbo = cboStyleFill;
break;
}
if ( cbo )
{
QIcon icon = QgsSymbolLayerV2Utils::symbolPreviewIcon( symbol, cbo->iconSize() );
cbo->addItem( icon, name );
}
delete symbol;
}

// populate color ramps
QStringList colorRamps = mStyle->colorRampNames();
for ( int i = 0; i < colorRamps.count(); ++i )
{
QString name = colorRamps[i];
QgsVectorColorRampV2* ramp = mStyle->colorRamp( name );
QIcon icon = QgsSymbolLayerV2Utils::colorRampPreviewIcon( ramp, cboStyleColorRamp->iconSize() );
cboStyleColorRamp->addItem( icon, name );
delete ramp;
}

// set current index if found
for ( int i = 0; i < cboList.count(); i++ )
{
int index = cboList[i]->findText( prefList[i], Qt::MatchCaseSensitive );
if ( index >= 0 )
cboList[i]->setCurrentIndex( index );
}

// random colors?
cbxStyleRandomColors->setChecked( QgsProject::instance()->readBoolEntry( "DefaultStyles", "/RandomColors", true ) );

}

void QgsProjectProperties::on_pbtnStyleManager_clicked()
{
QgsStyleV2ManagerDialog dlg( mStyle, this );
dlg.exec();
populateStyles();
}

void QgsProjectProperties::on_pbtnStyleMarker_clicked()
{
editSymbol( cboStyleMarker );
}

void QgsProjectProperties::on_pbtnStyleLine_clicked()
{
editSymbol( cboStyleLine );
}

void QgsProjectProperties::on_pbtnStyleFill_clicked()
{
editSymbol( cboStyleFill );
}

void QgsProjectProperties::editSymbol( QComboBox* cbo )
{
QString symbolName = cbo->currentText();
if ( symbolName == "" )
{
QMessageBox::information( this, "", tr( "Select a valid symbol" ) );
return;
}
QgsSymbolV2* symbol = mStyle->symbol( symbolName );
if ( ! symbol )
{
QMessageBox::warning( this, "", tr( "Invalid symbol : " ) + symbolName );
return;
}

// let the user edit the symbol and update list when done
QgsSymbolV2PropertiesDialog dlg( symbol, 0, this );
if ( dlg.exec() == 0 )
{
delete symbol;
return;
}

// by adding symbol to style with the same name the old effectively gets overwritten
mStyle->addSymbol( symbolName, symbol );

// update icon
QIcon icon = QgsSymbolLayerV2Utils::symbolPreviewIcon( symbol, cbo->iconSize() );
cbo->setItemIcon( cbo->currentIndex(), icon );
}

void QgsProjectProperties::on_pbtnStyleColorRamp_clicked()
{
// TODO for now just open style manager
// code in QgsStyleV2ManagerDialog::editColorRamp()
on_pbtnStyleManager_clicked();
}

15 changes: 14 additions & 1 deletion src/app/qgsprojectproperties.h
Expand Up @@ -23,7 +23,7 @@
#include "qgscontexthelp.h"

class QgsMapCanvas;

class QgsStyleV2;

/*! Dialog to set project level properties
Expand Down Expand Up @@ -91,6 +91,15 @@ class QgsProjectProperties : public QDialog, private Ui::QgsProjectPropertiesBas
void on_pbnWMSRemoveSRS_clicked();
void on_pbnWMSSetUsedSRS_clicked();

/*!
* Slots for Styles
*/
void on_pbtnStyleManager_clicked();
void on_pbtnStyleMarker_clicked();
void on_pbtnStyleLine_clicked();
void on_pbtnStyleFill_clicked();
void on_pbtnStyleColorRamp_clicked();

/*!
* Slot to show the context help for this dialog
*/
Expand All @@ -113,6 +122,10 @@ class QgsProjectProperties : public QDialog, private Ui::QgsProjectPropertiesBas

private:
QgsMapCanvas* mMapCanvas;
QgsStyleV2* mStyle;

void populateStyles();
void editSymbol( QComboBox* cbo );

/*!
* Function to save dialog window state
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -70,6 +70,7 @@
#include "qgssymbollayerv2.h"
#include "qgssinglesymbolrendererv2.h"
#include "qgsdiagramrendererv2.h"
#include "qgsstylev2.h"

#ifdef TESTPROVIDERLIB
#include <dlfcn.h>
Expand Down
45 changes: 38 additions & 7 deletions src/core/symbology-ng/qgssymbolv2.cpp
Expand Up @@ -24,6 +24,9 @@
#include "qgslogger.h"
#include "qgsrendercontext.h" // for bigSymbolPreview

#include "qgsproject.h"
#include "qgsstylev2.h"

#include <QColor>
#include <QImage>
#include <QPainter>
Expand Down Expand Up @@ -60,20 +63,48 @@ QgsSymbolV2::~QgsSymbolV2()

QgsSymbolV2* QgsSymbolV2::defaultSymbol( QGis::GeometryType geomType )
{
QgsSymbolV2* s;
QgsSymbolV2* s = 0;

// override global default if project has a default for this type
QString defaultSymbol;
switch ( geomType )
{
case QGis::Point: s = new QgsMarkerSymbolV2(); break;
case QGis::Line: s = new QgsLineSymbolV2(); break;
case QGis::Polygon: s = new QgsFillSymbolV2(); break;
default: QgsDebugMsg( "unknown layer's geometry type" ); return NULL;
case QGis::Point :
defaultSymbol = QgsProject::instance()->readEntry( "DefaultStyles", "/Marker", "" );
break;
case QGis::Line :
defaultSymbol = QgsProject::instance()->readEntry( "DefaultStyles", "/Line", "" );
break;
case QGis::Polygon :
defaultSymbol = QgsProject::instance()->readEntry( "DefaultStyles", "/Fill", "" );
break;
default: defaultSymbol = ""; break;
}
if ( defaultSymbol != "" )
s = QgsStyleV2::defaultStyle()->symbol( defaultSymbol );

// if no default found for this type, get global default (as previously)
if ( ! s )
{
switch ( geomType )
{
case QGis::Point: s = new QgsMarkerSymbolV2(); break;
case QGis::Line: s = new QgsLineSymbolV2(); break;
case QGis::Polygon: s = new QgsFillSymbolV2(); break;
default: QgsDebugMsg( "unknown layer's geometry type" ); return NULL;
}
}

// set random color, it project prefs allow
if ( defaultSymbol == "" ||
QgsProject::instance()->readBoolEntry( "DefaultStyles", "/RandomColors", true ) )
{
s->setColor( QColor::fromHsv( rand() % 360, 64 + rand() % 192, 128 + rand() % 128 ) );
}

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


QgsSymbolLayerV2* QgsSymbolV2::symbolLayer( int layer )
{
if ( layer < 0 || layer >= mLayers.count() )
Expand Down
12 changes: 12 additions & 0 deletions src/gui/symbology-ng/qgscategorizedsymbolrendererv2widget.cpp
Expand Up @@ -24,6 +24,9 @@
#include "qgssymbolv2selectordialog.h"

#include "qgsvectorlayer.h"

#include "qgsproject.h"

#include <QMenu>
#include <QMessageBox>
#include <QStandardItemModel>
Expand Down Expand Up @@ -63,6 +66,15 @@ QgsCategorizedSymbolRendererV2Widget::QgsCategorizedSymbolRendererV2Widget( QgsV

cboCategorizedColorRamp->populate( mStyle );

// set project default color ramp
QString defaultColorRamp = QgsProject::instance()->readEntry( "DefaultStyles", "/ColorRamp", "" );
if ( defaultColorRamp != "" )
{
int index = cboCategorizedColorRamp->findText( defaultColorRamp, Qt::MatchCaseSensitive );
if ( index >= 0 )
cboCategorizedColorRamp->setCurrentIndex( index );
}

QStandardItemModel* m = new QStandardItemModel( this );
QStringList labels;
labels << tr( "Symbol" ) << tr( "Value" ) << tr( "Label" );
Expand Down
11 changes: 11 additions & 0 deletions src/gui/symbology-ng/qgsgraduatedsymbolrendererv2widget.cpp
Expand Up @@ -25,6 +25,8 @@

#include "qgsludialog.h"

#include "qgsproject.h"

#include <QMenu>
#include <QMessageBox>
#include <QStandardItemModel>
Expand Down Expand Up @@ -61,6 +63,15 @@ QgsGraduatedSymbolRendererV2Widget::QgsGraduatedSymbolRendererV2Widget( QgsVecto

cboGraduatedColorRamp->populate( mStyle );

// set project default color ramp
QString defaultColorRamp = QgsProject::instance()->readEntry( "DefaultStyles", "/ColorRamp", "" );
if ( defaultColorRamp != "" )
{
int index = cboGraduatedColorRamp->findText( defaultColorRamp, Qt::MatchCaseSensitive );
if ( index >= 0 )
cboGraduatedColorRamp->setCurrentIndex( index );
}

QStandardItemModel* mg = new QStandardItemModel( this );
QStringList labels;
labels << tr( "Range" ) << tr( "Label" );
Expand Down

0 comments on commit fab96ed

Please sign in to comment.