QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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() ) : tr( "Set field for all selected features" );
66 QAction *setFieldAction = mMenu->addAction( title );
67 connect( setFieldAction, &QAction::triggered, this, &QgsMultiEditToolButton::setFieldTriggered );
68 break;
69 }
70 case Changed:
71 {
72 QAction *resetFieldAction = mMenu->addAction( tr( "Reset to Original Values" ) );
73 connect( resetFieldAction, &QAction::triggered, this, &QgsMultiEditToolButton::resetFieldTriggered );
74 break;
75 }
76 }
77}
78
79void QgsMultiEditToolButton::setFieldTriggered()
80{
81 mIsChanged = true;
82 updateState();
84}
85
86void QgsMultiEditToolButton::resetFieldTriggered()
87{
88 mIsChanged = false;
89 updateState();
91}
92
93void QgsMultiEditToolButton::updateState()
94{
95 //changed state takes priority over mixed values state
96 if ( mIsChanged )
97 mState = Changed;
98 else if ( mIsMixedValues )
99 mState = MixedValues;
100 else
101 mState = Default;
102
103 QIcon icon;
104 QString tooltip;
105 switch ( mState )
106 {
107 case Default:
108 icon = QgsApplication::getThemeIcon( u"/multieditSameValues.svg"_s );
109 tooltip = tr( "All features in selection have equal value for '%1'" ).arg( mField.name() );
110 break;
111 case MixedValues:
112 icon = QgsApplication::getThemeIcon( u"/multieditMixedValues.svg"_s );
113 tooltip = tr( "Some features in selection have different values for '%1'" ).arg( mField.name() );
114 break;
115 case Changed:
116 icon = QgsApplication::getThemeIcon( u"/multieditChangedValues.svg"_s );
117 tooltip = tr( "Values for '%1' have unsaved changes" ).arg( mField.name() );
118 break;
119 }
120
121 setIcon( icon );
122 setToolTip( tooltip );
123}
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,...