QGIS API Documentation 3.39.0-Master (47f7b3a4989)
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
20#include "qgsattributeform.h"
21#include "qgshighlight.h"
22#include "qgssettings.h"
23#include "qgsmessagebar.h"
24#include "qgsactionmenu.h"
26
27QgsAttributeDialog::QgsAttributeDialog( QgsVectorLayer *vl, QgsFeature *thepFeature, bool featureOwner, QWidget *parent, bool showDialogButtons, const QgsAttributeEditorContext &context )
28 : QDialog( parent )
29 , mOwnedFeature( featureOwner ? thepFeature : nullptr )
30{
31 init( vl, thepFeature, context, showDialogButtons );
32}
33
35{
36 if ( mHighlight )
37 {
38 mHighlight->hide();
39 delete mHighlight;
40 }
41
42 delete mOwnedFeature;
43
44 saveGeometry();
45}
46
47void QgsAttributeDialog::saveGeometry()
48{
49 // WARNING!!!! Don't use QgsGui::enableAutoGeometryRestore for this dialog -- the object name
50 // is dynamic and is set to match the layer/feature combination.
51 QgsSettings().setValue( QStringLiteral( "Windows/AttributeDialog/geometry" ), QDialog::saveGeometry() );
52}
53
54void QgsAttributeDialog::restoreGeometry()
55{
56 // WARNING!!!! Don't use QgsGui::enableAutoGeometryRestore for this dialog -- the object name
57 // is dynamic and is set to match the layer/feature combination.
58 QDialog::restoreGeometry( QgsSettings().value( QStringLiteral( "Windows/AttributeDialog/geometry" ) ).toByteArray() );
59}
60
62{
63 delete mHighlight;
64
65 mHighlight = h;
66}
67
69{
70 QString error;
71 const bool didSave = mAttributeForm->saveWithDetails( &error );
72 if ( didSave )
73 {
74 QDialog::accept();
75 }
76 else
77 {
78 if ( error.isEmpty() )
79 error = tr( "An unknown error was encountered saving attributes" );
80
81 mMessageBar->pushMessage( QString(),
82 error,
84 }
85}
86
88{
89 QDialog::show();
90
91 raise();
92 activateWindow();
93}
94
95void QgsAttributeDialog::showEvent( QShowEvent *event )
96{
97 QDialog::showEvent( event );
98 // We cannot call restoreGeometry() in the constructor or init because the dialog is not yet visible
99 // and the geometry restoration will not take the window decorations (frame) into account.
100 if ( mFirstShow )
101 {
102 mFirstShow = false;
103 restoreGeometry();
104 }
105}
106
108{
109 // Delete any actions on other layers that may have been triggered from this dialog
110 if ( mAttributeForm->mode() == QgsAttributeEditorContext::AddFeatureMode )
111 mTrackedVectorLayerTools.rollback();
112
113 QDialog::reject();
114}
115
116void QgsAttributeDialog::init( QgsVectorLayer *layer, QgsFeature *feature, const QgsAttributeEditorContext &context, bool showDialogButtons )
117{
118 QgsAttributeEditorContext trackedContext = context;
119 setWindowTitle( tr( "%1 - Feature Attributes" ).arg( layer->name() ) );
120 setLayout( new QGridLayout() );
121 layout()->setContentsMargins( 0, 0, 0, 0 );
122 mMessageBar = new QgsMessageBar( this );
123 mMessageBar->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed );
124 layout()->addWidget( mMessageBar );
125
126 setLayout( layout() );
127
128 mTrackedVectorLayerTools.setVectorLayerTools( trackedContext.vectorLayerTools() );
129 trackedContext.setVectorLayerTools( &mTrackedVectorLayerTools );
130 if ( showDialogButtons )
132
133 mAttributeForm = new QgsAttributeForm( layer, *feature, trackedContext, this );
134 mAttributeForm->disconnectButtonBox();
135 layout()->addWidget( mAttributeForm );
136 QDialogButtonBox *buttonBox = mAttributeForm->findChild<QDialogButtonBox *>();
137 connect( buttonBox, &QDialogButtonBox::rejected, this, &QgsAttributeDialog::reject );
138 connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsAttributeDialog::accept );
139 connect( layer, &QObject::destroyed, this, &QWidget::close );
140
141 mMenu = new QgsActionMenu( layer, mAttributeForm->feature(), QStringLiteral( "Feature" ), this );
142 mMenu->setActionContextGenerator( this );
143 if ( !mMenu->isEmpty() )
144 {
145 mMenuBar = new QMenuBar( this );
146 mMenuBar->addMenu( mMenu );
147 layout()->setMenuBar( mMenuBar );
148 }
149 focusNextChild();
150}
151
153{
154 mAttributeForm->setMode( mode );
155 mMenu->setMode( mode );
156
157 if ( !mMenu->isEmpty() && !mMenuBar )
158 {
159 mMenuBar = new QMenuBar( this );
160 mMenuBar->addMenu( mMenu );
161 layout()->setMenuBar( mMenuBar );
162 }
163 else if ( mMenu->isEmpty() && mMenuBar )
164 {
165 layout()->setMenuBar( nullptr );
166 delete mMenuBar;
167 mMenuBar = nullptr;
168 }
169}
170
172{
173 if ( e->type() == QEvent::WindowActivate && mHighlight )
174 mHighlight->show();
175 else if ( e->type() == QEvent::WindowDeactivate && mHighlight )
176 mHighlight->hide();
177
178 return QDialog::event( e );
179}
180
182{
183 mAttributeForm->setExtraContextScope( extraScope );
184}
185
187{
189 context.setAttributeDialog( this );
190 context.setMessageBar( mMessageBar );
191 return context;
192}
193
@ Critical
Critical/error message.
Definition qgis.h:102
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:79
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.