QGIS API Documentation 3.39.0-Master (47f7b3a4989)
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
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
34void QgsAggregateToolButton::setType( QMetaType::Type type )
35{
36 if ( mType == type )
37 return;
38
39 mType = type;
40 updateAvailableAggregates();
41}
42
47
48void QgsAggregateToolButton::aboutToShowMenu()
49{
50 mMenu->clear();
51
52 QAction *action = mMenu->addAction( tr( "Exclude" ) );
53 connect( action, &QAction::triggered, this, [ this ]
54 {
55 setActive( false );
56 } );
57
58 for ( const auto &aggregate : std::as_const( mAvailableAggregates ) )
59 {
60 QAction *action = mMenu->addAction( aggregate.name );
61 connect( action, &QAction::triggered, this, [ this, aggregate ]
62 {
63 setText( aggregate.name );
64 setAggregate( aggregate.function );
65 } );
66 }
67}
68
69void QgsAggregateToolButton::updateAvailableAggregates()
70{
71 QList<QgsAggregateCalculator::AggregateInfo> aggregates = QgsAggregateCalculator::aggregates();
72
73 for ( const auto &aggregate : aggregates )
74 {
75 if ( aggregate.supportedTypes.contains( mType ) )
76 {
77 mAvailableAggregates.append( aggregate );
78 }
79 }
80}
81
83{
84 if ( active == mActive )
85 return;
86
87 mActive = active;
88
89 if ( !active )
90 setText( tr( "Exclude" ) );
91 emit activeChanged();
92}
93
95{
96 return mAggregate;
97}
98
99void QgsAggregateToolButton::setAggregate( const QString &aggregate )
100{
101 if ( aggregate == mAggregate )
102 return;
103
104 mAggregate = QString();
105
106 for ( const auto &agg : std::as_const( mAvailableAggregates ) )
107 {
108 if ( agg.function == aggregate )
109 {
110 mAggregate = aggregate;
111 setText( agg.name );
112 break;
113 }
114 }
115
116 setActive( !mAggregate.isEmpty() );
117
118 emit aggregateChanged();
119}
120
122{
123 return mActive;
124}
125
126QMetaType::Type QgsAggregateToolButton::type() const
127{
128 return mType;
129}
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.