27#include <QItemSelectionModel>
34#include "moc_qgsmeshdatasetgrouptreeview.cpp"
36using namespace Qt::StringLiterals;
39 : QAbstractItemModel(
parent )
51 if ( !
index.isValid() )
64 case Qt::CheckStateRole:
65 if (
index.column() == 0 )
66 return static_cast<int>( item->
isEnabled() ? Qt::Checked : Qt::Unchecked );
78 if ( !
index.isValid() )
79 return Qt::NoItemFlags;
81 return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEditable;
88 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
89 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();
177 if ( !
index.isValid() )
180 const QVariant checked =
data(
index, Qt::CheckStateRole );
182 return checked != QVariant() && checked.toInt() == Qt::Checked;
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:
311 case Qt::BackgroundRole:
315 return sourceModel()->data( sourceIndex, role );
328 const QModelIndex sourceIndex = sourceModel()->index( source_row, 0, source_parent );
337 : QStyledItemDelegate( parent )
338 , mScalarSelectedPixmap( u
":/images/themes/default/propertyicons/meshcontours.svg"_s )
339 , mScalarDeselectedPixmap( u
":/images/themes/default/propertyicons/meshcontoursoff.svg"_s )
340 , mVectorSelectedPixmap( u
":/images/themes/default/propertyicons/meshvectors.svg"_s )
341 , mVectorDeselectedPixmap( u
":/images/themes/default/propertyicons/meshvectorsoff.svg"_s )
349 QStyledItemDelegate::paint( painter, option, index );
354 painter->drawPixmap(
iconRect( option.rect,
true ), isActive ? mVectorSelectedPixmap : mVectorDeselectedPixmap );
358 painter->drawPixmap(
iconRect( option.rect,
false ), isActive ? mScalarSelectedPixmap : mScalarDeselectedPixmap );
363 return iconRect( rect, isVector ? 1 : 2 );
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 );
376 QSize hint = QStyledItemDelegate::sizeHint( option, index );
377 if ( hint.height() < 16 )
378 hint.setHeight( 16 );
385 : QTreeView( parent )
388 setModel( mProxyModel );
389 setItemDelegate( &mDelegate );
390 setSelectionMode( QAbstractItemView::SingleSelection );
395 if ( layer != mMeshLayer )
403 return mProxyModel->activeScalarGroup();
408 if ( mProxyModel->activeScalarGroup() != group )
410 mProxyModel->setActiveScalarGroup( group );
411 mProxyModel->invalidate();
418 return mProxyModel->activeVectorGroup();
423 if ( mProxyModel->activeVectorGroup() != group )
425 mProxyModel->setActiveVectorGroup( group );
426 mProxyModel->invalidate();
433 mProxyModel->syncToLayer( mMeshLayer );
442 bool processed =
false;
443 const QModelIndex idx = indexAt( event->pos() );
446 const QRect vr = visualRect( idx );
447 if ( mDelegate.iconRect( vr,
true ).contains( event->pos() ) )
459 else if ( mDelegate.iconRect( vr,
false ).contains( event->pos() ) )
471 QTreeView::mousePressEvent( event );
474void QgsMeshActiveDatasetGroupTreeView::setActiveGroup()
476 int scalarGroup = -1;
477 int vectorGroup = -1;
492 : QAbstractListModel( parent )
507 return mRootItem->enabledDatasetGroupIndexes().count();
514 if ( !mRootItem || !index.isValid() )
517 if ( index.row() >=
rowCount( QModelIndex() ) )
520 const QList<int> list = mRootItem->enabledDatasetGroupIndexes();
521 if ( index.row() >= list.count() )
531 case Qt::DisplayRole:
532 if ( mDisplayProviderName )
537 case Qt::DecorationRole:
547 mDisplayProviderName = displayProviderName;
552 const int varCount =
rowCount( QModelIndex() );
554 for (
int i = 0; i < varCount; ++i )
561 : QTreeView( parent )
566 setStyleSheet(
"QgsMeshDatasetGroupTreeView::item {background:none}" );
569 setSelectionMode( QAbstractItemView::SingleSelection );
570 header()->setSectionResizeMode( QHeaderView::ResizeToContents );
578 mModel->syncToLayer( layer );
580 mSaveMenu->setMeshLayer( layer );
585 selectAllItem(
true );
590 selectAllItem(
false );
595 const QModelIndex idx = indexAt( event->pos() );
596 if ( !idx.isValid() )
597 setCurrentIndex( QModelIndex() );
599 std::unique_ptr<QMenu> menu( createContextMenu() );
600 if ( menu && menu->actions().count() != 0 )
601 menu->exec( mapToGlobal( event->pos() ) );
604void QgsMeshDatasetGroupTreeView::removeCurrentItem()
611 if ( !dependencies.isEmpty() )
614 for (
const int dependentGroupIndex : dependencies )
619 varList.append( item->
name() );
620 varList.append( u
"\n"_s );
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" ).arg( varList ) );
628 if ( QMessageBox::question(
this, tr(
"Remove Dataset Group" ), tr(
"Remove dataset group?" ) ) == QMessageBox::Yes )
629 mModel->removeItem( currentIndex() );
632void QgsMeshDatasetGroupTreeView::onDatasetGroupSaved(
const QString &uri )
638QMenu *QgsMeshDatasetGroupTreeView::createContextMenu()
640 QMenu *contextMenu =
new QMenu;
642 const QModelIndex &index = currentIndex();
643 if ( !index.isValid() )
647 QgsMeshDatasetGroupTreeItem *item = mModel->datasetGroupTreeItem( groupIndex );
660 contextMenu->addAction( tr(
"Remove Dataset Group" ),
this, &QgsMeshDatasetGroupTreeView::removeCurrentItem );
661 mSaveMenu->createSaveMenu( groupIndex, contextMenu );
669 mModel->resetDefault( meshLayer );
674 return mModel->datasetGroupTreeRootItem();
677void QgsMeshDatasetGroupTreeView::selectAllItem(
bool isChecked )
687 QMenu *menu =
new QMenu( parentMenu );
688 menu->setTitle( QObject::tr(
"Save Datasets Group as..." ) );
692 if ( providerMetadata )
697 const QString driverName = driver.name();
698 const QString suffix = driver.writeDatasetOnFileSuffix();
703 menu->addAction( driver.description(),
this, [groupIndex, driverName, suffix,
this] { this->saveDatasetGroup( groupIndex, driverName, suffix ); } );
708 if ( menu->actions().isEmpty() )
710 menu->addAction( QObject::tr(
"No Driver Available to Write this Dataset Group" ) );
711 menu->actions().last()->setDisabled(
true );
715 parentMenu->addMenu( menu );
722 mMeshLayer = meshLayer;
725void QgsMeshDatasetGroupSaveMenu::saveDatasetGroup(
int datasetGroup,
const QString &driver,
const QString &fileSuffix )
732 if ( !fileSuffix.isEmpty() )
733 filter = u
"%1 (*.%2)"_s.arg( driver ).arg( fileSuffix );
734 const QString exportFileDir = settings.
value( u
"lastMeshDatasetDir"_s, QDir::homePath(),
QgsSettings::App ).toString();
735 const QString saveFileName = QFileDialog::getSaveFileName(
nullptr, QObject::tr(
"Save Mesh Datasets" ), exportFileDir, filter );
737 if ( saveFileName.isEmpty() )
740 const QFileInfo openFileInfo( saveFileName );
744 if ( mMeshLayer->
saveDataset( saveFileName, datasetGroup, driver ) )
746 QMessageBox::warning(
nullptr, QObject::tr(
"Save Mesh Datasets" ), QObject::tr(
"Saving datasets fails" ) );
751 QMessageBox::information(
nullptr, QObject::tr(
"Save Mesh Datasets" ), QObject::tr(
"Datasets successfully saved on file" ) );
761 if ( !
index.isValid() )
766 case Qt::DisplayRole:
769 return textDisplayed(
index );
771 case Qt::BackgroundRole:
772 return backGroundColor(
index );
779 if ( !
index.isValid() )
789 if ( value != QString() )
791 item->
setName( value.toString() );
795 case Qt::CheckStateRole:
804 if ( !
index.isValid() )
805 return Qt::NoItemFlags;
807 if (
index.column() > 0 )
808 return Qt::ItemIsEnabled;
810 return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEditable;
817 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
820 return tr(
"Group" );
824 return tr(
"Description" );
836QString QgsMeshAvailableDatasetGroupTreeModel::textDisplayed(
const QModelIndex &index )
const
842 switch (
index.column() )
848 return tr(
"Vector" );
850 return tr(
"Scalar" );
857QColor QgsMeshAvailableDatasetGroupTreeModel::backGroundColor(
const QModelIndex &index )
const
859 QgsMeshDatasetGroupTreeItem *item =
static_cast<QgsMeshDatasetGroupTreeItem *
>(
index.internalPointer() );
864 return QColor( 103, 0, 243, 44 );
866 return QColor( 252, 155, 79, 44 );
868 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.