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