QGIS API Documentation  3.2.0-Bonn (bc43194)
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 
16 #include "qgsmultiedittoolbutton.h"
17 #include "qgsapplication.h"
18 #include <QMenu>
20  : QToolButton( parent )
21 {
22  setFocusPolicy( Qt::StrongFocus );
23 
24  // set default tool button icon properties
25  setFixedSize( 22, 22 );
26  setStyleSheet( QStringLiteral( "QToolButton{ background: none; border: 1px solid rgba(0, 0, 0, 0%);} QToolButton:focus { border: 1px solid palette(highlight); }" ) );
27  setIconSize( QSize( 16, 16 ) );
28  setPopupMode( QToolButton::InstantPopup );
29 
30  mMenu = new QMenu( this );
31  connect( mMenu, &QMenu::aboutToShow, this, &QgsMultiEditToolButton::aboutToShowMenu );
32  setMenu( mMenu );
33 
34  // sets initial appearance
35  updateState();
36 }
37 
38 void QgsMultiEditToolButton::aboutToShowMenu()
39 {
40  mMenu->clear();
41 
42  switch ( mState )
43  {
44  case Default:
45  {
46  QAction *noAction = mMenu->addAction( tr( "No Changes to Commit" ) );
47  noAction->setEnabled( false );
48  break;
49  }
50  case MixedValues:
51  {
52  QString title = !mField.name().isEmpty() ? tr( "Set %1 for All Selected Features" ).arg( mField.name() )
53  : tr( "Set field for all selected features" );
54  QAction *setFieldAction = mMenu->addAction( title );
55  connect( setFieldAction, &QAction::triggered, this, &QgsMultiEditToolButton::setFieldTriggered );
56  break;
57  }
58  case Changed:
59  {
60  QAction *resetFieldAction = mMenu->addAction( tr( "Reset to Original Values" ) );
61  connect( resetFieldAction, &QAction::triggered, this, &QgsMultiEditToolButton::resetFieldTriggered );
62  break;
63  }
64  }
65 }
66 
67 void QgsMultiEditToolButton::setFieldTriggered()
68 {
69  mIsChanged = true;
70  updateState();
72 }
73 
74 void QgsMultiEditToolButton::resetFieldTriggered()
75 {
76  mIsChanged = false;
77  updateState();
79 }
80 
81 void QgsMultiEditToolButton::updateState()
82 {
83  //changed state takes priority over mixed values state
84  if ( mIsChanged )
85  mState = Changed;
86  else if ( mIsMixedValues )
87  mState = MixedValues;
88  else
89  mState = Default;
90 
91  QIcon icon;
92  QString tooltip;
93  switch ( mState )
94  {
95  case Default:
96  icon = QgsApplication::getThemeIcon( QStringLiteral( "/multieditSameValues.svg" ) );
97  tooltip = tr( "All features in selection have equal value for '%1'" ).arg( mField.name() );
98  break;
99  case MixedValues:
100  icon = QgsApplication::getThemeIcon( QStringLiteral( "/multieditMixedValues.svg" ) );
101  tooltip = tr( "Some features in selection have different values for '%1'" ).arg( mField.name() );
102  break;
103  case Changed:
104  icon = QgsApplication::getThemeIcon( QStringLiteral( "/multieditChangedValues.svg" ) );
105  tooltip = tr( "Values for '%1' have unsaved changes" ).arg( mField.name() );
106  break;
107  }
108 
109  setIcon( icon );
110  setToolTip( tooltip );
111 }
QString name
Definition: qgsfield.h:57
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
void resetFieldValueTriggered()
Emitted when the "reset to original values" option is selected.
Mixed state, some features have different values for the widget.
QgsMultiEditToolButton(QWidget *parent=nullptr)
Constructor for QgsMultiEditToolButton.
void setFieldValueTriggered()
Emitted when the "set field value for all features" option is selected.
Value for widget has changed but changes have not yet been committed.
Default state, all features have same value for widget.