QGIS API Documentation 3.99.0-Master (d270888f95f)
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 "qgsactionmenu.h"
21#include "qgsattributeform.h"
22#include "qgshighlight.h"
24#include "qgsmessagebar.h"
25#include "qgssettings.h"
26
27#include <QString>
28
29#include "moc_qgsattributedialog.cpp"
30
31using namespace Qt::StringLiterals;
32
33QgsAttributeDialog::QgsAttributeDialog( QgsVectorLayer *vl, QgsFeature *thepFeature, bool featureOwner, QWidget *parent, bool showDialogButtons, const QgsAttributeEditorContext &context )
34 : QDialog( parent )
35 , mOwnedFeature( featureOwner ? thepFeature : nullptr )
36{
37 init( vl, thepFeature, context, showDialogButtons );
38}
39
41{
42 if ( mHighlight )
43 {
44 mHighlight->hide();
45 delete mHighlight;
46 }
47
48 delete mOwnedFeature;
49
50 saveGeometry();
51}
52
53void QgsAttributeDialog::saveGeometry()
54{
55 // WARNING!!!! Don't use QgsGui::enableAutoGeometryRestore for this dialog -- the object name
56 // is dynamic and is set to match the layer/feature combination.
57 QgsSettings().setValue( u"Windows/AttributeDialog/geometry"_s, QDialog::saveGeometry() );
58}
59
60void QgsAttributeDialog::restoreGeometry()
61{
62 // WARNING!!!! Don't use QgsGui::enableAutoGeometryRestore for this dialog -- the object name
63 // is dynamic and is set to match the layer/feature combination.
64 QDialog::restoreGeometry( QgsSettings().value( u"Windows/AttributeDialog/geometry"_s ).toByteArray() );
65}
66
68{
69 delete mHighlight;
70
71 mHighlight = h;
72}
73
75{
76 QString error;
77 const bool didSave = mAttributeForm->saveWithDetails( &error );
78 if ( didSave )
79 {
80 QDialog::accept();
81 }
82 else
83 {
84 if ( error.isEmpty() )
85 error = tr( "An unknown error was encountered saving attributes" );
86
87 mMessageBar->pushMessage( QString(), error, Qgis::MessageLevel::Critical );
88 }
89}
90
92{
93 QDialog::show();
94
95 raise();
96 activateWindow();
97}
98
100{
101 QDialog::showEvent( event );
102 // We cannot call restoreGeometry() in the constructor or init because the dialog is not yet visible
103 // and the geometry restoration will not take the window decorations (frame) into account.
104 if ( mFirstShow )
105 {
106 mFirstShow = false;
107 restoreGeometry();
108 }
109}
110
112{
113 // Delete any actions on other layers that may have been triggered from this dialog
114 if ( mAttributeForm->mode() == QgsAttributeEditorContext::AddFeatureMode )
115 mTrackedVectorLayerTools.rollback();
116
117 QDialog::reject();
118}
119
120void QgsAttributeDialog::init( QgsVectorLayer *layer, QgsFeature *feature, const QgsAttributeEditorContext &context, bool showDialogButtons )
121{
122 QgsAttributeEditorContext trackedContext = context;
123 setWindowTitle( tr( "%1 - Feature Attributes" ).arg( layer->name() ) );
124 setLayout( new QGridLayout() );
125 layout()->setContentsMargins( 0, 0, 0, 0 );
126 mMessageBar = new QgsMessageBar( this );
127 mMessageBar->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed );
128 layout()->addWidget( mMessageBar );
129
130 setLayout( layout() );
131
132 mTrackedVectorLayerTools.setVectorLayerTools( trackedContext.vectorLayerTools() );
133 trackedContext.setVectorLayerTools( &mTrackedVectorLayerTools );
134 if ( showDialogButtons )
136
137 mAttributeForm = new QgsAttributeForm( layer, *feature, trackedContext, this );
138 mAttributeForm->disconnectButtonBox();
139 layout()->addWidget( mAttributeForm );
140 QDialogButtonBox *buttonBox = mAttributeForm->findChild<QDialogButtonBox *>();
141 connect( buttonBox, &QDialogButtonBox::rejected, this, &QgsAttributeDialog::reject );
142 connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsAttributeDialog::accept );
143 connect( layer, &QObject::destroyed, this, &QWidget::close );
144
145 mMenu = new QgsActionMenu( layer, mAttributeForm->feature(), u"Feature"_s, this );
146 mMenu->setActionContextGenerator( this );
147 if ( !mMenu->isEmpty() )
148 {
149 mMenuBar = new QMenuBar( this );
150 mMenuBar->addMenu( mMenu );
151 layout()->setMenuBar( mMenuBar );
152 }
153 focusNextChild();
154}
155
157{
158 mAttributeForm->setMode( mode );
159 mMenu->setMode( mode );
160
161 if ( !mMenu->isEmpty() && !mMenuBar )
162 {
163 mMenuBar = new QMenuBar( this );
164 mMenuBar->addMenu( mMenu );
165 layout()->setMenuBar( mMenuBar );
166 }
167 else if ( mMenu->isEmpty() && mMenuBar )
168 {
169 layout()->setMenuBar( nullptr );
170 delete mMenuBar;
171 mMenuBar = nullptr;
172 }
173}
174
176{
177 if ( e->type() == QEvent::WindowActivate && mHighlight )
178 mHighlight->show();
179 else if ( e->type() == QEvent::WindowDeactivate && mHighlight )
180 mHighlight->hide();
181
182 return QDialog::event( e );
183}
184
186{
187 mAttributeForm->setExtraContextScope( extraScope );
188}
189
191{
193 context.setAttributeDialog( this );
194 context.setMessageBar( mMessageBar );
195 return context;
196}
@ Critical
Critical/error message.
Definition qgis.h:162
A menu that is populated automatically with the actions defined for a given layer.
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() const
Returns the current feature of the attribute form.
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.
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.
The attribute form widget for vector layer features.
void disconnectButtonBox()
Disconnects the button box (OK/Cancel) from the accept/resetValues slots If this method is called,...
const QgsFeature & feature() const
Returns feature of attribute 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:60
Highlights 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:87
A bar for displaying non-blocking messages to the user.
Stores settings for use within QGIS.
Definition qgssettings.h:68
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.
Represents a vector layer which manages a vector based dataset.