Skip to content

Commit b875fe0

Browse files
committed
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
1 parent 31c11ed commit b875fe0

11 files changed

+356
-58
lines changed

src/core/symbology-ng/qgscptcityarchive.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ QString QgsCptCityArchive::descFileName( const QString& path ) const
157157
baseDir() + QDir::separator() + path, baseDir() );
158158
}
159159

160-
QMap< QString, QString > QgsCptCityArchive::copyingInfo( const QString& fileName )
160+
QgsStringMap QgsCptCityArchive::copyingInfo( const QString& fileName )
161161
{
162-
QMap< QString, QString > copyingMap;
162+
QgsStringMap copyingMap;
163163

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

261-
QMap< QString, QString > QgsCptCityArchive::description( const QString& fileName )
261+
QgsStringMap QgsCptCityArchive::description( const QString& fileName )
262262
{
263-
QMap< QString, QString > descMap;
263+
QgsStringMap descMap;
264264

265265
QgsDebugMsg( "description fileName = " + fileName );
266266

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

436436
void QgsCptCityArchive::initArchives( bool loadAll )
437437
{
438-
QMap< QString, QString > archivesMap;
438+
QgsStringMap archivesMap;
439439
QString baseDir, defArchiveName;
440440
QSettings settings;
441441

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

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

src/core/symbology-ng/qgsvectorcolorrampv2.cpp

+85-1
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ QgsVectorGradientColorRampV2::QgsVectorGradientColorRampV2( QColor color1, QColo
4646

4747
QgsVectorColorRampV2* QgsVectorGradientColorRampV2::create( const QgsStringMap& props )
4848
{
49+
// color1 and color2
4950
QColor color1 = DEFAULT_GRADIENT_COLOR1;
5051
QColor color2 = DEFAULT_GRADIENT_COLOR2;
5152
if ( props.contains( "color1" ) )
5253
color1 = QgsSymbolLayerV2Utils::decodeColor( props["color1"] );
5354
if ( props.contains( "color2" ) )
5455
color2 = QgsSymbolLayerV2Utils::decodeColor( props["color2"] );
5556

57+
//stops
5658
QgsGradientStopsList stops;
5759
if ( props.contains( "stops" ) )
5860
{
@@ -66,14 +68,26 @@ QgsVectorColorRampV2* QgsVectorGradientColorRampV2::create( const QgsStringMap&
6668
stops.append( QgsGradientStop( stop.left( i ).toDouble(), c ) );
6769
}
6870
}
71+
72+
// discrete vs. continuous
6973
bool discrete = false;
7074
if ( props.contains( "discrete" ) )
7175
{
7276
if ( props["discrete"] == "1" )
7377
discrete = true;
7478
}
7579

80+
// search for information keys starting with "info_"
81+
QgsStringMap info;
82+
for ( QgsStringMap::const_iterator it = props.constBegin();
83+
it != props.constEnd(); ++it )
84+
{
85+
if ( it.key().startsWith( "info_" ) )
86+
info[ it.key().mid( 5 )] = it.value();
87+
}
88+
7689
QgsVectorGradientColorRampV2* r = new QgsVectorGradientColorRampV2( color1, color2, discrete, stops );
90+
r->setInfo( info );
7791
return r;
7892
}
7993

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

@@ -135,10 +150,58 @@ QgsStringMap QgsVectorGradientColorRampV2::properties() const
135150
}
136151
map["stops"] = lst.join( ":" );
137152
}
153+
138154
map["discrete"] = mDiscrete ? "1" : "0";
155+
156+
for ( QgsStringMap::const_iterator it = mInfo.constBegin();
157+
it != mInfo.constEnd(); ++it )
158+
{
159+
map["info_" + it.key()] = it.value();
160+
}
161+
139162
return map;
140163
}
164+
void QgsVectorGradientColorRampV2::convertToDiscrete( bool discrete )
165+
{
166+
if ( discrete == mDiscrete )
167+
return;
141168

169+
// if going to/from Discrete, re-arrange stops
170+
// this will only work when stops are equally-spaced
171+
QgsGradientStopsList newStops;
172+
if ( discrete )
173+
{
174+
// re-arrange stops offset
175+
int numStops = mStops.count() + 2;
176+
int i = 1;
177+
for ( QgsGradientStopsList::const_iterator it = mStops.begin();
178+
it != mStops.end(); ++it )
179+
{
180+
newStops.append( QgsGradientStop(( double ) i / numStops, it->color ) );
181+
if ( i == numStops - 1 )
182+
break;
183+
i++;
184+
}
185+
// replicate last color
186+
newStops.append( QgsGradientStop(( double ) i / numStops, mColor2 ) );
187+
}
188+
else
189+
{
190+
// re-arrange stops offset, remove duplicate last color
191+
int numStops = mStops.count() + 2;
192+
int i = 1;
193+
for ( QgsGradientStopsList::const_iterator it = mStops.begin();
194+
it != mStops.end(); ++it )
195+
{
196+
newStops.append( QgsGradientStop(( double ) i / ( numStops - 2 ), it->color ) );
197+
if ( i == numStops - 3 )
198+
break;
199+
i++;
200+
}
201+
}
202+
mStops = newStops;
203+
mDiscrete = discrete;
204+
}
142205

143206
//////////////
144207

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

403+
QgsVectorGradientColorRampV2* QgsCptCityColorRampV2::cloneGradientRamp() const
404+
{
405+
QgsVectorGradientColorRampV2* ramp =
406+
new QgsVectorGradientColorRampV2( mColor1, mColor2, mDiscrete, mStops );
407+
// add author and copyright information
408+
// TODO also add COPYING.xml file/link?
409+
QgsStringMap info = copyingInfo();
410+
info["source"] = "cpt-city archive - " + mSchemeName + mVariantName + ".svg";
411+
info["license/file"] = copyingFileName();
412+
ramp->setInfo( info );
413+
return ramp;
414+
}
415+
416+
340417
QgsStringMap QgsCptCityColorRampV2::properties() const
341418
{
342419
QgsStringMap map;
@@ -368,7 +445,7 @@ QString QgsCptCityColorRampV2::descFileName() const
368445
QgsCptCityArchive::defaultBaseDir() );
369446
}
370447

