Skip to content
Permalink
Browse files

allow to convert cpt-city ramps to standard gradients ramps, keeping …

…author and license info in xml props ; allow to convert gradient ramps to/from discrete/continuous ramps and use float instead of integer stops
  • Loading branch information
etiennesky committed Nov 17, 2012
1 parent 31c11ed commit b875fe09d873f4c73053298432856d97a464937f
@@ -157,9 +157,9 @@ QString QgsCptCityArchive::descFileName( const QString& path ) const
baseDir() + QDir::separator() + path, baseDir() );
}

QMap< QString, QString > QgsCptCityArchive::copyingInfo( const QString& fileName )
QgsStringMap QgsCptCityArchive::copyingInfo( const QString& fileName )
{
QMap< QString, QString > copyingMap;
QgsStringMap copyingMap;

if ( fileName.isNull() )
return copyingMap;
@@ -258,9 +258,9 @@ QMap< QString, QString > QgsCptCityArchive::copyingInfo( const QString& fileName
return copyingMap;
}

QMap< QString, QString > QgsCptCityArchive::description( const QString& fileName )
QgsStringMap QgsCptCityArchive::description( const QString& fileName )
{
QMap< QString, QString > descMap;
QgsStringMap descMap;

QgsDebugMsg( "description fileName = " + fileName );

@@ -435,7 +435,7 @@ void QgsCptCityArchive::initDefaultArchive()

void QgsCptCityArchive::initArchives( bool loadAll )
{
QMap< QString, QString > archivesMap;
QgsStringMap archivesMap;
QString baseDir, defArchiveName;
QSettings settings;

@@ -460,7 +460,7 @@ void QgsCptCityArchive::initArchives( bool loadAll )
archivesMap[ defArchiveName ] = baseDir + QDir::separator() + defArchiveName;
}

for ( QMap< QString, QString >::iterator it = archivesMap.begin();
for ( QgsStringMap::iterator it = archivesMap.begin();
it != archivesMap.end(); ++it )
{
if ( QDir( it.value() ).exists() )
@@ -871,7 +871,7 @@ QgsCptCityDirectoryItem::QgsCptCityDirectoryItem( QgsCptCityDataItem* parent,
mInfo = "";
QString fileName = QgsCptCityArchive::defaultBaseDir() + QDir::separator() + \
mPath + QDir::separator() + "DESC.xml";
QMap< QString, QString > descMap = QgsCptCityArchive::description( fileName );
QgsStringMap descMap = QgsCptCityArchive::description( fileName );
if ( descMap.contains( "name" ) )
mInfo = descMap.value( "name" );

@@ -46,13 +46,15 @@ QgsVectorGradientColorRampV2::QgsVectorGradientColorRampV2( QColor color1, QColo

QgsVectorColorRampV2* QgsVectorGradientColorRampV2::create( const QgsStringMap& props )
{
// color1 and color2
QColor color1 = DEFAULT_GRADIENT_COLOR1;
QColor color2 = DEFAULT_GRADIENT_COLOR2;
if ( props.contains( "color1" ) )
color1 = QgsSymbolLayerV2Utils::decodeColor( props["color1"] );
if ( props.contains( "color2" ) )
color2 = QgsSymbolLayerV2Utils::decodeColor( props["color2"] );

//stops
QgsGradientStopsList stops;
if ( props.contains( "stops" ) )
{
@@ -66,14 +68,26 @@ QgsVectorColorRampV2* QgsVectorGradientColorRampV2::create( const QgsStringMap&
stops.append( QgsGradientStop( stop.left( i ).toDouble(), c ) );
}
}

// discrete vs. continuous
bool discrete = false;
if ( props.contains( "discrete" ) )
{
if ( props["discrete"] == "1" )
discrete = true;
}

// search for information keys starting with "info_"
QgsStringMap info;
for ( QgsStringMap::const_iterator it = props.constBegin();
it != props.constEnd(); ++it )
{
if ( it.key().startsWith( "info_" ) )
info[ it.key().mid( 5 )] = it.value();
}

QgsVectorGradientColorRampV2* r = new QgsVectorGradientColorRampV2( color1, color2, discrete, stops );
r->setInfo( info );
return r;
}

@@ -118,6 +132,7 @@ QgsVectorColorRampV2* QgsVectorGradientColorRampV2::clone() const
{
QgsVectorGradientColorRampV2* r = new QgsVectorGradientColorRampV2( mColor1, mColor2,
mDiscrete, mStops );
r->setInfo( mInfo );
return r;
}

@@ -135,10 +150,58 @@ QgsStringMap QgsVectorGradientColorRampV2::properties() const
}
map["stops"] = lst.join( ":" );
}

map["discrete"] = mDiscrete ? "1" : "0";

for ( QgsStringMap::const_iterator it = mInfo.constBegin();
it != mInfo.constEnd(); ++it )
{
map["info_" + it.key()] = it.value();
}

return map;
}
void QgsVectorGradientColorRampV2::convertToDiscrete( bool discrete )
{
if ( discrete == mDiscrete )
return;

// if going to/from Discrete, re-arrange stops
// this will only work when stops are equally-spaced
QgsGradientStopsList newStops;
if ( discrete )
{
// re-arrange stops offset
int numStops = mStops.count() + 2;
int i = 1;
for ( QgsGradientStopsList::const_iterator it = mStops.begin();
it != mStops.end(); ++it )
{
newStops.append( QgsGradientStop(( double ) i / numStops, it->color ) );
if ( i == numStops - 1 )
break;
i++;
}
// replicate last color
newStops.append( QgsGradientStop(( double ) i / numStops, mColor2 ) );
}
else
{
// re-arrange stops offset, remove duplicate last color
int numStops = mStops.count() + 2;
int i = 1;
for ( QgsGradientStopsList::const_iterator it = mStops.begin();
it != mStops.end(); ++it )
{
newStops.append( QgsGradientStop(( double ) i / ( numStops - 2 ), it->color ) );
if ( i == numStops - 3 )
break;
i++;
}
}
mStops = newStops;
mDiscrete = discrete;
}

//////////////

@@ -337,6 +400,20 @@ void QgsCptCityColorRampV2::copy( const QgsCptCityColorRampV2* other )
mFileLoaded = other->mFileLoaded;
}

QgsVectorGradientColorRampV2* QgsCptCityColorRampV2::cloneGradientRamp() const
{
QgsVectorGradientColorRampV2* ramp =
new QgsVectorGradientColorRampV2( mColor1, mColor2, mDiscrete, mStops );
// add author and copyright information
// TODO also add COPYING.xml file/link?
QgsStringMap info = copyingInfo();
info["source"] = "cpt-city archive - " + mSchemeName + mVariantName + ".svg";
info["license/file"] = copyingFileName();
ramp->setInfo( info );
return ramp;
}


QgsStringMap QgsCptCityColorRampV2::properties() const
{
QgsStringMap map;
@@ -368,7 +445,7 @@ QString QgsCptCityColorRampV2::descFileName() const
QgsCptCityArchive::defaultBaseDir() );
}

QMap< QString, QString > QgsCptCityColorRampV2::copyingInfo( ) const
QgsStringMap QgsCptCityColorRampV2::copyingInfo( ) const
{
return QgsCptCityArchive::copyingInfo( copyingFileName() );
}
@@ -425,6 +502,7 @@ bool QgsCptCityColorRampV2::loadFile()
++it;
}

