226 changes: 133 additions & 93 deletions src/core/symbology-ng/qgsvectorcolorrampv2.cpp

Large diffs are not rendered by default.

43 changes: 22 additions & 21 deletions src/core/symbology-ng/qgsvectorcolorrampv2.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class CORE_EXPORT QgsVectorColorBrewerColorRampV2 : public QgsVectorColorRampV2
QList<QColor> mPalette;
};

#define DEFAULT_CPTCITY_COLLECTION "default"
#define DEFAULT_CPTCITY_COLLECTION "cpt-city-qgis-min"

class CORE_EXPORT QgsCptCityCollection
{
Expand All @@ -175,11 +175,13 @@ class CORE_EXPORT QgsCptCityCollection
bool loadSchemes( QString rootDir = "", bool reset = false );
/** Is the minimal (free to distribute) set of schemes available?
* Currently returns hasAllSchemes, because we don't have a minimal set yet. */
bool hasBasicSchemes();
/* bool hasBasicSchemes(); */
/** Is the entire archive available? Currently tests that there is at least one scheme. */
bool hasAllSchemes();
QStringList listSchemeCollections( QString collectionName = "", bool recursive = false );
QStringList listSchemeNames( QString collectionName );
/* bool hasAllSchemes(); */
bool isEmpty();

QStringList listDirNames( QString dirName = "", bool recursive = false );
QStringList listSchemeNames( QString dirName );
QgsCptCityCollection* colorRampFromSVGFile( QString svgFile );
QgsCptCityCollection* colorRampFromSVGString( QString svgString );

Expand All @@ -190,26 +192,26 @@ class CORE_EXPORT QgsCptCityCollection
QString collectionName() const { return mCollectionName; }
QMap< QString, QStringList > schemeMap() const { return mSchemeMap; }
QMap< QString, QStringList > schemeVariants() const { return mSchemeVariants; }
QMap< QString, QString > collectionNames() const { return mCollectionNames; }
QMap< QString, QStringList > collectionSelections() const { return mCollectionSelections; }
QMap< QString, QString > dirNamesMap() const { return mDirNamesMap; }
QMap< QString, QStringList > selectionsMap() const { return mSelectionsMap; }

static void initCollection( QString collectionName = DEFAULT_CPTCITY_COLLECTION,
QString collectionBaseDir = baseDir( DEFAULT_CPTCITY_COLLECTION ) );
static void initCollections( ) { initCollection(); }
static void initCollections( QMap< QString, QString > collectionsDefs );
static void initCollections( bool loadAll = false );
static void initCollection( QString collectionName, QString collectionBaseDir );
static void clearCollections();
static QgsCptCityCollection* defaultCollection();
static QString defaultCollectionName();
static QMap< QString, QgsCptCityCollection* > collectionRegistry();

protected:

QString mCollectionName;
QString mBaseDir;
QStringList mCollections;
QStringList mDirNames;
QMap< QString, QStringList > mSchemeMap; //key is collection, value is schemes
QMap< QString, QStringList > mSchemeVariants; //key is scheme, value is variants
QMap< QString, QString > mCollectionNames; //key is name, value is description
QMap< QString, QStringList > mCollectionSelections;
static QgsCptCityCollection* mDefaultCollection;
QMap< QString, QString > mDirNamesMap; //key is name, value is description
QMap< QString, QStringList > mSelectionsMap;
static QString mDefaultCollectionName;
static QMap< QString, QgsCptCityCollection* > mCollectionRegistry;
static QMap< QString, QMap< QString, QString > > mCopyingInfoMap; // mapping of copyinginfo, key is fileName
};
Expand All @@ -222,8 +224,7 @@ class CORE_EXPORT QgsCptCityColorRampV2 : public QgsVectorColorRampV2
{
public:
QgsCptCityColorRampV2( QString schemeName = DEFAULT_CPTCITY_SCHEMENAME,
QString variantName = DEFAULT_CPTCITY_VARIANTNAME,
QString collectionName = DEFAULT_CPTCITY_COLLECTION );
QString variantName = DEFAULT_CPTCITY_VARIANTNAME );


