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