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 .arg(
QgsApplication::qgisSettingsDirPath(), QStringLiteral(
"http://soliton.vm.bytemark.co.uk/pub/cpt-city/pkg/" ), QStringLiteral(
"http://soliton.vm.bytemark.co.uk/pub/cpt-city/pkg/cpt-city-svg-2.07.zip" ) );
87 edit->setText( helpText );
88 mStackedWidget->addWidget( edit );
89 mStackedWidget->setCurrentIndex( 1 );
90 tabBar->setVisible(
false );
106 delete mAuthorsModel;
109 delete mSelectionsModel;
111 setTreeModel( mSelectionsModel );
113 mTreeView->setSelectionMode( QAbstractItemView::SingleSelection );
114 mTreeView->setColumnHidden( 1,
true );
118 tabBar->blockSignals(
true );
119 tabBar->addTab( tr(
"Selections by theme" ) );
120 tabBar->addTab( tr(
"All by author" ) );
121 cboVariantName->setIconSize( QSize( 100, 15 ) );
122 lblPreview->installEventFilter(
this );
126 tabBar->blockSignals(
false );
128 connect(
this, &QDialog::finished,
this, &QgsCptCityColorRampDialog::onFinished );
137void QgsCptCityColorRampDialog::populateVariants()
139 const QStringList variantList = mRamp.
variantList();
143 cboVariantName->blockSignals(
true );
144 cboVariantName->clear();
146 if ( variantList.isEmpty() )
148 cboVariantName->setEnabled(
false );
149 cboVariantName->setVisible(
false );
150 cboVariantName_currentIndexChanged( -1 );
155 const QString oldVariant = cboVariantName->currentText();
157 QPixmap blankPixmap( cboVariantName->iconSize() );
158 blankPixmap.fill( Qt::white );
159 const QIcon blankIcon( blankPixmap );
162 const auto constVariantList = variantList;
163 for (
const QString &variant : constVariantList )
165 QString variantStr = variant;
166 if ( variantStr.startsWith(
'-' ) || variantStr.startsWith(
'_' ) )
167 variantStr.remove( 0, 1 );
168 cboVariantName->addItem(
' ' + variantStr );
169 index = cboVariantName->count() - 1;
170 cboVariantName->setItemData( index, variant, Qt::UserRole );
176 cboVariantName->setItemIcon( index, blankIcon );
177 cboVariantName->setItemData( index, Qt::AlignHCenter, Qt::TextAlignmentRole );
180 cboVariantName->blockSignals(
false );
186 if ( newVariant != QString() )
188 if ( newVariant.startsWith(
'-' ) || newVariant.startsWith(
'_' ) )
189 newVariant.remove( 0, 1 );
190 newVariant =
' ' + newVariant;
191 idx = cboVariantName->findText( newVariant );
194 idx = cboVariantName->findText( oldVariant );
199 idx = cboVariantName->count() / 2;
201 cboVariantName->setCurrentIndex( idx );
204 cboVariantName->setEnabled(
true );
205 cboVariantName->setVisible(
true );
209void QgsCptCityColorRampDialog::mTreeView_clicked(
const QModelIndex &index )
211 const QModelIndex &sourceIndex = mTreeFilter->mapToSource( index );
216 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
217 updateTreeView( item );
220void QgsCptCityColorRampDialog::updateTreeView(
QgsCptCityDataItem *item,
bool resetRamp )
231 mRamp.
setName( QString(), QString() );
233 lblSchemeName->clear();
236 updateListWidget( item );
237 lblSchemePath->setText( item->
path() );
238 lblCollectionInfo->setText( QStringLiteral(
"%1 (%2)" ).arg( item->
info() ).arg( item->
leafCount() ) );
243 lblSchemePath->clear();
245 updateListWidget( item );
246 lblCollectionInfo->setText( QStringLiteral(
"%1 (%2)" ).arg( item->
info() ).arg( item->
leafCount() ) );
250 lblSchemePath->clear();
252 updateListWidget( item );
253 lblCollectionInfo->setText( tr(
"All Ramps (%1)" ).arg( item->
leafCount() ) );
257 QgsDebugError( QStringLiteral(
"item %1 has invalid type %2" ).arg( item->
path() ).arg(
static_cast<int>( item->
type() ) ) );
261void QgsCptCityColorRampDialog::mListWidget_itemClicked( QListWidgetItem *item )
266 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled(
true );
267 lblSchemeName->setText( QFileInfo( rampItem->
name() ).fileName() );
278void QgsCptCityColorRampDialog::mListWidget_itemSelectionChanged()
280 if ( mListWidget->selectedItems().isEmpty() )
282 mRamp.
setName( QString(), QString() );
286void QgsCptCityColorRampDialog::tabBar_currentChanged(
int index )
290 setTreeModel( mSelectionsModel );
293 else if ( index == 1 )
295 setTreeModel( mAuthorsModel );
300 QgsDebugError( QStringLiteral(
"invalid index %1" ).arg( index ) );
301 setTreeModel( mAuthorsModel );
305 mListWidget->blockSignals(
true );
307 mListWidget->blockSignals(
false );
311void QgsCptCityColorRampDialog::pbtnLicenseDetails_pressed()
313 QString path, title, copyFile, descFile;
323 title = tr(
"%1 Directory Details" ).arg( item->
path() );
327 title = tr(
"%1 Gradient Details" ).arg( path );
337 QgsDialog dlg(
this, Qt::WindowFlags(), QDialogButtonBox::Close );
338 QVBoxLayout *layout = dlg.layout();
339 dlg.setWindowTitle( title );
340 QTextEdit *textEdit =
new QTextEdit( &dlg );
341 textEdit->setReadOnly(
true );
342 layout->addWidget( textEdit );
346 if ( !copyFile.isNull() )
348 QFile file( copyFile );
349 if ( file.open( QIODevice::ReadOnly | QIODevice::Text ) )
351 copyText = QString( file.readAll() );
356 if ( !descFile.isNull() )
358 QFile file( descFile );
359 if ( file.open( QIODevice::ReadOnly | QIODevice::Text ) )
361 descText = QString( file.readAll() );
365 textEdit->insertPlainText( title +
"\n\n" );
366 textEdit->insertPlainText( QStringLiteral(
"===================" ) );
367 textEdit->insertPlainText( QStringLiteral(
" copying " ) );
368 textEdit->insertPlainText( QStringLiteral(
"===================\n" ) );
369 textEdit->insertPlainText( copyText );
370 textEdit->insertPlainText( QStringLiteral(
"\n" ) );
371 textEdit->insertPlainText( QStringLiteral(
"==================" ) );
372 textEdit->insertPlainText( QStringLiteral(
" description " ) );
373 textEdit->insertPlainText( QStringLiteral(
"==================\n" ) );
374 textEdit->insertPlainText( descText );
375 textEdit->moveCursor( QTextCursor::Start );
377 dlg.resize( 600, 600 );
381void QgsCptCityColorRampDialog::updatePreview(
bool clear )
383 const QSize size = lblPreview->size();
387 lblSchemeName->clear();
388 lblSchemePath->clear();
389 lblLicensePreview->clear();
390 QPixmap blankPixmap( size );
391 blankPixmap.fill( Qt::transparent );
392 lblPreview->setPixmap( blankPixmap );
404 lblPreview->setPixmap( pixmap );
410void QgsCptCityColorRampDialog::clearCopyingInfo()
412 updateCopyingInfo( QMap<QString, QString>() );
415void QgsCptCityColorRampDialog::updateCopyingInfo(
const QMap<QString, QString> ©ingMap )
417 QString authorStr = copyingMap.value( QStringLiteral(
"authors" ) );
418 if ( authorStr.length() > 80 )
419 authorStr.replace( authorStr.indexOf(
' ', 80 ), 1, QStringLiteral(
"\n" ) );
420 lblAuthorName->setText( authorStr );
421 QString licenseStr = copyingMap.value( QStringLiteral(
"license/informal" ) );
422 if ( copyingMap.contains( QStringLiteral(
"license/year" ) ) )
423 licenseStr +=
" (" + copyingMap.value( QStringLiteral(
"license/year" ) ) +
')';
424 if ( licenseStr.length() > 80 )
425 licenseStr.replace( licenseStr.indexOf(
' ', 80 ), 1, QStringLiteral(
"\n" ) );
426 if ( copyingMap.contains( QStringLiteral(
"license/url" ) ) )
427 licenseStr +=
"\n[ " + copyingMap.value( QStringLiteral(
"license/url" ) ) +
" ]";
430 lblLicenseName->setText( licenseStr );
431 licenseStr.replace(
'\n', QLatin1String(
" " ) );
432 lblLicensePreview->setText( licenseStr );
433 lblLicensePreview->setCursorPosition( 0 );
434 if ( copyingMap.contains( QStringLiteral(
"src/link" ) ) )
435 lblSrcLink->setText( copyingMap.value( QStringLiteral(
"src/link" ) ) );
440void QgsCptCityColorRampDialog::cboVariantName_currentIndexChanged(
int index )
443 if ( cboVariantName->currentIndex() != -1 )
444 mRamp.
setVariantName( cboVariantName->currentData( Qt::UserRole ).toString() );
450void QgsCptCityColorRampDialog::onFinished()
454 settings.
setValue( QStringLiteral(
"Windows/CptCityColorRampV2Dialog/splitter" ), mSplitter->saveState() );
457void QgsCptCityColorRampDialog::showHelp()
460 const QString helpText = tr(
"You can download a more complete set of cpt-city gradients "
461 "by installing the \"Color Ramp Manager\" plugin "
462 "(you must enable Experimental plugins in the plugin manager).\n\n"
464 QMessageBox *msg =
new QMessageBox(
this );
465 msg->setWindowTitle( tr(
"Download More Cpt-city Gradients" ) );
466 msg->setText( helpText );
470void QgsCptCityColorRampDialog::updateUi()
476 bool found = updateRamp();
479 tabBar->setCurrentIndex( 1 );
480 setTreeModel( mAuthorsModel );
481 found = updateRamp();
485 tabBar->setCurrentIndex( 0 );
486 setTreeModel( mSelectionsModel );
490 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled(
true );
500 QgsDebugMsgLevel( QStringLiteral(
"result: %1 checked: %2" ).arg( result() ).arg( cboConvertStandard->isChecked() ), 2 );
502 return ( result() == Accepted && cboConvertStandard->isChecked() );
512 mListWidget->blockSignals(
true );
513 mListWidget->clear();
520 QVector<QgsCptCityDataItem *> childrenRamps = colItem->
childrenRamps(
true );
521 for (
int i = 0; i < childrenRamps.count(); i++ )
529 QListWidgetItem *listItem =
new QListWidgetItem();
530 listItem->setText( rampItem->
shortInfo() );
531 listItem->setIcon( rampItem->
icon( QSize( 75, 50 ) ) );
532 listItem->setToolTip( rampItem->
path() +
'\n' + rampItem->
info() );
533 listItem->setData( Qt::UserRole, QVariant( i ) );
534 mListWidget->addItem( listItem );
535 mListRamps << rampItem;
542 mListWidget->blockSignals(
false );
550 const QSize size = lblPreview->size();
552 if ( event->type() == QEvent::MouseButtonPress )
555 const QPixmap pixmap( mRamp.
fileName() );
556 if ( !pixmap.isNull() )
557 lblPreview->setPixmap( pixmap.scaled( size ) );
560 else if ( event->type() == QEvent::MouseButtonRelease )
564 lblPreview->setPixmap( pixmap );
570 return QDialog::eventFilter( obj, event );
574bool QgsCptCityColorRampDialog::updateRamp()
576 mListWidget->clear();
578 cboVariantName->clear();
580 lblCollectionInfo->clear();
582 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
583 updatePreview(
true );
594 if ( modelIndex == QModelIndex() )
610 lblSchemeName->setText( QFileInfo( mRamp.
schemeName() ).fileName() );
611 const QModelIndex parentIndex = modelIndex.parent();
612 const QModelIndex selIndex = mTreeFilter->mapFromSource( parentIndex );
615 mTreeView->setCurrentIndex( selIndex );
616 mTreeView->setExpanded( selIndex,
true );
617 mTreeView->scrollTo( selIndex, QAbstractItemView::PositionAtCenter );
618 updateTreeView( mModel->
dataItem( parentIndex ),
false );
621 for (
int i = 0; i < mListRamps.count(); i++ )
623 if ( mListRamps.at( i ) == childItem )
625 QgsDebugMsgLevel( QStringLiteral(
"found matching item %1 target=%2" ).arg( mListRamps.at( i )->path(), childItem->
path() ), 2 );
626 QListWidgetItem *listItem = mListWidget->item( i );
627 mListWidget->setCurrentItem( listItem );
630 mListWidget->scrollToItem( listItem, QAbstractItemView::EnsureVisible );
632 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled(
true );
641void QgsCptCityColorRampDialog::showAll()
643 const QModelIndex modelIndex = mModel->
findPath( QString() );
644 if ( modelIndex != QModelIndex() )
646 const QModelIndex selIndex = mTreeFilter->mapFromSource( modelIndex );
647 mTreeView->setCurrentIndex( selIndex );
648 mTreeView->setExpanded( selIndex,
true );
649 mTreeView->scrollTo( selIndex, QAbstractItemView::PositionAtCenter );
650 updateTreeView( mModel->
dataItem( modelIndex ),
false );
659 mTreeFilter =
new TreeFilterProxyModel(
this, mModel );
660 mTreeView->setModel( mTreeFilter );
664void QgsCptCityColorRampDialog::refresh()
666 QApplication::setOverrideCursor( Qt::WaitCursor );
667 refreshModel( QModelIndex() );
668 QApplication::restoreOverrideCursor();
671void QgsCptCityColorRampDialog::refreshModel(
const QModelIndex &index )
673 if ( index.isValid() )
688 for (
int i = 0; i < mModel->
rowCount( index ); i++ )
690 QModelIndex idx = mModel->
index( i, 0, index );
691 if ( mTreeView->isExpanded( idx ) || !mModel->
hasChildren( idx ) )
702 : QSortFilterProxyModel( parent )
705 setSourceModel( mModel );
708bool TreeFilterProxyModel::filterAcceptsRow(
int sourceRow,
const QModelIndex &sourceParent )
const
710 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)