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 = tr(
"Error - cpt-city gradient files not found.\n\n"
80 "You have two means of installing them:\n\n"
81 "1) Install the \"Color Ramp Manager\" python plugin "
82 "(you must enable Experimental plugins in the plugin manager) "
83 "and use it to download latest cpt-city package.\n"
84 "You can install the entire cpt-city archive or a selection for QGIS.\n\n"
85 "2) Download the complete archive (in svg format) "
86 "and unzip it to your QGIS settings directory [%1] .\n\n"
87 "This file can be found at [%2]\nand current file is [%3]"
89 .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 );
90 edit->setText( helpText );
91 mStackedWidget->addWidget( edit );
92 mStackedWidget->setCurrentIndex( 1 );
93 tabBar->setVisible(
false );
104 QgsDebugMsgLevel( u
"ramp name= %1 variant= %2 - %3 variants"_s.arg( mRamp.schemeName(), mRamp.variantName() ).arg( mRamp.variantList().count() ), 2 );
109 delete mAuthorsModel;
112 delete mSelectionsModel;
114 setTreeModel( mSelectionsModel );
116 mTreeView->setSelectionMode( QAbstractItemView::SingleSelection );
117 mTreeView->setColumnHidden( 1,
true );
121 tabBar->blockSignals(
true );
122 tabBar->addTab( tr(
"Selections by theme" ) );
123 tabBar->addTab( tr(
"All by author" ) );
124 cboVariantName->setIconSize( QSize( 100, 15 ) );
125 lblPreview->installEventFilter(
this );
129 tabBar->blockSignals(
false );
131 connect(
this, &QDialog::finished,
this, &QgsCptCityColorRampDialog::onFinished );
140void QgsCptCityColorRampDialog::populateVariants()
142 const QStringList variantList = mRamp.
variantList();
146 cboVariantName->blockSignals(
true );
147 cboVariantName->clear();
149 if ( variantList.isEmpty() )
151 cboVariantName->setEnabled(
false );
152 cboVariantName->setVisible(
false );
153 cboVariantName_currentIndexChanged( -1 );
158 const QString oldVariant = cboVariantName->currentText();
159 QgsCptCityColorRamp
ramp( mRamp.schemeName(), mRamp.variantList(), QString() );
160 QPixmap blankPixmap( cboVariantName->iconSize() );
161 blankPixmap.fill( Qt::white );
162 const QIcon blankIcon( blankPixmap );
165 const auto constVariantList = variantList;
166 for (
const QString &variant : constVariantList )
168 QString variantStr = variant;
169 if ( variantStr.startsWith(
'-' ) || variantStr.startsWith(
'_' ) )
170 variantStr.remove( 0, 1 );
171 cboVariantName->addItem(
' ' + variantStr );
172 index = cboVariantName->count() - 1;
173 cboVariantName->setItemData( index, variant, Qt::UserRole );
175 ramp.setVariantName( variant );
176 if (
ramp.loadFile() )
179 cboVariantName->setItemIcon( index, blankIcon );
180 cboVariantName->setItemData( index, Qt::AlignHCenter, Qt::TextAlignmentRole );
183 cboVariantName->blockSignals(
false );
187 QString newVariant = mRamp.variantName();
188 QgsDebugMsgLevel( u
"variant= %1 - %2 variants"_s.arg( mRamp.variantName() ).arg( mRamp.variantList().count() ), 2 );
189 if ( newVariant != QString() )
191 if ( newVariant.startsWith(
'-' ) || newVariant.startsWith(
'_' ) )
192 newVariant.remove( 0, 1 );
193 newVariant =
' ' + newVariant;
194 idx = cboVariantName->findText( newVariant );
197 idx = cboVariantName->findText( oldVariant );
202 idx = cboVariantName->count() / 2;
204 cboVariantName->setCurrentIndex( idx );
207 cboVariantName->setEnabled(
true );
208 cboVariantName->setVisible(
true );
212void QgsCptCityColorRampDialog::mTreeView_clicked(
const QModelIndex &index )
214 const QModelIndex &sourceIndex = mTreeFilter->mapToSource( index );
215 QgsCptCityDataItem *item = mModel->dataItem( sourceIndex );
219 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
220 updateTreeView( item );
223void QgsCptCityColorRampDialog::updateTreeView(
QgsCptCityDataItem *item,
bool resetRamp )
234 mRamp.setName( QString(), QString() );
235 QgsDebugMsgLevel( u
"variant= %1 - %2 variants"_s.arg( mRamp.variantName() ).arg( mRamp.variantList().count() ), 2 );
236 lblSchemeName->clear();
239 updateListWidget( item );
240 lblSchemePath->setText( item->
path() );
241 lblCollectionInfo->setText( u
"%1 (%2)"_s.arg( item->
info() ).arg( item->
leafCount() ) );
246 lblSchemePath->clear();
248 updateListWidget( item );
249 lblCollectionInfo->setText( u
"%1 (%2)"_s.arg( item->
info() ).arg( item->
leafCount() ) );
253 lblSchemePath->clear();
255 updateListWidget( item );
256 lblCollectionInfo->setText( tr(
"All Ramps (%1)" ).arg( item->
leafCount() ) );
260 QgsDebugError( u
"item %1 has invalid type %2"_s.arg( item->
path() ).arg(
static_cast<int>( item->
type() ) ) );
264void QgsCptCityColorRampDialog::mListWidget_itemClicked( QListWidgetItem *item )
266 QgsCptCityColorRampItem *rampItem = mListRamps.at( item->data( Qt::UserRole ).toInt() );
269 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled(
true );
270 lblSchemeName->setText( QFileInfo( rampItem->
name() ).fileName() );
271 mRamp.copy( &rampItem->
ramp() );
272 QgsDebugMsgLevel( u
"variant= %1 - %2 variants"_s.arg( mRamp.variantName() ).arg( mRamp.variantList().count() ), 2 );
281void QgsCptCityColorRampDialog::mListWidget_itemSelectionChanged()
283 if ( mListWidget->selectedItems().isEmpty() )
285 mRamp.setName( QString(), QString() );
289void QgsCptCityColorRampDialog::tabBar_currentChanged(
int index )
293 setTreeModel( mSelectionsModel );
296 else if ( index == 1 )
298 setTreeModel( mAuthorsModel );
304 setTreeModel( mAuthorsModel );
308 mListWidget->blockSignals(
true );
310 mListWidget->blockSignals(
false );
314void QgsCptCityColorRampDialog::pbtnLicenseDetails_pressed()
316 QString path, title, copyFile, descFile;
319 QgsCptCityDataItem *item = mModel->dataItem( mTreeFilter->mapToSource( mTreeView->currentIndex() ) );
326 title = tr(
"%1 Directory Details" ).arg( item->
path() );
330 title = tr(
"%1 Gradient Details" ).arg( path );
336 copyFile = mArchive->copyingFileName( path );
337 descFile = mArchive->descFileName( path );
340 QgsDialog dlg(
this, Qt::WindowFlags(), QDialogButtonBox::Close );
341 QVBoxLayout *layout = dlg.layout();
342 dlg.setWindowTitle( title );
343 QTextEdit *textEdit =
new QTextEdit( &dlg );
344 textEdit->setReadOnly(
true );
345 layout->addWidget( textEdit );
349 if ( !copyFile.isNull() )
351 QFile file( copyFile );
352 if ( file.open( QIODevice::ReadOnly | QIODevice::Text ) )
354 copyText = QString( file.readAll() );
359 if ( !descFile.isNull() )
361 QFile file( descFile );
362 if ( file.open( QIODevice::ReadOnly | QIODevice::Text ) )
364 descText = QString( file.readAll() );
368 textEdit->insertPlainText( title +
"\n\n" );
369 textEdit->insertPlainText( u
"==================="_s );
370 textEdit->insertPlainText( u
" copying "_s );
371 textEdit->insertPlainText( u
"===================\n"_s );
372 textEdit->insertPlainText( copyText );
373 textEdit->insertPlainText( u
"\n"_s );
374 textEdit->insertPlainText( u
"=================="_s );
375 textEdit->insertPlainText( u
" description "_s );
376 textEdit->insertPlainText( u
"==================\n"_s );
377 textEdit->insertPlainText( descText );
378 textEdit->moveCursor( QTextCursor::Start );
380 dlg.resize( 600, 600 );
384void QgsCptCityColorRampDialog::updatePreview(
bool clear )
386 const QSize size = lblPreview->size();
388 if ( clear || mRamp.schemeName().isEmpty() )
390 lblSchemeName->clear();
391 lblSchemePath->clear();
392 lblLicensePreview->clear();
393 QPixmap blankPixmap( size );
394 blankPixmap.fill( Qt::transparent );
395 lblPreview->setPixmap( blankPixmap );
401 lblSchemePath->setText( mRamp.schemeName() + mRamp.variantName() );
407 lblPreview->setPixmap( pixmap );
410 updateCopyingInfo( mRamp.copyingInfo() );
413void QgsCptCityColorRampDialog::clearCopyingInfo()
415 updateCopyingInfo( QMap<QString, QString>() );
418void QgsCptCityColorRampDialog::updateCopyingInfo(
const QMap<QString, QString> ©ingMap )
420 QString authorStr = copyingMap.value( u
"authors"_s );
421 if ( authorStr.length() > 80 )
422 authorStr.replace( authorStr.indexOf(
' ', 80 ), 1, u
"\n"_s );
423 lblAuthorName->setText( authorStr );
424 QString licenseStr = copyingMap.value( u
"license/informal"_s );
425 if ( copyingMap.contains( u
"license/year"_s ) )
426 licenseStr +=
" (" + copyingMap.value( u
"license/year"_s ) +
')';
427 if ( licenseStr.length() > 80 )
428 licenseStr.replace( licenseStr.indexOf(
' ', 80 ), 1, u
"\n"_s );
429 if ( copyingMap.contains( u
"license/url"_s ) )
430 licenseStr +=
"\n[ " + copyingMap.value( u
"license/url"_s ) +
" ]";
433 lblLicenseName->setText( licenseStr );
434 licenseStr.replace(
'\n',
" "_L1 );
435 lblLicensePreview->setText( licenseStr );
436 lblLicensePreview->setCursorPosition( 0 );
437 if ( copyingMap.contains( u
"src/link"_s ) )
438 lblSrcLink->setText( copyingMap.value( u
"src/link"_s ) );
443void QgsCptCityColorRampDialog::cboVariantName_currentIndexChanged(
int index )
446 if ( cboVariantName->currentIndex() != -1 )
447 mRamp.setVariantName( cboVariantName->currentData( Qt::UserRole ).toString() );
448 QgsDebugMsgLevel( u
"variant= %1 - %2 variants"_s.arg( mRamp.variantName() ).arg( mRamp.variantList().count() ), 2 );
453void QgsCptCityColorRampDialog::onFinished()
456 QgsSettings settings;
457 settings.
setValue( u
"Windows/CptCityColorRampV2Dialog/splitter"_s, mSplitter->saveState() );
460void QgsCptCityColorRampDialog::showHelp()
463 const QString helpText = tr(
"You can download a more complete set of cpt-city gradients "
464 "by installing the \"Color Ramp Manager\" plugin "
465 "(you must enable Experimental plugins in the plugin manager).\n\n"
467 QMessageBox *msg =
new QMessageBox(
this );
468 msg->setWindowTitle( tr(
"Download More Cpt-city Gradients" ) );
469 msg->setText( helpText );
473void QgsCptCityColorRampDialog::updateUi()
477 if ( !mRamp.schemeName().isEmpty() )
479 bool found = updateRamp();
482 tabBar->setCurrentIndex( 1 );
483 setTreeModel( mAuthorsModel );
484 found = updateRamp();
488 tabBar->setCurrentIndex( 0 );
489 setTreeModel( mSelectionsModel );
493 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled(
true );
503 QgsDebugMsgLevel( u
"result: %1 checked: %2"_s.arg( result() ).arg( cboConvertStandard->isChecked() ), 2 );
505 return ( result() == Accepted && cboConvertStandard->isChecked() );
515 mListWidget->blockSignals(
true );
516 mListWidget->clear();
523 QVector<QgsCptCityDataItem *> childrenRamps = colItem->
childrenRamps(
true );
524 for (
int i = 0; i < childrenRamps.count(); i++ )
532 QListWidgetItem *listItem =
new QListWidgetItem();
533 listItem->setText( rampItem->
shortInfo() );
534 listItem->setIcon( rampItem->
icon( QSize( 75, 50 ) ) );
535 listItem->setToolTip( rampItem->
path() +
'\n' + rampItem->
info() );
536 listItem->setData( Qt::UserRole, QVariant( i ) );
537 mListWidget->addItem( listItem );
538 mListRamps << rampItem;
545 mListWidget->blockSignals(
false );
553 const QSize size = lblPreview->size();
555 if ( event->type() == QEvent::MouseButtonPress )
558 const QPixmap pixmap( mRamp.fileName() );
559 if ( !pixmap.isNull() )
560 lblPreview->setPixmap( pixmap.scaled( size ) );
563 else if ( event->type() == QEvent::MouseButtonRelease )
567 lblPreview->setPixmap( pixmap );
573 return QDialog::eventFilter( obj, event );
577bool QgsCptCityColorRampDialog::updateRamp()
579 mListWidget->clear();
581 cboVariantName->clear();
583 lblCollectionInfo->clear();
585 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
586 updatePreview(
true );
596 const QModelIndex modelIndex = mModel->findPath( mRamp.schemeName() );
597 if ( modelIndex == QModelIndex() )
601 QgsCptCityColorRampItem *childItem = qobject_cast<QgsCptCityColorRampItem *>( mModel->dataItem( modelIndex ) );
613 lblSchemeName->setText( QFileInfo( mRamp.schemeName() ).fileName() );
614 const QModelIndex parentIndex = modelIndex.parent();
615 const QModelIndex selIndex = mTreeFilter->mapFromSource( parentIndex );
618 mTreeView->setCurrentIndex( selIndex );
619 mTreeView->setExpanded( selIndex,
true );
620 mTreeView->scrollTo( selIndex, QAbstractItemView::PositionAtCenter );
621 updateTreeView( mModel->dataItem( parentIndex ),
false );
624 for (
int i = 0; i < mListRamps.count(); i++ )
626 if ( mListRamps.at( i ) == childItem )
628 QgsDebugMsgLevel( u
"found matching item %1 target=%2"_s.arg( mListRamps.at( i )->path(), childItem->
path() ), 2 );
629 QListWidgetItem *listItem = mListWidget->item( i );
630 mListWidget->setCurrentItem( listItem );
633 mListWidget->scrollToItem( listItem, QAbstractItemView::EnsureVisible );
635 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled(
true );
644void QgsCptCityColorRampDialog::showAll()
646 const QModelIndex modelIndex = mModel->findPath( QString() );
647 if ( modelIndex != QModelIndex() )
649 const QModelIndex selIndex = mTreeFilter->mapFromSource( modelIndex );
650 mTreeView->setCurrentIndex( selIndex );
651 mTreeView->setExpanded( selIndex,
true );
652 mTreeView->scrollTo( selIndex, QAbstractItemView::PositionAtCenter );
653 updateTreeView( mModel->dataItem( modelIndex ),
false );
662 mTreeFilter =
new TreeFilterProxyModel(
this, mModel );
663 mTreeView->setModel( mTreeFilter );
667void QgsCptCityColorRampDialog::refresh()
669 QApplication::setOverrideCursor( Qt::WaitCursor );
670 refreshModel( QModelIndex() );
671 QApplication::restoreOverrideCursor();
674void QgsCptCityColorRampDialog::refreshModel(
const QModelIndex &index )
676 if ( index.isValid() )
678 QgsCptCityDataItem *item = mModel->dataItem( index );
689 mModel->refresh( index );
691 for (
int i = 0; i < mModel->rowCount( index ); i++ )
693 QModelIndex idx = mModel->index( i, 0, index );
694 if ( mTreeView->isExpanded( idx ) || !mModel->hasChildren( idx ) )
705 : QSortFilterProxyModel( parent )
708 setSourceModel( mModel );
711bool TreeFilterProxyModel::filterAcceptsRow(
int sourceRow,
const QModelIndex &sourceParent )
const
713 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)