27#include <QItemSelectionModel>
34#include "moc_qgsmeshdatasetgrouptreeview.cpp"
36using namespace Qt::StringLiterals;
39 : QAbstractItemModel(
parent )
52 if ( !
index.isValid() )
65 case Qt::CheckStateRole:
66 if (
index.column() == 0 )
67 return static_cast<int>( item->
isEnabled() ? Qt::Checked : Qt::Unchecked );
79 if ( !
index.isValid() )
80 return Qt::NoItemFlags;
82 return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEditable;
89 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
90 return tr(
"Groups" );
98 if ( !hasIndex( row, column,
parent ) )
104 parentItem = mRootItem.get();
110 return createIndex( row, column, childItem );
112 return QModelIndex();
117 if ( !
index.isValid() )
118 return QModelIndex();
123 if ( parentItem == mRootItem.get() )
124 return QModelIndex();
126 return createIndex( parentItem->
row(), 0, parentItem );
132 if (
parent.column() > 0 )
136 parentItem = mRootItem.get();
158 return mRootItem.get();
179 if ( !
index.isValid() )
182 const QVariant checked =
data(
index, Qt::CheckStateRole );
184 return checked != QVariant() && checked.toInt() == Qt::Checked;
203 for (
int i = 0; i < mRootItem->childCount(); ++i )
207 for (
int j = 0; j < item->
childCount(); ++j )
213 dataChanged(
index( 0, 0 ),
index( mRootItem->childCount(), 0 ) );
218 if ( !
index.isValid() )
227 parent->removeChild( item );
233 if ( !
index.isValid() )
246 : QSortFilterProxyModel( sourceModel )
248 setSourceModel( sourceModel );
253 return mActiveScalarGroupIndex;
258 if ( mActiveScalarGroupIndex == group )
261 const int oldGroupIndex = mActiveScalarGroupIndex;
262 mActiveScalarGroupIndex = group;
264 if ( oldGroupIndex > -1 || group > -1 )
270 return mActiveVectorGroupIndex;
275 if ( mActiveVectorGroupIndex == group )
278 const int oldGroupIndex = mActiveVectorGroupIndex;
279 mActiveVectorGroupIndex = group;
281 if ( oldGroupIndex > -1 || group > -1 )
287 if ( !index.isValid() )
288 return Qt::NoItemFlags;
290 return Qt::ItemIsEnabled;
295 if ( !index.isValid() )
298 const QModelIndex sourceIndex = mapToSource( index );
309 case Qt::CheckStateRole:
311 case Qt::DecorationRole:
313 case Qt::BackgroundRole:
317 return sourceModel()->data( sourceIndex, role );
330 const QModelIndex sourceIndex = sourceModel()->index( source_row, 0, source_parent );
339 : QStyledItemDelegate( parent )
340 , mScalarSelectedPixmap( u
":/images/themes/default/propertyicons/meshcontours.svg"_s )
341 , mScalarDeselectedPixmap( u
":/images/themes/default/propertyicons/meshcontoursoff.svg"_s )
342 , mVectorSelectedPixmap( u
":/images/themes/default/propertyicons/meshvectors.svg"_s )
343 , mVectorDeselectedPixmap( u
":/images/themes/default/propertyicons/meshvectorsoff.svg"_s )
352 QStyledItemDelegate::paint( painter, option, index );
357 painter->drawPixmap(
iconRect( option.rect,
true ), isActive ? mVectorSelectedPixmap : mVectorDeselectedPixmap );
361 painter->drawPixmap(
iconRect( option.rect,
false ), isActive ? mScalarSelectedPixmap : mScalarDeselectedPixmap );
366 return iconRect( rect, isVector ? 1 : 2 );
371 const int iw = mScalarSelectedPixmap.width();
372 const int ih = mScalarSelectedPixmap.height();
373 const int margin = ( rect.height() - ih ) / 2;
374 return QRect( rect.right() - pos * ( iw + margin ), rect.top() + margin, iw, ih );
379 QSize hint = QStyledItemDelegate::sizeHint( option, index );
380 if ( hint.height() < 16 )
381 hint.setHeight( 16 );
390 setModel( mProxyModel );
391 setItemDelegate( &mDelegate );
392 setSelectionMode( QAbstractItemView::SingleSelection );
397 if ( layer != mMeshLayer )
405 return mProxyModel->activeScalarGroup();
410 if ( mProxyModel->activeScalarGroup() != group )
412 mProxyModel->setActiveScalarGroup( group );
413 mProxyModel->invalidate();
420 return mProxyModel->activeVectorGroup();
425 if ( mProxyModel->activeVectorGroup() != group )
427 mProxyModel->setActiveVectorGroup( group );
428 mProxyModel->invalidate();
435 mProxyModel->syncToLayer( mMeshLayer );
444 bool processed =
false;
445 const QModelIndex idx = indexAt( event->pos() );
448 const QRect vr = visualRect( idx );
449 if ( mDelegate.iconRect( vr,
true ).contains( event->pos() ) )
461 else if ( mDelegate.iconRect( vr,
false ).contains( event->pos() ) )
473 QTreeView::mousePressEvent( event );
476void QgsMeshActiveDatasetGroupTreeView::setActiveGroup()
478 int scalarGroup = -1;
479 int vectorGroup = -1;
494 : QAbstractListModel( parent )
509 return mRootItem->enabledDatasetGroupIndexes().count();
516 if ( !mRootItem || !index.isValid() )
519 if ( index.row() >=
rowCount( QModelIndex() ) )
522 const QList<int> list = mRootItem->enabledDatasetGroupIndexes();
523 if ( index.row() >= list.count() )
533 case Qt::DisplayRole:
534 if ( mDisplayProviderName )
539 case Qt::DecorationRole:
549 mDisplayProviderName = displayProviderName;
554 const int varCount =
rowCount( QModelIndex() );
556 for (
int i = 0; i < varCount; ++i )
563 : QTreeView( parent )
568 setStyleSheet(
"QgsMeshDatasetGroupTreeView::item {background:none}" );
571 setSelectionMode( QAbstractItemView::SingleSelection );
572 header()->setSectionResizeMode( QHeaderView::ResizeToContents );
580 mModel->syncToLayer( layer );
582 mSaveMenu->setMeshLayer( layer );
587 selectAllItem(
true );
592 selectAllItem(
false );
597 const QModelIndex idx = indexAt( event->pos() );
598 if ( !idx.isValid() )
599 setCurrentIndex( QModelIndex() );
601 std::unique_ptr<QMenu> menu( createContextMenu() );
602 if ( menu && menu->actions().count() != 0 )
603 menu->exec( mapToGlobal( event->pos() ) );
606void QgsMeshDatasetGroupTreeView::removeCurrentItem()
613 if ( !dependencies.isEmpty() )
616 for (
const int dependentGroupIndex : dependencies )
621 varList.append( item->
name() );
622 varList.append( u
"\n"_s );
625 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 ) );
630 if ( QMessageBox::question(
this, tr(
"Remove Dataset Group" ), tr(
"Remove dataset group?" ) ) == QMessageBox::Yes )
631 mModel->removeItem( currentIndex() );
634void QgsMeshDatasetGroupTreeView::onDatasetGroupSaved(
const QString &uri )
640QMenu *QgsMeshDatasetGroupTreeView::createContextMenu()
642 QMenu *contextMenu =
new QMenu;
644 const QModelIndex &index = currentIndex();
645 if ( !index.isValid() )
649 QgsMeshDatasetGroupTreeItem *item = mModel->datasetGroupTreeItem( groupIndex );
662 contextMenu->addAction( tr(
"Remove Dataset Group" ),
this, &QgsMeshDatasetGroupTreeView::removeCurrentItem );
663 mSaveMenu->createSaveMenu( groupIndex, contextMenu );
671 mModel->resetDefault( meshLayer );
676 return mModel->datasetGroupTreeRootItem();
679void QgsMeshDatasetGroupTreeView::selectAllItem(
bool isChecked )
689 QMenu *menu =
new QMenu( parentMenu );
690 menu->setTitle( QObject::tr(
"Save Datasets Group as..." ) );
694 if ( providerMetadata )
699 const QString driverName = driver.name();
700 const QString suffix = driver.writeDatasetOnFileSuffix();
705 menu->addAction( driver.description(),
this, [groupIndex, driverName, suffix,
this] {
706 this->saveDatasetGroup( groupIndex, driverName, suffix );
712 if ( menu->actions().isEmpty() )
714 menu->addAction( QObject::tr(
"No Driver Available to Write this Dataset Group" ) );
715 menu->actions().last()->setDisabled(
true );
719 parentMenu->addMenu( menu );
726 mMeshLayer = meshLayer;
729void QgsMeshDatasetGroupSaveMenu::saveDatasetGroup(
int datasetGroup,
const QString &driver,
const QString &fileSuffix )
736 if ( !fileSuffix.isEmpty() )
737 filter = u
"%1 (*.%2)"_s.arg( driver ).arg( fileSuffix );
738 const QString exportFileDir = settings.
value( u
"lastMeshDatasetDir"_s, QDir::homePath(),
QgsSettings::App ).toString();
739 const QString saveFileName = QFileDialog::getSaveFileName(
nullptr, QObject::tr(
"Save Mesh Datasets" ), exportFileDir, filter );
741 if ( saveFileName.isEmpty() )
744 const QFileInfo openFileInfo( saveFileName );
748 if ( mMeshLayer->
saveDataset( saveFileName, datasetGroup, driver ) )
750 QMessageBox::warning(
nullptr, QObject::tr(
"Save Mesh Datasets" ), QObject::tr(
"Saving datasets fails" ) );
755 QMessageBox::information(
nullptr, QObject::tr(
"Save Mesh Datasets" ), QObject::tr(
"Datasets successfully saved on file" ) );
765 if ( !
index.isValid() )
770 case Qt::DisplayRole:
773 return textDisplayed(
index );
775 case Qt::BackgroundRole:
776 return backGroundColor(
index );
783 if ( !
index.isValid() )
793 if ( value != QString() )
795 item->
setName( value.toString() );
799 case Qt::CheckStateRole:
808 if ( !
index.isValid() )
809 return Qt::NoItemFlags;
811 if (
index.column() > 0 )
812 return Qt::ItemIsEnabled;
814 return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEditable;
821 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
824 return tr(
"Group" );
828 return tr(
"Description" );
840QString QgsMeshAvailableDatasetGroupTreeModel::textDisplayed(
const QModelIndex &index )
const
846 switch (
index.column() )
852 return tr(
"Vector" );
854 return tr(
"Scalar" );
861QColor QgsMeshAvailableDatasetGroupTreeModel::backGroundColor(
const QModelIndex &index )
const
863 QgsMeshDatasetGroupTreeItem *item =
static_cast<QgsMeshDatasetGroupTreeItem *
>(
index.internalPointer() );
868 return QColor( 103, 0, 243, 44 );
870 return QColor( 252, 155, 79, 44 );
872 return QColor( 252, 255, 79, 44 );
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.
Item model for available mesh dataset groups.
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.
QgsMeshDatasetGroupListModel(QObject *parent)
QStringList variableNames() const
A proxy model for filtering mesh dataset groups.
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
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
QgsMeshDatasetGroupTreeItemDelegate(QObject *parent=nullptr)
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...
bool isVector() const
Return true if the dataset group is vector.
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.
int datasetGroupIndex() const
Returns the dataset group index.
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.
bool isEnabled() const
Returns true if the item is enabled, i.e.
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
Returns the dataset group type.
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.
@ IsActiveVectorDatasetGroup
@ IsActiveScalarDatasetGroup
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
void syncToLayer(QgsMeshLayer *layer)
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.
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.
QgsMeshDatasetGroupTreeItem * datasetGroupTreeRootItem() const
Returns the root items of the dataset group tree item.
Represents all mesh renderer settings.
int activeVectorDatasetGroup() const
Returns the active vector dataset group.
int activeScalarDatasetGroup() const
Returns the active scalar dataset group.
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.
Stores settings for use within QGIS.
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.