371-
QMap< QString, QString > QgsCptCityColorRampV2::copyingInfo( ) const
448+
QgsStringMap QgsCptCityColorRampV2::copyingInfo( ) const
372449
{
373450
return QgsCptCityArchive::copyingInfo( copyingFileName() );
374451
}
@@ -425,6 +502,7 @@ bool QgsCptCityColorRampV2::loadFile()
425502
++it;
426503
}
427504

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

528+
// remove first and last items (mColor1 and mColor2)
529+
if ( ! mStops.isEmpty() && mStops.first().offset == 0.0 )
530+
mColor1 = mStops.takeFirst().color;
531+
if ( ! mStops.isEmpty() && mStops.last().offset == 1.0 )
532+
mColor2 = mStops.takeLast().color;
533+
450534
mFileLoaded = true;
451535
return true;
452536
}

src/core/symbology-ng/qgsvectorcolorrampv2.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,24 @@ class CORE_EXPORT QgsVectorGradientColorRampV2 : public QgsVectorColorRampV2
6868

6969
QColor color1() const { return mColor1; }
7070
QColor color2() const { return mColor2; }
71-
7271
void setColor1( QColor color ) { mColor1 = color; }
7372
void setColor2( QColor color ) { mColor2 = color; }
7473

7574
bool isDiscrete() const { return mDiscrete; }
7675
void setDiscrete( bool discrete ) { mDiscrete = discrete; }
76+
void convertToDiscrete( bool discrete );
7777

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

81+
QgsStringMap info() const { return mInfo; }
82+
void setInfo( const QgsStringMap& info ) { mInfo = info; }
83+
8184
protected:
8285
QColor mColor1, mColor2;
8386
bool mDiscrete;
8487
QgsGradientStopsList mStops;
88+
QgsStringMap mInfo;
8589
};
8690

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

192196
virtual QgsVectorColorRampV2* clone() const;
193197
void copy( const QgsCptCityColorRampV2* other );
198+
QgsVectorGradientColorRampV2* cloneGradientRamp() const;
194199

195200
virtual QgsStringMap properties() const;
196201

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

216221
QString copyingFileName() const;
217222
QString descFileName() const;
218-
QMap< QString, QString > copyingInfo() const;
223+
QgsStringMap copyingInfo() const;
219224

220225
protected:
221226

src/gui/symbology-ng/qgscptcitycolorrampv2dialog.cpp

