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:
117 return objectToVariant(
mObjectManager->objects().at( objectRow ) );
120 case Qt::DecorationRole:
133 if ( !index.isValid() || role != Qt::EditRole )
137 if ( index.row() >= mObjectManager->objects().count() )
142 if ( index.row() == 0 && mAllowEmpty )
145 if ( value.toString().isEmpty() )
148 T *
object = objectFromIndex( index );
153 bool changed =
object->name() != value.toString();
158 QStringList existingNames;
159 const QList< T * > objects = mObjectManager->objects();
160 for ( T *l : objects )
162 existingNames << l->name();
164 if ( existingNames.contains( value.toString() ) )
167 QMessageBox::warning(
nullptr, tr(
"Rename Layout" ), tr(
"There is already a layout named “%1”." ).arg( value.toString() ) );
171 object->setName( value.toString() );
178 Qt::ItemFlags flags = QAbstractListModel::flags( index );
180 if ( index.isValid() )
182 return flags | Qt::ItemIsEditable;
195 int row = mObjectManager->objects().count() + ( mAllowEmpty ? 1 : 0 );
196 beginInsertRows( QModelIndex(), row, row );
202 T *l = mObjectManager->objectByName( name );
203 int row = mObjectManager->objects().indexOf( l ) + ( mAllowEmpty ? 1 : 0 );
205 beginRemoveRows( QModelIndex(), row, row );
223 int row = mObjectManager->objects().indexOf(
object ) + ( mAllowEmpty ? 1 : 0 );
224 QModelIndex index = createIndex( row, 0 );
225 emit dataChanged( index, index, QVector<int>() << Qt::DisplayRole );
231 if ( T *l =
dynamic_cast< T *
>(
object ) )
232 return QVariant::fromValue( l );
242 if ( index.row() == 0 && mAllowEmpty )
245 if ( T *l = qobject_cast< T * >( qvariant_cast<QObject *>( data( index,
static_cast< int >( CustomRole::Object ) ) ) ) )
256 return QModelIndex();
261 return QModelIndex();
263 QModelIndex idx = index( mAllowEmpty ? r + 1 : r, 0, QModelIndex() );
269 return QModelIndex();
275 if ( allowEmpty == mAllowEmpty )
280 beginInsertRows( QModelIndex(), 0, 0 );
286 beginRemoveRows( QModelIndex(), 0, 0 );
302 return QVariant::fromValue( l );
303 else if ( QgsReport *r =
dynamic_cast< QgsReport *
>(
object ) )
304 return QVariant::fromValue( r );
311 if ( index.row() == 0 && mAllowEmpty )
314 if (
QgsPrintLayout *l = qobject_cast< QgsPrintLayout * >( qvariant_cast<QObject *>( data( index,
static_cast< int >( CustomRole::Object ) ) ) ) )
316 else if ( QgsReport *r = qobject_cast< QgsReport * >( qvariant_cast<QObject *>( data( index,
static_cast< int >( CustomRole::Object ) ) ) ) )
335 : QSortFilterProxyModel( parent )
337 setDynamicSortFilter(
true );
339 setSortCaseSensitivity( Qt::CaseInsensitive );
344 const QString leftText = sourceModel()->data( left, Qt::DisplayRole ).toString();
345 const QString rightText = sourceModel()->data( right, Qt::DisplayRole ).toString();
346 if ( leftText.isEmpty() )
348 if ( rightText.isEmpty() )
351 return QString::localeAwareCompare( leftText, rightText ) < 0;
388 T *
object = model->
objectFromIndex( model->index( sourceRow, 0, sourceParent ) );
394 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
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.