// fill all stops
it = prev = colorMap.constBegin();
while ( it != colorMap.constEnd() )
{
@@ -447,6 +525,12 @@ bool QgsCptCityColorRampV2::loadFile()
++it;
}

// remove first and last items (mColor1 and mColor2)
if ( ! mStops.isEmpty() && mStops.first().offset == 0.0 )
mColor1 = mStops.takeFirst().color;
if ( ! mStops.isEmpty() && mStops.last().offset == 1.0 )
mColor2 = mStops.takeLast().color;

mFileLoaded = true;
return true;
}
@@ -68,20 +68,24 @@ class CORE_EXPORT QgsVectorGradientColorRampV2 : public QgsVectorColorRampV2

QColor color1() const { return mColor1; }
QColor color2() const { return mColor2; }

void setColor1( QColor color ) { mColor1 = color; }
void setColor2( QColor color ) { mColor2 = color; }

bool isDiscrete() const { return mDiscrete; }
void setDiscrete( bool discrete ) { mDiscrete = discrete; }
void convertToDiscrete( bool discrete );

void setStops( const QgsGradientStopsList& stops ) { mStops = stops; }
const QgsGradientStopsList& stops() const { return mStops; }

QgsStringMap info() const { return mInfo; }
void setInfo( const QgsStringMap& info ) { mInfo = info; }

