QGIS API Documentation 3.41.0-Master (d5b93354e9c)
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#include "moc_qgslayoutcombobox.cpp"
19#include "qgslayoutmodel.h"
20
22 : QComboBox( parent )
23{
24 setLayoutManager( manager );
25
26 connect( this, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutComboBox::indexChanged );
27}
28
30{
31 if ( mModel )
32 mModel->deleteLater();
33 if ( mProxyModel )
34 mProxyModel->deleteLater();
35
36 mModel = new QgsLayoutManagerModel( manager, this );
37 mProxyModel = new QgsLayoutManagerProxyModel( this );
38 mProxyModel->setSourceModel( mModel );
39
40 connect( mProxyModel, &QAbstractItemModel::rowsInserted, this, &QgsLayoutComboBox::rowsChanged );
41 connect( mProxyModel, &QAbstractItemModel::rowsRemoved, this, &QgsLayoutComboBox::rowsChanged );
42 setModel( mProxyModel );
43 mProxyModel->sort( 0, Qt::AscendingOrder );
44}
45
50
55
57{
58 mModel->setAllowEmptyLayout( allowEmpty );
59}
60
62{
63 return mModel->allowEmptyLayout();
64}
65
67{
68 if ( !mModel )
69 return;
70
71 const QModelIndex idx = mModel->indexFromLayout( layout );
72 if ( idx.isValid() )
73 {
74 const QModelIndex proxyIdx = mProxyModel->mapFromSource( idx );
75 if ( proxyIdx.isValid() )
76 {
77 setCurrentIndex( proxyIdx.row() );
78 return;
79 }
80 }
81 setCurrentIndex( allowEmptyLayout() ? 0 : -1 );
82}
83
85{
86 return layout( currentIndex() );
87}
88
89void QgsLayoutComboBox::indexChanged( int i )
90{
91 Q_UNUSED( i )
93}
94
95void QgsLayoutComboBox::rowsChanged()
96{
97 if ( count() == 1 )
98 {
99 //currently selected item has changed
101 }
102 else if ( count() == 0 )
103 {
104 emit layoutChanged( nullptr );
105 }
106}
107
109{
110 const QModelIndex proxyIndex = mProxyModel->index( index, 0 );
111 if ( !proxyIndex.isValid() )
112 {
113 return nullptr;
114 }
115
116 const QModelIndex sourceIndex = mProxyModel->mapToSource( proxyIndex );
117 if ( !sourceIndex.isValid() )
118 {
119 return nullptr;
120 }
121
122 return mModel->layoutFromIndex( sourceIndex );
123}
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.
QModelIndex indexFromLayout(QgsMasterLayoutInterface *layout) const
Returns the model index corresponding to a layout.
QgsMasterLayoutInterface * layoutFromIndex(const QModelIndex &index) const
Returns the layout at the corresponding index.
void setAllowEmptyLayout(bool allowEmpty)
Sets whether an optional empty layout ("not set") option is present in the model.
bool allowEmptyLayout() const
Returns true if the model allows the empty layout ("not set") choice.
QSortFilterProxyModel subclass for QgsLayoutManagerModel.
void setFilters(QgsLayoutManagerProxyModel::Filters filters)
Sets the current filters used for filtering available layouts.
QgsLayoutManagerProxyModel::Filters filters() const
Returns the current filters used for filtering available layouts.
Manages storage of a set of layouts.
Interface for master layout type objects, such as print layouts and reports.