QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
qgsmultiedittoolbutton.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmultiedittoolbutton.cpp
3 --------------------------
4 Date : March 2016
5 Copyright : (C) 2016 Nyall Dawson
6 Email : nyall dot dawson at gmail.com
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
18#include "qgsapplication.h"
19#include "qgsguiutils.h"
20
21#include <QMenu>
22
23#include "moc_qgsmultiedittoolbutton.cpp"
24
26 : QToolButton( parent )
27{
28 setFocusPolicy( Qt::StrongFocus );
29
30 // set default tool button icon properties
31 setStyleSheet( QStringLiteral( "QToolButton{ background: none; border: 1px solid rgba(0, 0, 0, 0%);} QToolButton:focus { border: 1px solid palette(highlight); }" ) );
32
33 const int iconSize = QgsGuiUtils::scaleIconSize( 24 );
34 setIconSize( QSize( iconSize, iconSize ) );
35 // button width is 1.25 * icon size, height 1.1 * icon size. But we round to ensure even pixel sizes for equal margins
36 setFixedSize( 2 * static_cast<int>( 1.25 * iconSize / 2.0 ), 2 * static_cast<int>( iconSize * 1.1 / 2.0 ) );
37
38 setPopupMode( QToolButton::InstantPopup );
39
40 mMenu = new QMenu( this );
41 connect( mMenu, &QMenu::aboutToShow, this, &QgsMultiEditToolButton::aboutToShowMenu );
42 setMenu( mMenu );
43
44 // sets initial appearance
45 updateState();
46}
47
48void QgsMultiEditToolButton::aboutToShowMenu()
49{
50 mMenu->clear();
51
52 switch ( mState )
53 {
54 case Default:
55 {
56 QAction *noAction = mMenu->addAction( tr( "No Changes to Commit" ) );
57 noAction->setEnabled( false );
58 break;
59 }
60 case MixedValues:
61 {
62 const QString title = !mField.name().isEmpty() ? tr( "Set %1 for All Selected Features" ).arg( mField.name() )
63 : tr( "Set field for all selected features" );
64 QAction *setFieldAction = mMenu->addAction( title );
65 connect( setFieldAction, &QAction::triggered, this, &QgsMultiEditToolButton::setFieldTriggered );
66 break;
67 }
68 case Changed:
69 {
70 QAction *resetFieldAction = mMenu->addAction( tr( "Reset to Original Values" ) );
71 connect( resetFieldAction, &QAction::triggered, this, &QgsMultiEditToolButton::resetFieldTriggered );
72 break;
73 }
74 }
75}
76
77void QgsMultiEditToolButton::setFieldTriggered()
78{
79 mIsChanged = true;
80 updateState();
82}
83
84void QgsMultiEditToolButton::resetFieldTriggered()
85{
86 mIsChanged = false;
87 updateState();
89}
90
91void QgsMultiEditToolButton::updateState()
92{
93 //changed state takes priority over mixed values state
94 if ( mIsChanged )
95 mState = Changed;
96 else if ( mIsMixedValues )
97 mState = MixedValues;
98 else
99 mState = Default;
100
101 QIcon icon;
102 QString tooltip;
103 switch ( mState )
104 {
105 case Default:
106 icon = QgsApplication::getThemeIcon( QStringLiteral( "/multieditSameValues.svg" ) );
107 tooltip = tr( "All features in selection have equal value for '%1'" ).arg( mField.name() );
108 break;
109 case MixedValues:
110 icon = QgsApplication::getThemeIcon( QStringLiteral( "/multieditMixedValues.svg" ) );
111 tooltip = tr( "Some features in selection have different values for '%1'" ).arg( mField.name() );
112 break;
113 case Changed:
114 icon = QgsApplication::getThemeIcon( QStringLiteral( "/multieditChangedValues.svg" ) );
115 tooltip = tr( "Values for '%1' have unsaved changes" ).arg( mField.name() );
116 break;
117 }
118
119 setIcon( icon );
120 setToolTip( tooltip );
121}
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
@ Changed
Value for widget has changed but changes have not yet been committed.
@ MixedValues
Mixed state, some features have different values for the widget.
@ Default
Default state, all features have same value for widget.
void resetFieldValueTriggered()
Emitted when the "reset to original values" option is selected.
QgsMultiEditToolButton(QWidget *parent=nullptr)
Constructor for QgsMultiEditToolButton.
void setFieldValueTriggered()
Emitted when the "set field value for all features" option is selected.
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...