21#include "moc_qgsprojectstoredobjectmanagermodel.cpp"
28 : QAbstractListModel( parent )
35 return rowCountInternal( parent );
40 return dataInternal( index, role );
45 return setDataInternal( index, value, role );
50 return flagsInternal( index );
54void QgsProjectStoredObjectManagerModelBase::objectAboutToBeAdded(
const QString &name )
56 objectAboutToBeAddedInternal( name );
59void QgsProjectStoredObjectManagerModelBase::objectAboutToBeRemoved(
const QString &name )
61 objectAboutToBeRemovedInternal( name );
64void QgsProjectStoredObjectManagerModelBase::objectAdded(
const QString &name )
66 objectAddedInternal( name );
69void QgsProjectStoredObjectManagerModelBase::objectRemoved(
const QString &name )
71 objectRemovedInternal( name );
94 return ( mObjectManager ? mObjectManager->objects().count() : 0 ) + ( mAllowEmpty ? 1 : 0 );
100 if ( index.row() < 0 || index.row() >= rowCount( QModelIndex() ) )
103 const bool isEmpty = index.row() == 0 && mAllowEmpty;
104 const int objectRow = mAllowEmpty ? index.row() - 1 : index.row();
108 case Qt::DisplayRole:
109 case Qt::ToolTipRole:
120 case static_cast< int >( CustomRole::IsEmptyObject ):
125 case Qt::DecorationRole:
127 if ( isEmpty || !mObjectManager )
131 if constexpr(
requires {
object->icon(); } )
133 return object->icon();
149 if ( !index.isValid() || role != Qt::EditRole )
153 if ( index.row() >= mObjectManager->objects().count() )
158 if ( index.row() == 0 && mAllowEmpty )
161 if ( value.toString().isEmpty() )
164 T *
object = objectFromIndex( index );
169 bool changed =
object->name() != value.toString();
174 QStringList existingNames;
175 const QList< T * > objects = mObjectManager->objects();
176 for ( T *l : objects )
178 existingNames << l->name();
180 if ( existingNames.contains( value.toString() ) )
183 QMessageBox::warning(
nullptr, tr(
"Rename Layout" ), tr(
"There is already a layout named “%1”." ).arg( value.toString() ) );
187 object->setName( value.toString() );
194 Qt::ItemFlags flags = QAbstractListModel::flags( index );
196 if ( index.isValid() )
198 return flags | Qt::ItemIsEditable;
211 int row = mObjectManager->objects().count() + ( mAllowEmpty ? 1 : 0 );
212 beginInsertRows( QModelIndex(), row, row );
218 T *l = mObjectManager->objectByName( name );
219 int row = mObjectManager->objects().indexOf( l ) + ( mAllowEmpty ? 1 : 0 );
221 beginRemoveRows( QModelIndex(), row, row );
239 int row = mObjectManager->objects().indexOf(
object ) + ( mAllowEmpty ? 1 : 0 );
240 QModelIndex index = createIndex( row, 0 );
241 emit dataChanged( index, index, QVector<int>() << Qt::DisplayRole );
247 if ( T *l =
dynamic_cast< T *
>(
object ) )
248 return QVariant::fromValue( l );
258 if ( index.row() == 0 && mAllowEmpty )
261 if ( T *l = qobject_cast< T * >( qvariant_cast<QObject *>(
data( index,
static_cast< int >(
CustomRole::Object ) ) ) ) )
272 return QModelIndex();
277 return QModelIndex();
279 QModelIndex idx = index( mAllowEmpty ? r + 1 : r, 0, QModelIndex() );
285 return QModelIndex();
291 if ( allowEmpty == mAllowEmpty )
296 beginInsertRows( QModelIndex(), 0, 0 );
302 beginRemoveRows( QModelIndex(), 0, 0 );
318 return QVariant::fromValue( l );
319 else if ( QgsReport *r =
dynamic_cast< QgsReport *
>(
object ) )
320 return QVariant::fromValue( r );
327 if ( index.row() == 0 && mAllowEmpty )
330 if (
QgsPrintLayout *l = qobject_cast< QgsPrintLayout * >( qvariant_cast<QObject *>( data( index,
static_cast< int >( CustomRole::Object ) ) ) ) )
332 else if ( QgsReport *r = qobject_cast< QgsReport * >( qvariant_cast<QObject *>( data( index,
static_cast< int >( CustomRole::Object ) ) ) ) )
350 return QVariant::fromValue(
object );
357 if ( index.row() == 0 && mAllowEmpty )
360 const QVariant variant = data( index,
static_cast< int >( CustomRole::Object ) );
361 return qvariant_cast< QgsSelectiveMaskingSourceSet *>( variant );
367 if ( !mObjectManager )
369 return QModelIndex();
372 const QList< QgsSelectiveMaskingSourceSet * > objects = mObjectManager->objects();
374 bool foundMatch =
false;
377 if ( set && set->id() == object->
id() )
385 return QModelIndex();
387 QModelIndex idx = index( mAllowEmpty ? r + 1 : r, 0, QModelIndex() );
393 return QModelIndex();
406 : QSortFilterProxyModel( parent )
408 setDynamicSortFilter(
true );
410 setSortCaseSensitivity( Qt::CaseInsensitive );
415 if ( qobject_cast< QgsProjectStoredObjectManagerModelBase * >( sourceModel() ) )
425 const QString leftText = sourceModel()->data( left, Qt::DisplayRole ).toString();
426 const QString rightText = sourceModel()->data( right, Qt::DisplayRole ).toString();
427 if ( leftText.isEmpty() )
429 if ( rightText.isEmpty() )
432 return QString::localeAwareCompare( leftText, rightText ) < 0;
469 T *
object = model->
objectFromIndex( model->index( sourceRow, 0, sourceParent ) );
475 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.