protected:
QColor mColor1, mColor2;
bool mDiscrete;
QgsGradientStopsList mStops;
QgsStringMap mInfo;
};

#define DEFAULT_RANDOM_COUNT 10
@@ -191,6 +195,7 @@ class CORE_EXPORT QgsCptCityColorRampV2 : public QgsVectorGradientColorRampV2

virtual QgsVectorColorRampV2* clone() const;
void copy( const QgsCptCityColorRampV2* other );
QgsVectorGradientColorRampV2* cloneGradientRamp() const;

virtual QgsStringMap properties() const;

@@ -215,7 +220,7 @@ class CORE_EXPORT QgsCptCityColorRampV2 : public QgsVectorGradientColorRampV2

QString copyingFileName() const;
QString descFileName() const;
QMap< QString, QString > copyingInfo() const;
QgsStringMap copyingInfo() const;

protected:

@@ -58,6 +58,8 @@ QgsCptCityColorRampV2Dialog::QgsCptCityColorRampV2Dialog( QgsCptCityColorRampV2*
{
setupUi( this );

buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );

QSettings settings;
restoreGeometry( settings.value( "/Windows/CptCityColorRampV2Dialog/geometry" ).toByteArray() );
mSplitter->setSizes( QList<int>() << 250 << 550 );
@@ -93,7 +95,6 @@ QgsCptCityColorRampV2Dialog::QgsCptCityColorRampV2Dialog( QgsCptCityColorRampV2*
mStackedWidget->addWidget( edit );
mStackedWidget->setCurrentIndex( 1 );
tabBar->setVisible( false );
buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
// dlg.layout()->addWidget( edit );
// dlg.resize(500,400);
// dlg.exec();
@@ -154,11 +155,13 @@ QgsCptCityColorRampV2Dialog::QgsCptCityColorRampV2Dialog( QgsCptCityColorRampV2*
setTreeModel( mSelectionsModel );
}
}
if ( found )
buttonBox->button( QDialogButtonBox::Ok )->setEnabled( true );
}

tabBar->blockSignals( false );

connect( this, SIGNAL( finished( int ) ), this, SLOT( onFinished() ) );
connect( this, SIGNAL( finished( int ) ), this, SLOT( onFinished( int ) ) );

// TODO - remove this when basic archive is complete
if ( mArchive->archiveName() == DEFAULT_CPTCITY_ARCHIVE )
@@ -261,6 +264,7 @@ void QgsCptCityColorRampV2Dialog::on_mTreeView_clicked( const QModelIndex &index
QgsCptCityDataItem *item = mModel->dataItem( sourceIndex );
if ( ! item )
return;
buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
updateTreeView( item );
}

