26 #include <QPushButton> 29 #include <QMessageBox> 30 #include <QSortFilterProxyModel> 45 connect( mTreeView, &QTreeView::clicked,
this, &QgsCptCityColorRampDialog::mTreeView_clicked );
46 connect( mListWidget, &QListWidget::itemClicked,
this, &QgsCptCityColorRampDialog::mListWidget_itemClicked );
47 connect( mListWidget, &QListWidget::itemSelectionChanged,
this, &QgsCptCityColorRampDialog::mListWidget_itemSelectionChanged );
48 connect( tabBar, &QTabBar::currentChanged,
this, &QgsCptCityColorRampDialog::tabBar_currentChanged );
49 connect( pbtnLicenseDetails, &QToolButton::pressed,
this, &QgsCptCityColorRampDialog::pbtnLicenseDetails_pressed );
50 connect( cboVariantName,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsCptCityColorRampDialog::cboVariantName_currentIndexChanged );
51 connect( buttonBox, &QDialogButtonBox::helpRequested,
this, &QgsCptCityColorRampDialog::showHelp );
53 buttonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
56 restoreGeometry( settings.
value( QStringLiteral(
"Windows/CptCityColorRampV2Dialog/geometry" ) ).toByteArray() );
57 mSplitter->setSizes( QList<int>() << 250 << 550 );
58 mSplitter->restoreState( settings.
value( QStringLiteral(
"Windows/CptCityColorRampV2Dialog/splitter" ) ).toByteArray() );
60 mModel = mAuthorsModel = mSelectionsModel =
nullptr;
61 mTreeFilter =
nullptr;
67 if ( ! mArchive || mArchive->
isEmpty() )
71 QTextEdit *edit =
new QTextEdit(
nullptr );
72 edit->setReadOnly(
true );
74 QString helpText = tr(
"Error - cpt-city gradient files not found.\n\n" 75 "You have two means of installing them:\n\n" 76 "1) Install the \"Color Ramp Manager\" python plugin " 77 "(you must enable Experimental plugins in the plugin manager) " 78 "and use it to download latest cpt-city package.\n" 79 "You can install the entire cpt-city archive or a selection for QGIS.\n\n" 80 "2) Download the complete archive (in svg format) " 81 "and unzip it to your QGIS settings directory [%1] .\n\n" 82 "This file can be found at [%2]\nand current file is [%3]" 84 QStringLiteral(
"http://soliton.vm.bytemark.co.uk/pub/cpt-city/pkg/" ),
85 QStringLiteral(
"http://soliton.vm.bytemark.co.uk/pub/cpt-city/pkg/cpt-city-svg-2.07.zip" ) );
86 edit->setText( helpText );
87 mStackedWidget->addWidget( edit );
88 mStackedWidget->setCurrentIndex( 1 );
89 tabBar->setVisible(
false );
103 QgsDebugMsg( QStringLiteral(
"loading model/view objects" ) );
105 delete mAuthorsModel;
108 delete mSelectionsModel;
110 setTreeModel( mSelectionsModel );
112 mTreeView->setSelectionMode( QAbstractItemView::SingleSelection );
113 mTreeView->setColumnHidden( 1,
true );
114 QgsDebugMsg( QStringLiteral(
"done loading model/view objects" ) );
117 tabBar->blockSignals(
true );
118 tabBar->addTab( tr(
"Selections by theme" ) );
119 tabBar->addTab( tr(
"All by author" ) );
120 cboVariantName->setIconSize( QSize( 100, 15 ) );
121 lblPreview->installEventFilter(
this );
125 tabBar->blockSignals(
false );
127 connect(
this, &QDialog::finished,
this, &QgsCptCityColorRampDialog::onFinished );
137 void QgsCptCityColorRampDialog::populateVariants()
143 cboVariantName->blockSignals(
true );
144 cboVariantName->clear();
146 if ( variantList.isEmpty() )
148 cboVariantName->setEnabled(
false );
149 cboVariantName->setVisible(
false );
150 cboVariantName_currentIndexChanged( -1 );
155 QString oldVariant = cboVariantName->currentText();
157 QPixmap blankPixmap( cboVariantName->iconSize() );
158 blankPixmap.fill( Qt::white );
159 QIcon blankIcon( blankPixmap );
162 Q_FOREACH (
const QString &variant, variantList )
164 QString variantStr = variant;
165 if ( variantStr.startsWith(
'-' ) || variantStr.startsWith(
'_' ) )
166 variantStr.remove( 0, 1 );
167 cboVariantName->addItem(
' ' + variantStr );
168 index = cboVariantName->count() - 1;
169 cboVariantName->setItemData( index, variant, Qt::UserRole );
173 cboVariantName->setItemIcon( index,
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 );
210 void QgsCptCityColorRampDialog::mTreeView_clicked(
const QModelIndex &index )
212 const QModelIndex &sourceIndex = mTreeFilter->mapToSource( index );
216 QgsDebugMsg( QStringLiteral(
"item %1 clicked" ).arg( item->
name() ) );
217 buttonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
218 updateTreeView( item );
221 void QgsCptCityColorRampDialog::updateTreeView(
QgsCptCityDataItem *item,
bool resetRamp )
232 mRamp.
setName( QString(), QString() );
234 lblSchemeName->clear();
237 updateListWidget( item );
238 lblSchemePath->setText( item->
path() );
239 lblCollectionInfo->setText( QStringLiteral(
"%1 (%2)" ).arg( item->
info() ).arg( item->
leafCount() ) );
244 lblSchemePath->clear();
246 updateListWidget( item );
247 lblCollectionInfo->setText( QStringLiteral(
"%1 (%2)" ).arg( item->
info() ).arg( item->
leafCount() ) );
251 lblSchemePath->clear();
253 updateListWidget( item );
254 lblCollectionInfo->setText( tr(
"All Ramps (%1)" ).arg( item->
leafCount() ) );
258 QgsDebugMsg( QStringLiteral(
"item %1 has invalid type %2" ).arg( item->
path() ).arg( static_cast<int>( item->
type() ) ) );
262 void QgsCptCityColorRampDialog::mListWidget_itemClicked( QListWidgetItem *item )
267 buttonBox->button( QDialogButtonBox::Ok )->setEnabled(
true );
268 lblSchemeName->setText( QFileInfo( rampItem->
name() ).fileName() );
279 void QgsCptCityColorRampDialog::mListWidget_itemSelectionChanged()
281 if ( mListWidget->selectedItems().isEmpty() )
283 mRamp.
setName( QString(), QString() );
287 void QgsCptCityColorRampDialog::tabBar_currentChanged(
int index )
291 setTreeModel( mSelectionsModel );
294 else if ( index == 1 )
296 setTreeModel( mAuthorsModel );
301 QgsDebugMsg( QStringLiteral(
"invalid index %1" ).arg( index ) );
302 setTreeModel( mAuthorsModel );
306 mListWidget->blockSignals(
true );
308 mListWidget->blockSignals(
false );
312 void QgsCptCityColorRampDialog::pbtnLicenseDetails_pressed()
314 QString path, title, copyFile, descFile;
324 title = tr(
"%1 Directory Details" ).arg( item->
path() );
328 title = tr(
"%1 Gradient Details" ).arg( path );
338 QgsDialog dlg(
this,
nullptr, QDialogButtonBox::Close );
339 QVBoxLayout *layout = dlg.
layout();
340 dlg.setWindowTitle( title );
341 QTextEdit *textEdit =
new QTextEdit( &dlg );
342 textEdit->setReadOnly(
true );
343 layout->addWidget( textEdit );
347 if ( ! copyFile.isNull() )
349 QFile file( copyFile );
350 if ( file.open( QIODevice::ReadOnly | QIODevice::Text ) )
352 copyText = QString( file.readAll() );
357 if ( ! descFile.isNull() )
359 QFile file( descFile );
360 if ( file.open( QIODevice::ReadOnly | QIODevice::Text ) )
362 descText = QString( file.readAll() );
366 textEdit->insertPlainText( title +
"\n\n" );
367 textEdit->insertPlainText( QStringLiteral(
"===================" ) );
368 textEdit->insertPlainText( QStringLiteral(
" copying " ) );
369 textEdit->insertPlainText( QStringLiteral(
"===================\n" ) );
370 textEdit->insertPlainText( copyText );
371 textEdit->insertPlainText( QStringLiteral(
"\n" ) );
372 textEdit->insertPlainText( QStringLiteral(
"==================" ) );
373 textEdit->insertPlainText( QStringLiteral(
" description " ) );
374 textEdit->insertPlainText( QStringLiteral(
"==================\n" ) );
375 textEdit->insertPlainText( descText );
376 textEdit->moveCursor( QTextCursor::Start );
378 dlg.resize( 600, 600 );
382 void QgsCptCityColorRampDialog::updatePreview(
bool clear )
384 QSize size = lblPreview->size();
388 lblSchemeName->clear();
389 lblSchemePath->clear();
390 lblLicensePreview->clear();
391 QPixmap blankPixmap( size );
392 blankPixmap.fill( Qt::transparent );
393 lblPreview->setPixmap( blankPixmap );
405 lblPreview->setPixmap( pixmap );
411 void QgsCptCityColorRampDialog::clearCopyingInfo()
413 updateCopyingInfo( QMap< QString, QString >() );
416 void QgsCptCityColorRampDialog::updateCopyingInfo(
const QMap< QString, QString > ©ingMap )
418 QString authorStr = copyingMap.value( QStringLiteral(
"authors" ) );
419 if ( authorStr.length() > 80 )
420 authorStr.replace( authorStr.indexOf(
' ', 80 ), 1, QStringLiteral(
"\n" ) );
421 lblAuthorName->setText( authorStr );
422 QString licenseStr = copyingMap.value( QStringLiteral(
"license/informal" ) );
423 if ( copyingMap.contains( QStringLiteral(
"license/year" ) ) )
424 licenseStr +=
" (" + copyingMap.value( QStringLiteral(
"license/year" ) ) +
')';
425 if ( licenseStr.length() > 80 )
426 licenseStr.replace( licenseStr.indexOf(
' ', 80 ), 1, QStringLiteral(
"\n" ) );
427 if ( copyingMap.contains( QStringLiteral(
"license/url" ) ) )
428 licenseStr +=
"\n[ " + copyingMap.value( QStringLiteral(
"license/url" ) ) +
" ]";
431 lblLicenseName->setText( licenseStr );
432 licenseStr.replace(
'\n', QLatin1String(
" " ) );
433 lblLicensePreview->setText( licenseStr );
434 lblLicensePreview->setCursorPosition( 0 );
435 if ( copyingMap.contains( QStringLiteral(
"src/link" ) ) )
436 lblSrcLink->setText( copyingMap.value( QStringLiteral(
"src/link" ) ) );
441 void QgsCptCityColorRampDialog::cboVariantName_currentIndexChanged(
int index )
444 if ( cboVariantName->currentIndex() != -1 )
445 mRamp.
setVariantName( cboVariantName->currentData( Qt::UserRole ).toString() );
451 void QgsCptCityColorRampDialog::onFinished()
455 settings.
setValue( QStringLiteral(
"Windows/CptCityColorRampV2Dialog/geometry" ),
saveGeometry() );
456 settings.
setValue( QStringLiteral(
"Windows/CptCityColorRampV2Dialog/splitter" ), mSplitter->saveState() );
459 void QgsCptCityColorRampDialog::showHelp()
462 QString helpText = tr(
"You can download a more complete set of cpt-city gradients " 463 "by installing the \"Color Ramp Manager\" plugin " 464 "(you must enable Experimental plugins in the plugin manager).\n\n" 466 QMessageBox *msg =
new QMessageBox(
this );
467 msg->setWindowTitle( tr(
"Download More Cpt-city Gradients" ) );
468 msg->setText( helpText );
472 void QgsCptCityColorRampDialog::updateUi()
478 bool found = updateRamp();
481 tabBar->setCurrentIndex( 1 );
482 setTreeModel( mAuthorsModel );
483 found = updateRamp();
487 tabBar->setCurrentIndex( 0 );
488 setTreeModel( mSelectionsModel );
492 buttonBox->button( QDialogButtonBox::Ok )->setEnabled(
true );
502 QgsDebugMsg( QStringLiteral(
"result: %1 checked: %2" ).arg( result() ).arg( cboConvertStandard->isChecked() ) );
504 return ( result() == Accepted && cboConvertStandard->isChecked() );
509 mListWidget->blockSignals(
true );
510 mListWidget->clear();
517 QVector<QgsCptCityDataItem *> childrenRamps = colItem->
childrenRamps(
true );
518 for (
int i = 0; i < childrenRamps.count(); i++ )
523 QgsDebugMsg(
"invalid item " + childrenRamps[i]->path() );
526 QListWidgetItem *listItem =
new QListWidgetItem();
527 listItem->setText( rampItem->
shortInfo() );
528 listItem->setIcon( rampItem->
icon( QSize( 75, 50 ) ) );
529 listItem->setToolTip( rampItem->
path() +
'\n' + rampItem->
info() );
530 listItem->setData( Qt::UserRole, QVariant( i ) );
531 mListWidget->addItem( listItem );
532 mListRamps << rampItem;
539 mListWidget->blockSignals(
false );
547 QSize size = lblPreview->size();
549 if ( event->type() == QEvent::MouseButtonPress )
553 if ( ! pixmap.isNull() )
554 lblPreview->setPixmap( pixmap.scaled( size ) );
557 else if ( event->type() == QEvent::MouseButtonRelease )
561 lblPreview->setPixmap( pixmap );
567 return QDialog::eventFilter( obj, event );
571 bool QgsCptCityColorRampDialog::updateRamp()
573 mListWidget->clear();
575 cboVariantName->clear();
577 lblCollectionInfo->clear();
579 buttonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
580 updatePreview(
true );
591 if ( modelIndex == QModelIndex() )
608 lblSchemeName->setText( QFileInfo( mRamp.
schemeName() ).fileName() );
609 QModelIndex parentIndex = modelIndex.parent();
610 QModelIndex selIndex = mTreeFilter->mapFromSource( parentIndex );
613 mTreeView->setCurrentIndex( selIndex );
614 mTreeView->setExpanded( selIndex,
true );
615 mTreeView->scrollTo( selIndex, QAbstractItemView::PositionAtCenter );
616 updateTreeView( mModel->
dataItem( parentIndex ), false );
619 for (
int i = 0; i < mListRamps.count(); i++ )
621 if ( mListRamps.at( i ) == childItem )
623 QgsDebugMsg( QStringLiteral(
"found matching item %1 target=%2" ).arg( mListRamps.at( i )->path(), childItem->
path() ) );
624 QListWidgetItem *listItem = mListWidget->item( i );
625 mListWidget->setCurrentItem( listItem );
628 mListWidget->scrollToItem( listItem, QAbstractItemView::EnsureVisible );
630 buttonBox->button( QDialogButtonBox::Ok )->setEnabled(
true );
639 void QgsCptCityColorRampDialog::showAll()
641 QModelIndex modelIndex = mModel->
findPath( QString() );
642 if ( modelIndex != QModelIndex() )
644 QModelIndex selIndex = mTreeFilter->mapFromSource( modelIndex );
645 mTreeView->setCurrentIndex( selIndex );
646 mTreeView->setExpanded( selIndex,
true );
647 mTreeView->scrollTo( selIndex, QAbstractItemView::PositionAtCenter );
648 updateTreeView( mModel->
dataItem( modelIndex ), false );
657 mTreeFilter =
new TreeFilterProxyModel(
this, mModel );
658 mTreeView->setModel( mTreeFilter );
662 void QgsCptCityColorRampDialog::refresh()
664 QApplication::setOverrideCursor( Qt::WaitCursor );
665 refreshModel( QModelIndex() );
666 QApplication::restoreOverrideCursor();
669 void QgsCptCityColorRampDialog::refreshModel(
const QModelIndex &index )
671 if ( index.isValid() )
686 for (
int i = 0; i < mModel->
rowCount( index ); i++ )
688 QModelIndex idx = mModel->
index( i, 0, index );
689 if ( mTreeView->isExpanded( idx ) || !mModel->
hasChildren( idx ) )
700 : QSortFilterProxyModel( parent )
703 setSourceModel( mModel );
706 bool TreeFilterProxyModel::filterAcceptsRow(
int sourceRow,
const QModelIndex &sourceParent )
const bool eventFilter(QObject *obj, QEvent *event) override
QgsStringMap copyingInfo() const
static QgsCptCityArchive * defaultArchive()
static QString qgisSettingsDirPath()
Returns the path to the settings directory in user's home dir.
This class is a composition of two QSettings instances:
QgsCptCityColorRampDialog(const QgsCptCityColorRamp &ramp, QWidget *parent=nullptr)
Constructor for QgsCptCityColorRampDialog.
void changed()
Emitted when the dialog settings change.
QString archiveName() const
QgsCptCityColorRamp ramp() const
Returns a color ramp representing the current settings from the dialog.
A generic dialog with layout and button box.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Item that represents a layer that can be opened with one of the providers.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
QgsCptCityDataItem * dataItem(const QModelIndex &idx) const
Returns a list of mime that can describe model indexes.
void setVariantName(const QString &variantName)
QString schemeName() const
QModelIndex findPath(const QString &path)
Returns index of a path.
void saveGeometry(QWidget *widget, const QString &keyName)
Save the wigget geometry into settings.
void copy(const QgsCptCityColorRamp *other)
void setName(const QString &schemeName, const QString &variantName=QString(), const QStringList &variantList=QStringList())
bool restoreGeometry(QWidget *widget, const QString &keyName)
Restore the wigget geometry from settings.
QString variantName() const
QString copyingFileName(const QString &dirName) const
int rowCount(const QModelIndex &parent=QModelIndex()) const override
const QgsCptCityColorRamp & ramp() const
bool hasChildren(const QModelIndex &parent=QModelIndex()) const override
static QIcon colorRampPreviewIcon(QgsColorRamp *ramp, QSize size, int padding=0)
Returns an icon preview for a color ramp.
Base class for all items in the model.
static QMap< QString, QString > copyingInfo(const QString &fileName)
A Collection: logical collection of subcollections and color ramps.
QString descFileName(const QString &dirName) const
QString shortInfo() const
static void initDefaultArchive()
static QPixmap colorRampPreviewPixmap(QgsColorRamp *ramp, QSize size, int padding=0)
Returns a pixmap preview for a color ramp.
virtual int leafCount() const
void refresh(const QString &path)
void setVariantList(const QStringList &variantList)
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
QVBoxLayout * layout()
Returns the central layout. Widgets added to it must have this dialog as parent.
bool saveAsGradientRamp() const
Returns true if the ramp should be converted to a QgsGradientColorRamp.
void setRamp(const QgsCptCityColorRamp &ramp)
Sets the color ramp to show in the dialog.
QVector< QgsCptCityDataItem * > childrenRamps(bool recursive)
QStringList variantList() const