21#include "moc_qgsprojectstoredobjectmanagermodel.cpp"
28 : QAbstractListModel( parent )
33 return rowCountInternal( parent );
38 return dataInternal( index, role );
43 return setDataInternal( index, value, role );
48 return flagsInternal( index );
52void QgsProjectStoredObjectManagerModelBase::objectAboutToBeAdded(
const QString &name )
54 objectAboutToBeAddedInternal( name );
57void QgsProjectStoredObjectManagerModelBase::objectAboutToBeRemoved(
const QString &name )
59 objectAboutToBeRemovedInternal( name );
62void QgsProjectStoredObjectManagerModelBase::objectAdded(
const QString &name )
64 objectAddedInternal( name );
67void QgsProjectStoredObjectManagerModelBase::objectRemoved(
const QString &name )
69 objectRemovedInternal( name );
90 return ( mObjectManager ? mObjectManager->objects().count() : 0 ) + ( mAllowEmpty ? 1 : 0 );
95 if ( index.row() < 0 || index.row() >= rowCount( QModelIndex() ) )
98 const bool isEmpty = index.row() == 0 && mAllowEmpty;
99 const int objectRow = mAllowEmpty ? index.row() - 1 : index.row();
103 case Qt::DisplayRole:
104 case Qt::ToolTipRole:
115 case static_cast< int >( CustomRole::IsEmptyObject ):
120 case Qt::DecorationRole:
122 if ( isEmpty || !mObjectManager )
126 if constexpr (
requires {
object->icon(); } )
128 return object->icon();
143 if ( !index.isValid() || role != Qt::EditRole )
147 if ( index.row() >= mObjectManager->objects().count() )
152 if ( index.row() == 0 && mAllowEmpty )
155 if ( value.toString().isEmpty() )
158 T *
object = objectFromIndex( index );
163 bool changed =
object->name() != value.toString();
168 QStringList existingNames;
169 const QList< T * > objects = mObjectManager->objects();
170 for ( T *l : objects )
172 existingNames << l->name();
174 if ( existingNames.contains( value.toString() ) )
177 QMessageBox::warning(
nullptr, tr(
"Rename Layout" ), tr(
"There is already a layout named “%1”." ).arg( value.toString() ) );
181 object->setName( value.toString() );
187 Qt::ItemFlags flags = QAbstractListModel::flags( index );
189 if ( index.isValid() )
191 return flags | Qt::ItemIsEditable;
203 int row = mObjectManager->objects().count() + ( mAllowEmpty ? 1 : 0 );
204 beginInsertRows( QModelIndex(), row, row );
209 T *l = mObjectManager->objectByName( name );
210 int row = mObjectManager->objects().indexOf( l ) + ( mAllowEmpty ? 1 : 0 );
212 beginRemoveRows( QModelIndex(), row, row );
227 int row = mObjectManager->objects().indexOf(
object ) + ( mAllowEmpty ? 1 : 0 );
228 QModelIndex index = createIndex( row, 0 );
229 emit dataChanged( index, index, QVector<int>() << Qt::DisplayRole );
234 if ( T *l =
dynamic_cast< T *
>(
object ) )
235 return QVariant::fromValue( l );
244 if ( index.row() == 0 && mAllowEmpty )
247 if ( T *l = qobject_cast< T * >( qvariant_cast<QObject *>(
data( index,
static_cast< int >(
CustomRole::Object ) ) ) ) )
257 return QModelIndex();
262 return QModelIndex();
264 QModelIndex idx = index( mAllowEmpty ? r + 1 : r, 0, QModelIndex() );
270 return QModelIndex();
275 if ( allowEmpty == mAllowEmpty )
280 beginInsertRows( QModelIndex(), 0, 0 );
286 beginRemoveRows( QModelIndex(), 0, 0 );
301 return QVariant::fromValue( l );
302 else if ( QgsReport *r =
dynamic_cast< QgsReport *
>(
object ) )
303 return QVariant::fromValue( r );
309 if ( index.row() == 0 && mAllowEmpty )
312 if (
QgsPrintLayout *l = qobject_cast< QgsPrintLayout * >( qvariant_cast<QObject *>( data( index,
static_cast< int >( CustomRole::Object ) ) ) ) )
314 else if ( QgsReport *r = qobject_cast< QgsReport * >( qvariant_cast<QObject *>( data( index,
static_cast< int >( CustomRole::Object ) ) ) ) )
331 return QVariant::fromValue(
object );
337 if ( index.row() == 0 && mAllowEmpty )
340 const QVariant variant = data( index,
static_cast< int >( CustomRole::Object ) );
341 return qvariant_cast< QgsSelectiveMaskingSourceSet *>( variant );
346 if ( !mObjectManager )
348 return QModelIndex();
351 const QList< QgsSelectiveMaskingSourceSet * > objects = mObjectManager->objects();
353 bool foundMatch =
false;
356 if ( set && set->id() == object->
id() )
364 return QModelIndex();
366 QModelIndex idx = index( mAllowEmpty ? r + 1 : r, 0, QModelIndex() );
372 return QModelIndex();
385 : QSortFilterProxyModel( parent )
387 setDynamicSortFilter(
true );
389 setSortCaseSensitivity( Qt::CaseInsensitive );
394 if ( qobject_cast< QgsProjectStoredObjectManagerModelBase * >( sourceModel() ) )
404 const QString leftText = sourceModel()->data( left, Qt::DisplayRole ).toString();
405 const QString rightText = sourceModel()->data( right, Qt::DisplayRole ).toString();
406 if ( leftText.isEmpty() )
408 if ( rightText.isEmpty() )
411 return QString::localeAwareCompare( leftText, rightText ) < 0;
444 T *
object = model->
objectFromIndex( model->index( sourceRow, 0, sourceParent ) );
450 if ( !object->name().contains(
mFilterString, Qt::CaseInsensitive ) )
Template class for storage of a set of objects attached to a QgsProject.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Interface for master layout type objects, such as print layouts and reports.
Print layout, a QgsLayout subclass for static or atlas-based layouts.
void objectAdded(const QString &name)
Emitted when an object has been added to the manager.
void objectRemoved(const QString &name)
Emitted when an object was removed from the manager.
void objectAboutToBeAdded(const QString &name)
Emitted when an object is about to be added to the manager.
void objectAboutToBeRemoved(const QString &name)
Emitted when an object is about to be removed from the manager.
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
@ IsEmptyObject
true if row represents the empty object
Qt::ItemFlags flags(const QModelIndex &index) const override
QgsProjectStoredObjectManagerModelBase(QObject *parent=nullptr)
Constructor for QgsProjectStoredObjectManagerModelBase, with the specified parent object.
int rowCount(const QModelIndex &parent) const override
QVariant data(const QModelIndex &index, int role) const override
Template class for models representing the objects available in a QgsAbstractProjectStoredObjectManag...
void setAllowEmptyObject(bool allowEmpty)
Sets whether an optional empty object ("not set") option is present in the model.
QModelIndex indexFromObject(T *object) const
Returns the model index corresponding to an object.
T * objectFromIndex(const QModelIndex &index) const
Returns the object at the corresponding index.
QgsProjectStoredObjectManagerModel(QgsAbstractProjectStoredObjectManager< T > *manager, QObject *parent=nullptr)
Constructor for QgsProjectStoredObjectManagerModel, showing the objects from the specified manager.
PRIVATE QgsAbstractProjectStoredObjectManager< T > * mObjectManager
Object manager.
bool allowEmptyObject() const
Returns true if the model allows the empty object ("not set") choice.
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
void setFilterString(const QString &filter)
Sets a filter string, such that only layouts with names containing the specified string will be shown...
QString mFilterString
Filter string.
QgsProjectStoredObjectManagerProxyModelBase(QObject *parent=nullptr)
Constructor for QgsProjectStoredObjectManagerProxyModelBase.
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override
virtual bool filterAcceptsRowInternal(int sourceRow, const QModelIndex &sourceParent) const
Returns true if the proxy accepts the matching source row.
Template class QSortFilterProxyModel subclass for QgsProjectStoredObjectManagerModel.
bool filterAcceptsRowInternal(int sourceRow, const QModelIndex &sourceParent) const override
Returns true if the proxy accepts the matching source row.
QgsProjectStoredObjectManagerProxyModel(QObject *parent=nullptr)
Constructor for QgsProjectStoredObjectManagerProxyModel.
Represents a named set of selective masking sources (QgsSelectiveMaskSource).
QString id() const
Returns a unique identifier for the set.