+17-2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ QgsCptCityColorRampV2Dialog::QgsCptCityColorRampV2Dialog( QgsCptCityColorRampV2*
5858
{
5959
setupUi( this );
6060

61+
buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
62+
6163
QSettings settings;
6264
restoreGeometry( settings.value( "/Windows/CptCityColorRampV2Dialog/geometry" ).toByteArray() );
6365
mSplitter->setSizes( QList<int>() << 250 << 550 );
@@ -93,7 +95,6 @@ QgsCptCityColorRampV2Dialog::QgsCptCityColorRampV2Dialog( QgsCptCityColorRampV2*
9395
mStackedWidget->addWidget( edit );
9496
mStackedWidget->setCurrentIndex( 1 );
9597
tabBar->setVisible( false );
96-
buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
9798
// dlg.layout()->addWidget( edit );
9899
// dlg.resize(500,400);
99100
// dlg.exec();
@@ -154,11 +155,13 @@ QgsCptCityColorRampV2Dialog::QgsCptCityColorRampV2Dialog( QgsCptCityColorRampV2*
154155
setTreeModel( mSelectionsModel );
155156
}
156157
}
158+
if ( found )
159+
buttonBox->button( QDialogButtonBox::Ok )->setEnabled( true );
157160
}
158161

159162
tabBar->blockSignals( false );
160163

161-
connect( this, SIGNAL( finished( int ) ), this, SLOT( onFinished() ) );
164+
connect( this, SIGNAL( finished( int ) ), this, SLOT( onFinished( int ) ) );
162165

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

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

480485
void QgsCptCityColorRampV2Dialog::onFinished()
481486
{
487+
// save settings
482488
QSettings settings;
483489
settings.setValue( "/Windows/CptCityColorRampV2Dialog/geometry", saveGeometry() );
484490
settings.setValue( "/Windows/CptCityColorRampV2Dialog/splitter", mSplitter->saveState() );
485491
}
486492

493+
bool QgsCptCityColorRampV2Dialog::saveAsGradientRamp() const
494+
{
495+
QgsDebugMsg( QString( "result: %1 checked: %2" ).arg( result() ).arg( cboConvertStandard->isChecked() ) );
496+
// if "save as standard gradient" is checked, convert to QgsVectorGradientColorRampV2
497+
return ( result() == Accepted && cboConvertStandard->isChecked() );
498+
}
499+
487500
void QgsCptCityColorRampV2Dialog::updateListWidget( QgsCptCityDataItem *item )
488501
{
489502
QgsCptCityCollectionItem* colItem = dynamic_cast<QgsCptCityCollectionItem*>( item );
@@ -554,6 +567,7 @@ bool QgsCptCityColorRampV2Dialog::updateRamp()
554567
cboVariantName->clear();
555568
updatePreview( true );
556569
clearCopyingInfo( );
570+
buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
557571

558572
QgsDebugMsg( "schemeName= " + mRamp->schemeName() );
559573
if ( mRamp->schemeName() == "" )
@@ -598,6 +612,7 @@ bool QgsCptCityColorRampV2Dialog::updateRamp()
598612
populateVariants();
599613
mListWidget->scrollToItem( listItem, QAbstractItemView::EnsureVisible );
600614
// mListView->selectionModel()->select( childIndex, QItemSelectionModel::Select );
615+
buttonBox->button( QDialogButtonBox::Ok )->setEnabled( true );
601616
return true;
602617
}
603618
}

src/gui/symbology-ng/qgscptcitycolorrampv2dialog.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class GUI_EXPORT QgsCptCityColorRampV2Dialog : public QDialog, private Ui::QgsCp
4141
QString selectedName() const
4242
{ return mRamp ? QFileInfo( mRamp->schemeName() ).baseName() + mRamp->variantName() : QString(); }
4343

44+
bool saveAsGradientRamp() const;
45+
4446
public slots:
4547
void populateVariants();
4648

@@ -50,7 +52,6 @@ class GUI_EXPORT QgsCptCityColorRampV2Dialog : public QDialog, private Ui::QgsCp
5052
void on_pbtnLicenseDetails_pressed();
5153
void on_cboVariantName_currentIndexChanged( int index );
5254
void onFinished();
53-
5455
/* void refresh(); */
5556

5657
protected:

src/gui/symbology-ng/qgsstylev2managerdialog.cpp

+14-1
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,17 @@ QString QgsStyleV2ManagerDialog::addColorRampStatic( QWidget* parent, QgsStyleV2
466466
delete cptCityRamp;
467467
return QString();
468468
}
469-
ramp = cptCityRamp;
470469
// name = dlg.selectedName();
471470
name = QFileInfo( cptCityRamp->schemeName() ).baseName() + cptCityRamp->variantName();
471+
if ( dlg.saveAsGradientRamp() )
472+
{
473+
ramp = cptCityRamp->cloneGradientRamp();
474+
delete cptCityRamp;
475+
}
476+
else
477+
{
478+
ramp = cptCityRamp;
479+
}
472480
}
473481
else
474482
{
@@ -632,6 +640,11 @@ bool QgsStyleV2ManagerDialog::editColorRamp()
632640
delete ramp;
633641
return false;
634642
}
643+
if ( dlg.saveAsGradientRamp() )
644+
{
645+
ramp = cptCityRamp->cloneGradientRamp();
646+
delete cptCityRamp;
647+
}
635648
}
636649
else
637650
{

0 commit comments

Comments
 (0)