QGIS API Documentation  2.99.0-Master (cb63e82)
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 "qgsactionmenu.h"
24 #include "qgssettings.h"
25 
26 QgsAttributeDialog::QgsAttributeDialog( QgsVectorLayer *vl, QgsFeature *thepFeature, bool featureOwner, QWidget *parent, bool showDialogButtons, const QgsAttributeEditorContext &context )
27  : QDialog( parent )
28  , mHighlight( nullptr )
29  , mOwnedFeature( featureOwner ? thepFeature : nullptr )
30 {
31  init( vl, thepFeature, context, showDialogButtons );
32 }
33 
35 {
36  if ( mHighlight )
37  {
38  mHighlight->hide();
39  delete mHighlight;
40  }
41 
42  if ( mOwnedFeature )
43  delete mOwnedFeature;
44 
45  saveGeometry();
46 }
47 
49 {
50  QgsSettings().setValue( mSettingsPath + "geometry", QDialog::saveGeometry() );
51 }
52 
54 {
55  QDialog::restoreGeometry( QgsSettings().value( mSettingsPath + "geometry" ).toByteArray() );
56 }
57 
59 {
60  delete mHighlight;
61 
62  mHighlight = h;
63 }
64 
66 {
67  mAttributeForm->save();
68  QDialog::accept();
69 }
70 
72 {
73  QDialog::show();
74  raise();
75  activateWindow();
76 }
77 
79 {
80  // Delete any actions on other layers that may have been triggered from this dialog
81  if ( mAttributeForm->mode() == QgsAttributeForm::AddFeatureMode )
82  mTrackedVectorLayerTools.rollback();
83 
84  QDialog::reject();
85 }
86 
87 void QgsAttributeDialog::init( QgsVectorLayer *layer, QgsFeature *feature, const QgsAttributeEditorContext &context, bool showDialogButtons )
88 {
89  QgsAttributeEditorContext trackedContext = context;
90  setWindowTitle( tr( "%1 - Feature Attributes" ).arg( layer->name() ) );
91  setLayout( new QGridLayout() );
92  layout()->setMargin( 0 );
93  mTrackedVectorLayerTools.setVectorLayerTools( trackedContext.vectorLayerTools() );
94  trackedContext.setVectorLayerTools( &mTrackedVectorLayerTools );
95  if ( showDialogButtons )
97 
98  mAttributeForm = new QgsAttributeForm( layer, *feature, trackedContext, this );
99  mAttributeForm->disconnectButtonBox();
100  layout()->addWidget( mAttributeForm );
101  QDialogButtonBox *buttonBox = mAttributeForm->findChild<QDialogButtonBox *>();
102  connect( buttonBox, &QDialogButtonBox::rejected, this, &QgsAttributeDialog::reject );
103  connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsAttributeDialog::accept );
104  connect( layer, &QObject::destroyed, this, &QWidget::close );
105 
106  QgsActionMenu *menu = new QgsActionMenu( layer, mAttributeForm->feature(), QStringLiteral( "AttributeTableRow" ), this );
107  if ( !menu->actions().isEmpty() )
108  {
109  QMenuBar *menuBar = new QMenuBar( this );
110  menuBar->addMenu( menu );
111  layout()->setMenuBar( menuBar );
112  }
113  else
114  {
115  delete menu;
116  }
117 
118  restoreGeometry();
119  focusNextChild();
120 }
121 
122 bool QgsAttributeDialog::event( QEvent *e )
123 {
124  if ( e->type() == QEvent::WindowActivate && mHighlight )
125  mHighlight->show();
126  else if ( e->type() == QEvent::WindowDeactivate && mHighlight )
127  mHighlight->hide();
128 
129  return QDialog::event( e );
130 }
const QgsVectorLayerTools * vectorLayerTools() const
void restoreGeometry()
Restores the size and position from the last time this dialog box was used.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:54
This class contains context information for attribute editor widgets.
void saveGeometry()
Saves the size and position for the next time this dialog box will be used.
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:61
void setVectorLayerTools(const QgsVectorLayerTools *tools)
Set the vector layer tools that will be used to interact with the data.
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:35
A class for highlight features on the map.
Definition: qgshighlight.h:39
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.
virtual 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.
const QgsFeature * feature()
const QgsFeature & feature()
QString name
Definition: qgsmaplayer.h:57
void accept() override
Represents a vector layer which manages a vector based data sets.