Skip to content

Commit 2d07fa6

Browse files
committedApr 15, 2013
Dual View: Commit feature edits to edit buffer, when the attribute table is closed
or when the view mode is changed or when the focus is lost
1 parent f9a6f0a commit 2d07fa6

File tree

4 files changed

+121
-33
lines changed

4 files changed

+121
-33
lines changed
 

‎src/core/qgsvectorlayer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2491,6 +2491,8 @@ bool QgsVectorLayer::commitChanges()
24912491
return false;
24922492
}
24932493

2494+
emit beforeCommitChanges();
2495+
24942496
bool success = mEditBuffer->commitChanges( mCommitErrors );
24952497

24962498
if ( success )

‎src/core/qgsvectorlayer.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,8 +1242,15 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
12421242
/** This signal is emitted when modifications has been done on layer */
12431243
void layerModified();
12441244

1245+
/** Is emitted, when editing on this layer has started*/
12451246
void editingStarted();
1247+
1248+
/** Is emitted, when edited changes succesfully have been written to the data provider */
12461249
void editingStopped();
1250+
1251+
/** Is emitted, before changes are commited to the data provider */
1252+
void beforeCommitChanges();
1253+
12471254
/**
12481255
* Will be emitted, when a new attribute has been added to this vector layer.
12491256
* Applies only to types {@link QgsFields::OriginEdit} and {@link QgsFields::OriginProvider}
@@ -1267,6 +1274,8 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
12671274
/**
12681275
* Is emitted, whenever the fields available from this layer have been changed.
12691276
* This can be due to manually adding attributes or due to a join.
1277+
*
1278+
* @note Added in 2.0
12701279
*/
12711280
void updatedFields();
12721281
void layerDeleted();

‎src/gui/attributetable/qgsdualview.cpp

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ QgsDualView::QgsDualView( QWidget* parent )
4747
connect( mActionExpressionPreview, SIGNAL( triggered() ), SLOT( previewExpressionBuilder() ) );
4848
connect( mPreviewActionMapper, SIGNAL( mapped( QObject* ) ), SLOT( previewColumnChanged( QObject* ) ) );
4949
connect( mFeatureList, SIGNAL( displayExpressionChanged( QString ) ), this, SLOT( previewExpressionChanged( QString ) ) );
50+
connect( this, SIGNAL( currentChanged(int) ), this, SLOT( saveEditChanges() ) );
5051
}
5152

5253
QgsDualView::~QgsDualView()
@@ -61,7 +62,7 @@ void QgsDualView::init( QgsVectorLayer* layer, QgsMapCanvas* mapCanvas, QgsDista
6162
connect( mTableView, SIGNAL( willShowContextMenu( QMenu*, QModelIndex ) ), this, SLOT( viewWillShowContextMenu( QMenu*, QModelIndex ) ) );
6263

6364
connect( layer, SIGNAL( editingStarted() ), this, SLOT( editingToggled() ) );
64-
connect( layer, SIGNAL( editingStopped() ), this, SLOT( editingToggled() ) );
65+
connect( layer, SIGNAL( beforeCommitChanges() ), this, SLOT( editingToggled() ) );
6566

6667
initLayerCache( layer );
6768
initModels( mapCanvas );
@@ -170,6 +171,18 @@ void QgsDualView::columnBoxInit()
170171
}
171172
}
172173

174+
void QgsDualView::hideEvent( QHideEvent* event )
175+
{
176+
saveEditChanges();
177+
QStackedWidget::hideEvent( event );
178+
}
179+
180+
void QgsDualView::focusOutEvent( QFocusEvent* event )
181+
{
182+
saveEditChanges();
183+
QStackedWidget::focusOutEvent( event );
184+
}
185+
173186
void QgsDualView::setView( QgsDualView::ViewMode view )
174187
{
175188
setCurrentIndex( view );
@@ -226,6 +239,26 @@ void QgsDualView::on_mFeatureList_currentEditSelectionChanged( const QgsFeature
226239
// Backup old dialog and delete only after creating the new dialog, so we can "hot-swap" the contained QgsFeature
227240
QgsAttributeDialog* oldDialog = mAttributeDialog;
228241

242+
if ( mAttributeDialog->dialog() )
243+
{
244+
saveEditChanges();
245+
mAttributeEditorLayout->removeWidget( mAttributeDialog->dialog() );
246+
}
247+
248+
mAttributeDialog = new QgsAttributeDialog( mLayerCache->layer(), new QgsFeature( feat ), true, mDistanceArea, this, false );
249+
mAttributeEditorLayout->addWidget( mAttributeDialog->dialog() );
250+
mAttributeDialog->dialog()->setVisible( true );
251+
252+
delete oldDialog;
253+
}
254+
255+
void QgsDualView::setCurrentEditSelection( const QgsFeatureIds& fids )
256+
{
257+
mFeatureList->setEditSelection( fids );
258+
}
259+
260+
void QgsDualView::saveEditChanges()
261+
{
229262
if ( mAttributeDialog->dialog() )
230263
{
231264
if ( mLayerCache->layer()->isEditable() )
@@ -253,20 +286,7 @@ void QgsDualView::on_mFeatureList_currentEditSelectionChanged( const QgsFeature
253286

254287
mLayerCache->layer()->endEditCommand();
255288
}
256-
257-
mAttributeEditorLayout->removeWidget( mAttributeDialog->dialog() );
258289
}
259-
260-
mAttributeDialog = new QgsAttributeDialog( mLayerCache->layer(), new QgsFeature( feat ), true, mDistanceArea, this, false );
261-
mAttributeEditorLayout->addWidget( mAttributeDialog->dialog() );
262-
mAttributeDialog->dialog()->setVisible( true );
263-
264-
delete oldDialog;
265-
}
266-
267-
void QgsDualView::setCurrentEditSelection( const QgsFeatureIds& fids )
268-
{
269-
mFeatureList->setEditSelection( fids );
270290
}
271291

272292
void QgsDualView::previewExpressionBuilder()
@@ -388,7 +408,6 @@ void QgsDualView::finished()
388408
mProgressDlg = 0;
389409
}
390410

