QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 #include "qgis.h"
19 
20 #include <QMenu>
21 
23 {
24  setFocusPolicy( Qt::StrongFocus );
25  setPopupMode( QToolButton::InstantPopup );
26 
27  mMenu = new QMenu( this );
28  connect( mMenu, &QMenu::aboutToShow, this, &QgsAggregateToolButton::aboutToShowMenu );
29  setMenu( mMenu );
30 
31  setText( tr( "Exclude" ) );
32 }
33 
34 void QgsAggregateToolButton::setType( QVariant::Type type )
35 {
36  if ( mType == type )
37  return;
38 
39  mType = type;
40  updateAvailableAggregates();
41 }
42 
43 void QgsAggregateToolButton::aboutToShowMenu()
44 {
45  mMenu->clear();
46 
47  QAction *action = mMenu->addAction( tr( "Exclude" ) );
48  connect( action, &QAction::triggered, this, [ this ]
49  {
50  setActive( false );
51  } );
52 
53  for ( const auto &aggregate : qgis::as_const( mAvailableAggregates ) )
54  {
55  QAction *action = mMenu->addAction( aggregate.name );
56  connect( action, &QAction::triggered, this, [ this, aggregate ]
57  {
58  setText( aggregate.name );
59  setAggregate( aggregate.function );
60  } );
61  }
62 }
63 
64 void QgsAggregateToolButton::updateAvailableAggregates()
65 {
66  QList<QgsAggregateCalculator::AggregateInfo> aggregates = QgsAggregateCalculator::aggregates();
67 
68  for ( const auto &aggregate : aggregates )
69  {
70  if ( aggregate.supportedTypes.contains( mType ) )
71  {
72  mAvailableAggregates.append( aggregate );
73  }
74  }
75 }
76 
78 {
79  if ( active == mActive )
80  return;
81 
82  mActive = active;
83 
84  if ( !active )
85  setText( tr( "Exclude" ) );
86  emit activeChanged();
87 }
88 
90 {
91  return mAggregate;
92 }
93 
95 {
96  if ( aggregate == mAggregate )
97  return;
98 
99  mAggregate = QString();
100 
101  for ( const auto &agg : qgis::as_const( mAvailableAggregates ) )
102  {
103  if ( agg.function == aggregate )
104  {
105  mAggregate = aggregate;
106  setText( agg.name );
107  break;
108  }
109  }
110 
111  setActive( !mAggregate.isEmpty() );
112 
113  emit aggregateChanged();
114 }
115 
117 {
118  return mActive;
119 }
120 
121 QVariant::Type QgsAggregateToolButton::type() const
122 {
123  return mType;
124 }
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.