QGIS API Documentation  3.0.2-Girona (307d082)
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 : [email protected]
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  if ( mOwnedFeature )
41  delete mOwnedFeature;
42 
43  saveGeometry();
44 }
45 
46 void QgsAttributeDialog::saveGeometry()
47 {
48  // WARNING!!!! Don't use QgsGui::enableAutoGeometryRestore for this dialog -- the object name
49  // is dynamic and is set to match the layer/feature combination.
50  QgsSettings().setValue( QStringLiteral( "Windows/AttributeDialog/geometry" ), QDialog::saveGeometry() );
51 }
52 
53 void QgsAttributeDialog::restoreGeometry()
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  QDialog::restoreGeometry( QgsSettings().value( QStringLiteral( "Windows/AttributeDialog/geometry" ) ).toByteArray() );
58 }
59 
61 {
62  delete mHighlight;
63 
64  mHighlight = h;
65 }
66 
68 {
69  mAttributeForm->save();
70  QDialog::accept();
71 }
72 
74 {
75  QDialog::show();
76  raise();
77  activateWindow();
78 }
79 
81 {
82  // Delete any actions on other layers that may have been triggered from this dialog
83  if ( mAttributeForm->mode() == QgsAttributeForm::AddFeatureMode )
84  mTrackedVectorLayerTools.rollback();
85 
86  QDialog::reject();
87 }
88 
89 void QgsAttributeDialog::init( QgsVectorLayer *layer, QgsFeature *feature, const QgsAttributeEditorContext &context, bool showDialogButtons )
90 {
91  QgsAttributeEditorContext trackedContext = context;
92  setWindowTitle( tr( "%1 - Feature Attributes" ).arg( layer->name() ) );
93  setLayout( new QGridLayout() );
94  layout()->setMargin( 0 );
95  mTrackedVectorLayerTools.setVectorLayerTools( trackedContext.vectorLayerTools() );
96  trackedContext.setVectorLayerTools( &mTrackedVectorLayerTools );
97  if ( showDialogButtons )
99 
100  mAttributeForm = new QgsAttributeForm( layer, *feature, trackedContext, this );
101  mAttributeForm->disconnectButtonBox();
102  layout()->addWidget( mAttributeForm );
103  QDialogButtonBox *buttonBox = mAttributeForm->findChild<QDialogButtonBox *>();
104  connect( buttonBox, &QDialogButtonBox::rejected, this, &QgsAttributeDialog::reject );
105  connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsAttributeDialog::accept );
106  connect( layer, &QObject::destroyed, this, &QWidget::close );
107 
108  mMenu = new QgsActionMenu( layer, mAttributeForm->feature(), QStringLiteral( "Feature" ), this );
109  if ( !mMenu->actions().isEmpty() )
110  {
111  QMenuBar *menuBar = new QMenuBar( this );
112  menuBar->addMenu( mMenu );
113  layout()->setMenuBar( menuBar );
114  }
115 
116  restoreGeometry();
117  focusNextChild();
118 }
119 
121 {
122  mAttributeForm->setMode( mode );
123  mMenu->setMode( mode );
124 }
125 
126 bool QgsAttributeDialog::event( QEvent *e )
127 {
128  if ( e->type() == QEvent::WindowActivate && mHighlight )
129  mHighlight->show();
130  else if ( e->type() == QEvent::WindowDeactivate && mHighlight )
131  mHighlight->hide();
132 
133  return QDialog::event( e );
134 }
const QgsVectorLayerTools * vectorLayerTools() const
This class is a composition of two QSettings instances:
Definition: qgssettings.h:57
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)
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
void setVectorLayerTools(const QgsVectorLayerTools *tools)
Set the vector layer tools that will be used to interact with the data.
void saveGeometry(QWidget *widget, const QString &keyName)
Save the wigget geometry into settings.
void setMode(Mode mode)
Sets the current mode of the form.
bool restoreGeometry(QWidget *widget, const QString &keyName)
Restore the wigget geometry from settings.
void setValue(const QString &key, const QVariant &value, const QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
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.
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 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.
bool event(QEvent *e) override
Intercept window activate/deactive events to show/hide the highlighted feature.
void reject() override
void setFormMode(FormMode mode)
Sets the form mode.
Mode mode() const
Returns the current mode of the form.
void setMode(const QgsAttributeForm::Mode mode)
Change the mode of the actions.
const QgsFeature * feature()
const QgsFeature & feature()
QString name
Definition: qgsmaplayer.h:60
void accept() override
void setMode(QgsAttributeForm::Mode mode)
Toggles the form mode.
Represents a vector layer which manages a vector based data sets.