391-
392411
/*
393412
* QgsAttributeTableAction
394413
*/

‎src/gui/attributetable/qgsdualview.h

Lines changed: 76 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ class QgsAttributeDialog;
2828
class QSignalMapper;
2929

3030
/**
31-
* @brief
31+
* This widget is used to show the attributes of a set of features of a {@link QgsVectorLayer}.
32+
* The attributes can be edited.
33+
* It supports two different layouts: the table layout, in which the attributes for the features
34+
* are shown in a table and the editor layout, where the features are shown as a selectable list
35+
* and the attributes for the currently selected feature are shown in a form.
36+
*
37+
* @note Added in 2.0
3238
*/
3339
class GUI_EXPORT QgsDualView : public QStackedWidget, private Ui::QgsDualViewBase
3440
{
@@ -37,65 +43,115 @@ class GUI_EXPORT QgsDualView : public QStackedWidget, private Ui::QgsDualViewBas
3743
public:
3844

3945
/**
40-
* @brief
46+
* The view modes, in which this widget can present information.
47+
* Relates to the QStackedWidget stacks.
4148
*
4249
*/
4350
enum ViewMode
4451
{
52+
/**
53+
* Shows the features and attributes in a table layout
54+
*/
4555
AttributeTable = 0,
56+
/**
57+
* Show a list of the features, where one can be chosen
58+
* and the according attribute dialog will be presented
59+
* in the neighbouring frame.
60+
*/
4661
AttributeEditor = 1
4762
};
4863

4964
/**
50-
* @brief
51-
*
52-
* @param parent
65+
* @brief Constructor
66+
* @param parent The parent widget
5367
*/
5468
explicit QgsDualView( QWidget* parent = 0 );
5569
virtual ~QgsDualView();
5670

71+
/**
72+
* Has to be called to initialize the dual view.
73+
*
74+
* @param layer The layer which should be used to fetch features
75+
* @param mapCanvas The mapCanvas (used for the FilterMode
76+
* {@link QgsAttributeTableFilterModel::ShowVisible}
77+
* @param myDa Used for attribute dialog creation
78+
*/
5779
void init( QgsVectorLayer* layer, QgsMapCanvas* mapCanvas, QgsDistanceArea myDa );
58-
void columnBoxInit();
5980

6081
/**
61-
* @brief
82+
* Change the current view mode.
6283
*
63-
* @param view
84+
* @param The view mode to set
6485
*/
6586
void setView( ViewMode view );
6687

6788
/**
68-
* @brief
89+
* Set the filter mode
6990
*
7091
* @param filterMode
7192
*/
7293
void setFilterMode( QgsAttributeTableFilterModel::FilterMode filterMode );
7394

74-
void setSelectedOnTop( bool selectedOnTop );
75-
7695
/**
77-
* @brief
96+
* Toggle the selectedOnTop flag. If enabled, selected features will be moved to top.
7897
*
79-
* @param featureRequest
98+
* @param selectedOnTop True: Show selected features on top.
99+
* False: Use defined sorting column.
80100
*/
81-
void setFeatureRequest( const QgsFeatureRequest* featureRequest );
101+
void setSelectedOnTop( bool selectedOnTop );
82102

83103
/**
84-
* @brief
104+
* Returns the number of features on the layer.
85105
*
106+
* @return Number of features
86107
*/
87-
void setSelectionMode();
88-
89108
int featureCount();
90109

110+
/**
111+
* Returns the number of features which are currently visible, according to the
112+
* filter restrictions
113+
*
114+
* @return Number of features
115+
*/
91116
int filteredFeatureCount();
92117

118+
/**
119+
* Set a list of currently visible features
120+
*
121+
* @param filteredFeatures A list of feature ids
122+
*
123+
*/
93124
void setFilteredFeatures( QgsFeatureIds filteredFeatures );
94125

126+
/**
127+
* Returns the model which has the information about all features (not only filtered)
128+
*
129+
* @return The master model
130+
*/
95131
QgsAttributeTableModel* masterModel() const { return mMasterModel; }
96132

133+
protected:
134+
/**
135+
* Initializes widgets which depend on the attributes of this layer
136+
*/
137+
void columnBoxInit();
138+
139+
virtual void hideEvent(QHideEvent *);
140+
virtual void focusOutEvent(QFocusEvent *);
141+
97142
public slots:
98-
void setCurrentEditSelection( const QgsFeatureIds& fids );
143+
/**
144+
* @brief Set the current edit selection in the {@link AttributeEditor} mode.
145+
*
146+
* @param fids A list of edited features (Currently only one at a time is supported)
147+
*/
148+
void setCurrentEditSelection( const QgsFeatureIds& fids );
149+
150+
/**
151+
* @brief saveEditChanges
152+
*/
153+
154+
void saveEditChanges();
99155

100156
signals:
101157
/**
@@ -112,6 +168,8 @@ class GUI_EXPORT QgsDualView : public QStackedWidget, private Ui::QgsDualViewBas
112168
private slots:
113169
/**
114170
* Changes the currently visible feature within the attribute editor
171+
*
172+
* @param feat The newly visible feature
115173
*/
116174
void on_mFeatureList_currentEditSelectionChanged( const QgsFeature &feat );
117175

0 commit comments

Comments
 (0)
Please sign in to comment.