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