enum GradientType
Expand Down Expand Up @@ -251,9 +252,9 @@ class CORE_EXPORT QgsCptCityColorRampV2 : public QgsVectorColorRampV2
QString variantName() const { return mVariantName; }
QStringList variantList() const { return mVariantList; }
/* QgsCptCityCollection* collection() const { return mCollection; } */
QString collectionName() const { return mCollectionName; }
QgsCptCityCollection* collection() const
{ return QgsCptCityCollection::collectionRegistry().value( mCollectionName ); }
/* QString collectionName() const { return mCollectionName; } */
/* QgsCptCityCollection* collection() const */
/* { return QgsCptCityCollection::collectionRegistry().value( mCollectionName ); } */

/* lazy loading - have to call loadPalette() explicitly */
void setSchemeName( QString schemeName ) { mSchemeName = schemeName; mFileLoaded = false; }
Expand All @@ -276,7 +277,7 @@ class CORE_EXPORT QgsCptCityColorRampV2 : public QgsVectorColorRampV2

QString mSchemeName;
QString mVariantName;
QString mCollectionName;
/* QString mCollectionName; */
/* QgsCptCityCollection* mCollection; */
GradientType mGradientType;
GradientList mPalette;
Expand Down
210 changes: 112 additions & 98 deletions src/gui/symbology-ng/qgscptcitycolorrampv2dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <QPushButton>
#include <QTextEdit>
#include <QTime>
#include <QErrorMessage>

/////////

