QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgsmeshdatasetgrouptreeview.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmeshdatasetgrouptreeview.cpp
3 -------------------------------
4 begin : June 2018
5 copyright : (C) 2018 by Peter Petrik
6 email : zilolv at gmail dot 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_qgsmeshdatasetgrouptreeview.cpp"
18
19#include "qgis.h"
20#include "qgsapplication.h"
21#include "qgsmeshlayer.h"
22#include "qgsprovidermetadata.h"
23#include "qgsproviderregistry.h"
24#include "qgssettings.h"
25
26#include <QList>
27#include <QItemSelectionModel>
28#include <QMouseEvent>
29#include <QMenu>
30#include <QFileDialog>
31#include <QMessageBox>
32#include <QHeaderView>
33
34
36 : QAbstractItemModel( parent )
37 , mRootItem( new QgsMeshDatasetGroupTreeItem() )
38{
39}
40
41int QgsMeshDatasetGroupTreeModel::columnCount( const QModelIndex &parent ) const
42{
43 Q_UNUSED( parent )
44 return 1;
45}
46
47QVariant QgsMeshDatasetGroupTreeModel::data( const QModelIndex &index, int role ) const
48{
49 if ( !index.isValid() )
50 return QVariant();
51
52 QgsMeshDatasetGroupTreeItem *item = static_cast<QgsMeshDatasetGroupTreeItem *>( index.internalPointer() );
53 if ( !item )
54 return QVariant();
55
56 switch ( role )
57 {
58 case Qt::DisplayRole:
59 case Name:
60 return item->name();
61 break;
62 case Qt::CheckStateRole:
63 if ( index.column() == 0 )
64 return static_cast<int>( item->isEnabled() ? Qt::Checked : Qt::Unchecked );
65 break;
67 return item->datasetGroupIndex();
68 break;
69 }
70
71 return QVariant();
72}
73
74Qt::ItemFlags QgsMeshDatasetGroupTreeModel::flags( const QModelIndex &index ) const
75{
76 if ( !index.isValid() )
77 return Qt::NoItemFlags;
78
79 return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEditable;
80}
81
82QVariant QgsMeshDatasetGroupTreeModel::headerData( int section, Qt::Orientation orientation, int role ) const
83{
84 Q_UNUSED( section )
85
86 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
87 return tr( "Groups" );
88
89 return QVariant();
90}
91
92QModelIndex QgsMeshDatasetGroupTreeModel::index( int row, int column, const QModelIndex &parent )
93 const
94{
95 if ( !hasIndex( row, column, parent ) )
96 return QModelIndex();
97
99
100 if ( !parent.isValid() )
101 parentItem = mRootItem.get();
102 else
103 parentItem = static_cast<QgsMeshDatasetGroupTreeItem *>( parent.internalPointer() );
104
105 QgsMeshDatasetGroupTreeItem *childItem = parentItem->child( row );
106 if ( childItem )
107 return createIndex( row, column, childItem );
108 else
109 return QModelIndex();
110}
111
112QModelIndex QgsMeshDatasetGroupTreeModel::parent( const QModelIndex &index ) const
113{
114 if ( !index.isValid() )
115 return QModelIndex();
116
117 QgsMeshDatasetGroupTreeItem *childItem = static_cast<QgsMeshDatasetGroupTreeItem *>( index.internalPointer() );
118 QgsMeshDatasetGroupTreeItem *parentItem = childItem->parentItem();
119
120 if ( parentItem == mRootItem.get() )
121 return QModelIndex();
122
123 return createIndex( parentItem->row(), 0, parentItem );
124}
125
126int QgsMeshDatasetGroupTreeModel::rowCount( const QModelIndex &parent ) const
127{
128 QgsMeshDatasetGroupTreeItem *parentItem;
129 if ( parent.column() > 0 )
130 return 0;
131
132 if ( !parent.isValid() )
133 parentItem = mRootItem.get();
134 else
135 parentItem = static_cast<QgsMeshDatasetGroupTreeItem *>( parent.internalPointer() );
136
137 if ( parentItem )
138 return parentItem->childCount();
139 else
140 return 0;
141}
142
144{
145 beginResetModel();
146 if ( layer && layer->datasetGroupTreeRootItem() )
147 mRootItem.reset( layer->datasetGroupTreeRootItem()->clone() );
148 else
149 mRootItem.reset();
150 endResetModel();
151}
152
157
159{
160 if ( mRootItem )
161 return mRootItem->childFromDatasetGroupIndex( groupIndex );
162 else
163 return nullptr;
164}
165
167{
168 if ( !index.isValid() || !hasIndex( index.row(), index.column(), index.parent() ) )
169 return nullptr;
170
171 return static_cast<QgsMeshDatasetGroupTreeItem *>( index.internalPointer() );
172}
173
174bool QgsMeshDatasetGroupTreeModel::isEnabled( const QModelIndex &index ) const
175{
176 if ( !index.isValid() )
177 return false;
178
179 const QVariant checked = data( index, Qt::CheckStateRole );
180
181 return checked != QVariant() && checked.toInt() == Qt::Checked;
182}
183
185{
186 if ( !meshLayer && !mRootItem )
187 return;
188
189 beginResetModel();
190 meshLayer->resetDatasetGroupTreeItem();
191 mRootItem.reset( meshLayer->datasetGroupTreeRootItem()->clone() );
192 endResetModel();
193}
194
196{
197 if ( !mRootItem )
198 return;
199
200 for ( int i = 0; i < mRootItem->childCount(); ++i )
201 {
202 QgsMeshDatasetGroupTreeItem *item = mRootItem->child( i );
203 item->setIsEnabled( isEnabled );
204 for ( int j = 0; j < item->childCount(); ++j )
205 {
206 QgsMeshDatasetGroupTreeItem *child = item->child( j );
207 child->setIsEnabled( isEnabled );
208 }
209 }
210 dataChanged( index( 0, 0 ), index( mRootItem->childCount(), 0 ) );
211}
212
213void QgsMeshDatasetGroupTreeModel::removeItem( const QModelIndex &index )
214{
215 if ( !index.isValid() )
216 return;
217
218 QgsMeshDatasetGroupTreeItem *item = static_cast<QgsMeshDatasetGroupTreeItem *>( index.internalPointer() );
219 if ( !item || item->datasetGroupType() == QgsMeshDatasetGroup::Persistent )
220 return;
221
222 beginRemoveRows( index.parent(), index.row(), index.row() );
224 parent->removeChild( item );
225 endRemoveRows();
226}
227
228void QgsMeshDatasetGroupTreeModel::setPersistentDatasetGroup( const QModelIndex &index, const QString &uri )
229{
230 if ( !index.isValid() )
231 return;
232
233 QgsMeshDatasetGroupTreeItem *item = static_cast<QgsMeshDatasetGroupTreeItem *>( index.internalPointer() );
234 if ( !item )
235 return;
236 item->setPersistentDatasetGroup( uri );
237 dataChanged( index, index );
238}
239
241
243 : QSortFilterProxyModel( sourceModel )
244{
245 setSourceModel( sourceModel );
246}
247
249{
250 return mActiveScalarGroupIndex;
251}
252
254{
255 if ( mActiveScalarGroupIndex == group )
256 return;
257
258 const int oldGroupIndex = mActiveScalarGroupIndex;
259 mActiveScalarGroupIndex = group;
260
261 if ( oldGroupIndex > -1 || group > -1 )
262 invalidate();
263}
264
266{
267 return mActiveVectorGroupIndex;
268}
269
271{
272 if ( mActiveVectorGroupIndex == group )
273 return;
274
275 const int oldGroupIndex = mActiveVectorGroupIndex;
276 mActiveVectorGroupIndex = group;
277
278 if ( oldGroupIndex > -1 || group > -1 )
279 invalidate();
280}
281
282Qt::ItemFlags QgsMeshDatasetGroupProxyModel::flags( const QModelIndex &index ) const
283{
284 if ( !index.isValid() )
285 return Qt::NoItemFlags;
286
287 return Qt::ItemIsEnabled;
288}
289
290QVariant QgsMeshDatasetGroupProxyModel::data( const QModelIndex &index, int role ) const
291{
292 if ( !index.isValid() )
293 return QVariant();
294
295 const QModelIndex sourceIndex = mapToSource( index );
296 QgsMeshDatasetGroupTreeItem *item = static_cast<QgsMeshDatasetGroupTreeItem *>( sourceIndex.internalPointer() );
297
298 switch ( role )
299 {
301 return item->isVector();
303 return item->datasetGroupIndex() == mActiveScalarGroupIndex;
305 return item->datasetGroupIndex() == mActiveVectorGroupIndex;
306 case Qt::CheckStateRole:
307 return QVariant();
308 case Qt::DecorationRole:
309 return QVariant();
310 case Qt::BackgroundRole:
311 return QVariant();
312 }
313
314 return sourceModel()->data( sourceIndex, role );
315}
316
318{
319 static_cast<QgsMeshDatasetGroupTreeModel *>( sourceModel() )->syncToLayer( layer );
320 mActiveScalarGroupIndex = layer->rendererSettings().activeScalarDatasetGroup();
321 mActiveVectorGroupIndex = layer->rendererSettings().activeVectorDatasetGroup();
322 invalidate();
323}
324
325bool QgsMeshDatasetGroupProxyModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
326{
327 const QModelIndex sourceIndex = sourceModel()->index( source_row, 0, source_parent );
328
329 return static_cast<QgsMeshDatasetGroupTreeModel *>( sourceModel() )->isEnabled( sourceIndex );
330}
331
332
334
336 : QStyledItemDelegate( parent )
337 , mScalarSelectedPixmap( QStringLiteral( ":/images/themes/default/propertyicons/meshcontours.svg" ) )
338 , mScalarDeselectedPixmap( QStringLiteral( ":/images/themes/default/propertyicons/meshcontoursoff.svg" ) )
339 , mVectorSelectedPixmap( QStringLiteral( ":/images/themes/default/propertyicons/meshvectors.svg" ) )
340 , mVectorDeselectedPixmap( QStringLiteral( ":/images/themes/default/propertyicons/meshvectorsoff.svg" ) )
341{
342}
343
344void QgsMeshDatasetGroupTreeItemDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
345{
346 if ( !painter )
347 return;
348
349 QStyledItemDelegate::paint( painter, option, index );
350 const bool isVector = index.data( QgsMeshDatasetGroupTreeModel::IsVector ).toBool();
351 if ( isVector )
352 {
353 const bool isActive = index.data( QgsMeshDatasetGroupTreeModel::IsActiveVectorDatasetGroup ).toBool();
354 painter->drawPixmap( iconRect( option.rect, true ), isActive ? mVectorSelectedPixmap : mVectorDeselectedPixmap );
355 }
356
357 const bool isActive = index.data( QgsMeshDatasetGroupTreeModel::IsActiveScalarDatasetGroup ).toBool();
358 painter->drawPixmap( iconRect( option.rect, false ), isActive ? mScalarSelectedPixmap : mScalarDeselectedPixmap );
359}
360
361QRect QgsMeshDatasetGroupTreeItemDelegate::iconRect( const QRect &rect, bool isVector ) const
362{
363 return iconRect( rect, isVector ? 1 : 2 );
364}
365
366QRect QgsMeshDatasetGroupTreeItemDelegate::iconRect( const QRect &rect, int pos ) const
367{
368 const int iw = mScalarSelectedPixmap.width();
369 const int ih = mScalarSelectedPixmap.height();
370 const int margin = ( rect.height() - ih ) / 2;
371 return QRect( rect.right() - pos * ( iw + margin ), rect.top() + margin, iw, ih );
372}
373
374QSize QgsMeshDatasetGroupTreeItemDelegate::sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const
375{
376 QSize hint = QStyledItemDelegate::sizeHint( option, index );
377 if ( hint.height() < 16 )
378 hint.setHeight( 16 );
379 return hint;
380}
381
383
385 : QTreeView( parent ), mProxyModel( new QgsMeshDatasetGroupProxyModel( new QgsMeshDatasetGroupTreeModel( this ) ) )
386{
387 setModel( mProxyModel );
388 setItemDelegate( &mDelegate );
389 setSelectionMode( QAbstractItemView::SingleSelection );
390}
391
393{
394 if ( layer != mMeshLayer )
395 {
396 mMeshLayer = layer;
397 }
398}
399
401{
402 return mProxyModel->activeScalarGroup();
403}
404
406{
407 if ( mProxyModel->activeScalarGroup() != group )
408 {
409 mProxyModel->setActiveScalarGroup( group );
410 mProxyModel->invalidate();
411 emit activeScalarGroupChanged( group );
412 }
413}
414
416{
417 return mProxyModel->activeVectorGroup();
418}
419
421{
422 if ( mProxyModel->activeVectorGroup() != group )
423 {
424 mProxyModel->setActiveVectorGroup( group );
425 mProxyModel->invalidate();
426 emit activeVectorGroupChanged( group );
427 }
428}
429
431{
432 mProxyModel->syncToLayer( mMeshLayer );
433 setActiveGroup();
434}
435
437{
438 if ( !event )
439 return;
440
441 bool processed = false;
442 const QModelIndex idx = indexAt( event->pos() );
443 if ( idx.isValid() )
444 {
445 const QRect vr = visualRect( idx );
446 if ( mDelegate.iconRect( vr, true ).contains( event->pos() ) )
447 {
448 const bool isVector = idx.data( QgsMeshDatasetGroupTreeModel::IsVector ).toBool();
449 if ( isVector )
450 {
451 int datasetIndex = idx.data( QgsMeshDatasetGroupTreeModel::DatasetGroupIndex ).toInt();
452 if ( datasetIndex == activeVectorGroup() )
453 datasetIndex = -1;
454 setActiveVectorGroup( datasetIndex );
455 processed = true;
456 }
457 }
458 else if ( mDelegate.iconRect( vr, false ).contains( event->pos() ) )
459 {
460 int datasetIndex = idx.data( QgsMeshDatasetGroupTreeModel::DatasetGroupIndex ).toInt();
461 if ( datasetIndex == activeScalarGroup() )
462 datasetIndex = -1;
463 setActiveScalarGroup( datasetIndex );
464 processed = true;
465 }
466 }
467
468 // only if the user did not click one of the icons do usual handling
469 if ( !processed )
470 QTreeView::mousePressEvent( event );
471}
472
473void QgsMeshActiveDatasetGroupTreeView::setActiveGroup()
474{
475 int scalarGroup = -1;
476 int vectorGroup = -1;
477
478 // find active dataset
479 if ( mMeshLayer )
480 {
481 const QgsMeshRendererSettings rendererSettings = mMeshLayer->rendererSettings();
482 scalarGroup = rendererSettings.activeScalarDatasetGroup();
483 vectorGroup = rendererSettings.activeVectorDatasetGroup();
484 }
485
486 setActiveScalarGroup( scalarGroup );
487 setActiveVectorGroup( vectorGroup );
488}
489
491 : QAbstractListModel( parent )
492{}
493
495{
496 beginResetModel();
497 if ( layer )
498 mRootItem = layer->datasetGroupTreeRootItem();
499 endResetModel();
500}
501
502int QgsMeshDatasetGroupListModel::rowCount( const QModelIndex &parent ) const
503{
504 Q_UNUSED( parent );
505 if ( mRootItem )
506 return mRootItem->enabledDatasetGroupIndexes().count();
507 else
508 return 0;
509}
510
511QVariant QgsMeshDatasetGroupListModel::data( const QModelIndex &index, int role ) const
512{
513 if ( !mRootItem || !index.isValid() )
514 return QVariant();
515
516 if ( index.row() >= rowCount( QModelIndex() ) )
517 return QVariant();
518
519 const QList<int> list = mRootItem->enabledDatasetGroupIndexes();
520 if ( index.row() >= list.count() )
521 return QVariant();
522
523 QgsMeshDatasetGroupTreeItem *item = mRootItem->childFromDatasetGroupIndex( list.at( index.row() ) );
524
525 if ( !item )
526 return QVariant();
527
528 switch ( role )
529 {
530 case Qt::DisplayRole:
531 if ( mDisplayProviderName )
532 return item->providerName();
533 else
534 return item->name();
535 break;
536 case Qt::DecorationRole:
537 return QVariant();
538 break;
539 }
540
541 return QVariant();
542}
543
545{
546 mDisplayProviderName = displayProviderName;
547}
548
550{
551 const int varCount = rowCount( QModelIndex() );
552 QStringList variableNames;
553 for ( int i = 0; i < varCount; ++i )
554 variableNames.append( data( createIndex( i, 0 ), Qt::DisplayRole ).toString() );
555
556 return variableNames;
557}
558
560 : QTreeView( parent )
561 , mModel( new QgsMeshAvailableDatasetGroupTreeModel( this ) )
562 , mSaveMenu( new QgsMeshDatasetGroupSaveMenu( this ) )
563{
564 // To avoid the theme style overrides the background defined by the model
565 setStyleSheet( "QgsMeshDatasetGroupTreeView::item {background:none}" );
566
567 setModel( mModel );
568 setSelectionMode( QAbstractItemView::SingleSelection );
569 header()->setSectionResizeMode( QHeaderView::ResizeToContents );
570
571 connect( mSaveMenu, &QgsMeshDatasetGroupSaveMenu::datasetGroupSaved, this, &QgsMeshDatasetGroupTreeView::onDatasetGroupSaved );
572}
573
575{
576 if ( mModel )
577 mModel->syncToLayer( layer );
578 if ( mSaveMenu )
579 mSaveMenu->setMeshLayer( layer );
580}
581
583{
584 selectAllItem( true );
585}
586
588{
589 selectAllItem( false );
590}
591
592void QgsMeshDatasetGroupTreeView::contextMenuEvent( QContextMenuEvent *event )
593{
594 const QModelIndex idx = indexAt( event->pos() );
595 if ( !idx.isValid() )
596 setCurrentIndex( QModelIndex() );
597
598 std::unique_ptr<QMenu> menu( createContextMenu() );
599 if ( menu && menu->actions().count() != 0 )
600 menu->exec( mapToGlobal( event->pos() ) );
601}
602
603void QgsMeshDatasetGroupTreeView::removeCurrentItem()
604{
605 QgsMeshDatasetGroupTreeItem *item = mModel->datasetGroupTreeItem( currentIndex() );
606
607 if ( item )
608 {
609 const QList<int> dependencies = item->groupIndexDependencies();
610 if ( !dependencies.isEmpty() )
611 {
612 QString varList;
613 for ( const int dependentGroupIndex : dependencies )
614 {
615 QgsMeshDatasetGroupTreeItem *item = mModel->datasetGroupTreeItem( dependentGroupIndex );
616 if ( item )
617 {
618 varList.append( item->name() );
619 varList.append( QStringLiteral( "\n" ) );
620 }
621 }
622 QMessageBox::information( this, tr( "Remove Dataset Group" ), tr( "This dataset group can't be removed because other dataset groups depend on it:\n%1" ).arg( varList ) );
623 return;
624 }
625 }
626
627 if ( QMessageBox::question( this, tr( "Remove Dataset Group" ), tr( "Remove dataset group?" ) ) == QMessageBox::Yes )
628 mModel->removeItem( currentIndex() );
629}
630
631void QgsMeshDatasetGroupTreeView::onDatasetGroupSaved( const QString &uri )
632{
633 mModel->setPersistentDatasetGroup( currentIndex(), uri );
634 emit apply();
635}
636
637QMenu *QgsMeshDatasetGroupTreeView::createContextMenu()
638{
639 QMenu *contextMenu = new QMenu;
640
641 const QModelIndex &index = currentIndex();
642 if ( !index.isValid() )
643 return nullptr;
644
645 const int groupIndex = mModel->data( index, QgsMeshDatasetGroupTreeModel::DatasetGroupIndex ).toInt();
646 QgsMeshDatasetGroupTreeItem *item = mModel->datasetGroupTreeItem( groupIndex );
647
648 if ( !item )
649 return nullptr;
650
651 switch ( item->datasetGroupType() )
652 {
654 break;
656 break;
659 contextMenu->addAction( tr( "Remove Dataset Group" ), this, &QgsMeshDatasetGroupTreeView::removeCurrentItem );
660 mSaveMenu->createSaveMenu( groupIndex, contextMenu );
661 break;
662 }
663 return contextMenu;
664}
665
667{
668 mModel->resetDefault( meshLayer );
669}
670
675
676void QgsMeshDatasetGroupTreeView::selectAllItem( bool isChecked )
677{
678 mModel->setAllGroupsAsEnabled( isChecked );
679}
680
681QMenu *QgsMeshDatasetGroupSaveMenu::createSaveMenu( int groupIndex, QMenu *parentMenu )
682{
683 if ( !mMeshLayer )
684 return nullptr;
685
686 QMenu *menu = new QMenu( parentMenu );
687 menu->setTitle( QObject::tr( "Save Datasets Group as..." ) );
688 const QgsMeshDatasetGroupMetadata groupMeta = mMeshLayer->datasetGroupMetadata( groupIndex );
689
691 if ( providerMetadata )
692 {
693 const QList<QgsMeshDriverMetadata> allDrivers = providerMetadata->meshDriversMetadata();
694 for ( const QgsMeshDriverMetadata &driver : allDrivers )
695 {
696 const QString driverName = driver.name();
697 const QString suffix = driver.writeDatasetOnFileSuffix();
698 if ( ( driver.capabilities().testFlag( QgsMeshDriverMetadata::MeshDriverCapability::CanWriteFaceDatasets )
701 {
702 menu->addAction( driver.description(), this, [groupIndex, driverName, suffix, this] {
703 this->saveDatasetGroup( groupIndex, driverName, suffix );
704 } );
705 }
706 }
707 }
708
709 if ( menu->actions().isEmpty() )
710 {
711 menu->addAction( QObject::tr( "No Driver Available to Write this Dataset Group" ) );
712 menu->actions().last()->setDisabled( true );
713 }
714
715 if ( parentMenu )
716 parentMenu->addMenu( menu );
717
718 return menu;
719}
720
722{
723 mMeshLayer = meshLayer;
724}
725
726void QgsMeshDatasetGroupSaveMenu::saveDatasetGroup( int datasetGroup, const QString &driver, const QString &fileSuffix )
727{
728 if ( !mMeshLayer )
729 return;
730
731 QgsSettings settings;
732 QString filter;
733 if ( !fileSuffix.isEmpty() )
734 filter = QStringLiteral( "%1 (*.%2)" ).arg( driver ).arg( fileSuffix );
735 const QString exportFileDir = settings.value( QStringLiteral( "lastMeshDatasetDir" ), QDir::homePath(), QgsSettings::App ).toString();
736 const QString saveFileName = QFileDialog::getSaveFileName( nullptr, QObject::tr( "Save Mesh Datasets" ), exportFileDir, filter );
737
738 if ( saveFileName.isEmpty() )
739 return;
740
741 const QFileInfo openFileInfo( saveFileName );
742 settings.setValue( QStringLiteral( "lastMeshDatasetDir" ), openFileInfo.absolutePath(), QgsSettings::App );
743
744
745 if ( mMeshLayer->saveDataset( saveFileName, datasetGroup, driver ) )
746 {
747 QMessageBox::warning( nullptr, QObject::tr( "Save Mesh Datasets" ), QObject::tr( "Saving datasets fails" ) );
748 }
749 else
750 {
751 emit datasetGroupSaved( saveFileName );
752 QMessageBox::information( nullptr, QObject::tr( "Save Mesh Datasets" ), QObject::tr( "Datasets successfully saved on file" ) );
753 }
754}
755
759
760QVariant QgsMeshAvailableDatasetGroupTreeModel::data( const QModelIndex &index, int role ) const
761{
762 if ( !index.isValid() )
763 return QVariant();
764
765 switch ( role )
766 {
767 case Qt::DisplayRole:
768 case Qt::EditRole:
769 case Name:
770 return textDisplayed( index );
771
772 case Qt::BackgroundRole:
773 return backGroundColor( index );
774 }
776}
777
778bool QgsMeshAvailableDatasetGroupTreeModel::setData( const QModelIndex &index, const QVariant &value, int role )
779{
780 if ( !index.isValid() )
781 return false;
782
783 QgsMeshDatasetGroupTreeItem *item = static_cast<QgsMeshDatasetGroupTreeItem *>( index.internalPointer() );
784 if ( !item )
785 return false;
786
787 switch ( role )
788 {
789 case Qt::EditRole:
790 if ( value != QString() )
791 {
792 item->setName( value.toString() );
793 return true;
794 }
795 break;
796 case Qt::CheckStateRole:
797 item->setIsEnabled( value.toBool() );
798 return true;
799 }
800 return false;
801}
802
803Qt::ItemFlags QgsMeshAvailableDatasetGroupTreeModel::flags( const QModelIndex &index ) const
804{
805 if ( !index.isValid() )
806 return Qt::NoItemFlags;
807
808 if ( index.column() > 0 )
809 return Qt::ItemIsEnabled;
810
811 return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEditable;
812}
813
814QVariant QgsMeshAvailableDatasetGroupTreeModel::headerData( int section, Qt::Orientation orientation, int role ) const
815{
816 Q_UNUSED( section )
817
818 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
819 {
820 if ( section == 0 )
821 return tr( "Group" );
822 if ( section == 1 )
823 return tr( "Type" );
824 if ( section == 2 )
825 return tr( "Description" );
826 }
827
828 return QVariant();
829}
830
831int QgsMeshAvailableDatasetGroupTreeModel::columnCount( const QModelIndex &parent ) const
832{
833 Q_UNUSED( parent );
834 return 3;
835}
836
837QString QgsMeshAvailableDatasetGroupTreeModel::textDisplayed( const QModelIndex &index ) const
838{
839 QgsMeshDatasetGroupTreeItem *item = static_cast<QgsMeshDatasetGroupTreeItem *>( index.internalPointer() );
840 if ( !item )
841 return QString();
842
843 switch ( index.column() )
844 {
845 case 0:
846 return item->name();
847 case 1:
848 if ( item->isVector() )
849 return tr( "Vector" );
850 else
851 return tr( "Scalar" );
852 case 2:
853 return item->description();
854 }
855 return QString();
856}
857
858QColor QgsMeshAvailableDatasetGroupTreeModel::backGroundColor( const QModelIndex &index ) const
859{
860 QgsMeshDatasetGroupTreeItem *item = static_cast<QgsMeshDatasetGroupTreeItem *>( index.internalPointer() );
861 if ( !item )
862 return QColor();
863
865 return QColor( 103, 0, 243, 44 );
866 else if ( item->datasetGroupType() == QgsMeshDatasetGroup::Memory )
867 return QColor( 252, 155, 79, 44 );
868 else
869 return QColor( 252, 255, 79, 44 );
870}
virtual QString name() const =0
Returns a provider name.
void setActiveScalarGroup(int group)
Sets active group for contours.
QgsMeshActiveDatasetGroupTreeView(QWidget *parent=nullptr)
int activeScalarGroup() const
Returns index of active group for contours.
void setActiveVectorGroup(int group)
Sets active vector group.
void syncToLayer()
Synchronize widgets state with associated mesh layer.
void activeVectorGroupChanged(int groupIndex)
Selected dataset group for vectors changed. -1 for invalid group.
void mousePressEvent(QMouseEvent *event) override
void activeScalarGroupChanged(int groupIndex)
Selected dataset group for contours changed. -1 for invalid group.
int activeVectorGroup() const
Returns index of active group for vectors.
void setLayer(QgsMeshLayer *layer)
Associates mesh layer with the widget.
Qt::ItemFlags flags(const QModelIndex &index) const override
bool setData(const QModelIndex &index, const QVariant &value, int role) override
int columnCount(const QModelIndex &parent=QModelIndex()) const override
QVariant data(const QModelIndex &index, int role) const override
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
void setDisplayProviderName(bool displayProviderName)
QVariant data(const QModelIndex &index, int role) const override
int rowCount(const QModelIndex &parent) const override
void syncToLayer(QgsMeshLayer *layer)
Add groups to the model from mesh layer.
QgsMeshDatasetGroupMetadata is a collection of dataset group metadata such as whether the data is vec...
DataType dataType() const
Returns whether dataset group data is defined on vertices or faces or volumes.
@ DataOnEdges
Data is defined on edges.
@ DataOnFaces
Data is defined on faces.
@ DataOnVertices
Data is defined on vertices.
void setActiveVectorGroup(int group)
Sets active vector group.
int activeScalarGroup() const
Returns index of active group for contours.
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override
void setActiveScalarGroup(int group)
Sets active group for contours.
Qt::ItemFlags flags(const QModelIndex &index) const override
void syncToLayer(QgsMeshLayer *layer)
Add groups to the model from mesh layer.
int activeVectorGroup() const
Returns index of active group for vectors.
QgsMeshDatasetGroupProxyModel(QAbstractItemModel *sourceModel)
QVariant data(const QModelIndex &index, int role) const override
QMenu * createSaveMenu(int groupIndex, QMenu *parentMenu=nullptr)
void datasetGroupSaved(const QString &uri)
void setMeshLayer(QgsMeshLayer *meshLayer)
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
QRect iconRect(const QRect &rect, bool isVector) const
Icon rectangle for given item rectangle.
Tree item for display of the mesh dataset groups.
void setName(const QString &name)
Overrides the default name with the name to display.
QgsMeshDatasetGroupTreeItem * clone() const
Clones the item.
QList< int > groupIndexDependencies() const
Returns a list of group index corresponding to dataset group that depends on the dataset group repres...
void setPersistentDatasetGroup(const QString &uri)
Set parameters of the item in accordance with the persistent dataset group with uri.
int childCount() const
Returns the count of children.
QgsMeshDatasetGroupTreeItem * parentItem() const
Returns the parent item, nullptr if it is root item.
QgsMeshDatasetGroupTreeItem * childFromDatasetGroupIndex(int index)
Returns the child with dataset group index Searches as depper as needed on the child hierarchy.
void removeChild(QgsMeshDatasetGroupTreeItem *item)
Removes and destroy a item child if exists.
void setIsEnabled(bool isEnabled)
Sets whether the item is enabled, that is if it is displayed in view.
QList< int > enabledDatasetGroupIndexes() const
Returns a list of enabled dataset group indexes, included deeper children.
QString providerName() const
Returns the name used by the provider to identify the dataset.
QgsMeshDatasetGroupTreeItem * child(int row) const
Returns a child.
QString description() const
Returns description about the dataset group (URI, formula,...)
int row() const
Returns the position of the item in the parent.
QString name() const
Returns the name of the item This name is the default name if the name has not been overridden (.
QgsMeshDatasetGroup::Type datasetGroupType() const
Item Model for QgsMeshDatasetGroupTreeItem.
QgsMeshDatasetGroupTreeItem * datasetGroupTreeItem(int groupIndex)
Returns the dataset group tree item with index, keeps ownership.
QVariant data(const QModelIndex &index, int role) const override
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
void syncToLayer(QgsMeshLayer *layer)
Synchronizes groups to the model from mesh layer.
bool isEnabled(const QModelIndex &index) const
Returns whether the dataset groups related to the QModelIndex is set as enabled.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
void resetDefault(QgsMeshLayer *meshLayer)
Resets all groups with default state from the mesh layer.
QgsMeshDatasetGroupTreeItem * datasetGroupTreeRootItem()
Returns the dataset group root tree item, keeps ownership.
int columnCount(const QModelIndex &parent=QModelIndex()) const override
QgsMeshDatasetGroupTreeModel(QObject *parent=nullptr)
void removeItem(const QModelIndex &index)
Removes an item from the tree.
QModelIndex parent(const QModelIndex &index) const override
void setAllGroupsAsEnabled(bool isEnabled)
Sets all groups as enabled.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
void setPersistentDatasetGroup(const QModelIndex &index, const QString &uri)
Sets the dataset group as persistent with specified uri and for specified index.
Qt::ItemFlags flags(const QModelIndex &index) const override
QgsMeshDatasetGroupTreeView(QWidget *parent=nullptr)
QgsMeshDatasetGroupTreeItem * datasetGroupTreeRootItem()
void contextMenuEvent(QContextMenuEvent *event) override
void resetDefault(QgsMeshLayer *meshLayer)
@ Unknown
Generic type used for non typed dataset group.
@ Virtual
Virtual Dataset group defined by a formula.
@ Memory
Temporary dataset group in memory.
@ Persistent
Dataset group store in a file.
Holds metadata about mesh driver.
@ CanWriteVertexDatasets
If the driver can persist datasets defined on vertices.
@ CanWriteEdgeDatasets
If the driver can persist datasets defined on edges.
@ CanWriteFaceDatasets
If the driver can persist datasets defined on faces.
Represents a mesh layer supporting display of data on structured or unstructured meshes.
QgsMeshRendererSettings rendererSettings() const
Returns renderer settings.
void resetDatasetGroupTreeItem()
Reset the dataset group tree item to default from provider.
bool saveDataset(const QString &path, int datasetGroupIndex, QString driver)
Saves datasets group on file with the specified driver.
QgsMeshDataProvider * dataProvider() override
Returns the layer's data provider, it may be nullptr.
QgsMeshDatasetGroupTreeItem * datasetGroupTreeRootItem() const
Returns the root items of the dataset group tree item.
QgsMeshDatasetGroupMetadata datasetGroupMetadata(const QgsMeshDatasetIndex &index) const
Returns the dataset groups metadata.
Represents all mesh renderer settings.
int activeVectorDatasetGroup() const
Returns the active vector dataset group.
int activeScalarDatasetGroup() const
Returns the active scalar dataset group.
Holds data provider key, description, and associated shared library file or function pointer informat...
virtual QList< QgsMeshDriverMetadata > meshDriversMetadata()
Builds the list of available mesh drivers metadata.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
QgsProviderMetadata * providerMetadata(const QString &providerKey) const
Returns metadata of the provider or nullptr if not found.
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.