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