QGIS API Documentation 3.99.0-Master (d270888f95f)
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#include <QString>
23
24#include "moc_qgsmultiedittoolbutton.cpp"
25
26using namespace Qt::StringLiterals;
27
29 : QToolButton( parent )
30{
31 setFocusPolicy( Qt::StrongFocus );
32
33 // set default tool button icon properties
34 setStyleSheet( u"QToolButton{ background: none; border: 1px solid rgba(0, 0, 0, 0%);} QToolButton:focus { border: 1px solid palette(highlight); }"_s );
35
36 const int iconSize = QgsGuiUtils::scaleIconSize( 24 );
37 setIconSize( QSize( iconSize, iconSize ) );
38 // button width is 1.25 * icon size, height 1.1 * icon size. But we round to ensure even pixel sizes for equal margins
39 setFixedSize( 2 * static_cast<int>( 1.25 * iconSize / 2.0 ), 2 * static_cast<int>( iconSize * 1.1 / 2.0 ) );
40
41 setPopupMode( QToolButton::InstantPopup );
42
43 mMenu = new QMenu( this );
44 connect( mMenu, &QMenu::aboutToShow, this, &QgsMultiEditToolButton::aboutToShowMenu );
45 setMenu( mMenu );
46
47 // sets initial appearance
48 updateState();
49}
50
51void QgsMultiEditToolButton::aboutToShowMenu()
52{
53 mMenu->clear();
54
55 switch ( mState )
56 {
57 case Default:
58 {
59 QAction *noAction = mMenu->addAction( tr( "No Changes to Commit" ) );
60 noAction->setEnabled( false );
61 break;
62 }
63 case MixedValues:
64 {
65 const QString title = !mField.name().isEmpty() ? tr( "Set %1 for All Selected Features" ).arg( mField.name() )
66 : tr( "Set field for all selected features" );
67 QAction *setFieldAction = mMenu->addAction( title );
68 connect( setFieldAction, &QAction::triggered, this, &QgsMultiEditToolButton::setFieldTriggered );
69 break;
70 }
71 case Changed:
72 {
73 QAction *resetFieldAction = mMenu->addAction( tr( "Reset to Original Values" ) );
74 connect( resetFieldAction, &QAction::triggered, this, &QgsMultiEditToolButton::resetFieldTriggered );
75 break;
76 }
77 }
78}
79
80void QgsMultiEditToolButton::setFieldTriggered()
81{
82 mIsChanged = true;
83 updateState();
85}
86
87void QgsMultiEditToolButton::resetFieldTriggered()
88{
89 mIsChanged = false;
90 updateState();
92}
93
94void QgsMultiEditToolButton::updateState()
95{
96 //changed state takes priority over mixed values state
97 if ( mIsChanged )
98 mState = Changed;
99 else if ( mIsMixedValues )
100 mState = MixedValues;
101 else
102 mState = Default;
103
104 QIcon icon;
105 QString tooltip;
106 switch ( mState )
107 {
108 case Default:
109 icon = QgsApplication::getThemeIcon( u"/multieditSameValues.svg"_s );
110 tooltip = tr( "All features in selection have equal value for '%1'" ).arg( mField.name() );
111 break;
112 case MixedValues:
113 icon = QgsApplication::getThemeIcon( u"/multieditMixedValues.svg"_s );
114 tooltip = tr( "Some features in selection have different values for '%1'" ).arg( mField.name() );
115 break;
116 case Changed:
117 icon = QgsApplication::getThemeIcon( u"/multieditChangedValues.svg"_s );
118 tooltip = tr( "Values for '%1' have unsaved changes" ).arg( mField.name() );
119 break;
120 }
121
122 setIcon( icon );
123 setToolTip( tooltip );
124}
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,...