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