QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
qgslayoutcombobox.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayoutcombobox.cpp
3  --------------------------------------
4  Date : March 2019
5  Copyright : (C) 2019 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 
17 #include "qgslayoutcombobox.h"
18 #include "qgslayoutmodel.h"
19 
21  : QComboBox( parent )
22 {
23  setLayoutManager( manager );
24 
25  connect( this, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutComboBox::indexChanged );
26 }
27 
29 {
30  if ( mModel )
31  mModel->deleteLater();
32  if ( mProxyModel )
33  mProxyModel->deleteLater();
34 
35  mModel = new QgsLayoutManagerModel( manager, this );
36  mProxyModel = new QgsLayoutManagerProxyModel( this );
37  mProxyModel->setSourceModel( mModel );
38 
39  connect( mProxyModel, &QAbstractItemModel::rowsInserted, this, &QgsLayoutComboBox::rowsChanged );
40  connect( mProxyModel, &QAbstractItemModel::rowsRemoved, this, &QgsLayoutComboBox::rowsChanged );
41  setModel( mProxyModel );
42  mProxyModel->sort( 0, Qt::AscendingOrder );
43 }
44 
45 QgsLayoutManagerProxyModel::Filters QgsLayoutComboBox::filters() const
46 {
47  return mProxyModel->filters();
48 }
49 
50 void QgsLayoutComboBox::setFilters( QgsLayoutManagerProxyModel::Filters filters )
51 {
52  mProxyModel->setFilters( filters );
53 }
54 
56 {
57  mModel->setAllowEmptyLayout( allowEmpty );
58 }
59 
61 {
62  return mModel->allowEmptyLayout();
63 }
64 
66 {
67  if ( !mModel )
68  return;
69 
70  QModelIndex idx = mModel->indexFromLayout( layout );
71  if ( idx.isValid() )
72  {
73  QModelIndex proxyIdx = mProxyModel->mapFromSource( idx );
74  if ( proxyIdx.isValid() )
75  {
76  setCurrentIndex( proxyIdx.row() );
77  return;
78  }
79  }
80  setCurrentIndex( allowEmptyLayout() ? 0 : -1 );
81 }
82 
84 {
85  return layout( currentIndex() );
86 }
87 
88 void QgsLayoutComboBox::indexChanged( int i )
89 {
90  Q_UNUSED( i )
91  emit layoutChanged( currentLayout() );
92 }
93 
94 void QgsLayoutComboBox::rowsChanged()
95 {
96  if ( count() == 1 )
97  {
98  //currently selected item has changed
99  emit layoutChanged( currentLayout() );
100  }
101  else if ( count() == 0 )
102  {
103  emit layoutChanged( nullptr );
104  }
105 }
106 
108 {
109  const QModelIndex proxyIndex = mProxyModel->index( index, 0 );
110  if ( !proxyIndex.isValid() )
111  {
112  return nullptr;
113  }
114 
115  QModelIndex sourceIndex = mProxyModel->mapToSource( proxyIndex );
116  if ( !sourceIndex.isValid() )
117  {
118  return nullptr;
119  }
120 
121  return mModel->layoutFromIndex( sourceIndex );
122 }
QModelIndex indexFromLayout(QgsMasterLayoutInterface *layout) const
Returns the model index corresponding to a layout.
void layoutChanged(QgsMasterLayoutInterface *layout)
Emitted whenever the currently selected layout changes.
bool allowEmptyLayout() const
Returns true if the model allows the empty layout ("not set") choice.
QgsLayoutManagerProxyModel::Filters filters() const
Returns the current filters used for filtering available layouts.
bool allowEmptyLayout() const
Returns true if the combobox includes the empty layout ("not set") choice.
QgsLayoutManagerProxyModel::Filters filters() const
Returns the current filters used for filtering available layouts.
void setFilters(QgsLayoutManagerProxyModel::Filters filters)
Sets the current filters used for filtering available layouts.
void setAllowEmptyLayout(bool allowEmpty)
Sets whether an optional empty layout ("not set") option is present in the model. ...
QSortFilterProxyModel subclass for QgsLayoutManagerModel.
void setCurrentLayout(QgsMasterLayoutInterface *layout)
Sets the currently selected layout in the combo box.
QgsLayoutComboBox(QWidget *parent=nullptr, QgsLayoutManager *manager=nullptr)
QgsLayoutComboBox creates a combo box to display a list of items in a layout manager.
void setAllowEmptyLayout(bool allowEmpty)
Sets whether an optional empty layout ("not set") option is present in the combobox.
List model representing the print layouts and reports available in a layout manager.
void setLayoutManager(QgsLayoutManager *manager)
Sets the layout manager containing the layouts to list in the combo box.
void setFilters(QgsLayoutManagerProxyModel::Filters filters)
Sets the current filters used for filtering available layouts.
Manages storage of a set of layouts.
QgsMasterLayoutInterface * currentLayout() const
Returns the layout currently selected in the combo box.
QgsMasterLayoutInterface * layout(int index) const
Returns the layout at the specified index.
Interface for master layout type objects, such as print layouts and reports.
QgsMasterLayoutInterface * layoutFromIndex(const QModelIndex &index) const
Returns the layout at the corresponding index.