QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgslayoutitemslistview.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayoutitemslistview.cpp
3  --------------------------
4  Date : October 2017
5  Copyright : (C) 2017 Nyall Dawson
6  Email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgslayoutitemslistview.h"
17 #include "qgslayout.h"
18 #include "qgslayoutmodel.h"
20 #include "qgslayoutview.h"
21 #include <QHeaderView>
22 #include <QMenu>
23 #include <QMouseEvent>
24 
25 
27  : QSortFilterProxyModel( parent )
28  , mModel( model )
29 {
30  setSourceModel( mModel );
31 }
32 
34 {
35  return mModel->itemFromIndex( mapToSource( index ) );
36 }
37 
38 void QgsLayoutItemsListViewModel::setSelected( const QModelIndex &index )
39 {
40  mModel->setSelected( mapToSource( index ) );
41 }
42 
43 bool QgsLayoutItemsListViewModel::filterAcceptsRow( int sourceRow, const QModelIndex & ) const
44 {
45  if ( sourceRow == 0 )
46  return false; // hide empty null item row
47  return true;
48 }
49 
50 QVariant QgsLayoutItemsListViewModel::data( const QModelIndex &index, int role ) const
51 {
52  if ( !index.isValid() )
53  return QVariant();
54 
55  QgsLayoutItem *item = itemFromIndex( index );
56  if ( !item )
57  {
58  return QVariant();
59  }
60 
61  if ( role == Qt::FontRole )
62  {
63  if ( index.column() == QgsLayoutModel::ItemId && item->isSelected() )
64  {
65  //draw name of selected items in bold
66  QFont boldFont;
67  boldFont.setBold( true );
68  return boldFont;
69  }
70  }
71 
72  return QSortFilterProxyModel::data( index, role );
73 }
74 
75 
76 //
77 // QgsLayoutItemsListView
78 //
79 
81  : QTreeView( parent )
82  , mDesigner( designer )
83 {
84  setColumnWidth( 0, 30 );
85  setColumnWidth( 1, 30 );
86  setDragEnabled( true );
87  setAcceptDrops( true );
88  setDropIndicatorShown( true );
89  setDragDropMode( QAbstractItemView::InternalMove );
90  setContextMenuPolicy( Qt::CustomContextMenu );
91  setIndentation( 0 );
92  connect( this, &QWidget::customContextMenuRequested, this, &QgsLayoutItemsListView::showContextMenu );
93 }
94 
96 {
97  mLayout = layout;
98  mModel = new QgsLayoutItemsListViewModel( layout->itemsModel(), this );
99  setModel( mModel );
100 
101  header()->setSectionResizeMode( 0, QHeaderView::Fixed );
102  header()->setSectionResizeMode( 1, QHeaderView::Fixed );
103 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
104  setColumnWidth( 0, Qgis::UI_SCALE_FACTOR * fontMetrics().width( QStringLiteral( "xxxx" ) ) );
105  setColumnWidth( 1, Qgis::UI_SCALE_FACTOR * fontMetrics().width( QStringLiteral( "xxxx" ) ) );
106 #else
107  setColumnWidth( 0, Qgis::UI_SCALE_FACTOR * fontMetrics().horizontalAdvance( 'x' ) * 4 );
108  setColumnWidth( 1, Qgis::UI_SCALE_FACTOR * fontMetrics().horizontalAdvance( 'x' ) * 4 );
109 #endif
110  header()->setSectionsMovable( false );
111 
112  connect( selectionModel(), &QItemSelectionModel::currentChanged, mModel, &QgsLayoutItemsListViewModel::setSelected );
113 }
114 
115 void QgsLayoutItemsListView::showContextMenu( QPoint point )
116 {
117  if ( !mModel )
118  return;
119  QModelIndex index = indexAt( point );
120  QgsLayoutItem *item = mModel->itemFromIndex( index );
121  if ( !item )
122  return;
123 
124  QMenu *menu = new QMenu( this );
125 
126  QAction *copyAction = new QAction( tr( "Copy Item" ), menu );
127  connect( copyAction, &QAction::triggered, this, [this, item]()
128  {
129  mDesigner->view()->copyItems( QList< QgsLayoutItem * >() << item, QgsLayoutView::ClipboardCopy );
130  } );
131  menu->addAction( copyAction );
132  QAction *deleteAction = new QAction( tr( "Delete Item" ), menu );
133  connect( deleteAction, &QAction::triggered, this, [this, item]()
134  {
135  mDesigner->view()->deleteItems( QList< QgsLayoutItem * >() << item );
136  } );
137  menu->addAction( deleteAction );
138  menu->addSeparator();
139 
140  QAction *itemPropertiesAction = new QAction( tr( "Item Properties…" ), menu );
141  connect( itemPropertiesAction, &QAction::triggered, this, [this, item]()
142  {
143  mDesigner->showItemOptions( item, true );
144  } );
145  menu->addAction( itemPropertiesAction );
146 
147  menu->popup( mapToGlobal( point ) );
148 }
QgsLayoutDesignerInterface::view
virtual QgsLayoutView * view()=0
Returns the layout view utilized by the designer.
qgslayoutview.h
QgsLayoutItemsListViewModel::data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Definition: qgslayoutitemslistview.cpp:50
QgsLayoutView::ClipboardCopy
@ ClipboardCopy
Copy items.
Definition: qgslayoutview.h:72
QgsLayoutItemsListViewModel::QgsLayoutItemsListViewModel
QgsLayoutItemsListViewModel(QgsLayoutModel *model, QObject *parent)
constructor
Definition: qgslayoutitemslistview.cpp:26
QgsLayoutItemsListViewModel
Model for the layout items list view.
Definition: qgslayoutitemslistview.h:41
QgsLayoutItemsListViewModel::filterAcceptsRow
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
Definition: qgslayoutitemslistview.cpp:43
QgsLayoutItemsListView::setCurrentLayout
void setCurrentLayout(QgsLayout *layout)
Sets the current layout.
Definition: qgslayoutitemslistview.cpp:95
QgsLayoutModel
A model for items attached to a layout.
Definition: qgslayoutmodel.h:53
QgsLayoutItemsListViewModel::itemFromIndex
QgsLayoutItem * itemFromIndex(const QModelIndex &index) const
Returns the layout item listed at the specified index.
Definition: qgslayoutitemslistview.cpp:33
QgsLayoutModel::ItemId
@ ItemId
Item ID.
Definition: qgslayoutmodel.h:63
Qgis::UI_SCALE_FACTOR
static const double UI_SCALE_FACTOR
UI scaling factor.
Definition: qgis.h:182
QgsLayoutItem
Base class for graphical items within a QgsLayout.
Definition: qgslayoutitem.h:113
qgslayout.h
QgsLayoutItemsListView::QgsLayoutItemsListView
QgsLayoutItemsListView(QWidget *parent, QgsLayoutDesignerInterface *designer)
Constructor for QgsLayoutItemsListView.
Definition: qgslayoutitemslistview.cpp:80
QgsLayout
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:50
QgsLayoutDesignerInterface
A common interface for layout designer dialogs and widgets.
Definition: qgslayoutdesignerinterface.h:55
QgsLayoutItemsListViewModel::setSelected
void setSelected(const QModelIndex &index)
Sets the selected index.
Definition: qgslayoutitemslistview.cpp:38
qgslayoutitemslistview.h
qgslayoutdesignerinterface.h
QgsLayoutModel::itemFromIndex
QgsLayoutItem * itemFromIndex(const QModelIndex &index) const
Returns the QgsLayoutItem corresponding to a QModelIndex index, if possible.
Definition: qgslayoutmodel.cpp:39
QgsLayoutDesignerInterface::showItemOptions
virtual void showItemOptions(QgsLayoutItem *item, bool bringPanelToFront=true)=0
Shows the configuration widget for the specified layout item.
qgslayoutmodel.h
QgsLayout::itemsModel
QgsLayoutModel * itemsModel()
Returns the items model attached to the layout.
Definition: qgslayout.cpp:137