QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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  , mIsMixedValues( false )
22  , mIsChanged( false )
23  , mState( Default )
24  , mMenu( nullptr )
25 {
26  setFocusPolicy( Qt::StrongFocus );
27 
28  // set default tool button icon properties
29  setFixedSize( 22, 22 );
30  setStyleSheet( QString( "QToolButton{ background: none; border: 1px solid rgba(0, 0, 0, 0%);} QToolButton:focus { border: 1px solid palette(highlight); }" ) );
31  setIconSize( QSize( 16, 16 ) );
32  setPopupMode( QToolButton::InstantPopup );
33 
34  mMenu = new QMenu( this );
35  connect( mMenu, SIGNAL( aboutToShow() ), this, SLOT( aboutToShowMenu() ) );
36  setMenu( mMenu );
37 
38  // sets initial appearance
39  updateState();
40 }
41 
42 void QgsMultiEditToolButton::aboutToShowMenu()
43 {
44  mMenu->clear();
45 
46  switch ( mState )
47  {
48  case Default:
49  {
50  QAction* noAction = mMenu->addAction( tr( "No changes to commit" ) );
51  noAction->setEnabled( false );
52  break;
53  }
54  case MixedValues:
55  {
56  QString title = !mField.name().isEmpty() ? tr( "Set %1 for all selected features" ).arg( mField.name() )
57  : tr( "Set field for all selected features" );
58  QAction* setFieldAction = mMenu->addAction( title );
59  connect( setFieldAction, SIGNAL( triggered( bool ) ), this, SLOT( setFieldTriggered() ) );
60  break;
61  }
62  case Changed:
63  {
64  QAction* resetFieldAction = mMenu->addAction( tr( "Reset to original values" ) );
65  connect( resetFieldAction, SIGNAL( triggered( bool ) ), this, SLOT( resetFieldTriggered() ) );
66  break;
67  }
68  }
69 }
70 
71 void QgsMultiEditToolButton::setFieldTriggered()
72 {
73  mIsChanged = true;
74  updateState();
76 }
77 
78 void QgsMultiEditToolButton::resetFieldTriggered()
79 {
80  mIsChanged = false;
81  updateState();
83 }
84 
85 void QgsMultiEditToolButton::updateState()
86 {
87  //changed state takes priority over mixed values state
88  if ( mIsChanged )
89  mState = Changed;
90  else if ( mIsMixedValues )
91  mState = MixedValues;
92  else
93  mState = Default;
94 
95  QIcon icon;
96  QString tooltip;
97  switch ( mState )
98  {
99  case Default:
100  icon = QgsApplication::getThemeIcon( "/multieditSameValues.svg" );
101  tooltip = tr( "All features in selection have equal value for '%1'" ).arg( mField.name() );
102  break;
103  case MixedValues:
104  icon = QgsApplication::getThemeIcon( "/multieditMixedValues.svg" );
105  tooltip = tr( "Some features in selection have different values for '%1'" ).arg( mField.name() );
106  break;
107  case Changed:
108  icon = QgsApplication::getThemeIcon( "/multieditChangedValues.svg" );
109  tooltip = tr( "Values for '%1' have unsaved changes" ).arg( mField.name() );
110  break;
111  }
112 
113  setIcon( icon );
114  setToolTip( tooltip );
115 }
void setStyleSheet(const QString &styleSheet)
void setMenu(QMenu *menu)
QString name
Definition: qgsfield.h:52
void setFocusPolicy(Qt::FocusPolicy policy)
static QIcon getThemeIcon(const QString &theName)
Helper to get a theme icon.
void addAction(QAction *action)
void triggered(QAction *action)
QIcon icon() const
QString tr(const char *sourceText, const char *disambiguation, int n)
void clear()
void setIconSize(const QSize &size)
bool isEmpty() const
void resetFieldValueTriggered()
Emitted when the "reset to original values" option is selected.
void setFixedSize(const QSize &s)
QgsMultiEditToolButton(QWidget *parent=nullptr)
Constructor for QgsMultiEditToolButton.
void setFieldValueTriggered()
Emitted when the "set field value for all features" option is selected.
void setPopupMode(ToolButtonPopupMode mode)
void setToolTip(const QString &)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
void setEnabled(bool)