QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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,
83 Qgis::MessageLevel::Critical );
84 }
85}
86
88{
89 QDialog::show();
90
91 // We cannot call restoreGeometry() in the constructor or init because the dialog is not yet visible
92 // and the geometry restoration will not take the window decorations (frame) into account.
93 if ( mFirstShow )
94 {
95 mFirstShow = false;
96 restoreGeometry();
97 }
98 raise();
99 activateWindow();
100}
101
103{
104 // Delete any actions on other layers that may have been triggered from this dialog
105 if ( mAttributeForm->mode() == QgsAttributeEditorContext::AddFeatureMode )
106 mTrackedVectorLayerTools.rollback();
107
108 QDialog::reject();
109}
110
111void QgsAttributeDialog::init( QgsVectorLayer *layer, QgsFeature *feature, const QgsAttributeEditorContext &context, bool showDialogButtons )
112{
113 QgsAttributeEditorContext trackedContext = context;
114 setWindowTitle( tr( "%1 - Feature Attributes" ).arg( layer->name() ) );
115 setLayout( new QGridLayout() );
116 layout()->setContentsMargins( 0, 0, 0, 0 );
117 mMessageBar = new QgsMessageBar( this );
118 mMessageBar->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed );
119 layout()->addWidget( mMessageBar );
120
121 setLayout( layout() );
122
123 mTrackedVectorLayerTools.setVectorLayerTools( trackedContext.vectorLayerTools() );
124 trackedContext.setVectorLayerTools( &mTrackedVectorLayerTools );
125 if ( showDialogButtons )
127
128 mAttributeForm = new QgsAttributeForm( layer, *feature, trackedContext, this );
129 mAttributeForm->disconnectButtonBox();
130 layout()->addWidget( mAttributeForm );
131 QDialogButtonBox *buttonBox = mAttributeForm->findChild<QDialogButtonBox *>();
132 connect( buttonBox, &QDialogButtonBox::rejected, this, &QgsAttributeDialog::reject );
133 connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsAttributeDialog::accept );
134 connect( layer, &QObject::destroyed, this, &QWidget::close );
135
136 mMenu = new QgsActionMenu( layer, mAttributeForm->feature(), QStringLiteral( "Feature" ), this );
137 mMenu->setActionContextGenerator( this );
138 if ( !mMenu->isEmpty() )
139 {
140 mMenuBar = new QMenuBar( this );
141 mMenuBar->addMenu( mMenu );
142 layout()->setMenuBar( mMenuBar );
143 }
144 focusNextChild();
145}
146
148{
149 mAttributeForm->setMode( mode );
150 mMenu->setMode( mode );
151
152 if ( !mMenu->isEmpty() && !mMenuBar )
153 {
154 mMenuBar = new QMenuBar( this );
155 mMenuBar->addMenu( mMenu );
156 layout()->setMenuBar( mMenuBar );
157 }
158 else if ( mMenu->isEmpty() && mMenuBar )
159 {
160 layout()->setMenuBar( nullptr );
161 delete mMenuBar;
162 mMenuBar = nullptr;
163 }
164}
165
167{
168 if ( e->type() == QEvent::WindowActivate && mHighlight )
169 mHighlight->show();
170 else if ( e->type() == QEvent::WindowDeactivate && mHighlight )
171 mHighlight->hide();
172
173 return QDialog::event( e );
174}
175
177{
178 mAttributeForm->setExtraContextScope( extraScope );
179}
180
182{
184 context.setAttributeDialog( this );
185 context.setMessageBar( mMessageBar );
186 return context;
187}
188
This class is a menu that is populated automatically with the actions defined for a given layer.
Definition: qgsactionmenu.h:39
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.
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 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.
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:56
A class for highlight features on the map.
Definition: qgshighlight.h:62
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:78
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:61
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.
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.