Expand All @@ -45,80 +46,7 @@ QgsCptCityColorRampV2Dialog::QgsCptCityColorRampV2Dialog( QgsCptCityColorRampV2*
: QDialog( parent ), mRamp( ramp )
{
setupUi( this );

if ( QgsCptCityCollection::collectionRegistry().isEmpty() )
QgsCptCityCollection::initCollections( );
mCollection = QgsCptCityCollection::collectionRegistry()[ DEFAULT_CPTCITY_COLLECTION ];
if ( ! mCollection )
return;

// show information on how to install cpt-city files if none are found
if ( ! mCollection->hasAllSchemes() )
{
QTextEdit *edit = new QTextEdit();
edit->setReadOnly( true );
// not sure if we want this long string to be translated
QString helpText = tr( "Error - cpt-city gradient files not found.\n\n"
"You have two means of installing them:\n\n"
"1) Install the \"Color Ramp Manager\" python plugin "
"(you must enable Experimental plugins in the plugin manager) "
"and use it to download latest cpt-city package.\n\n"
"2) Download the complete collection (in svg format) "
"and unzip it to your QGis settings directory [%1] .\n\n"
"This file can be found at [%2]\nand current file is [%3]"
).arg( QgsApplication::qgisSettingsDirPath()
).arg( "http://soliton.vm.bytemark.co.uk/pub/cpt-city/pkg/"
).arg( "http://soliton.vm.bytemark.co.uk/pub/cpt-city/pkg/cpt-city-svg-2.02.zip" );
edit->setText( helpText );
stackedWidget->addWidget( edit );
stackedWidget->setCurrentIndex( 2 );
tabBar->setVisible( false );
buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
return;
}

// model / view
mAuthorsModel = new QgsCptCityBrowserModel( mBrowserView, DEFAULT_CPTCITY_COLLECTION, "authors" );
mSelectionsModel = new QgsCptCityBrowserModel( mBrowserView, DEFAULT_CPTCITY_COLLECTION, "selections" );
mModel = mSelectionsModel;
mBrowserView->setModel( mModel );
mBrowserView->setSelectionMode( QAbstractItemView::SingleSelection );
mBrowserView->setIconSize( QSize( 100, 15 ) );
// provide a horizontal scroll bar instead of using ellipse (...) for longer items
// mBrowserView->setTextElideMode( Qt::ElideNone );
// mBrowserView->header()->setResizeMode( 0, QHeaderView::ResizeToContents );
mBrowserView->header()->resizeSection( 0, 250 );
mBrowserView->header()->setStretchLastSection( true );

// setup ui
tabBar->blockSignals( true );
tabBar->addTab( tr( "Selections by theme" ) );
tabBar->addTab( tr( "All by author" ) );
cboVariantName->setIconSize( QSize( 100, 15 ) );
lblPreview->installEventFilter( this ); // mouse click on preview label shows svg render

// populate tree widget - if item not found in selections collection, look for in authors
// try to apply selection to view
QModelIndex modelIndex = mModel->findPath( mRamp->schemeName() );
if ( modelIndex == QModelIndex() )
{
modelIndex = mAuthorsModel->findPath( mRamp->schemeName() );
if ( modelIndex != QModelIndex() )
{
tabBar->setCurrentIndex( 1 );
mModel = mAuthorsModel;
mBrowserView->setModel( mModel );
}
}
if ( modelIndex != QModelIndex() )
{
lblSchemeName->setText( mRamp->schemeName() );
mBrowserView->setCurrentIndex( modelIndex );
mBrowserView->scrollTo( modelIndex, QAbstractItemView::PositionAtCenter );
populateVariants( mRamp->variantName() );
// updatePreview();
}
tabBar->blockSignals( false );
mAuthorsModel = mSelectionsModel = 0;
}


Expand All @@ -142,7 +70,7 @@ void QgsCptCityColorRampV2Dialog::populateVariants( QString newVariant )
else
{
QString oldVariant = cboVariantName->currentText();
QgsCptCityColorRampV2 ramp( mRamp->schemeName(), QString(), mRamp->collectionName() );
QgsCptCityColorRampV2 ramp( mRamp->schemeName(), QString() );
QPixmap blankPixmap( cboVariantName->iconSize() );
blankPixmap.fill( Qt::white );
QIcon blankIcon( blankPixmap );
Expand Down Expand Up @@ -195,7 +123,6 @@ void QgsCptCityColorRampV2Dialog::populateVariants( QString newVariant )

void QgsCptCityColorRampV2Dialog::on_mBrowserView_clicked( const QModelIndex &index )
{
QgsDebugMsg( "Entered" );
QgsCptCityDataItem *item = mModel->dataItem( index );
if ( ! item )
return;
Expand All @@ -217,7 +144,6 @@ void QgsCptCityColorRampV2Dialog::on_mBrowserView_clicked( const QModelIndex &in

void QgsCptCityColorRampV2Dialog::on_tabBar_currentChanged( int index )
{
QgsDebugMsg( QString( "index = %1" ).arg( index ) );
if ( index == 0 )
{
mCollectionGroup = "selections";
Expand Down Expand Up @@ -407,34 +333,123 @@ bool QgsCptCityColorRampV2Dialog::eventFilter( QObject *obj, QEvent *event )
}
}

// TODO - delay initialization, but is this necessary?
#if 0
// delay initialization and update collection if it has changed
void QgsCptCityColorRampV2Dialog::showEvent( QShowEvent * e )
{
// delayed initialization of the model
if ( mModel == NULL )
// setup collections
if ( QgsCptCityCollection::collectionRegistry().isEmpty() )
{
QgsCptCityCollection::initCollections( true );
}
mCollection = QgsCptCityCollection::defaultCollection();
// if empty collection, try loading again - this may happen after installing new package
if ( ! mCollection || mCollection->isEmpty() )
{
mModel = new QgsCptCityBrowserModel( mBrowserView );
mBrowserView->setModel( mModel );

// provide a horizontal scroll bar instead of using ellipse (...) for longer items
mBrowserView->setTextElideMode( Qt::ElideNone );
mBrowserView->header()->setResizeMode( 0, QHeaderView::ResizeToContents );
mBrowserView->header()->setStretchLastSection( false );

// // find root favourites item
// for ( int i = 0; i < mModel->rowCount(); i++ )
// {
// QModelIndex index = mModel->index( i, 0 );
// QgsCptCityDataItem* item = mModel->dataItem( index );
// if ( item && item->type() == QgsCptCityDataItem::Favourites )
// mBrowserView->expand( index );
// }
QgsCptCityCollection::initCollections( true );
mCollection = QgsCptCityCollection::defaultCollection();
}

// show information on how to install cpt-city files if none are found
if ( ! mCollection || mCollection->isEmpty() )
{
// QgsDialog dlg( this );
// dlg.setWindowTitle( tr( "cpt-city gradient files not found" ) );
QTextEdit *edit = new QTextEdit( 0 );
edit->setReadOnly( true );
// not sure if we want this long string to be translated
QString helpText = tr( "Error - cpt-city gradient files not found.\n\n"
"You have two means of installing them:\n\n"
"1) Install the \"Color Ramp Manager\" python plugin "
"(you must enable Experimental plugins in the plugin manager) "
"and use it to download latest cpt-city package.\n"
"You can install the entire cpt-city archive or a selection for QGIS.\n\n"
"2) Download the complete collection (in svg format) "
"and unzip it to your QGis settings directory [%1] .\n\n"
"This file can be found at [%2]\nand current file is [%3]"
).arg( QgsApplication::qgisSettingsDirPath()
).arg( "http://soliton.vm.bytemark.co.uk/pub/cpt-city/pkg/"
).arg( "http://soliton.vm.bytemark.co.uk/pub/cpt-city/pkg/cpt-city-svg-2.02.zip" );
edit->setText( helpText );
stackedWidget->addWidget( edit );
stackedWidget->setCurrentIndex( 1 );
tabBar->setVisible( false );
buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
// dlg.layout()->addWidget( edit );
// dlg.resize(500,400);
// dlg.exec();
return;
}

if ( ! mCollection )
return;
QgsDebugMsg( "collection: " + mCollection->collectionName() );

// model / view
QgsDebugMsg( "loading model/view objects" );
if ( mAuthorsModel )
delete mAuthorsModel;
mAuthorsModel = new QgsCptCityBrowserModel( mBrowserView, mCollection, "authors" );
if ( mSelectionsModel )
delete mSelectionsModel;
mSelectionsModel = new QgsCptCityBrowserModel( mBrowserView, mCollection, "selections" );
mModel = mSelectionsModel;
mBrowserView->setModel( mModel );
mBrowserView->setSelectionMode( QAbstractItemView::SingleSelection );
mBrowserView->setIconSize( QSize( 100, 15 ) );
// provide a horizontal scroll bar instead of using ellipse (...) for longer items
// mBrowserView->setTextElideMode( Qt::ElideNone );
// mBrowserView->header()->setResizeMode( 0, QHeaderView::ResizeToContents );
mBrowserView->header()->resizeSection( 0, 250 );
mBrowserView->header()->setStretchLastSection( true );

// setup ui
tabBar->blockSignals( true );
tabBar->addTab( tr( "Selections by theme" ) );
tabBar->addTab( tr( "All by author" ) );
cboVariantName->setIconSize( QSize( 100, 15 ) );
lblPreview->installEventFilter( this ); // mouse click on preview label shows svg render

// populate tree widget - if item not found in selections collection, look for in authors
// try to apply selection to view
QModelIndex modelIndex = mModel->findPath( mRamp->schemeName() );
if ( modelIndex == QModelIndex() )
{
modelIndex = mAuthorsModel->findPath( mRamp->schemeName() );
if ( modelIndex != QModelIndex() )
{
tabBar->setCurrentIndex( 1 );
mModel = mAuthorsModel;
mBrowserView->setModel( mModel );
}
}
if ( modelIndex != QModelIndex() )
{
lblSchemeName->setText( mRamp->schemeName() );
mBrowserView->setCurrentIndex( modelIndex );
mBrowserView->scrollTo( modelIndex, QAbstractItemView::PositionAtCenter );
populateVariants( mRamp->variantName() );
// updatePreview();
}
tabBar->blockSignals( false );
if ( mCollection->collectionName() == DEFAULT_CPTCITY_COLLECTION )
tabBar->setCurrentIndex( 1 );

QDialog::showEvent( e );

// show error message to use color ramp manager to get more gradients
if ( mCollection->collectionName() == DEFAULT_CPTCITY_COLLECTION &&
QgsCptCityCollection::collectionRegistry().count() == 1 )
{
QString helpText = tr( "You can download a more complete set of cpt-city gradients "
"by installing the \"Color Ramp Manager\" plugin "
"(you must enable Experimental plugins in the plugin manager).\n\n"
);
QErrorMessage* msg = new QErrorMessage( this );
msg->showMessage( helpText, "cpt-city" );
}
}

#if 0
void QgsCptCityColorRampV2Dialog::refresh()
{
QApplication::setOverrideCursor( Qt::WaitCursor );
Expand All @@ -444,7 +459,6 @@ void QgsCptCityColorRampV2Dialog::refresh()

void QgsCptCityColorRampV2Dialog::refreshModel( const QModelIndex& index )
{
QgsDebugMsg( "Entered" );
if ( index.isValid() )
{
QgsCptCityDataItem *item = mModel->dataItem( index );
Expand Down
2 changes: 1 addition & 1 deletion src/gui/symbology-ng/qgscptcitycolorrampv2dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class GUI_EXPORT QgsCptCityColorRampV2Dialog : public QDialog, private Ui::QgsCp
QString mCollectionGroup;

/* void refreshModel( const QModelIndex& index ); */
/* void showEvent( QShowEvent * event ); */
void showEvent( QShowEvent * event );

QgsCptCityBrowserModel* mModel;
QgsCptCityBrowserModel* mAuthorsModel;
Expand Down