QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsattributedialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsattributedialog.cpp - description
3  -------------------
4  begin : October 2004
5  copyright : (C) 2004 by Marco Hugentobler
6  email : [email protected]
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgsattributedialog.h"
19 
20 #include "qgsattributeform.h"
21 #include "qgshighlight.h"
22 #include "qgsapplication.h"
23 #include "qgssettings.h"
24 #include "qgsmessagebar.h"
25 
26 QgsAttributeDialog::QgsAttributeDialog( QgsVectorLayer *vl, QgsFeature *thepFeature, bool featureOwner, QWidget *parent, bool showDialogButtons, const QgsAttributeEditorContext &context )
27  : QDialog( parent )
28  , mOwnedFeature( featureOwner ? thepFeature : nullptr )
29 {
30  init( vl, thepFeature, context, showDialogButtons );
31 }
32 
34 {
35  if ( mHighlight )
36  {
37  mHighlight->hide();
38  delete mHighlight;
39  }
40 
41  delete mOwnedFeature;
42 
43  saveGeometry();
44 }
45 
46 void QgsAttributeDialog::saveGeometry()
47 {
48  // WARNING!!!! Don't use QgsGui::enableAutoGeometryRestore for this dialog -- the object name
49  // is dynamic and is set to match the layer/feature combination.
50  QgsSettings().setValue( QStringLiteral( "Windows/AttributeDialog/geometry" ), QDialog::saveGeometry() );
51 }
52 
53 void QgsAttributeDialog::restoreGeometry()
54 {
55  // WARNING!!!! Don't use QgsGui::enableAutoGeometryRestore for this dialog -- the object name
56  // is dynamic and is set to match the layer/feature combination.
57  QDialog::restoreGeometry( QgsSettings().value( QStringLiteral( "Windows/AttributeDialog/geometry" ) ).toByteArray() );
58 }
59 
61 {
62  delete mHighlight;
63 
64  mHighlight = h;
65 }
66 
68 {
69  bool didSave = mAttributeForm->save();
70  if ( didSave )
71  {
72  QDialog::accept();
73  }
74  else
75  {
76  mMessageBar->pushMessage( QString(),
77  tr( "Your JSON value is invalid and has not been saved" ),
78  Qgis::MessageLevel::Critical,
79  5 );
80  }
81 }
82 
84 {
85  QDialog::show();
86  raise();
87  activateWindow();
88 }
89 
91 {
92  // Delete any actions on other layers that may have been triggered from this dialog
93  if ( mAttributeForm->mode() == QgsAttributeEditorContext::AddFeatureMode )
94  mTrackedVectorLayerTools.rollback();
95 
96  QDialog::reject();
97 }
98 
99 void QgsAttributeDialog::init( QgsVectorLayer *layer, QgsFeature *feature, const QgsAttributeEditorContext &context, bool showDialogButtons )
100 {
101  QgsAttributeEditorContext trackedContext = context;
102  setWindowTitle( tr( "%1 - Feature Attributes" ).arg( layer->name() ) );
103  setLayout( new QGridLayout() );
104  layout()->setContentsMargins( 0, 0, 0, 0 );
105  mMessageBar = new QgsMessageBar( this );
106  mMessageBar->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed );
107  layout()->addWidget( mMessageBar );
108 
109  setLayout( layout() );
110 
111  mTrackedVectorLayerTools.setVectorLayerTools( trackedContext.vectorLayerTools() );
112  trackedContext.setVectorLayerTools( &mTrackedVectorLayerTools );
113  if ( showDialogButtons )
115 
116  mAttributeForm = new QgsAttributeForm( layer, *feature, trackedContext, this );
117  mAttributeForm->disconnectButtonBox();
118  layout()->addWidget( mAttributeForm );
119  QDialogButtonBox *buttonBox = mAttributeForm->findChild<QDialogButtonBox *>();
120  connect( buttonBox, &QDialogButtonBox::rejected, this, &QgsAttributeDialog::reject );
121  connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsAttributeDialog::accept );
122  connect( layer, &QObject::destroyed, this, &QWidget::close );
123 
124  mMenu = new QgsActionMenu( layer, mAttributeForm->feature(), QStringLiteral( "Feature" ), this );
125  if ( !mMenu->menuActions().isEmpty() )
126  {
127  QMenuBar *menuBar = new QMenuBar( this );
128  menuBar->addMenu( mMenu );
129  layout()->setMenuBar( menuBar );
130  }
131 
132  restoreGeometry();
133  focusNextChild();
134 }
135 
137 {
138  mAttributeForm->setMode( mode );
139  mMenu->setMode( mode );
140 }
141 
142 bool QgsAttributeDialog::event( QEvent *e )
143 {
144  if ( e->type() == QEvent::WindowActivate && mHighlight )
145  mHighlight->show();
146  else if ( e->type() == QEvent::WindowDeactivate && mHighlight )
147  mHighlight->hide();
148 
149  return QDialog::event( e );
150 }
151 
153 {
154  mAttributeForm->setExtraContextScope( extraScope );
155 }
QgsActionMenu::menuActions
QList< QgsAction > menuActions()
Returns menu actions.
Definition: qgsactionmenu.cpp:209
QgsAttributeDialog::setHighlight
void setHighlight(QgsHighlight *h)
setHighlight
Definition: qgsattributedialog.cpp:60
qgsattributeform.h
QgsAttributeEditorContext::setFormMode
void setFormMode(FormMode mode)
Sets the form mode.
Definition: qgsattributeeditorcontext.h:212
QgsActionMenu::setMode
void setMode(QgsAttributeEditorContext::Mode mode)
Change the mode of the actions.
Definition: qgsactionmenu.cpp:70
QgsAttributeEditorContext::AddFeatureMode
@ AddFeatureMode
Definition: qgsattributeeditorcontext.h:50
QgsAttributeForm::feature
const QgsFeature & feature()
Definition: qgsattributeform.h:76
QgsAttributeForm::save
bool save()
Save all the values from the editors to the layer.
Definition: qgsattributeform.cpp:738
QgsTrackedVectorLayerTools::setVectorLayerTools
void setVectorLayerTools(const QgsVectorLayerTools *tools)
Set the vector layer tools that will be used to interact with the data.
Definition: qgstrackedvectorlayertools.cpp:63
QgsSettings
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
QgsGuiUtils::saveGeometry
void saveGeometry(QWidget *widget, const QString &keyName)
Save the wigget geometry into settings.
Definition: qgsguiutils.cpp:212
QgsAttributeDialog::reject
void reject() override
Definition: qgsattributedialog.cpp:90
QgsGuiUtils::restoreGeometry
bool restoreGeometry(QWidget *widget, const QString &keyName)
Restore the wigget geometry from settings.
Definition: qgsguiutils.cpp:219
QgsAttributeDialog::feature
const QgsFeature * feature()
Definition: qgsattributedialog.h:66
QgsAttributeForm::disconnectButtonBox
void disconnectButtonBox()
Disconnects the button box (OK/Cancel) from the accept/resetValues slots If this method is called,...
Definition: qgsattributeform.cpp:115
QgsAttributeDialog::setMode
void setMode(QgsAttributeEditorContext::Mode mode)
Toggles the form mode.
Definition: qgsattributedialog.cpp:136
QgsHighlight
A class for highlight features on the map.
Definition: qgshighlight.h:58
qgsapplication.h
QgsAttributeEditorContext::Mode
Mode
modes
Definition: qgsattributeeditorcontext.h:48
QgsAttributeForm::mode
QgsAttributeEditorContext::Mode mode() const
Returns the current mode of the form.
Definition: qgsattributeform.h:143
QgsAttributeForm::setExtraContextScope
void setExtraContextScope(QgsExpressionContextScope *extraScope)
Sets an additional expression context scope to be used for calculations in this form.
Definition: qgsattributeform.cpp:2426
QgsAttributeEditorContext::StandaloneDialog
@ StandaloneDialog
A form was opened as a new dialog.
Definition: qgsattributeeditorcontext.h:73
QgsMessageBar
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:61
QgsActionMenu
This class is a menu that is populated automatically with the actions defined for a given layer.
Definition: qgsactionmenu.h:38
qgsattributedialog.h
QgsAttributeDialog::show
void show()
Show the dialog non-blocking. Reparents this dialog to be a child of the dialog form.
Definition: qgsattributedialog.cpp:83
qgsmessagebar.h
QgsAttributeDialog::QgsAttributeDialog
QgsAttributeDialog(QgsVectorLayer *vl, QgsFeature *thepFeature, bool featureOwner, QWidget *parent=nullptr, bool showDialogButtons=true, const QgsAttributeEditorContext &context=QgsAttributeEditorContext())
Create an attribute dialog for a given layer and feature.
Definition: qgsattributedialog.cpp:26
QgsSettings::setValue
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
Definition: qgssettings.cpp:289
QgsAttributeEditorContext::setVectorLayerTools
void setVectorLayerTools(QgsVectorLayerTools *vlTools)
Sets the associated vector layer tools.
Definition: qgsattributeeditorcontext.h:164
QgsAttributeDialog::setExtraContextScope
void setExtraContextScope(QgsExpressionContextScope *extraScope)
Sets extraScope as an additional expression context scope to be used for calculations in this form.
Definition: qgsattributedialog.cpp:152
QgsExpressionContextScope
Single scope for storing variables and functions for use within a QgsExpressionContext.
Definition: qgsexpressioncontext.h:112
QgsAttributeDialog::~QgsAttributeDialog
~QgsAttributeDialog() override
Definition: qgsattributedialog.cpp:33
QgsAttributeForm::setMode
void setMode(QgsAttributeEditorContext::Mode mode)
Sets the current mode of the form.
Definition: qgsattributeform.cpp:131
qgshighlight.h
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
qgssettings.h
QgsMapLayer::name
QString name
Definition: qgsmaplayer.h:86
QgsFeature
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
QgsAttributeForm
Definition: qgsattributeform.h:45
QgsAttributeEditorContext
This class contains context information for attribute editor widgets.
Definition: qgsattributeeditorcontext.h:41
QgsAttributeDialog::event
bool event(QEvent *e) override
Intercept window activate/deactivate events to show/hide the highlighted feature.
Definition: qgsattributedialog.cpp:142
QgsTrackedVectorLayerTools::rollback
void rollback()
Delete all features which have been added via this object.
Definition: qgstrackedvectorlayertools.cpp:68
QgsAttributeEditorContext::vectorLayerTools
const QgsVectorLayerTools * vectorLayerTools() const
Returns the associated vector layer tools.
Definition: qgsattributeeditorcontext.h:172
QgsMessageBar::pushMessage
void pushMessage(const QString &text, Qgis::MessageLevel level=Qgis::Info, int duration=5)
A convenience method for pushing a message with the specified text to the bar.
Definition: qgsmessagebar.cpp:379
QgsAttributeDialog::accept
void accept() override
Definition: qgsattributedialog.cpp:67