QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgscomposeritemcombobox.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscomposeritemcombobox.cpp
3  --------------------------------------
4  Date : August 2014
5  Copyright : (C) 2014 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 
17 #include "qgscomposermodel.h"
18 
20  : QComboBox( parent )
21  , mProxyModel( nullptr )
22 {
23  setComposition( composition );
24 
26  connect( this, SIGNAL( currentIndexChanged( int ) ), this, SLOT( indexChanged( int ) ) );
27  connect( mProxyModel, SIGNAL( rowsInserted( QModelIndex, int, int ) ), this, SLOT( rowsChanged() ) );
28  connect( mProxyModel, SIGNAL( rowsRemoved( QModelIndex, int, int ) ), this, SLOT( rowsChanged() ) );
29 }
30 
32 {
33  delete mProxyModel;
34  mProxyModel = new QgsComposerProxyModel( composition, this );
35  connect( mProxyModel, SIGNAL( rowsInserted( QModelIndex, int, int ) ), this, SLOT( rowsChanged() ) );
36  connect( mProxyModel, SIGNAL( rowsRemoved( QModelIndex, int, int ) ), this, SLOT( rowsChanged() ) );
37  setModel( mProxyModel );
39 }
40 
42 {
43  if ( !mProxyModel->sourceLayerModel() )
44  return;
45 
46  QModelIndex idx = mProxyModel->sourceLayerModel()->indexForItem( const_cast< QgsComposerItem* >( item ) );
47  if ( idx.isValid() )
48  {
49  QModelIndex proxyIdx = mProxyModel->mapFromSource( idx );
50  if ( proxyIdx.isValid() )
51  {
52  setCurrentIndex( proxyIdx.row() );
53  emit itemChanged( currentItem() );
54  return;
55  }
56  }
57  setCurrentIndex( -1 );
58  emit itemChanged( currentItem() );
59 }
60 
62 {
63  return item( currentIndex() );
64 }
65 
66 void QgsComposerItemComboBox::indexChanged( int i )
67 {
68  Q_UNUSED( i );
69  emit itemChanged( currentItem() );
70 }
71 
72 void QgsComposerItemComboBox::rowsChanged()
73 {
74  if ( count() == 1 )
75  {
76  //currently selected item has changed
77  emit itemChanged( currentItem() );
78  }
79  else if ( count() == 0 )
80  {
81  emit itemChanged( nullptr );
82  }
83 }
84 
86 {
87  mProxyModel->setFilterType( itemType );
88 }
89 
91 {
92  return mProxyModel->filterType();
93 }
94 
96 {
97  mProxyModel->setExceptedItemList( exceptList );
98 }
99 
101 {
102  return mProxyModel->exceptedItemList();
103 }
105 {
106  const QModelIndex proxyIndex = mProxyModel->index( index, 0 );
107  if ( !proxyIndex.isValid() )
108  {
109  return nullptr;
110  }
111 
112  QModelIndex sourceIndex = mProxyModel->mapToSource( proxyIndex );
113  if ( !sourceIndex.isValid() )
114  {
115  return nullptr;
116  }
117 
118  return mProxyModel->itemFromSourceIndex( sourceIndex );
119 }
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
void setItem(const QgsComposerItem *item)
Sets the currently selected item in the combo box.
static unsigned index
QgsComposerItemComboBox(QWidget *parent=nullptr, QgsComposition *composition=nullptr)
QgsComposerItemComboBox creates a combo box to display a list of items in a composition.
void setExceptedItemList(const QList< QgsComposerItem * > &exceptList)
Sets a list of specific items to exclude from the combo box.
void setComposition(QgsComposition *composition)
Sets the composition containing the items to list in the combo box.
A item that forms part of a map composition.
QModelIndex indexForItem(QgsComposerItem *item, const int column=0)
Returns the QModelIndex corresponding to a QgsComposerItem, if possible.
QList< QgsComposerItem *> exceptedItemList() const
Returns the list of specific items excluded from the combo box.
QgsComposerItem * item(int index) const
Return the item currently shown at the specified index within the combo box.
void setExceptedItemList(const QList< QgsComposerItem * > &exceptList)
Sets a list of specific items to exclude from the model.
QgsComposerItem * currentItem() const
Returns the item currently selected in the combo box.
bool isValid() const
int count() const
QgsComposerItem::ItemType filterType() const
Returns the current item type filter, or QgsComposerItem::ComposerItem if no item type filter is set...
void setModelColumn(int visibleColumn)
int row() const
QList< QgsComposerItem *> exceptedItemList() const
Returns the list of specific items excluded from the model.
QgsComposerModel * sourceLayerModel() const
Returns the QgsComposerModel used in this proxy model.
Graphics scene for map printing.
void setItemType(QgsComposerItem::ItemType itemType)
Sets a filter for the item type to show in the combo box.
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const
void setModel(QAbstractItemModel *model)
QgsComposerItem::ItemType itemType() const
Returns the filter for the item types to show in the combo box.
virtual QModelIndex mapFromSource(const QModelIndex &sourceIndex) const
void setCurrentIndex(int index)
void itemChanged(QgsComposerItem *item)
Emitted whenever the currently selected item changes.
Allows for filtering a QgsComposerModel by item type.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setFilterType(QgsComposerItem::ItemType itemType)
Sets the item type filter.
void currentIndexChanged(int index)
QgsComposerItem * itemFromSourceIndex(const QModelIndex &sourceIndex) const
Returns the QgsComposerItem corresponding to an index from the source QgsComposerModel model...