@@ -304,6 +308,7 @@ void QgsCptCityColorRampV2Dialog::on_mListWidget_itemClicked( QListWidgetItem *
QgsCptCityColorRampItem *rampItem = mListRamps.at( item->data( Qt::UserRole ).toInt() );
if ( rampItem )
{
buttonBox->button( QDialogButtonBox::Ok )->setEnabled( true );
lblSchemeName->setText( QFileInfo( rampItem->name() ).fileName() );
mRamp->copy( &rampItem->ramp() );
QgsDebugMsg( QString( "variant= %1 - %2 variants" ).arg( mRamp->variantName() ).arg( mRamp->variantList().count() ) );
@@ -479,11 +484,19 @@ void QgsCptCityColorRampV2Dialog::on_cboVariantName_currentIndexChanged( int ind

void QgsCptCityColorRampV2Dialog::onFinished()
{
// save settings
QSettings settings;
settings.setValue( "/Windows/CptCityColorRampV2Dialog/geometry", saveGeometry() );
settings.setValue( "/Windows/CptCityColorRampV2Dialog/splitter", mSplitter->saveState() );
}

bool QgsCptCityColorRampV2Dialog::saveAsGradientRamp() const
{
QgsDebugMsg( QString( "result: %1 checked: %2" ).arg( result() ).arg( cboConvertStandard->isChecked() ) );
// if "save as standard gradient" is checked, convert to QgsVectorGradientColorRampV2
return ( result() == Accepted && cboConvertStandard->isChecked() );
}

void QgsCptCityColorRampV2Dialog::updateListWidget( QgsCptCityDataItem *item )
{
QgsCptCityCollectionItem* colItem = dynamic_cast<QgsCptCityCollectionItem*>( item );
@@ -554,6 +567,7 @@ bool QgsCptCityColorRampV2Dialog::updateRamp()
cboVariantName->clear();
updatePreview( true );
clearCopyingInfo( );
buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );

QgsDebugMsg( "schemeName= " + mRamp->schemeName() );
if ( mRamp->schemeName() == "" )
@@ -598,6 +612,7 @@ bool QgsCptCityColorRampV2Dialog::updateRamp()
populateVariants();
mListWidget->scrollToItem( listItem, QAbstractItemView::EnsureVisible );
// mListView->selectionModel()->select( childIndex, QItemSelectionModel::Select );
buttonBox->button( QDialogButtonBox::Ok )->setEnabled( true );
return true;
}
}
@@ -41,6 +41,8 @@ class GUI_EXPORT QgsCptCityColorRampV2Dialog : public QDialog, private Ui::QgsCp
QString selectedName() const
{ return mRamp ? QFileInfo( mRamp->schemeName() ).baseName() + mRamp->variantName() : QString(); }

bool saveAsGradientRamp() const;

public slots:
void populateVariants();

@@ -50,7 +52,6 @@ class GUI_EXPORT QgsCptCityColorRampV2Dialog : public QDialog, private Ui::QgsCp
void on_pbtnLicenseDetails_pressed();
void on_cboVariantName_currentIndexChanged( int index );
void onFinished();

/* void refresh(); */

protected:
@@ -466,9 +466,17 @@ QString QgsStyleV2ManagerDialog::addColorRampStatic( QWidget* parent, QgsStyleV2
delete cptCityRamp;
return QString();
}
ramp = cptCityRamp;
// name = dlg.selectedName();
name = QFileInfo( cptCityRamp->schemeName() ).baseName() + cptCityRamp->variantName();
if ( dlg.saveAsGradientRamp() )
{
ramp = cptCityRamp->cloneGradientRamp();
delete cptCityRamp;
}
else
{
ramp = cptCityRamp;
}
}
else
{
@@ -632,6 +640,11 @@ bool QgsStyleV2ManagerDialog::editColorRamp()
delete ramp;
return false;
}
if ( dlg.saveAsGradientRamp() )
{
ramp = cptCityRamp->cloneGradientRamp();
delete cptCityRamp;
}
}
else
{

0 comments on commit b875fe0

Please sign in to comment.
You can’t perform that action at this time.