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