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