QGIS API Documentation  3.9.0-Master (224899f119)
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
6  email : marco.hugentobler@autoform.ch
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 "qgsapplication.h"
23 #include "qgssettings.h"
24 
25 QgsAttributeDialog::QgsAttributeDialog( QgsVectorLayer *vl, QgsFeature *thepFeature, bool featureOwner, QWidget *parent, bool showDialogButtons, const QgsAttributeEditorContext &context )
26  : QDialog( parent )
27  , mOwnedFeature( featureOwner ? thepFeature : nullptr )
28 {
29  init( vl, thepFeature, context, showDialogButtons );
30 }
31 
33 {
34  if ( mHighlight )
35  {
36  mHighlight->hide();
37  delete mHighlight;
38  }
39 
40  delete mOwnedFeature;
41 
42  saveGeometry();
43 }
44 
45 void QgsAttributeDialog::saveGeometry()
46 {
47  // WARNING!!!! Don't use QgsGui::enableAutoGeometryRestore for this dialog -- the object name
48  // is dynamic and is set to match the layer/feature combination.
49  QgsSettings().setValue( QStringLiteral( "Windows/AttributeDialog/geometry" ), QDialog::saveGeometry() );
50 }
51 
52 void QgsAttributeDialog::restoreGeometry()
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  QDialog::restoreGeometry( QgsSettings().value( QStringLiteral( "Windows/AttributeDialog/geometry" ) ).toByteArray() );
57 }
58 
60 {
61  delete mHighlight;
62 
63  mHighlight = h;
64 }
65 
67 {
68  mAttributeForm->save();
69  QDialog::accept();
70 }
71 
73 {
74  QDialog::show();
75  raise();
76  activateWindow();
77 }
78 
80 {
81  // Delete any actions on other layers that may have been triggered from this dialog
82  if ( mAttributeForm->mode() == QgsAttributeEditorContext::AddFeatureMode )
83  mTrackedVectorLayerTools.rollback();
84 
85  QDialog::reject();
86 }
87 
88 void QgsAttributeDialog::init( QgsVectorLayer *layer, QgsFeature *feature, const QgsAttributeEditorContext &context, bool showDialogButtons )
89 {
90  QgsAttributeEditorContext trackedContext = context;
91  setWindowTitle( tr( "%1 - Feature Attributes" ).arg( layer->name() ) );
92  setLayout( new QGridLayout() );
93  layout()->setMargin( 0 );
94  mTrackedVectorLayerTools.setVectorLayerTools( trackedContext.vectorLayerTools() );
95  trackedContext.setVectorLayerTools( &mTrackedVectorLayerTools );
96  if ( showDialogButtons )
98 
99  mAttributeForm = new QgsAttributeForm( layer, *feature, trackedContext, this );
100  mAttributeForm->disconnectButtonBox();
101  layout()->addWidget( mAttributeForm );
102  QDialogButtonBox *buttonBox = mAttributeForm->findChild<QDialogButtonBox *>();
103  connect( buttonBox, &QDialogButtonBox::rejected, this, &QgsAttributeDialog::reject );
104  connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsAttributeDialog::accept );
105  connect( layer, &QObject::destroyed, this, &QWidget::close );
106 
107  mMenu = new QgsActionMenu( layer, mAttributeForm->feature(), QStringLiteral( "Feature" ), this );
108  if ( !mMenu->actions().isEmpty() )
109  {
110  QMenuBar *menuBar = new QMenuBar( this );
111  menuBar->addMenu( mMenu );
112  layout()->setMenuBar( menuBar );
113  }
114 
115  restoreGeometry();
116  focusNextChild();
117 }
118 
120 {
121  mAttributeForm->setMode( mode );
122  mMenu->setMode( mode );
123 }
124 
125 bool QgsAttributeDialog::event( QEvent *e )
126 {
127  if ( e->type() == QEvent::WindowActivate && mHighlight )
128  mHighlight->show();
129  else if ( e->type() == QEvent::WindowDeactivate && mHighlight )
130  mHighlight->hide();
131 
132  return QDialog::event( e );
133 }
const QgsVectorLayerTools * vectorLayerTools() const
Returns the associated vector layer tools.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:58
This class contains context information for attribute editor widgets.
bool save()
Save all the values from the editors to the layer.
void setHighlight(QgsHighlight *h)
setHighlight
void setVectorLayerTools(QgsVectorLayerTools *vlTools)
Sets the associated vector layer tools.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
void setVectorLayerTools(const QgsVectorLayerTools *tools)
Set the vector layer tools that will be used to interact with the data.
void setMode(QgsAttributeEditorContext::Mode mode)
Change the mode of the actions.
void saveGeometry(QWidget *widget, const QString &keyName)
Save the wigget geometry into settings.
bool restoreGeometry(QWidget *widget, const QString &keyName)
Restore the wigget geometry from settings.
void show()
Show the dialog non-blocking. Reparents this dialog to be a child of the dialog form.
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.
QgsAttributeEditorContext::Mode mode() const
Returns the current mode of the form.
This class is a menu that is populated automatically with the actions defined for a given layer...
Definition: qgsactionmenu.h:38
A class for highlight features on the map.
Definition: qgshighlight.h:49
void setMode(QgsAttributeEditorContext::Mode mode)
Toggles the form mode.
void disconnectButtonBox()
Disconnects the button box (OK/Cancel) from the accept/resetValues slots If this method is called...
void rollback()
Delete all features which have been added via this object.
void setMode(QgsAttributeEditorContext::Mode mode)
Sets the current mode of the form.
bool event(QEvent *e) override
Intercept window activate/deactivate events to show/hide the highlighted feature. ...
void reject() override
void setFormMode(FormMode mode)
Sets the form mode.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
const QgsFeature * feature()
const QgsFeature & feature()
QString name
Definition: qgsmaplayer.h:83
void accept() override
Represents a vector layer which manages a vector based data sets.