QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
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 raise();
91 activateWindow();
92}
93
95{
96 // Delete any actions on other layers that may have been triggered from this dialog
97 if ( mAttributeForm->mode() == QgsAttributeEditorContext::AddFeatureMode )
98 mTrackedVectorLayerTools.rollback();
99
100 QDialog::reject();
101}
102
103void QgsAttributeDialog::init( QgsVectorLayer *layer, QgsFeature *feature, const QgsAttributeEditorContext &context, bool showDialogButtons )
104{
105 QgsAttributeEditorContext trackedContext = context;
106 setWindowTitle( tr( "%1 - Feature Attributes" ).arg( layer->name() ) );
107 setLayout( new QGridLayout() );
108 layout()->setContentsMargins( 0, 0, 0, 0 );
109 mMessageBar = new QgsMessageBar( this );
110 mMessageBar->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed );
111 layout()->addWidget( mMessageBar );
112
113 setLayout( layout() );
114
115 mTrackedVectorLayerTools.setVectorLayerTools( trackedContext.vectorLayerTools() );
116 trackedContext.setVectorLayerTools( &mTrackedVectorLayerTools );
117 if ( showDialogButtons )
119
120 mAttributeForm = new QgsAttributeForm( layer, *feature, trackedContext, this );
121 mAttributeForm->disconnectButtonBox();
122 layout()->addWidget( mAttributeForm );
123 QDialogButtonBox *buttonBox = mAttributeForm->findChild<QDialogButtonBox *>();
124 connect( buttonBox, &QDialogButtonBox::rejected, this, &QgsAttributeDialog::reject );
125 connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsAttributeDialog::accept );
126 connect( layer, &QObject::destroyed, this, &QWidget::close );
127
128 mMenu = new QgsActionMenu( layer, mAttributeForm->feature(), QStringLiteral( "Feature" ), this );
129 mMenu->setActionContextGenerator( this );
130 if ( !mMenu->isEmpty() )
131 {
132 mMenuBar = new QMenuBar( this );
133 mMenuBar->addMenu( mMenu );
134 layout()->setMenuBar( mMenuBar );
135 }
136
137 restoreGeometry();
138 focusNextChild();
139}
140
142{
143 mAttributeForm->setMode( mode );
144 mMenu->setMode( mode );
145
146 if ( !mMenu->isEmpty() && !mMenuBar )
147 {
148 mMenuBar = new QMenuBar( this );
149 mMenuBar->addMenu( mMenu );
150 layout()->setMenuBar( mMenuBar );
151 }
152 else if ( mMenu->isEmpty() && mMenuBar )
153 {
154 layout()->setMenuBar( nullptr );
155 delete mMenuBar;
156 mMenuBar = nullptr;
157 }
158}
159
161{
162 if ( e->type() == QEvent::WindowActivate && mHighlight )
163 mHighlight->show();
164 else if ( e->type() == QEvent::WindowDeactivate && mHighlight )
165 mHighlight->hide();
166
167 return QDialog::event( e );
168}
169
171{
172 mAttributeForm->setExtraContextScope( extraScope );
173}
174
176{
178 context.setAttributeDialog( this );
179 context.setMessageBar( mMessageBar );
180 return context;
181}
182
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 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.
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:76
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:63
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.