Skip to content

Commit

Permalink
Start on sublayer model
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 6, 2021
1 parent ab713e2 commit 2883215
Show file tree
Hide file tree
Showing 8 changed files with 594 additions and 0 deletions.
19 changes: 19 additions & 0 deletions python/core/auto_additions/qgsprovidersublayermodel.py
@@ -0,0 +1,19 @@
# The following has been generated automatically from src/core/providers/qgsprovidersublayermodel.h
# monkey patching scoped based enum
QgsProviderSublayerModel.Role.ProviderKey.__doc__ = "Provider key"
QgsProviderSublayerModel.Role.LayerType.__doc__ = "Layer type"
QgsProviderSublayerModel.Role.Uri.__doc__ = "Layer URI"
QgsProviderSublayerModel.Role.Name.__doc__ = "Layer name"
QgsProviderSublayerModel.Role.Description.__doc__ = "Layer description"
QgsProviderSublayerModel.Role.Path.__doc__ = "Layer path"
QgsProviderSublayerModel.Role.FeatureCount.__doc__ = "Feature count (for vector sublayers)"
QgsProviderSublayerModel.Role.WkbType.__doc__ = "WKB geometry type (for vector sublayers)"
QgsProviderSublayerModel.Role.GeometryColumnName.__doc__ = "Geometry column name (for vector sublayers)"
QgsProviderSublayerModel.Role.LayerNumber.__doc__ = "Layer number"
QgsProviderSublayerModel.Role.__doc__ = 'Custom model roles\n\n' + '* ``ProviderKey``: ' + QgsProviderSublayerModel.Role.ProviderKey.__doc__ + '\n' + '* ``LayerType``: ' + QgsProviderSublayerModel.Role.LayerType.__doc__ + '\n' + '* ``Uri``: ' + QgsProviderSublayerModel.Role.Uri.__doc__ + '\n' + '* ``Name``: ' + QgsProviderSublayerModel.Role.Name.__doc__ + '\n' + '* ``Description``: ' + QgsProviderSublayerModel.Role.Description.__doc__ + '\n' + '* ``Path``: ' + QgsProviderSublayerModel.Role.Path.__doc__ + '\n' + '* ``FeatureCount``: ' + QgsProviderSublayerModel.Role.FeatureCount.__doc__ + '\n' + '* ``WkbType``: ' + QgsProviderSublayerModel.Role.WkbType.__doc__ + '\n' + '* ``GeometryColumnName``: ' + QgsProviderSublayerModel.Role.GeometryColumnName.__doc__ + '\n' + '* ``LayerNumber``: ' + QgsProviderSublayerModel.Role.LayerNumber.__doc__
# --
# monkey patching scoped based enum
QgsProviderSublayerModel.Column.Name.__doc__ = "Layer name"
QgsProviderSublayerModel.Column.Description.__doc__ = "Layer description"
QgsProviderSublayerModel.Column.__doc__ = 'Model columns\n\n' + '* ``Name``: ' + QgsProviderSublayerModel.Column.Name.__doc__ + '\n' + '* ``Description``: ' + QgsProviderSublayerModel.Column.Description.__doc__
# --
@@ -0,0 +1,85 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/providers/qgsprovidersublayermodel.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/





class QgsProviderSublayerModel: QAbstractItemModel
{
%Docstring(signature="appended")


.. versionadded:: 3.22
%End

%TypeHeaderCode
#include "qgsprovidersublayermodel.h"
%End
public:

enum class Role
{
ProviderKey,
LayerType,
Uri,
Name,
Description,
Path,
FeatureCount,
WkbType,
GeometryColumnName,
LayerNumber,
};

enum class Column
{
Name,
Description,
};

QgsProviderSublayerModel( QObject *parent /TransferThis/ = 0 );
%Docstring
Constructor for QgsProviderSublayerModel, with the specified ``parent`` object.
%End

void setSublayerDetails( const QList< QgsProviderSublayerDetails > &details );
%Docstring
Sets the sublayer ``details`` to show in the model.

.. seealso:: :py:func:`sublayerDetails`
%End

QList< QgsProviderSublayerDetails > sublayerDetails() const;
%Docstring
Returns the sublayer details shown in the model.

.. seealso:: :py:func:`setSublayerDetails`
%End
virtual QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const;

virtual QModelIndex parent( const QModelIndex &index ) const;

virtual int columnCount( const QModelIndex &parent = QModelIndex() ) const;

virtual int rowCount( const QModelIndex &parent ) const;

virtual QVariant data( const QModelIndex &index, int role ) const;

virtual QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;


};

