QGIS API Documentation 3.41.0-Master (cea29feecf2)
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 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 setText( aggregate.name );
63 setAggregate( aggregate.function );
64 } );
65 }
66}
67
68void QgsAggregateToolButton::updateAvailableAggregates()
69{
70 QList<QgsAggregateCalculator::AggregateInfo> aggregates = QgsAggregateCalculator::aggregates();
71
72 for ( const auto &aggregate : aggregates )
73 {
74 if ( aggregate.supportedTypes.contains( mType ) )
75 {
76 mAvailableAggregates.append( aggregate );
77 }
78 }
79}
80
82{
83 if ( active == mActive )
84 return;
85
86 mActive = active;
87
88 if ( !active )
89 setText( tr( "Exclude" ) );
90 emit activeChanged();
91}
92
94{
95 return mAggregate;
96}
97
98void QgsAggregateToolButton::setAggregate( const QString &aggregate )
99{
100 if ( aggregate == mAggregate )
101 return;
102
103 mAggregate = QString();
104
105 for ( const auto &agg : std::as_const( mAvailableAggregates ) )
106 {
107 if ( agg.function == aggregate )
108 {
109 mAggregate = aggregate;
110 setText( agg.name );
111 break;
112 }
113 }
114
115 setActive( !mAggregate.isEmpty() );
116
117 emit aggregateChanged();
118}
119
121{
122 return mActive;
123}
124
125QMetaType::Type QgsAggregateToolButton::type() const
126{
127 return mType;
128}
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.