QGIS API Documentation  3.2.0-Bonn (bc43194)
qgsaggregatetoolbutton.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsaggregatetoolbutton.cpp
3  --------------------------------------
4  Date : Nov 2017
5  Copyright : (C) 2017 Matthias Kuhn
6  Email : [email protected]
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 #include "qgsaggregatetoolbutton.h"
17 #include "qgsaggregatecalculator.h"
18 
19 #include <QMenu>
20 
22 {
23  setFocusPolicy( Qt::StrongFocus );
24  setPopupMode( QToolButton::InstantPopup );
25 
26  mMenu = new QMenu( this );
27  connect( mMenu, &QMenu::aboutToShow, this, &QgsAggregateToolButton::aboutToShowMenu );
28  setMenu( mMenu );
29 
30  setText( tr( "Exclude" ) );
31 }
32 
33 void QgsAggregateToolButton::setType( QVariant::Type type )
34 {
35  if ( mType == type )
36  return;
37 
38  mType = type;
39  updateAvailableAggregates();
40 }
41 
42 void QgsAggregateToolButton::aboutToShowMenu()
43 {
44  mMenu->clear();
45 
46  QAction *action = mMenu->addAction( tr( "Exclude" ) );
47  connect( action, &QAction::triggered, this, [ this ]
48  {
49  setActive( false );
50  } );
51 
52  for ( const auto &aggregate : qgis::as_const( mAvailableAggregates ) )
53  {
54  QAction *action = mMenu->addAction( aggregate.name );
55  connect( action, &QAction::triggered, this, [ this, aggregate ]
56  {
57  setText( aggregate.name );
58  setAggregate( aggregate.function );
59  } );
60  }
61 }
62 
63 void QgsAggregateToolButton::updateAvailableAggregates()
64 {
65  QList<QgsAggregateCalculator::AggregateInfo> aggregates = QgsAggregateCalculator::aggregates();
66 
67  for ( const auto &aggregate : aggregates )
68  {
69  if ( aggregate.supportedTypes.contains( mType ) )
70  {
71  mAvailableAggregates.append( aggregate );
72  }
73  }
74 }
75 
77 {
78  if ( active == mActive )
79  return;
80 
81  mActive = active;
82 
83  if ( !active )
84  setText( tr( "Exclude" ) );
85  emit activeChanged();
86 }
87 
89 {
90  return mAggregate;
91 }
92 
94 {
95  if ( aggregate == mAggregate )
96  return;
97 
98  mAggregate = QString();
99 
100  for ( const auto &agg : qgis::as_const( mAvailableAggregates ) )
101  {
102  if ( agg.function == aggregate )
103  {
104  mAggregate = aggregate;
105  setText( agg.name );
106  break;
107  }
108  }
109 
110  setActive( !mAggregate.isEmpty() );
111 
112  emit aggregateChanged();
113 }
114 
116 {
117  return mActive;
118 }
119 
120 QVariant::Type QgsAggregateToolButton::type() const
121 {
122  return mType;
123 }
void setActive(bool active)
When this flag is false, the aggregate will be deactivated.
bool active() const
When this flag is false, the aggregate will be deactivated.
void setType(QVariant::Type type)
Based on the type of underlying data, some aggregates will be available or not.
void setAggregate(const QString &aggregate)
The function name of the selected aggregate or a Null String if none is chosen.
void activeChanged()
A function has been selected or deselected.
QVariant::Type type() const
Based on the type of underlying data, some aggregates will be available or not.
static QList< QgsAggregateCalculator::AggregateInfo > aggregates()
Structured information for available aggregates.
void aggregateChanged()
The function name of the selected aggregate has changed.
QString aggregate() const
The function name of the selected aggregate or a Null String if none is chosen.