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