29#include <QSortFilterProxyModel>
34#include "moc_qgscptcitycolorrampdialog.cpp"
36using namespace Qt::StringLiterals;
51 connect( mTreeView, &QTreeView::clicked,
this, &QgsCptCityColorRampDialog::mTreeView_clicked );
52 connect( mListWidget, &QListWidget::itemClicked,
this, &QgsCptCityColorRampDialog::mListWidget_itemClicked );
53 connect( mListWidget, &QListWidget::itemSelectionChanged,
this, &QgsCptCityColorRampDialog::mListWidget_itemSelectionChanged );
54 connect( tabBar, &QTabBar::currentChanged,
this, &QgsCptCityColorRampDialog::tabBar_currentChanged );
55 connect( pbtnLicenseDetails, &QToolButton::pressed,
this, &QgsCptCityColorRampDialog::pbtnLicenseDetails_pressed );
56 connect( cboVariantName,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsCptCityColorRampDialog::cboVariantName_currentIndexChanged );
57 connect( mButtonBox, &QDialogButtonBox::helpRequested,
this, &QgsCptCityColorRampDialog::showHelp );
59 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
62 mSplitter->setSizes( QList<int>() << 250 << 550 );
63 mSplitter->restoreState( settings.
value( u
"Windows/CptCityColorRampV2Dialog/splitter"_s ).toByteArray() );
65 mModel = mAuthorsModel = mSelectionsModel =
nullptr;
66 mTreeFilter =
nullptr;
72 if ( !mArchive || mArchive->isEmpty() )
76 QTextEdit *edit =
new QTextEdit(
nullptr );
77 edit->setReadOnly(
true );
79 const QString helpText
81 "Error - cpt-city gradient files not found.\n\n"
82 "You have two means of installing them:\n\n"
83 "1) Install the \"Color Ramp Manager\" python plugin "
84 "(you must enable Experimental plugins in the plugin manager) "
85 "and use it to download latest cpt-city package.\n"
86 "You can install the entire cpt-city archive or a selection for QGIS.\n\n"
87 "2) Download the complete archive (in svg format) "
88 "and unzip it to your QGIS settings directory [%1] .\n\n"
89 "This file can be found at [%2]\nand current file is [%3]"
91 .arg(
QgsApplication::qgisSettingsDirPath(), u
"http://soliton.vm.bytemark.co.uk/pub/cpt-city/pkg/"_s, u
"http://soliton.vm.bytemark.co.uk/pub/cpt-city/pkg/cpt-city-svg-2.07.zip"_s );
92 edit->setText( helpText );
93 mStackedWidget->addWidget( edit );
94 mStackedWidget->setCurrentIndex( 1 );
95 tabBar->setVisible(
false );
106 QgsDebugMsgLevel( u
"ramp name= %1 variant= %2 - %3 variants"_s.arg( mRamp.schemeName(), mRamp.variantName() ).arg( mRamp.variantList().count() ), 2 );
111 delete mAuthorsModel;
114 delete mSelectionsModel;
116 setTreeModel( mSelectionsModel );
118 mTreeView->setSelectionMode( QAbstractItemView::SingleSelection );
119 mTreeView->setColumnHidden( 1,
true );
123 tabBar->blockSignals(
true );
124 tabBar->addTab( tr(
"Selections by theme" ) );
125 tabBar->addTab( tr(
"All by author" ) );
126 cboVariantName->setIconSize( QSize( 100, 15 ) );
127 lblPreview->installEventFilter(
this );
131 tabBar->blockSignals(
false );
133 connect(
this, &QDialog::finished,
this, &QgsCptCityColorRampDialog::onFinished );
142void QgsCptCityColorRampDialog::populateVariants()
144 const QStringList variantList = mRamp.
variantList();
148 cboVariantName->blockSignals(
true );
149 cboVariantName->clear();
151 if ( variantList.isEmpty() )
153 cboVariantName->setEnabled(
false );
154 cboVariantName->setVisible(
false );
155 cboVariantName_currentIndexChanged( -1 );
160 const QString oldVariant = cboVariantName->currentText();
161 QgsCptCityColorRamp
ramp( mRamp.schemeName(), mRamp.variantList(), QString() );
162 QPixmap blankPixmap( cboVariantName->iconSize() );
163 blankPixmap.fill( Qt::white );
164 const QIcon blankIcon( blankPixmap );
167 const auto constVariantList = variantList;
168 for (
const QString &variant : constVariantList )
170 QString variantStr = variant;
171 if ( variantStr.startsWith(
'-' ) || variantStr.startsWith(
'_' ) )
172 variantStr.remove( 0, 1 );
173 cboVariantName->addItem(
' ' + variantStr );
174 index = cboVariantName->count() - 1;
175 cboVariantName->setItemData( index, variant, Qt::UserRole );
177 ramp.setVariantName( variant );
178 if (
ramp.loadFile() )
181 cboVariantName->setItemIcon( index, blankIcon );
182 cboVariantName->setItemData( index, Qt::AlignHCenter, Qt::TextAlignmentRole );
185 cboVariantName->blockSignals(
false );
189 QString newVariant = mRamp.variantName();
190 QgsDebugMsgLevel( u
"variant= %1 - %2 variants"_s.arg( mRamp.variantName() ).arg( mRamp.variantList().count() ), 2 );
191 if ( newVariant != QString() )
193 if ( newVariant.startsWith(
'-' ) || newVariant.startsWith(
'_' ) )
194 newVariant.remove( 0, 1 );
195 newVariant =
' ' + newVariant;
196 idx = cboVariantName->findText( newVariant );
199 idx = cboVariantName->findText( oldVariant );
204 idx = cboVariantName->count() / 2;
206 cboVariantName->setCurrentIndex( idx );
209 cboVariantName->setEnabled(
true );
210 cboVariantName->setVisible(
true );
214void QgsCptCityColorRampDialog::mTreeView_clicked(
const QModelIndex &index )
216 const QModelIndex &sourceIndex = mTreeFilter->mapToSource( index );
217 QgsCptCityDataItem *item = mModel->dataItem( sourceIndex );
221 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
222 updateTreeView( item );
225void QgsCptCityColorRampDialog::updateTreeView(
QgsCptCityDataItem *item,
bool resetRamp )
236 mRamp.setName( QString(), QString() );
237 QgsDebugMsgLevel( u
"variant= %1 - %2 variants"_s.arg( mRamp.variantName() ).arg( mRamp.variantList().count() ), 2 );
238 lblSchemeName->clear();
241 updateListWidget( item );
242 lblSchemePath->setText( item->
path() );
243 lblCollectionInfo->setText( u
"%1 (%2)"_s.arg( item->
info() ).arg( item->
leafCount() ) );
248 lblSchemePath->clear();
250 updateListWidget( item );
251 lblCollectionInfo->setText( u
"%1 (%2)"_s.arg( item->
info() ).arg( item->
leafCount() ) );
255 lblSchemePath->clear();
257 updateListWidget( item );
258 lblCollectionInfo->setText( tr(
"All Ramps (%1)" ).arg( item->
leafCount() ) );
262 QgsDebugError( u
"item %1 has invalid type %2"_s.arg( item->
path() ).arg(
static_cast<int>( item->
type() ) ) );
266void QgsCptCityColorRampDialog::mListWidget_itemClicked( QListWidgetItem *item )
268 QgsCptCityColorRampItem *rampItem = mListRamps.at( item->data( Qt::UserRole ).toInt() );
271 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled(
true );
272 lblSchemeName->setText( QFileInfo( rampItem->
name() ).fileName() );
273 mRamp.copy( &rampItem->
ramp() );
274 QgsDebugMsgLevel( u
"variant= %1 - %2 variants"_s.arg( mRamp.variantName() ).arg( mRamp.variantList().count() ), 2 );
283void QgsCptCityColorRampDialog::mListWidget_itemSelectionChanged()
285 if ( mListWidget->selectedItems().isEmpty() )
287 mRamp.setName( QString(), QString() );
291void QgsCptCityColorRampDialog::tabBar_currentChanged(
int index )
295 setTreeModel( mSelectionsModel );
298 else if ( index == 1 )
300 setTreeModel( mAuthorsModel );
306 setTreeModel( mAuthorsModel );
310 mListWidget->blockSignals(
true );
312 mListWidget->blockSignals(
false );
316void QgsCptCityColorRampDialog::pbtnLicenseDetails_pressed()
318 QString path, title, copyFile, descFile;
321 QgsCptCityDataItem *item = mModel->dataItem( mTreeFilter->mapToSource( mTreeView->currentIndex() ) );
328 title = tr(
"%1 Directory Details" ).arg( item->
path() );
332 title = tr(
"%1 Gradient Details" ).arg( path );
338 copyFile = mArchive->copyingFileName( path );
339 descFile = mArchive->descFileName( path );
342 QgsDialog dlg(
this, Qt::WindowFlags(), QDialogButtonBox::Close );
343 QVBoxLayout *layout = dlg.layout();
344 dlg.setWindowTitle( title );
345 QTextEdit *textEdit =
new QTextEdit( &dlg );
346 textEdit->setReadOnly(
true );
347 layout->addWidget( textEdit );
351 if ( !copyFile.isNull() )
353 QFile file( copyFile );
354 if ( file.open( QIODevice::ReadOnly | QIODevice::Text ) )
356 copyText = QString( file.readAll() );
361 if ( !descFile.isNull() )
363 QFile file( descFile );
364 if ( file.open( QIODevice::ReadOnly | QIODevice::Text ) )
366 descText = QString( file.readAll() );
370 textEdit->insertPlainText( title +
"\n\n" );
371 textEdit->insertPlainText( u
"==================="_s );
372 textEdit->insertPlainText( u
" copying "_s );
373 textEdit->insertPlainText( u
"===================\n"_s );
374 textEdit->insertPlainText( copyText );
375 textEdit->insertPlainText( u
"\n"_s );
376 textEdit->insertPlainText( u
"=================="_s );
377 textEdit->insertPlainText( u
" description "_s );
378 textEdit->insertPlainText( u
"==================\n"_s );
379 textEdit->insertPlainText( descText );
380 textEdit->moveCursor( QTextCursor::Start );
382 dlg.resize( 600, 600 );
386void QgsCptCityColorRampDialog::updatePreview(
bool clear )
388 const QSize size = lblPreview->size();
390 if ( clear || mRamp.schemeName().isEmpty() )
392 lblSchemeName->clear();
393 lblSchemePath->clear();
394 lblLicensePreview->clear();
395 QPixmap blankPixmap( size );
396 blankPixmap.fill( Qt::transparent );
397 lblPreview->setPixmap( blankPixmap );
403 lblSchemePath->setText( mRamp.schemeName() + mRamp.variantName() );
409 lblPreview->setPixmap( pixmap );
412 updateCopyingInfo( mRamp.copyingInfo() );
415void QgsCptCityColorRampDialog::clearCopyingInfo()
417 updateCopyingInfo( QMap<QString, QString>() );
420void QgsCptCityColorRampDialog::updateCopyingInfo(
const QMap<QString, QString> ©ingMap )
422 QString authorStr = copyingMap.value( u
"authors"_s );
423 if ( authorStr.length() > 80 )
424 authorStr.replace( authorStr.indexOf(
' ', 80 ), 1, u
"\n"_s );
425 lblAuthorName->setText( authorStr );
426 QString licenseStr = copyingMap.value( u
"license/informal"_s );
427 if ( copyingMap.contains( u
"license/year"_s ) )
428 licenseStr +=
" (" + copyingMap.value( u
"license/year"_s ) +
')';
429 if ( licenseStr.length() > 80 )
430 licenseStr.replace( licenseStr.indexOf(
' ', 80 ), 1, u
"\n"_s );
431 if ( copyingMap.contains( u
"license/url"_s ) )
432 licenseStr +=
"\n[ " + copyingMap.value( u
"license/url"_s ) +
" ]";
435 lblLicenseName->setText( licenseStr );
436 licenseStr.replace(
'\n',
" "_L1 );
437 lblLicensePreview->setText( licenseStr );
438 lblLicensePreview->setCursorPosition( 0 );
439 if ( copyingMap.contains( u
"src/link"_s ) )
440 lblSrcLink->setText( copyingMap.value( u
"src/link"_s ) );
445void QgsCptCityColorRampDialog::cboVariantName_currentIndexChanged(
int index )
448 if ( cboVariantName->currentIndex() != -1 )
449 mRamp.setVariantName( cboVariantName->currentData( Qt::UserRole ).toString() );
450 QgsDebugMsgLevel( u
"variant= %1 - %2 variants"_s.arg( mRamp.variantName() ).arg( mRamp.variantList().count() ), 2 );
455void QgsCptCityColorRampDialog::onFinished()
458 QgsSettings settings;
459 settings.
setValue( u
"Windows/CptCityColorRampV2Dialog/splitter"_s, mSplitter->saveState() );
462void QgsCptCityColorRampDialog::showHelp()
465 const QString helpText = tr(
466 "You can download a more complete set of cpt-city gradients "
467 "by installing the \"Color Ramp Manager\" plugin "
468 "(you must enable Experimental plugins in the plugin manager).\n\n"
470 QMessageBox *msg =
new QMessageBox(
this );
471 msg->setWindowTitle( tr(
"Download More Cpt-city Gradients" ) );
472 msg->setText( helpText );
476void QgsCptCityColorRampDialog::updateUi()
480 if ( !mRamp.schemeName().isEmpty() )
482 bool found = updateRamp();
485 tabBar->setCurrentIndex( 1 );
486 setTreeModel( mAuthorsModel );
487 found = updateRamp();
491 tabBar->setCurrentIndex( 0 );
492 setTreeModel( mSelectionsModel );
496 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled(
true );
506 QgsDebugMsgLevel( u
"result: %1 checked: %2"_s.arg( result() ).arg( cboConvertStandard->isChecked() ), 2 );
508 return ( result() == Accepted && cboConvertStandard->isChecked() );
518 mListWidget->blockSignals(
true );
519 mListWidget->clear();
526 QVector<QgsCptCityDataItem *> childrenRamps = colItem->
childrenRamps(
true );
527 for (
int i = 0; i < childrenRamps.count(); i++ )
535 QListWidgetItem *listItem =
new QListWidgetItem();
536 listItem->setText( rampItem->
shortInfo() );
537 listItem->setIcon( rampItem->
icon( QSize( 75, 50 ) ) );
538 listItem->setToolTip( rampItem->
path() +
'\n' + rampItem->
info() );
539 listItem->setData( Qt::UserRole, QVariant( i ) );
540 mListWidget->addItem( listItem );
541 mListRamps << rampItem;
548 mListWidget->blockSignals(
false );
556 const QSize size = lblPreview->size();
558 if ( event->type() == QEvent::MouseButtonPress )
561 const QPixmap pixmap( mRamp.fileName() );
562 if ( !pixmap.isNull() )
563 lblPreview->setPixmap( pixmap.scaled( size ) );
566 else if ( event->type() == QEvent::MouseButtonRelease )
570 lblPreview->setPixmap( pixmap );
576 return QDialog::eventFilter( obj, event );
580bool QgsCptCityColorRampDialog::updateRamp()
582 mListWidget->clear();
584 cboVariantName->clear();
586 lblCollectionInfo->clear();
588 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
589 updatePreview(
true );
599 const QModelIndex modelIndex = mModel->findPath( mRamp.schemeName() );
600 if ( modelIndex == QModelIndex() )
604 QgsCptCityColorRampItem *childItem = qobject_cast<QgsCptCityColorRampItem *>( mModel->dataItem( modelIndex ) );
616 lblSchemeName->setText( QFileInfo( mRamp.schemeName() ).fileName() );
617 const QModelIndex parentIndex = modelIndex.parent();
618 const QModelIndex selIndex = mTreeFilter->mapFromSource( parentIndex );
621 mTreeView->setCurrentIndex( selIndex );
622 mTreeView->setExpanded( selIndex,
true );
623 mTreeView->scrollTo( selIndex, QAbstractItemView::PositionAtCenter );
624 updateTreeView( mModel->dataItem( parentIndex ),
false );
627 for (
int i = 0; i < mListRamps.count(); i++ )
629 if ( mListRamps.at( i ) == childItem )
631 QgsDebugMsgLevel( u
"found matching item %1 target=%2"_s.arg( mListRamps.at( i )->path(), childItem->
path() ), 2 );
632 QListWidgetItem *listItem = mListWidget->item( i );
633 mListWidget->setCurrentItem( listItem );
636 mListWidget->scrollToItem( listItem, QAbstractItemView::EnsureVisible );
638 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled(
true );
647void QgsCptCityColorRampDialog::showAll()
649 const QModelIndex modelIndex = mModel->findPath( QString() );
650 if ( modelIndex != QModelIndex() )
652 const QModelIndex selIndex = mTreeFilter->mapFromSource( modelIndex );
653 mTreeView->setCurrentIndex( selIndex );
654 mTreeView->setExpanded( selIndex,
true );
655 mTreeView->scrollTo( selIndex, QAbstractItemView::PositionAtCenter );
656 updateTreeView( mModel->dataItem( modelIndex ),
false );
665 mTreeFilter =
new TreeFilterProxyModel(
this, mModel );
666 mTreeView->setModel( mTreeFilter );
670void QgsCptCityColorRampDialog::refresh()
672 QApplication::setOverrideCursor( Qt::WaitCursor );
673 refreshModel( QModelIndex() );
674 QApplication::restoreOverrideCursor();
677void QgsCptCityColorRampDialog::refreshModel(
const QModelIndex &index )
679 if ( index.isValid() )
681 QgsCptCityDataItem *item = mModel->dataItem( index );
692 mModel->refresh( index );
694 for (
int i = 0; i < mModel->rowCount( index ); i++ )
696 QModelIndex idx = mModel->index( i, 0, index );
697 if ( mTreeView->isExpanded( idx ) || !mModel->hasChildren( idx ) )
708 : QSortFilterProxyModel( parent )
711 setSourceModel( mModel );
714bool TreeFilterProxyModel::filterAcceptsRow(
int sourceRow,
const QModelIndex &sourceParent )
const
716 QgsCptCityDataItem *item = mModel->dataItem( mModel->index( sourceRow, 0, sourceParent ) );
static QString qgisSettingsDirPath()
Returns the path to the settings directory in user's home dir.
static QMap< QString, QString > copyingInfo(const QString &fileName)
static QgsCptCityArchive * defaultArchive()
static void initDefaultArchive()
A custom item model for display of CPT City color palettes.
A logical collection of subcollections and color ramps for use in QgsCptCityBrowserModel.
QVector< QgsCptCityDataItem * > childrenRamps(bool recursive)
QgsCptCityColorRampDialog(const QgsCptCityColorRamp &ramp, QWidget *parent=nullptr)
Constructor for QgsCptCityColorRampDialog.
void setRamp(const QgsCptCityColorRamp &ramp)
Sets the color ramp to show in the dialog.
bool saveAsGradientRamp() const
Returns true if the ramp should be converted to a QgsGradientColorRamp.
QDialogButtonBox * buttonBox() const
Returns a reference to the dialog's button box.
bool eventFilter(QObject *obj, QEvent *event) override
void changed()
Emitted when the dialog settings change.
An item that represents a layer that can be opened with one of the providers for a QgsCptCityBrowserM...
const QgsCptCityColorRamp & ramp() const
A color ramp from the CPT City collection.
QStringList variantList() const
QString schemeName() const
QString variantName() const
Base class for all items in a QgsCptCityBrowserModel model.
QString shortInfo() const
virtual int leafCount() const
Returns the total count of "leaf" items (all children which are end nodes).
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Stores settings for use within QGIS.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
static QIcon colorRampPreviewIcon(QgsColorRamp *ramp, QSize size, int padding=0)
Returns an icon preview for a color ramp.
static QPixmap colorRampPreviewPixmap(QgsColorRamp *ramp, QSize size, int padding=0, Qt::Orientation direction=Qt::Horizontal, bool flipDirection=false, bool drawTransparentBackground=true)
Returns a pixmap preview for a color ramp.
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)