QGIS API Documentation  3.19.0-Master (26212d215f)
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  QString error;
70  const bool didSave = mAttributeForm->saveWithDetails( &error );
71  if ( didSave )
72  {
73  QDialog::accept();
74  }
75  else
76  {
77  if ( error.isEmpty() )
78  error = tr( "An unknown error was encountered saving attributes" );
79 
80  mMessageBar->pushMessage( QString(),
81  error,
82  Qgis::MessageLevel::Critical );
83  }
84 }
85 
87 {
88  QDialog::show();
89  raise();
90  activateWindow();
91 }
92 
94 {
95  // Delete any actions on other layers that may have been triggered from this dialog
96  if ( mAttributeForm->mode() == QgsAttributeEditorContext::AddFeatureMode )
97  mTrackedVectorLayerTools.rollback();
98 
99  QDialog::reject();
100 }
101 
102 void QgsAttributeDialog::init( QgsVectorLayer *layer, QgsFeature *feature, const QgsAttributeEditorContext &context, bool showDialogButtons )
103 {
104  QgsAttributeEditorContext trackedContext = context;
105  setWindowTitle( tr( "%1 - Feature Attributes" ).arg( layer->name() ) );
106  setLayout( new QGridLayout() );
107  layout()->setContentsMargins( 0, 0, 0, 0 );
108  mMessageBar = new QgsMessageBar( this );
109  mMessageBar->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed );
110  layout()->addWidget( mMessageBar );
111 
112  setLayout( layout() );
113 
114  mTrackedVectorLayerTools.setVectorLayerTools( trackedContext.vectorLayerTools() );
115  trackedContext.setVectorLayerTools( &mTrackedVectorLayerTools );
116  if ( showDialogButtons )
118 
119  mAttributeForm = new QgsAttributeForm( layer, *feature, trackedContext, this );
120  mAttributeForm->disconnectButtonBox();
121  layout()->addWidget( mAttributeForm );
122  QDialogButtonBox *buttonBox = mAttributeForm->findChild<QDialogButtonBox *>();
123  connect( buttonBox, &QDialogButtonBox::rejected, this, &QgsAttributeDialog::reject );
124  connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsAttributeDialog::accept );
125  connect( layer, &QObject::destroyed, this, &QWidget::close );
126 
127  mMenu = new QgsActionMenu( layer, mAttributeForm->feature(), QStringLiteral( "Feature" ), this );
128  if ( !mMenu->menuActions().isEmpty() )
129  {
130  QMenuBar *menuBar = new QMenuBar( this );
131  menuBar->addMenu( mMenu );
132  layout()->setMenuBar( menuBar );
133  }
134 
135  restoreGeometry();
136  focusNextChild();
137 }
138 
140 {
141  mAttributeForm->setMode( mode );
142  mMenu->setMode( mode );
143 }
144 
145 bool QgsAttributeDialog::event( QEvent *e )
146 {
147  if ( e->type() == QEvent::WindowActivate && mHighlight )
148  mHighlight->show();
149  else if ( e->type() == QEvent::WindowDeactivate && mHighlight )
150  mHighlight->hide();
151 
152  return QDialog::event( e );
153 }
154 
156 {
157  mAttributeForm->setExtraContextScope( extraScope );
158 }
This class is a menu that is populated automatically with the actions defined for a given layer.
Definition: qgsactionmenu.h:38
void setMode(QgsAttributeEditorContext::Mode mode)
Change the mode of the actions.
QList< QgsAction > menuActions()
Returns menu actions.
void setMode(QgsAttributeEditorContext::Mode mode)
Toggles the form mode.
bool event(QEvent *e) override
Intercept window activate/deactivate events to show/hide the highlighted feature.
const QgsFeature * feature()
void show()
Show the dialog non-blocking. Reparents this dialog to be a child of the dialog form.
void reject() override
void setExtraContextScope(QgsExpressionContextScope *extraScope)
Sets extraScope as an additional expression context scope to be used for calculations in this form.
void accept() override
void setHighlight(QgsHighlight *h)
setHighlight
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.
This class contains context information for attribute editor widgets.
@ StandaloneDialog
A form was opened as a new dialog.
void setFormMode(FormMode mode)
Sets the form mode.
void setVectorLayerTools(QgsVectorLayerTools *vlTools)
Sets the associated vector layer tools.
const QgsVectorLayerTools * vectorLayerTools() const
Returns the associated vector layer tools.
bool saveWithDetails(QString *error=nullptr)
Save all the values from the editors to the layer.
void setExtraContextScope(QgsExpressionContextScope *extraScope)
Sets an additional expression context scope to be used for calculations in this form.
const QgsFeature & feature()
void disconnectButtonBox()
Disconnects the button box (OK/Cancel) from the accept/resetValues slots If this method is called,...
void setMode(QgsAttributeEditorContext::Mode mode)
Sets the current mode of the form.
QgsAttributeEditorContext::Mode mode() const
Returns the current mode of the form.
Single scope for storing variables and functions for use within a QgsExpressionContext.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
A class for highlight features on the map.
Definition: qgshighlight.h:58
QString name
Definition: qgsmaplayer.h:88
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:61
void pushMessage(const QString &text, Qgis::MessageLevel level=Qgis::Info, int duration=-1)
A convenience method for pushing a message with the specified text to the bar.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
void setVectorLayerTools(const QgsVectorLayerTools *tools)
Set the vector layer tools that will be used to interact with the data.
void rollback()
Delete all features which have been added via this object.
Represents a vector layer which manages a vector based data sets.
bool restoreGeometry(QWidget *widget, const QString &keyName)
Restore the wigget geometry from settings.
void saveGeometry(QWidget *widget, const QString &keyName)
Save the wigget geometry into settings.