QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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
19#include "qgslayoutmodel.h"
20
21#include "moc_qgslayoutcombobox.cpp"
22
24 : QComboBox( parent )
25{
26 setLayoutManager( manager );
27
28 connect( this, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutComboBox::indexChanged );
29}
30
32{
33 if ( mModel )
34 mModel->deleteLater();
35 if ( mProxyModel )
36 mProxyModel->deleteLater();
37
38 mModel = new QgsLayoutManagerModel( manager, this );
39 mProxyModel = new QgsLayoutManagerProxyModel( this );
40 mProxyModel->setSourceModel( mModel );
41
42 connect( mProxyModel, &QAbstractItemModel::rowsInserted, this, &QgsLayoutComboBox::rowsChanged );
43 connect( mProxyModel, &QAbstractItemModel::rowsRemoved, this, &QgsLayoutComboBox::rowsChanged );
44 setModel( mProxyModel );
45 mProxyModel->sort( 0, Qt::AscendingOrder );
46}
47
49{
50 return mProxyModel->filters();
51}
52
57
59{
60 mModel->setAllowEmptyLayout( allowEmpty );
61}
62
64{
65 return mModel->allowEmptyLayout();
66}
67
69{
70 if ( !mModel )
71 return;
72
73 const QModelIndex idx = mModel->indexFromLayout( layout );
74 if ( idx.isValid() )
75 {
76 const QModelIndex proxyIdx = mProxyModel->mapFromSource( idx );
77 if ( proxyIdx.isValid() )
78 {
79 setCurrentIndex( proxyIdx.row() );
80 return;
81 }
82 }
83 setCurrentIndex( allowEmptyLayout() ? 0 : -1 );
84}
85
87{
88 return layout( currentIndex() );
89}
90
91void QgsLayoutComboBox::indexChanged( int i )
92{
93 Q_UNUSED( i )
95}
96
97void QgsLayoutComboBox::rowsChanged()
98{
99 if ( count() == 1 )
100 {
101 //currently selected item has changed
103 }
104 else if ( count() == 0 )
105 {
106 emit layoutChanged( nullptr );
107 }
108}
109
111{
112 const QModelIndex proxyIndex = mProxyModel->index( index, 0 );
113 if ( !proxyIndex.isValid() )
114 {
115 return nullptr;
116 }
117
118 const QModelIndex sourceIndex = mProxyModel->mapToSource( proxyIndex );
119 if ( !sourceIndex.isValid() )
120 {
121 return nullptr;
122 }
123
124 return mModel->layoutFromIndex( sourceIndex );
125}
bool allowEmptyLayout() const
Returns true if the combobox includes the empty layout ("not set") choice.
void setLayoutManager(QgsLayoutManager *manager)
Sets the layout manager containing the layouts to list in the combo box.
void layoutChanged(QgsMasterLayoutInterface *layout)
Emitted whenever the currently selected layout changes.
QgsMasterLayoutInterface * currentLayout() const
Returns the layout currently selected 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 setCurrentLayout(QgsMasterLayoutInterface *layout)
Sets the currently selected layout in the combo box.
void setAllowEmptyLayout(bool allowEmpty)
Sets whether an optional empty layout ("not set") option is present in the combobox.
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.
QgsMasterLayoutInterface * layout(int index) const
Returns the layout at the specified index.
List model representing the print layouts and reports available in a layout manager.
QSortFilterProxyModel subclass for QgsLayoutManagerModel.
Manages storage of a set of layouts.
Interface for master layout type objects, such as print layouts and reports.