/************************************************************************
* This file has been generated automatically from *
* *
* src/core/providers/qgsprovidersublayermodel.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
1 change: 1 addition & 0 deletions python/core/core_auto.sip
Expand Up @@ -515,6 +515,7 @@
%Include auto_generated/providers/qgsprovidermetadata.sip
%Include auto_generated/providers/qgsproviderregistry.sip
%Include auto_generated/providers/qgsprovidersublayerdetails.sip
%Include auto_generated/providers/qgsprovidersublayermodel.sip
%Include auto_generated/providers/qgsprovidersublayertask.sip
%Include auto_generated/providers/qgsproviderutils.sip
%Include auto_generated/providers/arcgis/qgsarcgisportalutils.sip
Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -233,6 +233,7 @@ set(QGIS_CORE_SRCS
providers/qgsprovidermetadata.cpp
providers/qgsproviderregistry.cpp
providers/qgsprovidersublayerdetails.cpp
providers/qgsprovidersublayermodel.cpp
providers/qgsprovidersublayertask.cpp
providers/qgsproviderutils.cpp

Expand Down Expand Up @@ -1524,6 +1525,7 @@ set(QGIS_CORE_HDRS
providers/qgsprovidermetadata.h
providers/qgsproviderregistry.h
providers/qgsprovidersublayerdetails.h
providers/qgsprovidersublayermodel.h
providers/qgsprovidersublayertask.h
providers/qgsproviderutils.h

Expand Down
235 changes: 235 additions & 0 deletions src/core/providers/qgsprovidersublayermodel.cpp
@@ -0,0 +1,235 @@
/***************************************************************************
qgsprovidersublayermodel.cpp
----------------------
begin : June 2021
copyright : (C) 2021 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsprovidersublayermodel.h"
#include "qgsprovidersublayerdetails.h"
#include "qgsiconutils.h"
#include <QLocale>

QgsProviderSublayerModel::QgsProviderSublayerModel( QObject *parent )
: QAbstractItemModel( parent )
{

}

void QgsProviderSublayerModel::setSublayerDetails( const QList<QgsProviderSublayerDetails> &details )
{
// remove layers which don't exist in new list
for ( int i = mSublayers.count() - 1; i >= 0; --i )
{
if ( !details.contains( mSublayers.at( i ) ) )
{
beginRemoveRows( QModelIndex(), i, i );
mSublayers.removeAt( i );
endRemoveRows();
}
}

// and add new layers which exist only in new list
for ( const QgsProviderSublayerDetails &layer : details )
{
if ( !mSublayers.contains( layer ) )
{
beginInsertRows( QModelIndex(), mSublayers.count(), mSublayers.count() );
mSublayers.append( layer );
endInsertRows();
}
}
}

QList<QgsProviderSublayerDetails> QgsProviderSublayerModel::sublayerDetails() const
{
return mSublayers;
}

QModelIndex QgsProviderSublayerModel::index( int row, int column, const QModelIndex &parent ) const
{
if ( column < 0 || column >= columnCount() )
{
//column out of bounds
return QModelIndex();
}

if ( !parent.isValid() && row >= 0 && row < mSublayers.size() )
{
//return an index for the sublayer at this position
return createIndex( row, column );
}

//only top level supported for now
return QModelIndex();
}

QModelIndex QgsProviderSublayerModel::parent( const QModelIndex &index ) const
{
Q_UNUSED( index )

//all items are top level for now
return QModelIndex();
}

int QgsProviderSublayerModel::columnCount( const QModelIndex &parent ) const
{
Q_UNUSED( parent )
return static_cast< int >( Column::Description ) + 1;
}

int QgsProviderSublayerModel::rowCount( const QModelIndex &parent ) const
{
if ( !parent.isValid() )
{
return mSublayers.size();
}
else
{
//no children for nw
return 0;
}
}

QVariant QgsProviderSublayerModel::data( const QModelIndex &index, int role ) const
{
if ( !index.isValid() )
return QVariant();

if ( index.row() < 0 || index.row() >= rowCount( QModelIndex() ) )
return QVariant();

const QgsProviderSublayerDetails details = mSublayers.at( index.row() );

switch ( role )
{
case Qt::DisplayRole:
case Qt::ToolTipRole:
case Qt::EditRole:
{
switch ( static_cast< Column >( index.column() ) )
{
case QgsProviderSublayerModel::Column::Name:
return details.name();
case QgsProviderSublayerModel::Column::Description:
{
switch ( details.type() )
{
case QgsMapLayerType::VectorLayer:
{
QString count;
if ( details.featureCount() == static_cast< long long >( Qgis::FeatureCountState::Uncounted )
|| details.featureCount() == static_cast< long long >( Qgis::FeatureCountState::UnknownCount ) )
count = tr( "Uncounted" );
else
count = QLocale().toString( details.featureCount() );

if ( !details.description().isEmpty() )
return QStringLiteral( "%1 - %2 (%3)" ).arg( details.description(),
QgsWkbTypes::displayString( details.wkbType() ),
count );
else
return QStringLiteral( "%2 (%3)" ).arg(
QgsWkbTypes::displayString( details.wkbType() ),
count );
}

case QgsMapLayerType::RasterLayer:
case QgsMapLayerType::PluginLayer:
case QgsMapLayerType::MeshLayer:
case QgsMapLayerType::VectorTileLayer:
case QgsMapLayerType::AnnotationLayer:
case QgsMapLayerType::PointCloudLayer:
return details.description();
}
break;

}
}
return details.name();

}

case Qt::DecorationRole:
{
if ( index.column() == 0 )
return details.type() == QgsMapLayerType::VectorLayer
? QgsIconUtils::iconForWkbType( details.wkbType() )
: QgsIconUtils::iconForLayerType( details.type() );
else
return QVariant();
}

case static_cast< int >( Role::ProviderKey ):
return details.providerKey();

case static_cast< int >( Role::LayerType ):
return static_cast< int >( details.type() );

case static_cast< int >( Role::Uri ):
return details.uri();

case static_cast< int >( Role::Name ):
return details.name();

case static_cast< int >( Role::Description ):
return details.description();

case static_cast< int >( Role::Path ):
return details.path();

case static_cast< int >( Role::FeatureCount ):
return details.featureCount();

case static_cast< int >( Role::WkbType ):
return details.wkbType();

case static_cast< int >( Role::GeometryColumnName ):
return details.geometryColumnName();

case static_cast< int >( Role::LayerNumber ):
return details.layerNumber();

default:
return QVariant();
}
}

QVariant QgsProviderSublayerModel::headerData( int section, Qt::Orientation orientation, int role ) const
{
switch ( orientation )
{
case Qt::Vertical:
break;
case Qt::Horizontal:
{
switch ( role )
{
case Qt::DisplayRole:
case Qt::ToolTipRole:
{
switch ( static_cast< Column>( section ) )
{
case QgsProviderSublayerModel::Column::Name:
return tr( "Layer" );
case QgsProviderSublayerModel::Column::Description:
return tr( "Description" );
}
break;
}
}
break;
}
}
return QVariant();
}

0 comments on commit 2883215

Please sign in to comment.