QGIS API Documentation  3.2.0-Bonn (bc43194)
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( QString( "ramp name= %1 variant= %2 - %3 variants" ).arg( mRamp.schemeName(), mRamp.variantName() ).arg( mRamp.variantList().count() ) );
101 
102  // model / view
103  QgsDebugMsg( "loading model/view objects" );
104  if ( mAuthorsModel )
105  delete mAuthorsModel;
106  mAuthorsModel = new QgsCptCityBrowserModel( this, mArchive, QgsCptCityBrowserModel::Authors );
107  if ( mSelectionsModel )
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( "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( QString( "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  Q_FOREACH ( const QString &variant, variantList )
163  {
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 );
170 
171  ramp.setVariantName( variant );
172  if ( ramp.loadFile() )
173  cboVariantName->setItemIcon( index,
174  QgsSymbolLayerUtils::colorRampPreviewIcon( &ramp, cboVariantName->iconSize() ) );
175  else
176  cboVariantName->setItemIcon( index, blankIcon );
177  cboVariantName->setItemData( index, Qt::AlignHCenter, Qt::TextAlignmentRole );
178  }
179 
180  cboVariantName->blockSignals( false );
181 
182  // try to set the original variant again (if exists)
183  int idx = -1;
184  QString newVariant = mRamp.variantName();
185  QgsDebugMsg( QString( "variant= %1 - %2 variants" ).arg( mRamp.variantName() ).arg( mRamp.variantList().count() ) );
186  if ( newVariant != QString() )
187  {
188  if ( newVariant.startsWith( '-' ) || newVariant.startsWith( '_' ) )
189  newVariant.remove( 0, 1 );
190  newVariant = ' ' + newVariant;
191  idx = cboVariantName->findText( newVariant );
192  }
193  else
194  idx = cboVariantName->findText( oldVariant );
195 
196  // if not found use the item in the middle
197  if ( idx == -1 )
198  {
199  idx = cboVariantName->count() / 2;
200  }
201  cboVariantName->setCurrentIndex( idx );
202  // updatePreview();
203 
204  cboVariantName->setEnabled( true );
205  cboVariantName->setVisible( true );
206  }
207 
208 }
209 
210 void QgsCptCityColorRampDialog::mTreeView_clicked( const QModelIndex &index )
211 {
212  const QModelIndex &sourceIndex = mTreeFilter->mapToSource( index );
213  QgsCptCityDataItem *item = mModel->dataItem( sourceIndex );
214  if ( ! item )
215  return;
216  QgsDebugMsg( QString( "item %1 clicked" ).arg( item->name() ) );
217  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
218  updateTreeView( item );
219 }
220 
221 void QgsCptCityColorRampDialog::updateTreeView( QgsCptCityDataItem *item, bool resetRamp )
222 {
223  if ( ! item )
224  {
225  QgsDebugMsg( "invalid item" );
226  return;
227  }
228  if ( item->type() == QgsCptCityDataItem::Directory )
229  {
230  if ( resetRamp )
231  {
232  mRamp.setName( QLatin1String( "" ), QLatin1String( "" ) );
233  QgsDebugMsg( QString( "variant= %1 - %2 variants" ).arg( mRamp.variantName() ).arg( mRamp.variantList().count() ) );
234  lblSchemeName->clear();
235  populateVariants();
236  }
237  updateListWidget( item );
238  lblSchemePath->setText( item->path() );
239  lblCollectionInfo->setText( QStringLiteral( "%1 (%2)" ).arg( item->info() ).arg( item->leafCount() ) );
240  updateCopyingInfo( mArchive->copyingInfo( mArchive->copyingFileName( item->path() ) ) );
241  }
242  else if ( item->type() == QgsCptCityDataItem::Selection )
243  {
244  lblSchemePath->clear();
245  clearCopyingInfo();
246  updateListWidget( item );
247  lblCollectionInfo->setText( QStringLiteral( "%1 (%2)" ).arg( item->info() ).arg( item->leafCount() ) );
248  }
249  else if ( item->type() == QgsCptCityDataItem::AllRamps )
250  {
251  lblSchemePath->clear();
252  clearCopyingInfo();
253  updateListWidget( item );
254  lblCollectionInfo->setText( tr( "All Ramps (%1)" ).arg( item->leafCount() ) );
255  }
256  else
257  {
258  QgsDebugMsg( QString( "item %1 has invalid type %2" ).arg( item->path() ).arg( ( int )item->type() ) );
259  }
260 }
261 
262 void QgsCptCityColorRampDialog::mListWidget_itemClicked( QListWidgetItem *item )
263 {
264  QgsCptCityColorRampItem *rampItem = mListRamps.at( item->data( Qt::UserRole ).toInt() );
265  if ( rampItem )
266  {
267  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( true );
268  lblSchemeName->setText( QFileInfo( rampItem->name() ).fileName() );
269  mRamp.copy( &rampItem->ramp() );
270  QgsDebugMsg( QString( "variant= %1 - %2 variants" ).arg( mRamp.variantName() ).arg( mRamp.variantList().count() ) );
271  populateVariants();
272  }
273  else
274  {
275  QgsDebugMsg( "invalid item" );
276  }
277 }
278 
279 void QgsCptCityColorRampDialog::mListWidget_itemSelectionChanged()
280 {
281  if ( mListWidget->selectedItems().isEmpty() )
282  {
283  mRamp.setName( QLatin1String( "" ), QLatin1String( "" ) );
284  }
285 }
286 
287 void QgsCptCityColorRampDialog::tabBar_currentChanged( int index )
288 {
289  if ( index == 0 )
290  {
291  setTreeModel( mSelectionsModel );
292  mArchiveViewType = QgsCptCityBrowserModel::Selections;
293  }
294  else if ( index == 1 )
295  {
296  setTreeModel( mAuthorsModel );
297  mArchiveViewType = QgsCptCityBrowserModel::Authors;
298  }
299  else
300  {
301  QgsDebugMsg( QString( "invalid index %1" ).arg( index ) );
302  setTreeModel( mAuthorsModel );
303  mArchiveViewType = QgsCptCityBrowserModel::Authors;
304  }
305 
306  mListWidget->blockSignals( true );
307  updateRamp();
308  mListWidget->blockSignals( false );
309 }
310 
311 
312 void QgsCptCityColorRampDialog::pbtnLicenseDetails_pressed()
313 {
314  QString path, title, copyFile, descFile;
315 
316  // get basic information, depending on if is color ramp or directory
317  QgsCptCityDataItem *item = mModel->dataItem( mTreeFilter->mapToSource( mTreeView->currentIndex() ) );
318  if ( ! item )
319  return;
320 
321  path = item->path();
322  if ( item->type() == QgsCptCityDataItem::Directory )
323  {
324  title = tr( "%1 Directory Details" ).arg( item->path() );
325  }
326  else if ( item->type() == QgsCptCityColorRampItem::Directory )
327  {
328  title = tr( "%1 Gradient Details" ).arg( path );
329  }
330  else
331  {
332  return;
333  }
334  copyFile = mArchive->copyingFileName( path );
335  descFile = mArchive->descFileName( path );
336 
337  // prepare dialog
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 );
344 
345  // add contents of DESC.xml and COPYING.xml
346  QString copyText;
347  if ( ! copyFile.isNull() )
348  {
349  QFile file( copyFile );
350  if ( file.open( QIODevice::ReadOnly | QIODevice::Text ) )
351  {
352  copyText = QString( file.readAll() );
353  file.close();
354  }
355  }
356  QString descText;
357  if ( ! descFile.isNull() )
358  {
359  QFile file( descFile );
360  if ( file.open( QIODevice::ReadOnly | QIODevice::Text ) )
361  {
362  descText = QString( file.readAll() );
363  file.close();
364  }
365  }
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 );
377 
378  dlg.resize( 600, 600 );
379  dlg.exec();
380 }
381 
382 void QgsCptCityColorRampDialog::updatePreview( bool clear )
383 {
384  QSize size = lblPreview->size();
385 
386  if ( clear || mRamp.schemeName().isEmpty() )
387  {
388  lblSchemeName->clear();
389  lblSchemePath->clear();
390  lblLicensePreview->clear();
391  QPixmap blankPixmap( size );
392  blankPixmap.fill( Qt::transparent );
393  lblPreview->setPixmap( blankPixmap );
394  return;
395  }
396 
397  mRamp.loadFile();
398 
399  lblSchemePath->setText( mRamp.schemeName() + mRamp.variantName() );
400 
401  // update pixmap
402  // TODO draw checker-board/transparent background
403  // for transparent, add [ pixmap.fill( Qt::transparent ); ] to QgsSymbolLayerUtils::colorRampPreviewPixmap
404  QPixmap pixmap = QgsSymbolLayerUtils::colorRampPreviewPixmap( &mRamp, size );
405  lblPreview->setPixmap( pixmap );
406 
407  // add copyright information from COPYING.xml file
408  updateCopyingInfo( mRamp.copyingInfo() );
409 }
410 
411 void QgsCptCityColorRampDialog::clearCopyingInfo()
412 {
413  updateCopyingInfo( QMap< QString, QString >() );
414 }
415 
416 void QgsCptCityColorRampDialog::updateCopyingInfo( const QMap< QString, QString > &copyingMap )
417 {
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" ) ) + " ]";
429  else
430  licenseStr += '\n';
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" ) ) );
437  else
438  lblSrcLink->clear();
439 }
440 
441 void QgsCptCityColorRampDialog::cboVariantName_currentIndexChanged( int index )
442 {
443  Q_UNUSED( index );
444  if ( cboVariantName->currentIndex() != -1 )
445  mRamp.setVariantName( cboVariantName->currentData( Qt::UserRole ).toString() );
446  QgsDebugMsg( QString( "variant= %1 - %2 variants" ).arg( mRamp.variantName() ).arg( mRamp.variantList().count() ) );
447  updatePreview();
448  emit changed();
449 }
450 
451 void QgsCptCityColorRampDialog::onFinished()
452 {
453  // save settings
454  QgsSettings settings;
455  settings.setValue( QStringLiteral( "Windows/CptCityColorRampV2Dialog/geometry" ), saveGeometry() );
456  settings.setValue( QStringLiteral( "Windows/CptCityColorRampV2Dialog/splitter" ), mSplitter->saveState() );
457 }
458 
459 void QgsCptCityColorRampDialog::showHelp()
460 {
461  // show error message to use color ramp manager to get more gradients
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"
465  );
466  QMessageBox *msg = new QMessageBox( this );
467  msg->setWindowTitle( tr( "Download More Cpt-city Gradients" ) );
468  msg->setText( helpText );
469  msg->exec();
470 }
471 
472 void QgsCptCityColorRampDialog::updateUi()
473 {
474  // look for item, if not found in selections archive, look for in authors
475  QgsDebugMsg( "looking for ramp " + mRamp.schemeName() );
476  if ( !mRamp.schemeName().isEmpty() )
477  {
478  bool found = updateRamp();
479  if ( ! found )
480  {
481  tabBar->setCurrentIndex( 1 );
482  setTreeModel( mAuthorsModel );
483  found = updateRamp();
484  // if not found, go back to selections model
485  if ( ! found )
486  {
487  tabBar->setCurrentIndex( 0 );
488  setTreeModel( mSelectionsModel );
489  }
490  }
491  if ( found )
492  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( true );
493  }
494  else
495  {
496  updateRamp();
497  }
498 }
499 
501 {
502  QgsDebugMsg( QString( "result: %1 checked: %2" ).arg( result() ).arg( cboConvertStandard->isChecked() ) );
503  // if "save as standard gradient" is checked, convert to QgsVectorGradientColorRamp
504  return ( result() == Accepted && cboConvertStandard->isChecked() );
505 }
506 
507 void QgsCptCityColorRampDialog::updateListWidget( QgsCptCityDataItem *item )
508 {
509  mListWidget->blockSignals( true );
510  mListWidget->clear();
511  mListRamps.clear();
512  QgsCptCityCollectionItem *colItem = dynamic_cast<QgsCptCityCollectionItem *>( item );
513  if ( colItem )
514  {
515  QgsDebugMsg( "path= " + item->path() );
516  // recursively get children ramps
517  QVector<QgsCptCityDataItem *> childrenRamps = colItem->childrenRamps( true );
518  for ( int i = 0; i < childrenRamps.count(); i++ )
519  {
520  QgsCptCityColorRampItem *rampItem = dynamic_cast<QgsCptCityColorRampItem *>( childrenRamps[i] );
521  if ( ! rampItem )
522  {
523  QgsDebugMsg( "invalid item " + childrenRamps[i]->path() );
524  continue;
525  }
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;
533  }
534  }
535  else
536  {
537  QgsDebugMsg( "invalid item" );
538  }
539  mListWidget->blockSignals( false );
540 }
541 
542 // this function is for a svg preview, available if the svg files have been processed with svgx
543 // e.g. for f in `ls */*/*/*/*.svg`; do echo $f ; svgx -p -t svg $f > tmp1.svg; mv tmp1.svg $f; done
544 // perhaps a future version of the cpt-city svg gradients will have them by default
545 bool QgsCptCityColorRampDialog::eventFilter( QObject *obj, QEvent *event )
546 {
547  QSize size = lblPreview->size();
548 
549  if ( event->type() == QEvent::MouseButtonPress )
550  {
551  // create preview from svg file if supported - depends on file versions
552  QPixmap pixmap( mRamp.fileName() );
553  if ( ! pixmap.isNull() )
554  lblPreview->setPixmap( pixmap.scaled( size ) );
555  return true;
556  }
557  else if ( event->type() == QEvent::MouseButtonRelease )
558  {
559  // restore preview
560  QPixmap pixmap = QgsSymbolLayerUtils::colorRampPreviewPixmap( &mRamp, size );
561  lblPreview->setPixmap( pixmap );
562  return true;
563  }
564  else
565  {
566  // standard event processing
567  return QDialog::eventFilter( obj, event );
568  }
569 }
570 
571 bool QgsCptCityColorRampDialog::updateRamp()
572 {
573  mListWidget->clear();
574  mListRamps.clear();
575  cboVariantName->clear();
576  clearCopyingInfo();
577  lblCollectionInfo->clear();
578 
579  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
580  updatePreview( true );
581 
582  QgsDebugMsg( "schemeName= " + mRamp.schemeName() );
583  if ( mRamp.schemeName().isEmpty() )
584  {
585  showAll();
586  return false;
587  }
588 
589  // search for item in model
590  QModelIndex modelIndex = mModel->findPath( mRamp.schemeName() );
591  if ( modelIndex == QModelIndex() )
592  {
593  return false;
594  }
595  QgsCptCityColorRampItem *childItem =
596  dynamic_cast<QgsCptCityColorRampItem *>( mModel->dataItem( modelIndex ) );
597  if ( ! childItem )
598  return false;
599  if ( mRamp.schemeName() != childItem->ramp().schemeName() )
600  return false;
601 
602  // found child, set mRamp variantList
603  // mRamp.copy( &childItem->ramp() );
604  mRamp.setVariantList( childItem->ramp().variantList() );
605 
606  // found child, update tree
607  QgsDebugMsg( QString( "found item %1" ).arg( mRamp.schemeName() ) );
608  lblSchemeName->setText( QFileInfo( mRamp.schemeName() ).fileName() );
609  QModelIndex parentIndex = modelIndex.parent();
610  QModelIndex selIndex = mTreeFilter->mapFromSource( parentIndex );
611 
612  // QgsDebugMsg(QString("parent row=%1 path=%2 parentRow=%3").arg(parentIndex.row()).arg(mModel->dataItem( parentIndex )->path()).arg(parentIndex.parent().row()));
613  mTreeView->setCurrentIndex( selIndex );
614  mTreeView->setExpanded( selIndex, true );
615  mTreeView->scrollTo( selIndex, QAbstractItemView::PositionAtCenter );
616  updateTreeView( mModel->dataItem( parentIndex ), false );
617 
618  // update listWidget, find appropriate item in mListRamps
619  for ( int i = 0; i < mListRamps.count(); i++ )
620  {
621  if ( mListRamps.at( i ) == childItem )
622  {
623  QgsDebugMsg( QString( "found matching item %1 target=%2" ).arg( mListRamps.at( i )->path(), childItem->path() ) );
624  QListWidgetItem *listItem = mListWidget->item( i );
625  mListWidget->setCurrentItem( listItem );
626  // mListWidget_itemClicked( listItem );
627  populateVariants();
628  mListWidget->scrollToItem( listItem, QAbstractItemView::EnsureVisible );
629  // mListView->selectionModel()->select( childIndex, QItemSelectionModel::Select );
630  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( true );
631  emit changed();
632  return true;
633  }
634  }
635 
636  return false;
637 }
638 
639 void QgsCptCityColorRampDialog::showAll()
640 {
641  QModelIndex modelIndex = mModel->findPath( QLatin1String( "" ) );
642  if ( modelIndex != QModelIndex() )
643  {
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 );
649  }
650 }
651 
652 void QgsCptCityColorRampDialog::setTreeModel( QgsCptCityBrowserModel *model )
653 {
654  mModel = model;
655 
656  if ( mTreeFilter )
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( "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.