QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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  const QModelIndex idx = mModel->indexFromLayout( layout );
71  if ( idx.isValid() )
72  {
73  const 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  const QModelIndex sourceIndex = mProxyModel->mapToSource( proxyIndex );
116  if ( !sourceIndex.isValid() )
117  {
118  return nullptr;
119  }
120 
121  return mModel->layoutFromIndex( sourceIndex );
122 }
QgsLayoutManager
Manages storage of a set of layouts.
Definition: qgslayoutmanager.h:44
QgsLayoutComboBox::setAllowEmptyLayout
void setAllowEmptyLayout(bool allowEmpty)
Sets whether an optional empty layout ("not set") option is present in the combobox.
Definition: qgslayoutcombobox.cpp:55
QgsLayoutComboBox::filters
QgsLayoutManagerProxyModel::Filters filters() const
Returns the current filters used for filtering available layouts.
Definition: qgslayoutcombobox.cpp:45
QgsLayoutManagerModel::allowEmptyLayout
bool allowEmptyLayout() const
Returns true if the model allows the empty layout ("not set") choice.
Definition: qgslayoutmanager.h:216
QgsLayoutComboBox::setFilters
void setFilters(QgsLayoutManagerProxyModel::Filters filters)
Sets the current filters used for filtering available layouts.
Definition: qgslayoutcombobox.cpp:50
QgsLayoutComboBox::allowEmptyLayout
bool allowEmptyLayout() const
Returns true if the combobox includes the empty layout ("not set") choice.
Definition: qgslayoutcombobox.cpp:60
QgsLayoutManagerModel::layoutFromIndex
QgsMasterLayoutInterface * layoutFromIndex(const QModelIndex &index) const
Returns the layout at the corresponding index.
Definition: qgslayoutmanager.cpp:451
QgsLayoutComboBox::QgsLayoutComboBox
QgsLayoutComboBox(QWidget *parent=nullptr, QgsLayoutManager *manager=nullptr)
QgsLayoutComboBox creates a combo box to display a list of items in a layout manager.
Definition: qgslayoutcombobox.cpp:20
QgsLayoutManagerModel
List model representing the print layouts and reports available in a layout manager.
Definition: qgslayoutmanager.h:172
QgsLayoutManagerProxyModel::filters
QgsLayoutManagerProxyModel::Filters filters() const
Returns the current filters used for filtering available layouts.
Definition: qgslayoutmanager.cpp:584
QgsLayoutComboBox::layout
QgsMasterLayoutInterface * layout(int index) const
Returns the layout at the specified index.
Definition: qgslayoutcombobox.cpp:107
qgslayoutcombobox.h
QgsLayoutManagerModel::indexFromLayout
QModelIndex indexFromLayout(QgsMasterLayoutInterface *layout) const
Returns the model index corresponding to a layout.
Definition: qgslayoutmanager.cpp:464
QgsLayoutComboBox::setCurrentLayout
void setCurrentLayout(QgsMasterLayoutInterface *layout)
Sets the currently selected layout in the combo box.
Definition: qgslayoutcombobox.cpp:65
QgsLayoutManagerModel::setAllowEmptyLayout
void setAllowEmptyLayout(bool allowEmpty)
Sets whether an optional empty layout ("not set") option is present in the model.
Definition: qgslayoutmanager.cpp:484
QgsLayoutComboBox::layoutChanged
void layoutChanged(QgsMasterLayoutInterface *layout)
Emitted whenever the currently selected layout changes.
QgsLayoutManagerProxyModel
QSortFilterProxyModel subclass for QgsLayoutManagerModel.
Definition: qgslayoutmanager.h:238
QgsLayoutComboBox::setLayoutManager
void setLayoutManager(QgsLayoutManager *manager)
Sets the layout manager containing the layouts to list in the combo box.
Definition: qgslayoutcombobox.cpp:28
QgsLayoutManagerProxyModel::setFilters
void setFilters(QgsLayoutManagerProxyModel::Filters filters)
Sets the current filters used for filtering available layouts.
Definition: qgslayoutmanager.cpp:589
QgsMasterLayoutInterface
Interface for master layout type objects, such as print layouts and reports.
Definition: qgsmasterlayoutinterface.h:42
QgsLayoutComboBox::currentLayout
QgsMasterLayoutInterface * currentLayout() const
Returns the layout currently selected in the combo box.
Definition: qgslayoutcombobox.cpp:83
qgslayoutmodel.h