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 );
116 header()->setSectionResizeMode( 0, QHeaderView::Fixed );
117 header()->setSectionResizeMode( 1, QHeaderView::Fixed );
120 header()->setSectionsMovable(
false );
122 connect( selectionModel(), &QItemSelectionModel::selectionChanged,
this, &QgsLayoutItemsListView::updateSelection );
127 if ( event->key() == Qt::Key_Space )
129 const auto constSelectedIndexes = selectionModel()->selectedIndexes();
130 if ( !constSelectedIndexes.isEmpty() )
132 const bool isFirstItemVisible = mModel->itemFromIndex( constSelectedIndexes[0] )->isVisible();
134 for (
const QModelIndex &index : constSelectedIndexes )
138 item->setVisibility( !isFirstItemVisible );
146 QTreeView::keyPressEvent( event );
149void QgsLayoutItemsListView::updateSelection()
154 if ( !mModel || mUpdatingFromView )
158 mUpdatingSelection =
true;
164 QList<QgsLayoutItem *> selectedItems;
165 for (
const QModelIndex &index : selectionModel()->selectedIndexes() )
169 selectedItems << item;
174 bool itemSelected =
false;
177 if ( selectionModel()->isSelected( selectionModel()->currentIndex() ) )
181 QgsLayoutItem *currentItem = mModel->itemFromIndex( selectionModel()->currentIndex() );
182 mLayout->setSelectedItem( currentItem );
185 for ( QgsLayoutItem *item : selectedItems )
191 mLayout->setSelectedItem( item );
196 item->setSelected(
true );
200 QgsLayoutItemGroup *group = item->parentGroup();
205 if ( group && group != item )
209 mUpdatingSelection =
false;
212void QgsLayoutItemsListView::onItemFocused(
QgsLayoutItem *focusedItem )
215 if ( !mModel || mUpdatingSelection )
219 mUpdatingFromView =
true;
225 QModelIndex index = mModel->indexForItem( focusedItem );
226 if ( index.isValid() )
228 setCurrentIndex( index );
232 const QList<QgsLayoutItem *> selectedItems = mLayout->selectedLayoutItems();
233 for ( QgsLayoutItem *item : selectedItems )
235 const QModelIndex firstCol = mModel->indexForItem( item );
236 if ( firstCol.isValid() )
239 QItemSelection selection;
240 selection.select( firstCol, firstCol.siblingAtColumn( mModel->columnCount( firstCol.parent() ) - 1 ) );
241 selectionModel()->select( selection, QItemSelectionModel::Select );
245 mUpdatingFromView =
false;
248void QgsLayoutItemsListView::showContextMenu( QPoint point )
252 const QModelIndex index = indexAt( point );
253 QgsLayoutItem *item = mModel->itemFromIndex( index );
257 QMenu *menu =
new QMenu(
this );
259 QAction *copyAction =
new QAction( tr(
"Copy Item" ), menu );
260 connect( copyAction, &QAction::triggered,
this, [
this, item]() {
263 menu->addAction( copyAction );
264 QAction *deleteAction =
new QAction( tr(
"Delete Item" ), menu );
265 connect( deleteAction, &QAction::triggered,
this, [
this, item]() {
266 mDesigner->view()->deleteItems( QList<QgsLayoutItem *>() << item );
268 menu->addAction( deleteAction );
269 menu->addSeparator();
271 QAction *itemPropertiesAction =
new QAction( tr(
"Item Properties…" ), menu );
272 connect( itemPropertiesAction, &QAction::triggered,
this, [
this, item]() {
273 mDesigner->showItemOptions( item,
true );
275 menu->addAction( itemPropertiesAction );
277 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.
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.