17#include "moc_qgscptcitycolorrampdialog.cpp"
32#include <QSortFilterProxyModel>
48 connect( mTreeView, &QTreeView::clicked,
this, &QgsCptCityColorRampDialog::mTreeView_clicked );
49 connect( mListWidget, &QListWidget::itemClicked,
this, &QgsCptCityColorRampDialog::mListWidget_itemClicked );
50 connect( mListWidget, &QListWidget::itemSelectionChanged,
this, &QgsCptCityColorRampDialog::mListWidget_itemSelectionChanged );
51 connect( tabBar, &QTabBar::currentChanged,
this, &QgsCptCityColorRampDialog::tabBar_currentChanged );
52 connect( pbtnLicenseDetails, &QToolButton::pressed,
this, &QgsCptCityColorRampDialog::pbtnLicenseDetails_pressed );
53 connect( cboVariantName,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsCptCityColorRampDialog::cboVariantName_currentIndexChanged );
54 connect( mButtonBox, &QDialogButtonBox::helpRequested,
this, &QgsCptCityColorRampDialog::showHelp );
56 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
59 mSplitter->setSizes( QList<int>() << 250 << 550 );
60 mSplitter->restoreState( settings.
value( QStringLiteral(
"Windows/CptCityColorRampV2Dialog/splitter" ) ).toByteArray() );
62 mModel = mAuthorsModel = mSelectionsModel =
nullptr;
63 mTreeFilter =
nullptr;
69 if ( ! mArchive || mArchive->
isEmpty() )
73 QTextEdit *edit =
new QTextEdit(
nullptr );
74 edit->setReadOnly(
true );
76 const QString helpText = tr(
"Error - cpt-city gradient files not found.\n\n"
77 "You have two means of installing them:\n\n"
78 "1) Install the \"Color Ramp Manager\" python plugin "
79 "(you must enable Experimental plugins in the plugin manager) "
80 "and use it to download latest cpt-city package.\n"
81 "You can install the entire cpt-city archive or a selection for QGIS.\n\n"
82 "2) Download the complete archive (in svg format) "
83 "and unzip it to your QGIS settings directory [%1] .\n\n"
84 "This file can be found at [%2]\nand current file is [%3]"
86 QStringLiteral(
"http://soliton.vm.bytemark.co.uk/pub/cpt-city/pkg/" ),
87 QStringLiteral(
"http://soliton.vm.bytemark.co.uk/pub/cpt-city/pkg/cpt-city-svg-2.07.zip" ) );
88 edit->setText( helpText );
89 mStackedWidget->addWidget( edit );
90 mStackedWidget->setCurrentIndex( 1 );
91 tabBar->setVisible(
false );
107 delete mAuthorsModel;
110 delete mSelectionsModel;
112 setTreeModel( mSelectionsModel );
114 mTreeView->setSelectionMode( QAbstractItemView::SingleSelection );
115 mTreeView->setColumnHidden( 1,
true );
119 tabBar->blockSignals(
true );
120 tabBar->addTab( tr(
"Selections by theme" ) );
121 tabBar->addTab( tr(
"All by author" ) );
122 cboVariantName->setIconSize( QSize( 100, 15 ) );
123 lblPreview->installEventFilter(
this );
127 tabBar->blockSignals(
false );
129 connect(
this, &QDialog::finished,
this, &QgsCptCityColorRampDialog::onFinished );
139void QgsCptCityColorRampDialog::populateVariants()
141 const QStringList variantList = mRamp.
variantList();
145 cboVariantName->blockSignals(
true );
146 cboVariantName->clear();
148 if ( variantList.isEmpty() )
150 cboVariantName->setEnabled(
false );
151 cboVariantName->setVisible(
false );
152 cboVariantName_currentIndexChanged( -1 );
157 const QString oldVariant = cboVariantName->currentText();
159 QPixmap blankPixmap( cboVariantName->iconSize() );
160 blankPixmap.fill( Qt::white );
161 const QIcon blankIcon( blankPixmap );
164 const auto constVariantList = variantList;
165 for (
const QString &variant : constVariantList )
167 QString variantStr = variant;
168 if ( variantStr.startsWith(
'-' ) || variantStr.startsWith(
'_' ) )
169 variantStr.remove( 0, 1 );
170 cboVariantName->addItem(
' ' + variantStr );
171 index = cboVariantName->count() - 1;
172 cboVariantName->setItemData( index, variant, Qt::UserRole );
176 cboVariantName->setItemIcon( index,
179 cboVariantName->setItemIcon( index, blankIcon );
180 cboVariantName->setItemData( index, Qt::AlignHCenter, Qt::TextAlignmentRole );
183 cboVariantName->blockSignals(
false );
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 );
213void QgsCptCityColorRampDialog::mTreeView_clicked(
const QModelIndex &index )
215 const QModelIndex &sourceIndex = mTreeFilter->mapToSource( index );
220 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
221 updateTreeView( item );
224void QgsCptCityColorRampDialog::updateTreeView(
QgsCptCityDataItem *item,
bool resetRamp )
235 mRamp.
setName( QString(), QString() );
237 lblSchemeName->clear();
240 updateListWidget( item );
241 lblSchemePath->setText( item->
path() );
242 lblCollectionInfo->setText( QStringLiteral(
"%1 (%2)" ).arg( item->
info() ).arg( item->
leafCount() ) );
247 lblSchemePath->clear();
249 updateListWidget( item );
250 lblCollectionInfo->setText( QStringLiteral(
"%1 (%2)" ).arg( item->
info() ).arg( item->
leafCount() ) );
254 lblSchemePath->clear();
256 updateListWidget( item );
257 lblCollectionInfo->setText( tr(
"All Ramps (%1)" ).arg( item->
leafCount() ) );
261 QgsDebugError( QStringLiteral(
"item %1 has invalid type %2" ).arg( item->
path() ).arg(
static_cast<int>( item->
type() ) ) );
265void QgsCptCityColorRampDialog::mListWidget_itemClicked( QListWidgetItem *item )
270 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled(
true );
271 lblSchemeName->setText( QFileInfo( rampItem->
name() ).fileName() );
282void QgsCptCityColorRampDialog::mListWidget_itemSelectionChanged()
284 if ( mListWidget->selectedItems().isEmpty() )
286 mRamp.
setName( QString(), QString() );
290void QgsCptCityColorRampDialog::tabBar_currentChanged(
int index )
294 setTreeModel( mSelectionsModel );
297 else if ( index == 1 )
299 setTreeModel( mAuthorsModel );
304 QgsDebugError( QStringLiteral(
"invalid index %1" ).arg( index ) );
305 setTreeModel( mAuthorsModel );
309 mListWidget->blockSignals(
true );
311 mListWidget->blockSignals(
false );
315void QgsCptCityColorRampDialog::pbtnLicenseDetails_pressed()
317 QString path, title, copyFile, descFile;
327 title = tr(
"%1 Directory Details" ).arg( item->
path() );
331 title = tr(
"%1 Gradient Details" ).arg( path );
341 QgsDialog dlg(
this, Qt::WindowFlags(), QDialogButtonBox::Close );
342 QVBoxLayout *layout = dlg.layout();
343 dlg.setWindowTitle( title );
344 QTextEdit *textEdit =
new QTextEdit( &dlg );
345 textEdit->setReadOnly(
true );
346 layout->addWidget( textEdit );
350 if ( ! copyFile.isNull() )
352 QFile file( copyFile );
353 if ( file.open( QIODevice::ReadOnly | QIODevice::Text ) )
355 copyText = QString( file.readAll() );
360 if ( ! descFile.isNull() )
362 QFile file( descFile );
363 if ( file.open( QIODevice::ReadOnly | QIODevice::Text ) )
365 descText = QString( file.readAll() );
369 textEdit->insertPlainText( title +
"\n\n" );
370 textEdit->insertPlainText( QStringLiteral(
"===================" ) );
371 textEdit->insertPlainText( QStringLiteral(
" copying " ) );
372 textEdit->insertPlainText( QStringLiteral(
"===================\n" ) );
373 textEdit->insertPlainText( copyText );
374 textEdit->insertPlainText( QStringLiteral(
"\n" ) );
375 textEdit->insertPlainText( QStringLiteral(
"==================" ) );
376 textEdit->insertPlainText( QStringLiteral(
" description " ) );
377 textEdit->insertPlainText( QStringLiteral(
"==================\n" ) );
378 textEdit->insertPlainText( descText );
379 textEdit->moveCursor( QTextCursor::Start );
381 dlg.resize( 600, 600 );
385void QgsCptCityColorRampDialog::updatePreview(
bool clear )
387 const QSize size = lblPreview->size();
391 lblSchemeName->clear();
392 lblSchemePath->clear();
393 lblLicensePreview->clear();
394 QPixmap blankPixmap( size );
395 blankPixmap.fill( Qt::transparent );
396 lblPreview->setPixmap( blankPixmap );
408 lblPreview->setPixmap( pixmap );
414void QgsCptCityColorRampDialog::clearCopyingInfo()
416 updateCopyingInfo( QMap< QString, QString >() );
419void QgsCptCityColorRampDialog::updateCopyingInfo(
const QMap< QString, QString > ©ingMap )
421 QString authorStr = copyingMap.value( QStringLiteral(
"authors" ) );
422 if ( authorStr.length() > 80 )
423 authorStr.replace( authorStr.indexOf(
' ', 80 ), 1, QStringLiteral(
"\n" ) );
424 lblAuthorName->setText( authorStr );
425 QString licenseStr = copyingMap.value( QStringLiteral(
"license/informal" ) );
426 if ( copyingMap.contains( QStringLiteral(
"license/year" ) ) )
427 licenseStr +=
" (" + copyingMap.value( QStringLiteral(
"license/year" ) ) +
')';
428 if ( licenseStr.length() > 80 )
429 licenseStr.replace( licenseStr.indexOf(
' ', 80 ), 1, QStringLiteral(
"\n" ) );
430 if ( copyingMap.contains( QStringLiteral(
"license/url" ) ) )
431 licenseStr +=
"\n[ " + copyingMap.value( QStringLiteral(
"license/url" ) ) +
" ]";
434 lblLicenseName->setText( licenseStr );
435 licenseStr.replace(
'\n', QLatin1String(
" " ) );
436 lblLicensePreview->setText( licenseStr );
437 lblLicensePreview->setCursorPosition( 0 );
438 if ( copyingMap.contains( QStringLiteral(
"src/link" ) ) )
439 lblSrcLink->setText( copyingMap.value( QStringLiteral(
"src/link" ) ) );
444void QgsCptCityColorRampDialog::cboVariantName_currentIndexChanged(
int index )
447 if ( cboVariantName->currentIndex() != -1 )
448 mRamp.
setVariantName( cboVariantName->currentData( Qt::UserRole ).toString() );
454void QgsCptCityColorRampDialog::onFinished()
458 settings.
setValue( QStringLiteral(
"Windows/CptCityColorRampV2Dialog/splitter" ), mSplitter->saveState() );
461void QgsCptCityColorRampDialog::showHelp()
464 const QString helpText = tr(
"You can download a more complete set of cpt-city gradients "
465 "by installing the \"Color Ramp Manager\" plugin "
466 "(you must enable Experimental plugins in the plugin manager).\n\n"
468 QMessageBox *msg =
new QMessageBox(
this );
469 msg->setWindowTitle( tr(
"Download More Cpt-city Gradients" ) );
470 msg->setText( helpText );
474void QgsCptCityColorRampDialog::updateUi()
480 bool found = updateRamp();
483 tabBar->setCurrentIndex( 1 );
484 setTreeModel( mAuthorsModel );
485 found = updateRamp();
489 tabBar->setCurrentIndex( 0 );
490 setTreeModel( mSelectionsModel );
494 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled(
true );
504 QgsDebugMsgLevel( QStringLiteral(
"result: %1 checked: %2" ).arg( result() ).arg( cboConvertStandard->isChecked() ), 2 );
506 return ( result() == Accepted && cboConvertStandard->isChecked() );
516 mListWidget->blockSignals(
true );
517 mListWidget->clear();
524 QVector<QgsCptCityDataItem *> childrenRamps = colItem->
childrenRamps(
true );
525 for (
int i = 0; i < childrenRamps.count(); i++ )
533 QListWidgetItem *listItem =
new QListWidgetItem();
534 listItem->setText( rampItem->
shortInfo() );
535 listItem->setIcon( rampItem->
icon( QSize( 75, 50 ) ) );
536 listItem->setToolTip( rampItem->
path() +
'\n' + rampItem->
info() );
537 listItem->setData( Qt::UserRole, QVariant( i ) );
538 mListWidget->addItem( listItem );
539 mListRamps << rampItem;
546 mListWidget->blockSignals(
false );
554 const QSize size = lblPreview->size();
556 if ( event->type() == QEvent::MouseButtonPress )
559 const QPixmap pixmap( mRamp.
fileName() );
560 if ( ! pixmap.isNull() )
561 lblPreview->setPixmap( pixmap.scaled( size ) );
564 else if ( event->type() == QEvent::MouseButtonRelease )
568 lblPreview->setPixmap( pixmap );
574 return QDialog::eventFilter( obj, event );
578bool QgsCptCityColorRampDialog::updateRamp()
580 mListWidget->clear();
582 cboVariantName->clear();
584 lblCollectionInfo->clear();
586 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
587 updatePreview(
true );
598 if ( modelIndex == QModelIndex() )
603 qobject_cast<QgsCptCityColorRampItem *>( mModel->
dataItem( modelIndex ) );
615 lblSchemeName->setText( QFileInfo( mRamp.
schemeName() ).fileName() );
616 const QModelIndex parentIndex = modelIndex.parent();
617 const QModelIndex selIndex = mTreeFilter->mapFromSource( parentIndex );
620 mTreeView->setCurrentIndex( selIndex );
621 mTreeView->setExpanded( selIndex,
true );
622 mTreeView->scrollTo( selIndex, QAbstractItemView::PositionAtCenter );
623 updateTreeView( mModel->
dataItem( parentIndex ),
false );
626 for (
int i = 0; i < mListRamps.count(); i++ )
628 if ( mListRamps.at( i ) == childItem )
630 QgsDebugMsgLevel( QStringLiteral(
"found matching item %1 target=%2" ).arg( mListRamps.at( i )->path(), childItem->
path() ), 2 );
631 QListWidgetItem *listItem = mListWidget->item( i );
632 mListWidget->setCurrentItem( listItem );
635 mListWidget->scrollToItem( listItem, QAbstractItemView::EnsureVisible );
637 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled(
true );
646void QgsCptCityColorRampDialog::showAll()
648 const QModelIndex modelIndex = mModel->
findPath( QString() );
649 if ( modelIndex != QModelIndex() )
651 const QModelIndex selIndex = mTreeFilter->mapFromSource( modelIndex );
652 mTreeView->setCurrentIndex( selIndex );
653 mTreeView->setExpanded( selIndex,
true );
654 mTreeView->scrollTo( selIndex, QAbstractItemView::PositionAtCenter );
655 updateTreeView( mModel->
dataItem( modelIndex ),
false );
664 mTreeFilter =
new TreeFilterProxyModel(
this, mModel );
665 mTreeView->setModel( mTreeFilter );
669void QgsCptCityColorRampDialog::refresh()
671 QApplication::setOverrideCursor( Qt::WaitCursor );
672 refreshModel( QModelIndex() );
673 QApplication::restoreOverrideCursor();
676void QgsCptCityColorRampDialog::refreshModel(
const QModelIndex &index )
678 if ( index.isValid() )
693 for (
int i = 0; i < mModel->
rowCount( index ); i++ )
695 QModelIndex idx = mModel->
index( i, 0, index );
696 if ( mTreeView->isExpanded( idx ) || !mModel->
hasChildren( idx ) )
707 : QSortFilterProxyModel( parent )
710 setSourceModel( mModel );
713bool TreeFilterProxyModel::filterAcceptsRow(
int sourceRow,
const QModelIndex &sourceParent )
const
715 QgsCptCityDataItem *item = mModel->dataItem( mModel->index( sourceRow, 0, sourceParent ) );
static QString qgisSettingsDirPath()
Returns the path to the settings directory in user's home dir.
QString descFileName(const QString &dirName) const
static QMap< QString, QString > copyingInfo(const QString &fileName)
QString archiveName() const
static QgsCptCityArchive * defaultArchive()
QString copyingFileName(const QString &dirName) const
bool isEmpty() const
Returns true if archive is empty.
static void initDefaultArchive()
A custom item model for display of CPT City color palettes.
void refresh(const QString &path)
Refresh the item specified by path.
QModelIndex findPath(const QString &path)
Returns index of a path.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
QgsCptCityDataItem * dataItem(const QModelIndex &idx) const
Returns the data item corresponding to the given index.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
bool hasChildren(const QModelIndex &parent=QModelIndex()) const override
A Collection: logical collection of subcollections and color ramps.
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.
Item that represents a layer that can be opened with one of the providers.
const QgsCptCityColorRamp & ramp() const
A color ramp from the CPT City collection.
void setName(const QString &schemeName, const QString &variantName=QString(), const QStringList &variantList=QStringList())
QgsStringMap copyingInfo() const
void setVariantList(const QStringList &variantList)
QStringList variantList() const
void copy(const QgsCptCityColorRamp *other)
void setVariantName(const QString &variantName)
QString schemeName() const
QString variantName() const
Base class for all items in the model.
QString shortInfo() const
virtual int leafCount() const
Returns the total count of "leaf" items (all children which are end nodes).
A generic dialog with layout and button box.
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...
This class is a composition of two QSettings instances:
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)