QGIS API Documentation  2.17.0-Master (f49f7ce)
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 
25 #include <QSettings>
26 
27 
28 QgsAttributeDialog::QgsAttributeDialog( QgsVectorLayer* vl, QgsFeature* thepFeature, bool featureOwner, const QgsDistanceArea &myDa, QWidget* parent, bool showDialogButtons )
29  : QDialog( parent )
30  , mHighlight( nullptr )
31  , mOwnedFeature( featureOwner ? thepFeature : nullptr )
32 {
34  context.setDistanceArea( myDa );
35 
36  init( vl, thepFeature, context, showDialogButtons );
37 }
38 
39 QgsAttributeDialog::QgsAttributeDialog( QgsVectorLayer* vl, QgsFeature* thepFeature, bool featureOwner, QWidget* parent, bool showDialogButtons, const QgsAttributeEditorContext &context )
40  : QDialog( parent )
41  , mHighlight( nullptr )
42  , mOwnedFeature( featureOwner ? thepFeature : nullptr )
43 {
44  init( vl, thepFeature, context, showDialogButtons );
45 }
46 
48 {
49  if ( mHighlight )
50  {
51  mHighlight->hide();
52  delete mHighlight;
53  }
54 
55  if ( mOwnedFeature )
56  delete mOwnedFeature;
57 
58  saveGeometry();
59 }
60 
62 {
63  QSettings().setValue( mSettingsPath + "geometry", QDialog::saveGeometry() );
64 }
65 
67 {
68  QDialog::restoreGeometry( QSettings().value( mSettingsPath + "geometry" ).toByteArray() );
69 }
70 
72 {
73  delete mHighlight;
74 
75  mHighlight = h;
76 }
77 
79 {
80  mAttributeForm->save();
82 }
83 
84 void QgsAttributeDialog::show( bool autoDelete )
85 {
86  if ( autoDelete )
87  setAttribute( Qt::WA_DeleteOnClose );
88 
89  QDialog::show();
90  raise();
92 }
93 
95 {
96  // Delete any actions on other layers that may have been triggered from this dialog
97  if ( mAttributeForm->mode() == QgsAttributeForm::AddFeatureMode )
98  mTrackedVectorLayerTools.rollback();
99 
100  QDialog::reject();
101 }
102 
103 void QgsAttributeDialog::init( QgsVectorLayer* layer, QgsFeature* feature, const QgsAttributeEditorContext& context, bool showDialogButtons )
104 {
105  QgsAttributeEditorContext trackedContext = context;
106  setWindowTitle( tr( "%1 - Feature Attributes" ).arg( layer->name() ) );
107  setLayout( new QGridLayout() );
108  layout()->setMargin( 0 );
109  mTrackedVectorLayerTools.setVectorLayerTools( trackedContext.vectorLayerTools() );
110  trackedContext.setVectorLayerTools( &mTrackedVectorLayerTools );
111  if ( showDialogButtons )
113 
114  mAttributeForm = new QgsAttributeForm( layer, *feature, trackedContext, this );
115  mAttributeForm->disconnectButtonBox();
116  layout()->addWidget( mAttributeForm );
117  QDialogButtonBox* buttonBox = mAttributeForm->findChild<QDialogButtonBox*>();
118  connect( buttonBox, SIGNAL( rejected() ), this, SLOT( reject() ) );
119  connect( buttonBox, SIGNAL( accepted() ), this, SLOT( accept() ) );
120  connect( layer, SIGNAL( layerDeleted() ), this, SLOT( close() ) );
121 
122  QgsActionMenu* menu = new QgsActionMenu( layer, &mAttributeForm->feature(), this );
123  if ( !menu->actions().isEmpty() )
124  {
125  QMenuBar* menuBar = new QMenuBar( this );
126  menuBar->addMenu( menu );
127  layout()->setMenuBar( menuBar );
128  }
129  else
130  {
131  delete menu;
132  }
133 
134  restoreGeometry();
135  focusNextChild();
136 }
137 
139 {
140  if ( e->type() == QEvent::WindowActivate && mHighlight )
141  mHighlight->show();
142  else if ( e->type() == QEvent::WindowDeactivate && mHighlight )
143  mHighlight->hide();
144 
145  return QDialog::event( e );
146 }
QLayout * layout() const
void setMenuBar(QWidget *widget)
bool close()
Q_DECL_DEPRECATED QgsAttributeDialog(QgsVectorLayer *vl, QgsFeature *thepFeature, bool featureOwner, const QgsDistanceArea &myDa, QWidget *parent=nullptr, bool showDialogButtons=true)
Create an attribute dialog for a given layer and feature.
virtual bool event(QEvent *e)
Type type() const
virtual void reject()
void restoreGeometry()
Restores the size and position from the last time this dialog box was used.
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.
void rejected()
bool save()
Save all the values from the editors to the layer.
void accepted()
void setAttribute(Qt::WidgetAttribute attribute, bool on)
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:187
void setVectorLayerTools(const QgsVectorLayerTools *tools)
Set the vector layer tools that will be used to interact with the data.
QString tr(const char *sourceText, const char *disambiguation, int n)
Mode mode() const
Returns the current mode of the form.
void setValue(const QString &key, const QVariant &value)
void setLayout(QLayout *layout)
bool restoreGeometry(const QByteArray &geometry)
This class is a menu that is populated automatically with the actions defined for a given layer...
Definition: qgsactionmenu.h:30
A class for highlight features on the map.
Definition: qgshighlight.h:37
void setDistanceArea(const QgsDistanceArea &distanceArea)
void setMargin(int margin)
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 addWidget(QWidget *w)
virtual void accept()
General purpose distance and area calculator.
virtual bool event(QEvent *e) override
Intercept window activate/deactive events to show/hide the highlighted feature.
void reject() override
QAction * addMenu(QMenu *menu)
void setFormMode(FormMode mode)
Sets the form mode.
void activateWindow()
QByteArray saveGeometry() const
void setWindowTitle(const QString &)
const QgsVectorLayerTools * vectorLayerTools() const
const QgsFeature * feature()
const QgsFeature & feature()
QString name
Read property of QString layerName.
Definition: qgsmaplayer.h:53
void accept() override
void show()
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QList< QAction * > actions() const
bool focusNextChild()
QObject * parent() const
Represents a vector layer which manages a vector based data sets.
T findChild(const QString &name) const