28#include "moc_qgslayoutitemslistview.cpp"
31 : QSortFilterProxyModel( parent )
34 setSourceModel( mModel );
39 return mModel->itemFromIndex( mapToSource( index ) );
44 return mapFromSource( mModel->indexForItem( item, column ) );
49 mModel->setSelected( mapToSource( index ) );
61 if ( !index.isValid() )
70 if ( role == Qt::FontRole )
76 boldFont.setBold(
true );
81 return QSortFilterProxyModel::data( index, role );
91 , mDesigner( designer )
93 setColumnWidth( 0, 30 );
94 setColumnWidth( 1, 30 );
95 setDragEnabled(
true );
96 setAcceptDrops(
true );
97 setDropIndicatorShown(
true );
98 setDragDropMode( QAbstractItemView::InternalMove );
99 setContextMenuPolicy( Qt::CustomContextMenu );
103 setSelectionMode( QAbstractItemView::ExtendedSelection );
104 setSelectionBehavior( QAbstractItemView::SelectRows );
106 connect(
this, &QWidget::customContextMenuRequested,
this, &QgsLayoutItemsListView::showContextMenu );
109 setLayoutView( mDesigner->view() );
119 header()->setSectionResizeMode( 0, QHeaderView::Fixed );
120 header()->setSectionResizeMode( 1, QHeaderView::Fixed );
123 header()->setSectionsMovable(
false );
125 connect( selectionModel(), &QItemSelectionModel::selectionChanged,
this, &QgsLayoutItemsListView::updateSelection );
130 if ( event->key() == Qt::Key_Space )
132 const auto constSelectedIndexes = selectionModel()->selectedIndexes();
133 if ( !constSelectedIndexes.isEmpty() )
135 const bool isFirstItemVisible = mModel->itemFromIndex( constSelectedIndexes[0] )->isVisible();
137 for (
const QModelIndex &index : constSelectedIndexes )
141 item->setVisibility( !isFirstItemVisible );
149 QTreeView::keyPressEvent( event );
152void QgsLayoutItemsListView::setLayoutView(
QgsLayoutView *view )
157void QgsLayoutItemsListView::updateSelection(
const QItemSelection &selected,
const QItemSelection &deselected )
159 QSet< QgsLayoutItem * > selectedItems;
160 QSet< QgsLayoutItem * > deselectedItems;
167 for (
const QModelIndex &index : selected.indexes() )
169 if ( QgsLayoutItem *item = mModel->itemFromIndex( index ) )
171 selectedItems << item;
174 for (
const QModelIndex &index : deselected.indexes() )
176 if ( QgsLayoutItem *item = mModel->itemFromIndex( index ) )
178 deselectedItems << item;
181 if ( selectedItems == deselectedItems )
189 if ( !mModel || mUpdatingFromView )
193 mUpdatingSelection =
true;
198 bool itemSelected =
false;
201 if ( selectionModel()->isSelected( selectionModel()->currentIndex() ) )
205 QgsLayoutItem *currentItem = mModel->itemFromIndex( selectionModel()->currentIndex() );
206 mLayout->setSelectedItem( currentItem );
209 for ( QgsLayoutItem *item : selectedItems )
215 mLayout->setSelectedItem( item );
220 item->setSelected(
true );
224 QgsLayoutItemGroup *group = item->parentGroup();
229 if ( group && group != item )
233 mUpdatingSelection =
false;
236void QgsLayoutItemsListView::onItemFocused(
QgsLayoutItem *focusedItem )
239 if ( !mModel || mUpdatingSelection )
243 mUpdatingFromView =
true;
249 QModelIndex index = mModel->indexForItem( focusedItem );
250 if ( index.isValid() )
252 setCurrentIndex( index );
256 const QList<QgsLayoutItem *> selectedItems = mLayout->selectedLayoutItems();
257 for ( QgsLayoutItem *item : selectedItems )
259 const QModelIndex firstCol = mModel->indexForItem( item );
260 if ( firstCol.isValid() )
263 QItemSelection selection;
264 selection.select( firstCol, firstCol.siblingAtColumn( mModel->columnCount( firstCol.parent() ) - 1 ) );
265 selectionModel()->select( selection, QItemSelectionModel::Select );
269 mUpdatingFromView =
false;
272void QgsLayoutItemsListView::showContextMenu( QPoint point )
274 if ( !mModel || !mDesigner )
276 const QModelIndex index = indexAt( point );
277 QgsLayoutItem *item = mModel->itemFromIndex( index );
281 QMenu *menu =
new QMenu(
this );
283 QAction *copyAction =
new QAction( tr(
"Copy Item" ), menu );
284 connect( copyAction, &QAction::triggered,
this, [
this, item]() { mDesigner->view()->copyItems( QList<QgsLayoutItem *>() << item,
QgsLayoutView::ClipboardCopy ); } );
285 menu->addAction( copyAction );
286 QAction *deleteAction =
new QAction( tr(
"Delete Item" ), menu );
287 connect( deleteAction, &QAction::triggered,
this, [
this, item]() { mDesigner->view()->deleteItems( QList<QgsLayoutItem *>() << item ); } );
288 menu->addAction( deleteAction );
289 menu->addSeparator();
291 QAction *itemPropertiesAction =
new QAction( tr(
"Item Properties…" ), menu );
292 connect( itemPropertiesAction, &QAction::triggered,
this, [
this, item]() { mDesigner->showItemOptions( item,
true ); } );
293 menu->addAction( itemPropertiesAction );
295 menu->popup( mapToGlobal( point ) );
static const double UI_SCALE_FACTOR
UI scaling factor.
A common interface for layout designer dialogs and widgets.
Base class for graphical items within a QgsLayout.
QgsLayoutItemGroup * parentGroup() const
Returns the item's parent group, if the item is part of a QgsLayoutItemGroup group.
virtual void setSelected(bool selected)
Sets whether the item should be selected.
Model for the layout items list view.
QgsLayoutItem * itemFromIndex(const QModelIndex &index) const
Returns the layout item listed at the specified index.
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
QgsLayoutItemsListViewModel(QgsLayoutModel *model, QObject *parent)
constructor
void setSelected(const QModelIndex &index)
Sets the selected index.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
QModelIndex indexForItem(QgsLayoutItem *item, const int column=0) const
Returns the model index matching the specified layout item.
void setCurrentLayout(QgsLayout *layout)
Sets the current layout.
QgsLayoutItemsListView(QWidget *parent, QgsLayoutDesignerInterface *designer)
Constructor for QgsLayoutItemsListView.
void keyPressEvent(QKeyEvent *event) override
A model for items attached to a layout.
A graphical widget to display and interact with QgsLayouts.
void itemFocused(QgsLayoutItem *item)
Emitted when an item is "focused" in the view, i.e.
@ ClipboardCopy
Copy items.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
QgsLayoutModel * itemsModel()
Returns the items model attached to the layout.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.