17#include "moc_qgsmeshdatasetgrouptreeview.cpp"
27#include <QItemSelectionModel>
36 : QAbstractItemModel( parent )
49 if ( !
index.isValid() )
62 case Qt::CheckStateRole :
63 if (
index.column() == 0 )
64 return static_cast< int >( item->
isEnabled() ? Qt::Checked : Qt::Unchecked );
76 if ( !
index.isValid() )
77 return Qt::NoItemFlags;
79 return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEditable;
83 Qt::Orientation orientation,
88 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
89 return tr(
"Groups" );
97 if ( !hasIndex( row, column,
parent ) )
103 parentItem = mRootItem.get();
109 return createIndex( row, column, childItem );
111 return QModelIndex();
116 if ( !
index.isValid() )
117 return QModelIndex();
122 if ( parentItem == mRootItem.get() )
123 return QModelIndex();
125 return createIndex( parentItem->
row(), 0, parentItem );
131 if (
parent.column() > 0 )
135 parentItem = mRootItem.get();
157 return mRootItem.get();
163 return mRootItem->childFromDatasetGroupIndex( groupIndex );
178 if ( !
index.isValid() )
181 const QVariant checked =
data(
index, Qt::CheckStateRole );
183 return checked != QVariant() && checked.toInt() == Qt::Checked;
188 if ( !meshLayer && !mRootItem )
202 for (
int i = 0; i < mRootItem->childCount(); ++i )
206 for (
int j = 0; j < item->
childCount(); ++j )
212 dataChanged(
index( 0, 0 ),
index( mRootItem->childCount(), 0 ) );
217 if ( !
index.isValid() )
232 if ( !
index.isValid() )
245 QSortFilterProxyModel( sourceModel )
247 setSourceModel( sourceModel );
252 return mActiveScalarGroupIndex;
257 if ( mActiveScalarGroupIndex == group )
260 const int oldGroupIndex = mActiveScalarGroupIndex;
261 mActiveScalarGroupIndex = group;
263 if ( oldGroupIndex > -1 || group > -1 )
269 return mActiveVectorGroupIndex;
274 if ( mActiveVectorGroupIndex == group )
277 const int oldGroupIndex = mActiveVectorGroupIndex;
278 mActiveVectorGroupIndex = group;
280 if ( oldGroupIndex > -1 || group > -1 )
286 if ( !index.isValid() )
287 return Qt::NoItemFlags;
289 return Qt::ItemIsEnabled;
294 if ( !index.isValid() )
297 const QModelIndex sourceIndex = mapToSource( index );
308 case Qt::CheckStateRole :
310 case Qt::DecorationRole:
312 case Qt::BackgroundRole:
316 return sourceModel()->data( sourceIndex, role );
329 const QModelIndex sourceIndex = sourceModel()->index( source_row, 0, source_parent );
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" ) )
351 QStyledItemDelegate::paint( painter, option, index );
356 painter->drawPixmap(
iconRect( option.rect,
true ), isActive ? mVectorSelectedPixmap : mVectorDeselectedPixmap );
360 painter->drawPixmap(
iconRect( option.rect,
false ), isActive ? mScalarSelectedPixmap : mScalarDeselectedPixmap );
365 return iconRect( rect, isVector ? 1 : 2 );
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 );
378 QSize hint = QStyledItemDelegate::sizeHint( option, index );
379 if ( hint.height() < 16 )
380 hint.setHeight( 16 );
387 : QTreeView( parent ),
390 setModel( mProxyModel );
391 setItemDelegate( &mDelegate );
392 setSelectionMode( QAbstractItemView::SingleSelection );
397 if ( layer != mMeshLayer )
413 mProxyModel->invalidate();
428 mProxyModel->invalidate();
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;
515 if ( !mRootItem || ! index.isValid() )
518 if ( index.row() >=
rowCount( QModelIndex() ) )
522 if ( index.row() >= list.count() )
532 case Qt::DisplayRole:
533 if ( mDisplayProviderName )
538 case Qt::DecorationRole:
548 mDisplayProviderName = displayProviderName;
553 const int varCount =
rowCount( QModelIndex() );
555 for (
int i = 0; i < varCount; ++i )
567 setStyleSheet(
"QgsMeshDatasetGroupTreeView::item {background:none}" );
570 setSelectionMode( QAbstractItemView::SingleSelection );
571 header()->setSectionResizeMode( QHeaderView::ResizeToContents );
586 selectAllItem(
true );
591 selectAllItem(
false );
596 const QModelIndex idx = indexAt( event->pos() );
597 if ( !idx.isValid() )
598 setCurrentIndex( QModelIndex() );
600 std::unique_ptr<QMenu> menu( createContextMenu() );
601 if ( menu && menu->actions().count() != 0 )
602 menu->exec( mapToGlobal( event->pos() ) );
605void QgsMeshDatasetGroupTreeView::removeCurrentItem()
612 if ( !dependencies.isEmpty() )
615 for (
const int dependentGroupIndex : dependencies )
620 varList.append( item->
name() );
621 varList.append( QStringLiteral(
"\n" ) );
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" )
630 if ( QMessageBox::question(
this, tr(
"Remove Dataset Group" ), tr(
"Remove dataset group?" ) ) == QMessageBox::Yes )
634void QgsMeshDatasetGroupTreeView::onDatasetGroupSaved(
const QString &uri )
640QMenu *QgsMeshDatasetGroupTreeView::createContextMenu()
642 QMenu *contextMenu =
new QMenu;
644 const QModelIndex &index = currentIndex();
645 if ( !index.isValid() )
662 contextMenu->addAction( tr(
"Remove Dataset Group" ),
this, &QgsMeshDatasetGroupTreeView::removeCurrentItem );
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();
708 menu->addAction( driver.description(),
this, [groupIndex, driverName, suffix,
this]
710 this->saveDatasetGroup( groupIndex, driverName, suffix );
716 if ( menu->actions().isEmpty() )
718 menu->addAction( QObject::tr(
"No Driver Available to Write this Dataset Group" ) );
719 menu->actions().last()->setDisabled(
true );
723 parentMenu->addMenu( menu );
730 mMeshLayer = meshLayer;
733void QgsMeshDatasetGroupSaveMenu::saveDatasetGroup(
int datasetGroup,
const QString &driver,
const QString &fileSuffix )
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" ),
748 if ( saveFileName.isEmpty() )
751 const QFileInfo openFileInfo( saveFileName );
755 if ( mMeshLayer->
saveDataset( saveFileName, datasetGroup, driver ) )
757 QMessageBox::warning(
nullptr, QObject::tr(
"Save Mesh Datasets" ), QObject::tr(
"Saving datasets fails" ) );
762 QMessageBox::information(
nullptr, QObject::tr(
"Save Mesh Datasets" ), QObject::tr(
"Datasets successfully saved on file" ) );
772 if ( !
index.isValid() )
777 case Qt::DisplayRole:
780 return textDisplayed(
index );
782 case Qt::BackgroundRole:
783 return backGroundColor(
index );
790 if ( !
index.isValid() )
800 if ( value != QString() )
802 item->
setName( value.toString() );
806 case Qt::CheckStateRole :
815 if ( !
index.isValid() )
816 return Qt::NoItemFlags;
818 if (
index.column() > 0 )
819 return Qt::ItemIsEnabled;
821 return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEditable;
828 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
831 return tr(
"Group" );
835 return tr(
"Description" );
843QString QgsMeshAvailableDatasetGroupTreeModel::textDisplayed(
const QModelIndex &index )
const
849 switch (
index.column() )
855 return tr(
"Vector" );
857 return tr(
"Scalar" );
864QColor QgsMeshAvailableDatasetGroupTreeModel::backGroundColor(
const QModelIndex &index )
const
871 return QColor( 103, 0, 243, 44 );
873 return QColor( 252, 155, 79, 44 );
875 return QColor( 252, 255, 79, 44 );
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.
QgsMeshDatasetGroupListModel(QObject *parent)
QStringList variableNames() const
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...
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
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.
@ 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.
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.
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:
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.