26 #include <QItemSelectionModel>
27 #include <QMouseEvent>
29 #include <QFileDialog>
30 #include <QMessageBox>
31 #include <QHeaderView>
35 : QAbstractItemModel( parent )
48 if ( !
index.isValid() )
61 case Qt::CheckStateRole :
62 if (
index.column() == 0 )
63 return static_cast< int >( item->
isEnabled() ? Qt::Checked : Qt::Unchecked );
75 if ( !
index.isValid() )
76 return Qt::NoItemFlags;
78 return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEditable;
82 Qt::Orientation orientation,
87 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
88 return tr(
"Groups" );
96 if ( !hasIndex( row, column,
parent ) )
102 parentItem = mRootItem.get();
108 return createIndex( row, column, childItem );
110 return QModelIndex();
115 if ( !
index.isValid() )
116 return QModelIndex();
121 if ( parentItem == mRootItem.get() )
122 return QModelIndex();
124 return createIndex( parentItem->
row(), 0, parentItem );
130 if (
parent.column() > 0 )
134 parentItem = mRootItem.get();
156 return mRootItem.get();
162 return mRootItem->childFromDatasetGroupIndex( groupIndex );
177 if ( !
index.isValid() )
180 const QVariant checked =
data(
index, Qt::CheckStateRole );
182 return checked != QVariant() && checked.toInt() == Qt::Checked;
187 if ( !meshLayer && !mRootItem )
201 for (
int i = 0; i < mRootItem->childCount(); ++i )
205 for (
int j = 0; j < item->
childCount(); ++j )
211 dataChanged(
index( 0, 0 ),
index( mRootItem->childCount(), 0 ) );
216 if ( !
index.isValid() )
225 parent->removeChild( item );
231 if ( !
index.isValid() )
244 QSortFilterProxyModel( sourceModel )
246 setSourceModel( sourceModel );
251 return mActiveScalarGroupIndex;
256 if ( mActiveScalarGroupIndex == group )
259 const int oldGroupIndex = mActiveScalarGroupIndex;
260 mActiveScalarGroupIndex = group;
262 if ( oldGroupIndex > -1 || group > -1 )
268 return mActiveVectorGroupIndex;
273 if ( mActiveVectorGroupIndex == group )
276 const int oldGroupIndex = mActiveVectorGroupIndex;
277 mActiveVectorGroupIndex = group;
279 if ( oldGroupIndex > -1 || group > -1 )
285 if ( !index.isValid() )
286 return Qt::NoItemFlags;
288 return Qt::ItemIsEnabled;
293 if ( !index.isValid() )
296 const QModelIndex sourceIndex = mapToSource( index );
307 case Qt::CheckStateRole :
309 case Qt::DecorationRole:
312 #if QT_VERSION < QT_VERSION_CHECK(5, 13, 0)
313 case Qt::BackgroundColorRole:
315 case Qt::BackgroundRole:
320 return sourceModel()->data( sourceIndex, role );
333 const QModelIndex sourceIndex = sourceModel()->index( source_row, 0, source_parent );
342 : QStyledItemDelegate( parent )
343 , mScalarSelectedPixmap( QStringLiteral(
":/images/themes/default/propertyicons/meshcontours.svg" ) )
344 , mScalarDeselectedPixmap( QStringLiteral(
":/images/themes/default/propertyicons/meshcontoursoff.svg" ) )
345 , mVectorSelectedPixmap( QStringLiteral(
":/images/themes/default/propertyicons/meshvectors.svg" ) )
346 , mVectorDeselectedPixmap( QStringLiteral(
":/images/themes/default/propertyicons/meshvectorsoff.svg" ) )
355 QStyledItemDelegate::paint( painter, option, index );
360 painter->drawPixmap(
iconRect( option.rect,
true ), isActive ? mVectorSelectedPixmap : mVectorDeselectedPixmap );
364 painter->drawPixmap(
iconRect( option.rect,
false ), isActive ? mScalarSelectedPixmap : mScalarDeselectedPixmap );
369 return iconRect( rect, isVector ? 1 : 2 );
374 const int iw = mScalarSelectedPixmap.width();
375 const int ih = mScalarSelectedPixmap.height();
376 const int margin = ( rect.height() - ih ) / 2;
377 return QRect( rect.right() - pos * ( iw + margin ), rect.top() + margin, iw, ih );
382 QSize hint = QStyledItemDelegate::sizeHint( option, index );
383 if ( hint.height() < 16 )
384 hint.setHeight( 16 );
391 : QTreeView( parent ),
394 setModel( mProxyModel );
395 setItemDelegate( &mDelegate );
396 setSelectionMode( QAbstractItemView::SingleSelection );
401 if ( layer != mMeshLayer )
417 mProxyModel->invalidate();
432 mProxyModel->invalidate();
448 bool processed =
false;
449 const QModelIndex idx = indexAt( event->pos() ) ;
452 const QRect vr = visualRect( idx );
453 if ( mDelegate.
iconRect( vr,
true ).contains( event->pos() ) )
465 else if ( mDelegate.
iconRect( vr,
false ).contains( event->pos() ) )
477 QTreeView::mousePressEvent( event );
480 void QgsMeshActiveDatasetGroupTreeView::setActiveGroup()
482 int scalarGroup = -1;
483 int vectorGroup = -1;
519 if ( !mRootItem || ! index.isValid() )
522 if ( index.row() >=
rowCount( QModelIndex() ) )
526 if ( index.row() >= list.count() )
536 case Qt::DisplayRole:
537 if ( mDisplayProviderName )
542 case Qt::DecorationRole:
552 mDisplayProviderName = displayProviderName;
557 const int varCount =
rowCount( QModelIndex() );
559 for (
int i = 0; i < varCount; ++i )
571 setStyleSheet(
"QgsMeshDatasetGroupTreeView::item {background:none}" );
574 setSelectionMode( QAbstractItemView::SingleSelection );
575 header()->setSectionResizeMode( QHeaderView::ResizeToContents );
590 selectAllItem(
true );
595 selectAllItem(
false );
600 const QModelIndex idx = indexAt( event->pos() );
601 if ( !idx.isValid() )
602 setCurrentIndex( QModelIndex() );
604 std::unique_ptr<QMenu> menu( createContextMenu() );
605 if ( menu && menu->actions().count() != 0 )
606 menu->exec( mapToGlobal( event->pos() ) );
609 void QgsMeshDatasetGroupTreeView::removeCurrentItem()
616 if ( !dependencies.isEmpty() )
619 for (
const int dependentGroupIndex : dependencies )
624 varList.append( item->
name() );
625 varList.append( QStringLiteral(
"\n" ) );
628 QMessageBox::information(
this, tr(
"Remove Dataset Group" ), tr(
"This dataset group can't be removed because other dataset groups depend on it:\n%1" )
634 if ( QMessageBox::question(
this, tr(
"Remove Dataset Group" ), tr(
"Remove dataset group?" ) ) == QMessageBox::Yes )
638 void QgsMeshDatasetGroupTreeView::onDatasetGroupSaved(
const QString &uri )
644 QMenu *QgsMeshDatasetGroupTreeView::createContextMenu()
646 QMenu *contextMenu =
new QMenu;
648 const QModelIndex &index = currentIndex();
649 if ( !index.isValid() )
666 contextMenu->addAction( tr(
"Remove Dataset Group" ),
this, &QgsMeshDatasetGroupTreeView::removeCurrentItem );
683 void QgsMeshDatasetGroupTreeView::selectAllItem(
bool isChecked )
693 QMenu *menu =
new QMenu( parentMenu );
694 menu->setTitle( QObject::tr(
"Save Datasets Group as..." ) );
698 if ( providerMetadata )
703 const QString driverName = driver.name();
704 const QString suffix = driver.writeDatasetOnFileSuffix();
705 if ( ( driver.capabilities().testFlag( QgsMeshDriverMetadata::MeshDriverCapability::CanWriteFaceDatasets )
707 ( driver.capabilities().testFlag( QgsMeshDriverMetadata::MeshDriverCapability::CanWriteVertexDatasets )
709 ( driver.capabilities().testFlag( QgsMeshDriverMetadata::MeshDriverCapability::CanWriteEdgeDatasets )
712 menu->addAction( driver.description(),
this, [groupIndex, driverName, suffix,
this]
714 this->saveDatasetGroup( groupIndex, driverName, suffix );
720 if ( menu->actions().isEmpty() )
722 menu->addAction( QObject::tr(
"No Driver Available to Write this Dataset Group" ) );
723 menu->actions().last()->setDisabled(
true );
727 parentMenu->addMenu( menu );
734 mMeshLayer = meshLayer;
737 void QgsMeshDatasetGroupSaveMenu::saveDatasetGroup(
int datasetGroup,
const QString &driver,
const QString &fileSuffix )
744 if ( !fileSuffix.isEmpty() )
745 filter = QStringLiteral(
"%1 (*.%2)" ).arg( driver ).arg( fileSuffix );
746 const QString exportFileDir = settings.
value( QStringLiteral(
"lastMeshDatasetDir" ), QDir::homePath(),
QgsSettings::App ).toString();
747 const QString saveFileName = QFileDialog::getSaveFileName(
nullptr,
748 QObject::tr(
"Save Mesh Datasets" ),
752 if ( saveFileName.isEmpty() )
755 const QFileInfo openFileInfo( saveFileName );
759 if ( mMeshLayer->
saveDataset( saveFileName, datasetGroup, driver ) )
761 QMessageBox::warning(
nullptr, QObject::tr(
"Save Mesh Datasets" ), QObject::tr(
"Saving datasets fails" ) );
766 QMessageBox::information(
nullptr, QObject::tr(
"Save Mesh Datasets" ), QObject::tr(
"Datasets successfully saved on file" ) );
776 if ( !
index.isValid() )
781 case Qt::DisplayRole:
784 return textDisplayed(
index );
786 #if QT_VERSION < QT_VERSION_CHECK(5, 13, 0)
787 case Qt::BackgroundColorRole:
789 case Qt::BackgroundRole:
791 return backGroundColor(
index );
798 if ( !
index.isValid() )
808 if ( value != QString() )
810 item->
setName( value.toString() );
814 case Qt::CheckStateRole :
823 if ( !
index.isValid() )
824 return Qt::NoItemFlags;
826 if (
index.column() > 0 )
827 return Qt::ItemIsEnabled;
829 return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEditable;
836 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
839 return tr(
"Group" );
843 return tr(
"Description" );
851 QString QgsMeshAvailableDatasetGroupTreeModel::textDisplayed(
const QModelIndex &index )
const
857 switch (
index.column() )
863 return tr(
"Vector" );
865 return tr(
"Scalar" );
872 QColor QgsMeshAvailableDatasetGroupTreeModel::backGroundColor(
const QModelIndex &index )
const
879 return QColor( 103, 0, 243, 44 );
881 return QColor( 252, 155, 79, 44 );
883 return QColor( 252, 255, 79, 44 );