QGIS API Documentation  3.13.0-Master (5a3b1ced84)
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 #include "qgsmessagebar.h"
25 
26 QgsAttributeDialog::QgsAttributeDialog( QgsVectorLayer *vl, QgsFeature *thepFeature, bool featureOwner, QWidget *parent, bool showDialogButtons, const QgsAttributeEditorContext &context )
27  : QDialog( parent )
28  , mOwnedFeature( featureOwner ? thepFeature : nullptr )
29 {
30  init( vl, thepFeature, context, showDialogButtons );
31 }
32 
34 {
35  if ( mHighlight )
36  {
37  mHighlight->hide();
38  delete mHighlight;
39  }
40 
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  bool didSave = mAttributeForm->save();
70  if ( didSave )
71  {
72  QDialog::accept();
73  }
74  else
75  {
76  mMessageBar->pushMessage( QString(),
77  tr( "Your JSON value is invalid and has not been saved" ),
78  Qgis::MessageLevel::Critical,
79  5 );
80  }
81 }
82 
84 {
85  QDialog::show();
86  raise();
87  activateWindow();
88 }
89 
91 {
92  // Delete any actions on other layers that may have been triggered from this dialog
93  if ( mAttributeForm->mode() == QgsAttributeEditorContext::AddFeatureMode )
94  mTrackedVectorLayerTools.rollback();
95 
96  QDialog::reject();
97 }
98 
99 void QgsAttributeDialog::init( QgsVectorLayer *layer, QgsFeature *feature, const QgsAttributeEditorContext &context, bool showDialogButtons )
100 {
101  QgsAttributeEditorContext trackedContext = context;
102  setWindowTitle( tr( "%1 - Feature Attributes" ).arg( layer->name() ) );
103  setLayout( new QGridLayout() );
104  layout()->setMargin( 0 );
105  mMessageBar = new QgsMessageBar( this );
106  mMessageBar->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed );
107  layout()->addWidget( mMessageBar );
108 
109  setLayout( layout() );
110 
111  mTrackedVectorLayerTools.setVectorLayerTools( trackedContext.vectorLayerTools() );
112  trackedContext.setVectorLayerTools( &mTrackedVectorLayerTools );
113  if ( showDialogButtons )
115 
116  mAttributeForm = new QgsAttributeForm( layer, *feature, trackedContext, this );
117  mAttributeForm->disconnectButtonBox();
118  layout()->addWidget( mAttributeForm );
119  QDialogButtonBox *buttonBox = mAttributeForm->findChild<QDialogButtonBox *>();
120  connect( buttonBox, &QDialogButtonBox::rejected, this, &QgsAttributeDialog::reject );
121  connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsAttributeDialog::accept );
122  connect( layer, &QObject::destroyed, this, &QWidget::close );
123 
124  mMenu = new QgsActionMenu( layer, mAttributeForm->feature(), QStringLiteral( "Feature" ), this );
125  if ( !mMenu->menuActions().isEmpty() )
126  {
127  QMenuBar *menuBar = new QMenuBar( this );
128  menuBar->addMenu( mMenu );
129  layout()->setMenuBar( menuBar );
130  }
131 
132  restoreGeometry();
133  focusNextChild();
134 }
135 
137 {
138  mAttributeForm->setMode( mode );
139  mMenu->setMode( mode );
140 }
141 
142 bool QgsAttributeDialog::event( QEvent *e )
143 {
144  if ( e->type() == QEvent::WindowActivate && mHighlight )
145  mHighlight->show();
146  else if ( e->type() == QEvent::WindowDeactivate && mHighlight )
147  mHighlight->hide();
148 
149  return QDialog::event( e );
150 }
const QgsVectorLayerTools * vectorLayerTools() const
Returns the associated vector layer tools.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:61
This class contains context information for attribute editor widgets.
bool save()
Save all the values from the editors to the layer.
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:45
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:37
A class for highlight features on the map.
Definition: qgshighlight.h:56
void setMode(QgsAttributeEditorContext::Mode mode)
Toggles the form mode.
void pushMessage(const QString &text, Qgis::MessageLevel level=Qgis::Info, int duration=5)
convenience method for pushing a message to the bar
Definition: qgsmessagebar.h:88
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:84
void accept() override
QList< QgsAction > menuActions()
Returns menu actions.
Represents a vector layer which manages a vector based data sets.