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