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