QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
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
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#include "moc_qgsattributedialog.cpp"
20
21#include "qgsattributeform.h"
22#include "qgshighlight.h"
23#include "qgssettings.h"
24#include "qgsmessagebar.h"
25#include "qgsactionmenu.h"
27
28QgsAttributeDialog::QgsAttributeDialog( QgsVectorLayer *vl, QgsFeature *thepFeature, bool featureOwner, QWidget *parent, bool showDialogButtons, const QgsAttributeEditorContext &context )
29 : QDialog( parent )
30 , mOwnedFeature( featureOwner ? thepFeature : nullptr )
31{
32 init( vl, thepFeature, context, showDialogButtons );
33}
34
36{
37 if ( mHighlight )
38 {
39 mHighlight->hide();
40 delete mHighlight;
41 }
42
43 delete mOwnedFeature;
44
45 saveGeometry();
46}
47
48void QgsAttributeDialog::saveGeometry()
49{
50 // WARNING!!!! Don't use QgsGui::enableAutoGeometryRestore for this dialog -- the object name
51 // is dynamic and is set to match the layer/feature combination.
52 QgsSettings().setValue( QStringLiteral( "Windows/AttributeDialog/geometry" ), QDialog::saveGeometry() );
53}
54
55void QgsAttributeDialog::restoreGeometry()
56{
57 // WARNING!!!! Don't use QgsGui::enableAutoGeometryRestore for this dialog -- the object name
58 // is dynamic and is set to match the layer/feature combination.
59 QDialog::restoreGeometry( QgsSettings().value( QStringLiteral( "Windows/AttributeDialog/geometry" ) ).toByteArray() );
60}
61
63{
64 delete mHighlight;
65
66 mHighlight = h;
67}
68
70{
71 QString error;
72 const bool didSave = mAttributeForm->saveWithDetails( &error );
73 if ( didSave )
74 {
75 QDialog::accept();
76 }
77 else
78 {
79 if ( error.isEmpty() )
80 error = tr( "An unknown error was encountered saving attributes" );
81
82 mMessageBar->pushMessage( QString(),
83 error,
85 }
86}
87
89{
90 QDialog::show();
91
92 raise();
93 activateWindow();
94}
95
96void QgsAttributeDialog::showEvent( QShowEvent *event )
97{
98 QDialog::showEvent( event );
99 // We cannot call restoreGeometry() in the constructor or init because the dialog is not yet visible
100 // and the geometry restoration will not take the window decorations (frame) into account.
101 if ( mFirstShow )
102 {
103 mFirstShow = false;
104 restoreGeometry();
105 }
106}
107
109{
110 // Delete any actions on other layers that may have been triggered from this dialog
111 if ( mAttributeForm->mode() == QgsAttributeEditorContext::AddFeatureMode )
112 mTrackedVectorLayerTools.rollback();
113
114 QDialog::reject();
115}
116
117void QgsAttributeDialog::init( QgsVectorLayer *layer, QgsFeature *feature, const QgsAttributeEditorContext &context, bool showDialogButtons )
118{
119 QgsAttributeEditorContext trackedContext = context;
120 setWindowTitle( tr( "%1 - Feature Attributes" ).arg( layer->name() ) );
121 setLayout( new QGridLayout() );
122 layout()->setContentsMargins( 0, 0, 0, 0 );
123 mMessageBar = new QgsMessageBar( this );
124 mMessageBar->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed );
125 layout()->addWidget( mMessageBar );
126
127 setLayout( layout() );
128
129 mTrackedVectorLayerTools.setVectorLayerTools( trackedContext.vectorLayerTools() );
130 trackedContext.setVectorLayerTools( &mTrackedVectorLayerTools );
131 if ( showDialogButtons )
133
134 mAttributeForm = new QgsAttributeForm( layer, *feature, trackedContext, this );
135 mAttributeForm->disconnectButtonBox();
136 layout()->addWidget( mAttributeForm );
137 QDialogButtonBox *buttonBox = mAttributeForm->findChild<QDialogButtonBox *>();
138 connect( buttonBox, &QDialogButtonBox::rejected, this, &QgsAttributeDialog::reject );
139 connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsAttributeDialog::accept );
140 connect( layer, &QObject::destroyed, this, &QWidget::close );
141
142 mMenu = new QgsActionMenu( layer, mAttributeForm->feature(), QStringLiteral( "Feature" ), this );
143 mMenu->setActionContextGenerator( this );
144 if ( !mMenu->isEmpty() )
145 {
146 mMenuBar = new QMenuBar( this );
147 mMenuBar->addMenu( mMenu );
148 layout()->setMenuBar( mMenuBar );
149 }
150 focusNextChild();
151}
152
154{
155 mAttributeForm->setMode( mode );
156 mMenu->setMode( mode );
157
158 if ( !mMenu->isEmpty() && !mMenuBar )
159 {
160 mMenuBar = new QMenuBar( this );
161 mMenuBar->addMenu( mMenu );
162 layout()->setMenuBar( mMenuBar );
163 }
164 else if ( mMenu->isEmpty() && mMenuBar )
165 {
166 layout()->setMenuBar( nullptr );
167 delete mMenuBar;
168 mMenuBar = nullptr;
169 }
170}
171
173{
174 if ( e->type() == QEvent::WindowActivate && mHighlight )
175 mHighlight->show();
176 else if ( e->type() == QEvent::WindowDeactivate && mHighlight )
177 mHighlight->hide();
178
179 return QDialog::event( e );
180}
181
183{
184 mAttributeForm->setExtraContextScope( extraScope );
185}
186
188{
190 context.setAttributeDialog( this );
191 context.setMessageBar( mMessageBar );
192 return context;
193}
194
@ Critical
Critical/error message.
Definition qgis.h:157
This class is a menu that is populated automatically with the actions defined for a given layer.
void setMode(QgsAttributeEditorContext::Mode mode)
Change the mode of the actions.
void setActionContextGenerator(QgsMapLayerActionContextGenerator *generator)
Sets a QgsMapLayerActionContextGenerator to create action contexts for the menu.
bool isEmpty() const
Returns true if the menu has no valid actions.
void setMode(QgsAttributeEditorContext::Mode mode)
Toggles the form mode.
const QgsFeature * feature()
bool event(QEvent *e) override
Intercept window activate/deactivate events to show/hide the highlighted feature.
void showEvent(QShowEvent *event) override
QgsMapLayerActionContext createActionContext() override
Creates a QgsMapLayerActionContext.
void show()
Show the dialog non-blocking. Reparents this dialog to be a child of the dialog form.
void setExtraContextScope(QgsExpressionContextScope *extraScope)
Sets extraScope as an additional expression context scope to be used for calculations in this form.
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.
const QgsVectorLayerTools * vectorLayerTools() const
Returns the associated vector layer tools.
@ 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.
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 unique ID, geometry and a list of field...
Definition qgsfeature.h:58
A class for highlight features on the map.
Encapsulates the context in which a QgsMapLayerAction action is executed.
void setAttributeDialog(QgsAttributeDialog *dialog)
Sets the attribute dialog associated with the action's execution.
void setMessageBar(QgsMessageBar *bar)
Sets the message bar associated with the action's execution.
QString name
Definition qgsmaplayer.h:80
A bar for displaying non-blocking messages to the user.
void pushMessage(const QString &text, Qgis::MessageLevel level=Qgis::MessageLevel::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:64
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.