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