QGIS API Documentation  3.8.0-Zanzibar (11aff65)
qgscptcitycolorrampdialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscptcitycolorrampdialog.cpp
3  ---------------------
4  begin : July 2012
5  copyright : (C) 2012 by Etienne Tourigny
6  email : etourigny dot dev at gmail.com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
17 
18 #include "qgscptcityarchive.h"
19 #include "qgscolorramp.h"
20 #include "qgslogger.h"
21 #include "qgsapplication.h"
22 #include "qgsdialog.h"
23 #include "qgssymbollayerutils.h"
24 #include "qgssettings.h"
25 
26 #include <QPushButton>
27 #include <QTextEdit>
28 #include <QTime>
29 #include <QMessageBox>
30 #include <QSortFilterProxyModel>
31 
33 
34 // TODO
35 // - fix Diverging children when first show Selections
36 // - fix crash on Diverging?
37 
38 
40  : QDialog( parent )
41  , mRamp( ramp )
42  , mArchiveViewType( QgsCptCityBrowserModel::Selections )
43 {
44  setupUi( this );
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 );
52 
53  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
54 
55  QgsSettings settings;
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() );
59 
60  mModel = mAuthorsModel = mSelectionsModel = nullptr; //mListModel = 0;
61  mTreeFilter = nullptr;
62 
65 
66  // show information on how to install cpt-city files if none are found
67  if ( ! mArchive || mArchive->isEmpty() )
68  {
69  // QgsDialog dlg( this );
70  // dlg.setWindowTitle( tr( "Cpt-city Gradient Files Not Found" ) );
71  QTextEdit *edit = new QTextEdit( nullptr );
72  edit->setReadOnly( true );
73  // not sure if we want this long string to be translated
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 );
90  // dlg.layout()->addWidget( edit );
91  // dlg.resize(500,400);
92  // dlg.exec();
93  return;
94  }
95 
96  if ( ! mArchive )
97  return;
98  QgsDebugMsg( "archive: " + mArchive->archiveName() );
99 
100  QgsDebugMsg( QStringLiteral( "ramp name= %1 variant= %2 - %3 variants" ).arg( mRamp.schemeName(), mRamp.variantName() ).arg( mRamp.variantList().count() ) );
101 
102  // model / view
103  QgsDebugMsg( QStringLiteral( "loading model/view objects" ) );
104 
105  delete mAuthorsModel;
106  mAuthorsModel = new QgsCptCityBrowserModel( this, mArchive, QgsCptCityBrowserModel::Authors );
107 
108  delete mSelectionsModel;
109  mSelectionsModel = new QgsCptCityBrowserModel( this, mArchive, QgsCptCityBrowserModel::Selections );
110  setTreeModel( mSelectionsModel );
111 
112  mTreeView->setSelectionMode( QAbstractItemView::SingleSelection );
113  mTreeView->setColumnHidden( 1, true );
114  QgsDebugMsg( QStringLiteral( "done loading model/view objects" ) );
115 
116  // setup ui
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 ); // mouse click on preview label shows svg render
122 
123  updateUi();
124 
125  tabBar->blockSignals( false );
126 
127  connect( this, &QDialog::finished, this, &QgsCptCityColorRampDialog::onFinished );
128 
129 }
130 
132 {
133  mRamp = ramp;
134  updateUi();
135 }
136 
137 void QgsCptCityColorRampDialog::populateVariants()
138 {
139  QStringList variantList = mRamp.variantList();
140 
141  QgsDebugMsg( QStringLiteral( "ramp %1%2 has %3 variants" ).arg( mRamp.schemeName(), mRamp.variantName() ).arg( variantList.count() ) );
142 
143  cboVariantName->blockSignals( true );
144  cboVariantName->clear();
145 
146  if ( variantList.isEmpty() )
147  {
148  cboVariantName->setEnabled( false );
149  cboVariantName->setVisible( false );
150  cboVariantName_currentIndexChanged( -1 );
151  }
152  else
153  {
154  // populate variant combobox
155  QString oldVariant = cboVariantName->currentText();
156  QgsCptCityColorRamp ramp( mRamp.schemeName(), mRamp.variantList(), QString() );
157  QPixmap blankPixmap( cboVariantName->iconSize() );
158  blankPixmap.fill( Qt::white );
159  QIcon blankIcon( blankPixmap );
160  int index;
161 
162  const auto constVariantList = variantList;
163  for ( const QString &variant : constVariantList )
164  {
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 );
171 
172  ramp.setVariantName( variant );
173  if ( ramp.loadFile() )
174  cboVariantName->setItemIcon( index,
175  QgsSymbolLayerUtils::colorRampPreviewIcon( &ramp, cboVariantName->iconSize() ) );
176  else
177  cboVariantName->setItemIcon( index, blankIcon );
178  cboVariantName->setItemData( index, Qt::AlignHCenter, Qt::TextAlignmentRole );
179  }
180 
181  cboVariantName->blockSignals( false );
182 
183  // try to set the original variant again (if exists)
184  int idx = -1;
185  QString newVariant = mRamp.variantName();
186  QgsDebugMsg( QStringLiteral( "variant= %1 - %2 variants" ).arg( mRamp.variantName() ).arg( mRamp.variantList().count() ) );
187  if ( newVariant != QString() )
188  {
189  if ( newVariant.startsWith( '-' ) || newVariant.startsWith( '_' ) )
190  newVariant.remove( 0, 1 );
191  newVariant = ' ' + newVariant;
192  idx = cboVariantName->findText( newVariant );
193  }
194  else
195  idx = cboVariantName->findText( oldVariant );
196 
197  // if not found use the item in the middle
198  if ( idx == -1 )
199  {
200  idx = cboVariantName->count() / 2;
201  }
202  cboVariantName->setCurrentIndex( idx );
203  // updatePreview();
204 
205  cboVariantName->setEnabled( true );
206  cboVariantName->setVisible( true );
207  }
208 
209 }
210 
211 void QgsCptCityColorRampDialog::mTreeView_clicked( const QModelIndex &index )
212 {
213  const QModelIndex &sourceIndex = mTreeFilter->mapToSource( index );
214  QgsCptCityDataItem *item = mModel->dataItem( sourceIndex );
215  if ( ! item )
216  return;
217  QgsDebugMsg( QStringLiteral( "item %1 clicked" ).arg( item->name() ) );
218  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
219  updateTreeView( item );
220 }
221 
222 void QgsCptCityColorRampDialog::updateTreeView( QgsCptCityDataItem *item, bool resetRamp )
223 {
224  if ( ! item )
225  {
226  QgsDebugMsg( QStringLiteral( "invalid item" ) );
227  return;
228  }
229  if ( item->type() == QgsCptCityDataItem::Directory )
230  {
231  if ( resetRamp )
232  {
233  mRamp.setName( QString(), QString() );
234  QgsDebugMsg( QStringLiteral( "variant= %1 - %2 variants" ).arg( mRamp.variantName() ).arg( mRamp.variantList().count() ) );
235  lblSchemeName->clear();
236  populateVariants();
237  }
238  updateListWidget( item );
239  lblSchemePath->setText( item->path() );
240  lblCollectionInfo->setText( QStringLiteral( "%1 (%2)" ).arg( item->info() ).arg( item->leafCount() ) );
241  updateCopyingInfo( mArchive->copyingInfo( mArchive->copyingFileName( item->path() ) ) );
242  }
243  else if ( item->type() == QgsCptCityDataItem::Selection )
244  {
245  lblSchemePath->clear();
246  clearCopyingInfo();
247  updateListWidget( item );
248  lblCollectionInfo->setText( QStringLiteral( "%1 (%2)" ).arg( item->info() ).arg( item->leafCount() ) );
249  }
250  else if ( item->type() == QgsCptCityDataItem::AllRamps )
251  {
252  lblSchemePath->clear();
253  clearCopyingInfo();
254  updateListWidget( item );
255  lblCollectionInfo->setText( tr( "All Ramps (%1)" ).arg( item->leafCount() ) );
256  }
257  else
258  {
259  QgsDebugMsg( QStringLiteral( "item %1 has invalid type %2" ).arg( item->path() ).arg( static_cast<int>( item->type() ) ) );
260  }
261 }
262 
263 void QgsCptCityColorRampDialog::mListWidget_itemClicked( QListWidgetItem *item )
264 {
265  QgsCptCityColorRampItem *rampItem = mListRamps.at( item->data( Qt::UserRole ).toInt() );
266  if ( rampItem )
267  {
268  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( true );
269  lblSchemeName->setText( QFileInfo( rampItem->name() ).fileName() );
270  mRamp.copy( &rampItem->ramp() );
271  QgsDebugMsg( QStringLiteral( "variant= %1 - %2 variants" ).arg( mRamp.variantName() ).arg( mRamp.variantList().count() ) );
272  populateVariants();
273  }
274  else
275  {
276  QgsDebugMsg( QStringLiteral( "invalid item" ) );
277  }
278 }
279 
280 void QgsCptCityColorRampDialog::mListWidget_itemSelectionChanged()
281 {
282  if ( mListWidget->selectedItems().isEmpty() )
283  {
284  mRamp.setName( QString(), QString() );
285  }
286 }
287 
288 void QgsCptCityColorRampDialog::tabBar_currentChanged( int index )
289 {
290  if ( index == 0 )
291  {
292  setTreeModel( mSelectionsModel );
293  mArchiveViewType = QgsCptCityBrowserModel::Selections;
294  }
295  else if ( index == 1 )
296  {
297  setTreeModel( mAuthorsModel );
298  mArchiveViewType = QgsCptCityBrowserModel::Authors;
299  }
300  else
301  {
302  QgsDebugMsg( QStringLiteral( "invalid index %1" ).arg( index ) );
303  setTreeModel( mAuthorsModel );
304  mArchiveViewType = QgsCptCityBrowserModel::Authors;
305  }
306 
307  mListWidget->blockSignals( true );
308  updateRamp();
309  mListWidget->blockSignals( false );
310 }
311 
312 
313 void QgsCptCityColorRampDialog::pbtnLicenseDetails_pressed()
314 {
315  QString path, title, copyFile, descFile;
316 
317  // get basic information, depending on if is color ramp or directory
318  QgsCptCityDataItem *item = mModel->dataItem( mTreeFilter->mapToSource( mTreeView->currentIndex() ) );
319  if ( ! item )
320  return;
321 
322  path = item->path();
323  if ( item->type() == QgsCptCityDataItem::Directory )
324  {
325  title = tr( "%1 Directory Details" ).arg( item->path() );
326  }
327  else if ( item->type() == QgsCptCityColorRampItem::Directory )
328  {
329  title = tr( "%1 Gradient Details" ).arg( path );
330  }
331  else
332  {
333  return;
334  }
335  copyFile = mArchive->copyingFileName( path );
336  descFile = mArchive->descFileName( path );
337 
338  // prepare dialog
339  QgsDialog dlg( this, nullptr, QDialogButtonBox::Close );
340  QVBoxLayout *layout = dlg.layout();
341  dlg.setWindowTitle( title );
342  QTextEdit *textEdit = new QTextEdit( &dlg );
343  textEdit->setReadOnly( true );
344  layout->addWidget( textEdit );
345 
346  // add contents of DESC.xml and COPYING.xml
347  QString copyText;
348  if ( ! copyFile.isNull() )
349  {
350  QFile file( copyFile );
351  if ( file.open( QIODevice::ReadOnly | QIODevice::Text ) )
352  {
353  copyText = QString( file.readAll() );
354  file.close();
355  }
356  }
357  QString descText;
358  if ( ! descFile.isNull() )
359  {
360  QFile file( descFile );
361  if ( file.open( QIODevice::ReadOnly | QIODevice::Text ) )
362  {
363  descText = QString( file.readAll() );
364  file.close();
365  }
366  }
367  textEdit->insertPlainText( title + "\n\n" );
368  textEdit->insertPlainText( QStringLiteral( "===================" ) );
369  textEdit->insertPlainText( QStringLiteral( " copying " ) );
370  textEdit->insertPlainText( QStringLiteral( "===================\n" ) );
371  textEdit->insertPlainText( copyText );
372  textEdit->insertPlainText( QStringLiteral( "\n" ) );
373  textEdit->insertPlainText( QStringLiteral( "==================" ) );
374  textEdit->insertPlainText( QStringLiteral( " description " ) );
375  textEdit->insertPlainText( QStringLiteral( "==================\n" ) );
376  textEdit->insertPlainText( descText );
377  textEdit->moveCursor( QTextCursor::Start );
378 
379  dlg.resize( 600, 600 );
380  dlg.exec();
381 }
382 
383 void QgsCptCityColorRampDialog::updatePreview( bool clear )
384 {
385  QSize size = lblPreview->size();
386 
387  if ( clear || mRamp.schemeName().isEmpty() )
388  {
389  lblSchemeName->clear();
390  lblSchemePath->clear();
391  lblLicensePreview->clear();
392  QPixmap blankPixmap( size );
393  blankPixmap.fill( Qt::transparent );
394  lblPreview->setPixmap( blankPixmap );
395  return;
396  }
397 
398  mRamp.loadFile();
399 
400  lblSchemePath->setText( mRamp.schemeName() + mRamp.variantName() );
401 
402  // update pixmap
403  // TODO draw checker-board/transparent background
404  // for transparent, add [ pixmap.fill( Qt::transparent ); ] to QgsSymbolLayerUtils::colorRampPreviewPixmap
405  QPixmap pixmap = QgsSymbolLayerUtils::colorRampPreviewPixmap( &mRamp, size );
406  lblPreview->setPixmap( pixmap );
407 
408  // add copyright information from COPYING.xml file
409  updateCopyingInfo( mRamp.copyingInfo() );
410 }
411 
412 void QgsCptCityColorRampDialog::clearCopyingInfo()
413 {
414  updateCopyingInfo( QMap< QString, QString >() );
415 }
416 
417 void QgsCptCityColorRampDialog::updateCopyingInfo( const QMap< QString, QString > &copyingMap )
418 {
419  QString authorStr = copyingMap.value( QStringLiteral( "authors" ) );
420  if ( authorStr.length() > 80 )
421  authorStr.replace( authorStr.indexOf( ' ', 80 ), 1, QStringLiteral( "\n" ) );
422  lblAuthorName->setText( authorStr );
423  QString licenseStr = copyingMap.value( QStringLiteral( "license/informal" ) );
424  if ( copyingMap.contains( QStringLiteral( "license/year" ) ) )
425  licenseStr += " (" + copyingMap.value( QStringLiteral( "license/year" ) ) + ')';
426  if ( licenseStr.length() > 80 )
427  licenseStr.replace( licenseStr.indexOf( ' ', 80 ), 1, QStringLiteral( "\n" ) );
428  if ( copyingMap.contains( QStringLiteral( "license/url" ) ) )
429  licenseStr += "\n[ " + copyingMap.value( QStringLiteral( "license/url" ) ) + " ]";
430  else
431  licenseStr += '\n';
432  lblLicenseName->setText( licenseStr );
433  licenseStr.replace( '\n', QLatin1String( " " ) );
434  lblLicensePreview->setText( licenseStr );
435  lblLicensePreview->setCursorPosition( 0 );
436  if ( copyingMap.contains( QStringLiteral( "src/link" ) ) )
437  lblSrcLink->setText( copyingMap.value( QStringLiteral( "src/link" ) ) );
438  else
439  lblSrcLink->clear();
440 }
441 
442 void QgsCptCityColorRampDialog::cboVariantName_currentIndexChanged( int index )
443 {
444  Q_UNUSED( index )
445  if ( cboVariantName->currentIndex() != -1 )
446  mRamp.setVariantName( cboVariantName->currentData( Qt::UserRole ).toString() );
447  QgsDebugMsg( QStringLiteral( "variant= %1 - %2 variants" ).arg( mRamp.variantName() ).arg( mRamp.variantList().count() ) );
448  updatePreview();
449  emit changed();
450 }
451 
452 void QgsCptCityColorRampDialog::onFinished()
453 {
454  // save settings
455  QgsSettings settings;
456  settings.setValue( QStringLiteral( "Windows/CptCityColorRampV2Dialog/geometry" ), saveGeometry() );
457  settings.setValue( QStringLiteral( "Windows/CptCityColorRampV2Dialog/splitter" ), mSplitter->saveState() );
458 }
459 
460 void QgsCptCityColorRampDialog::showHelp()
461 {
462  // show error message to use color ramp manager to get more gradients
463  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"
466  );
467  QMessageBox *msg = new QMessageBox( this );
468  msg->setWindowTitle( tr( "Download More Cpt-city Gradients" ) );
469  msg->setText( helpText );
470  msg->exec();
471 }
472 
473 void QgsCptCityColorRampDialog::updateUi()
474 {
475  // look for item, if not found in selections archive, look for in authors
476  QgsDebugMsg( "looking for ramp " + mRamp.schemeName() );
477  if ( !mRamp.schemeName().isEmpty() )
478  {
479  bool found = updateRamp();
480  if ( ! found )
481  {
482  tabBar->setCurrentIndex( 1 );
483  setTreeModel( mAuthorsModel );
484  found = updateRamp();
485  // if not found, go back to selections model
486  if ( ! found )
487  {
488  tabBar->setCurrentIndex( 0 );
489  setTreeModel( mSelectionsModel );
490  }
491  }
492  if ( found )
493  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( true );
494  }
495  else
496  {
497  updateRamp();
498  }
499 }
500 
502 {
503  QgsDebugMsg( QStringLiteral( "result: %1 checked: %2" ).arg( result() ).arg( cboConvertStandard->isChecked() ) );
504  // if "save as standard gradient" is checked, convert to QgsVectorGradientColorRamp
505  return ( result() == Accepted && cboConvertStandard->isChecked() );
506 }
507 
508 void QgsCptCityColorRampDialog::updateListWidget( QgsCptCityDataItem *item )
509 {
510  mListWidget->blockSignals( true );
511  mListWidget->clear();
512  mListRamps.clear();
513  QgsCptCityCollectionItem *colItem = qobject_cast<QgsCptCityCollectionItem *>( item );
514  if ( colItem )
515  {
516  QgsDebugMsg( "path= " + item->path() );
517  // recursively get children ramps
518  QVector<QgsCptCityDataItem *> childrenRamps = colItem->childrenRamps( true );
519  for ( int i = 0; i < childrenRamps.count(); i++ )
520  {
521  QgsCptCityColorRampItem *rampItem = qobject_cast<QgsCptCityColorRampItem *>( childrenRamps[i] );
522  if ( ! rampItem )
523  {
524  QgsDebugMsg( "invalid item " + childrenRamps[i]->path() );
525  continue;
526  }
527  QListWidgetItem *listItem = new QListWidgetItem();
528  listItem->setText( rampItem->shortInfo() );
529  listItem->setIcon( rampItem->icon( QSize( 75, 50 ) ) );
530  listItem->setToolTip( rampItem->path() + '\n' + rampItem->info() );
531  listItem->setData( Qt::UserRole, QVariant( i ) );
532  mListWidget->addItem( listItem );
533  mListRamps << rampItem;
534  }
535  }
536  else
537  {
538  QgsDebugMsg( QStringLiteral( "invalid item" ) );
539  }
540  mListWidget->blockSignals( false );
541 }
542 
543 // this function is for a svg preview, available if the svg files have been processed with svgx
544 // e.g. for f in `ls */*/*/*/*.svg`; do echo $f ; svgx -p -t svg $f > tmp1.svg; mv tmp1.svg $f; done
545 // perhaps a future version of the cpt-city svg gradients will have them by default
546 bool QgsCptCityColorRampDialog::eventFilter( QObject *obj, QEvent *event )
547 {
548  QSize size = lblPreview->size();
549 
550  if ( event->type() == QEvent::MouseButtonPress )
551  {
552  // create preview from svg file if supported - depends on file versions
553  QPixmap pixmap( mRamp.fileName() );
554  if ( ! pixmap.isNull() )
555  lblPreview->setPixmap( pixmap.scaled( size ) );
556  return true;
557  }
558  else if ( event->type() == QEvent::MouseButtonRelease )
559  {
560  // restore preview
561  QPixmap pixmap = QgsSymbolLayerUtils::colorRampPreviewPixmap( &mRamp, size );
562  lblPreview->setPixmap( pixmap );
563  return true;
564  }
565  else
566  {
567  // standard event processing
568  return QDialog::eventFilter( obj, event );
569  }
570 }
571 
572 bool QgsCptCityColorRampDialog::updateRamp()
573 {
574  mListWidget->clear();
575  mListRamps.clear();
576  cboVariantName->clear();
577  clearCopyingInfo();
578  lblCollectionInfo->clear();
579 
580  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
581  updatePreview( true );
582 
583  QgsDebugMsg( "schemeName= " + mRamp.schemeName() );
584  if ( mRamp.schemeName().isEmpty() )
585  {
586  showAll();
587  return false;
588  }
589 
590  // search for item in model
591  QModelIndex modelIndex = mModel->findPath( mRamp.schemeName() );
592  if ( modelIndex == QModelIndex() )
593  {
594  return false;
595  }
596  QgsCptCityColorRampItem *childItem =
597  qobject_cast<QgsCptCityColorRampItem *>( mModel->dataItem( modelIndex ) );
598  if ( ! childItem )
599  return false;
600  if ( mRamp.schemeName() != childItem->ramp().schemeName() )
601  return false;
602 
603  // found child, set mRamp variantList
604  // mRamp.copy( &childItem->ramp() );
605  mRamp.setVariantList( childItem->ramp().variantList() );
606 
607  // found child, update tree
608  QgsDebugMsg( QStringLiteral( "found item %1" ).arg( mRamp.schemeName() ) );
609  lblSchemeName->setText( QFileInfo( mRamp.schemeName() ).fileName() );
610  QModelIndex parentIndex = modelIndex.parent();
611  QModelIndex selIndex = mTreeFilter->mapFromSource( parentIndex );
612 
613  // QgsDebugMsg(QString("parent row=%1 path=%2 parentRow=%3").arg(parentIndex.row()).arg(mModel->dataItem( parentIndex )->path()).arg(parentIndex.parent().row()));
614  mTreeView->setCurrentIndex( selIndex );
615  mTreeView->setExpanded( selIndex, true );
616  mTreeView->scrollTo( selIndex, QAbstractItemView::PositionAtCenter );
617  updateTreeView( mModel->dataItem( parentIndex ), false );
618 
619  // update listWidget, find appropriate item in mListRamps
620  for ( int i = 0; i < mListRamps.count(); i++ )
621  {
622  if ( mListRamps.at( i ) == childItem )
623  {
624  QgsDebugMsg( QStringLiteral( "found matching item %1 target=%2" ).arg( mListRamps.at( i )->path(), childItem->path() ) );
625  QListWidgetItem *listItem = mListWidget->item( i );
626  mListWidget->setCurrentItem( listItem );
627  // mListWidget_itemClicked( listItem );
628  populateVariants();
629  mListWidget->scrollToItem( listItem, QAbstractItemView::EnsureVisible );
630  // mListView->selectionModel()->select( childIndex, QItemSelectionModel::Select );
631  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( true );
632  emit changed();
633  return true;
634  }
635  }
636 
637  return false;
638 }
639 
640 void QgsCptCityColorRampDialog::showAll()
641 {
642  QModelIndex modelIndex = mModel->findPath( QString() );
643  if ( modelIndex != QModelIndex() )
644  {
645  QModelIndex selIndex = mTreeFilter->mapFromSource( modelIndex );
646  mTreeView->setCurrentIndex( selIndex );
647  mTreeView->setExpanded( selIndex, true );
648  mTreeView->scrollTo( selIndex, QAbstractItemView::PositionAtCenter );
649  updateTreeView( mModel->dataItem( modelIndex ), false );
650  }
651 }
652 
653 void QgsCptCityColorRampDialog::setTreeModel( QgsCptCityBrowserModel *model )
654 {
655  mModel = model;
656 
657  delete mTreeFilter;
658  mTreeFilter = new TreeFilterProxyModel( this, mModel );
659  mTreeView->setModel( mTreeFilter );
660 }
661 
662 #if 0
663 void QgsCptCityColorRampDialog::refresh()
664 {
665  QApplication::setOverrideCursor( Qt::WaitCursor );
666  refreshModel( QModelIndex() );
667  QApplication::restoreOverrideCursor();
668 }
669 
670 void QgsCptCityColorRampDialog::refreshModel( const QModelIndex &index )
671 {
672  if ( index.isValid() )
673  {
674  QgsCptCityDataItem *item = mModel->dataItem( index );
675  if ( item )
676  {
677  QgsDebugMsg( "path = " + item->path() );
678  }
679  else
680  {
681  QgsDebugMsg( QStringLiteral( "invalid item" ) );
682  }
683  }
684 
685  mModel->refresh( index );
686 
687  for ( int i = 0; i < mModel->rowCount( index ); i++ )
688  {
689  QModelIndex idx = mModel->index( i, 0, index );
690  if ( mTreeView->isExpanded( idx ) || !mModel->hasChildren( idx ) )
691  {
692  refreshModel( idx );
693  }
694  }
695 }
696 #endif
697 
699 
700 TreeFilterProxyModel::TreeFilterProxyModel( QObject *parent, QgsCptCityBrowserModel *model )
701  : QSortFilterProxyModel( parent )
702  , mModel( model )
703 {
704  setSourceModel( mModel );
705 }
706 
707 bool TreeFilterProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const
708 {
709  QgsCptCityDataItem *item = mModel->dataItem( mModel->index( sourceRow, 0, sourceParent ) );
710  return ( item && !( item->type() == QgsCptCityDataItem::ColorRamp ) );
711 }
712 
713 
bool eventFilter(QObject *obj, QEvent *event) override
QString descFileName(const QString &dirName) const
QString fileName() const
QgsCptCityColorRamp ramp() const
Returns a color ramp representing the current settings from the dialog.
QgsCptCityDataItem * dataItem(const QModelIndex &idx) const
Returns a list of mime that can describe model indexes.
static QgsCptCityArchive * defaultArchive()
static QString qgisSettingsDirPath()
Returns the path to the settings directory in user&#39;s home dir.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:58
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QgsCptCityColorRampDialog(const QgsCptCityColorRamp &ramp, QWidget *parent=nullptr)
Constructor for QgsCptCityColorRampDialog.
QString name() const
void changed()
Emitted when the dialog settings change.
QString schemeName() const
Definition: qgscolorramp.h:666
A generic dialog with layout and button box.
Definition: qgsdialog.h:33
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.
void setVariantName(const QString &variantName)
Definition: qgscolorramp.h:672
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())
Definition: qgscolorramp.h:674
bool restoreGeometry(QWidget *widget, const QString &keyName)
Restore the wigget geometry from settings.
const QgsCptCityColorRamp & ramp() const
int rowCount(const QModelIndex &parent=QModelIndex()) const override
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.
QString path() const
static QMap< QString, QString > copyingInfo(const QString &fileName)
A Collection: logical collection of subcollections and color ramps.
QString shortInfo() const
static void initDefaultArchive()
QString archiveName() const
static QPixmap colorRampPreviewPixmap(QgsColorRamp *ramp, QSize size, int padding=0)
Returns a pixmap preview for a color ramp.
QgsStringMap copyingInfo() const
void refresh(const QString &path)
void setVariantList(const QStringList &variantList)
Definition: qgscolorramp.h:673
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.
Definition: qgsdialog.h:46
QString info() const
void setRamp(const QgsCptCityColorRamp &ramp)
Sets the color ramp to show in the dialog.
QStringList variantList() const
Definition: qgscolorramp.h:668
QVector< QgsCptCityDataItem * > childrenRamps(bool recursive)
virtual int leafCount() const
QString variantName() const
Definition: qgscolorramp.h:667
QString copyingFileName(const QString &dirName) const
bool saveAsGradientRamp() const
Returns true if the ramp should be converted to a QgsGradientColorRamp.