Skip to content

Commit 93e73ac

Browse files
committed
Make readOnly mode of vector layers configurable
1 parent 1c59eff commit 93e73ac

10 files changed

+64
-28
lines changed

python/core/qgsmaplayer.sip

+3
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ class QgsMapLayer : QObject
215215
/** Returns the current blending mode for a layer */
216216
QPainter::CompositionMode blendMode() const;
217217

218+
/** Returns if this layer is read only. */
219+
bool readOnly() const;
220+
218221
/** Synchronises with changes in the datasource
219222
*/
220223
virtual void reload();

src/app/qgisapp.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -7247,7 +7247,7 @@ bool QgisApp::toggleEditing( QgsMapLayer *layer, bool allowCancel )
72477247
if ( vlayer->isModified() || ( tg && tg->layers().contains( vlayer ) && tg->modified() ) )
72487248
isModified = true;
72497249

7250-
if ( !vlayer->isEditable() && !vlayer->isReadOnly() )
7250+
if ( !vlayer->isEditable() && !vlayer->readOnly() )
72517251
{
72527252
if ( !( vlayer->dataProvider()->capabilities() & QgsVectorDataProvider::EditingCapabilities ) )
72537253
{
@@ -10011,7 +10011,7 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer* layer )
1001110011

1001210012
mActionLayerSubsetString->setEnabled( !isEditable && dprovider->supportsSubsetString() );
1001310013

10014-
mActionToggleEditing->setEnabled( canSupportEditing && !vlayer->isReadOnly() );
10014+
mActionToggleEditing->setEnabled( canSupportEditing && !vlayer->readOnly() );
1001510015
mActionToggleEditing->setChecked( canSupportEditing && isEditable );
1001610016
mActionSaveLayerEdits->setEnabled( canSupportEditing && isEditable && vlayer->isModified() );
1001710017
mUndoWidget->dockContents()->setEnabled( canSupportEditing && isEditable );

src/app/qgsattributetabledialog.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *theLayer, QWid
193193
mToggleEditingButton->blockSignals( true );
194194
mToggleEditingButton->setCheckable( true );
195195
mToggleEditingButton->setChecked( mLayer->isEditable() );
196-
mToggleEditingButton->setEnabled(( canChangeAttributes || canDeleteFeatures || canAddAttributes || canDeleteAttributes || canAddFeatures ) && !mLayer->isReadOnly() );
196+
mToggleEditingButton->setEnabled(( canChangeAttributes || canDeleteFeatures || canAddAttributes || canDeleteAttributes || canAddFeatures ) && !mLayer->readOnly() );
197197
mToggleEditingButton->blockSignals( false );
198198

199199
mSaveEditsButton->setEnabled( mToggleEditingButton->isEnabled() && mLayer->isEditable() );

src/app/qgsfieldsproperties.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ void QgsFieldsProperties::updateButtons()
672672
{
673673
int cap = mLayer->dataProvider()->capabilities();
674674

675-
mToggleEditingButton->setEnabled(( cap & QgsVectorDataProvider::ChangeAttributeValues ) && !mLayer->isReadOnly() );
675+
mToggleEditingButton->setEnabled(( cap & QgsVectorDataProvider::ChangeAttributeValues ) && !mLayer->readOnly() );
676676

677677
if ( mLayer->isEditable() )
678678
{

src/app/qgsguivectorlayertools.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ bool QgsGuiVectorLayerTools::startEditing( QgsVectorLayer* layer ) const
5656

5757
bool res = true;
5858

59-
if ( !layer->isEditable() && !layer->isReadOnly() )
59+
if ( !layer->isEditable() && !layer->readOnly() )
6060
{
6161
if ( !( layer->dataProvider()->capabilities() & QgsVectorDataProvider::EditingCapabilities ) )
6262
{

src/app/qgsprojectproperties.cpp

+20-7
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,12 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas* mapCanvas, QWidget *pa
245245
mLayerSrsId = mProjectSrsId;
246246
}
247247

248-
twIdentifyLayers->setColumnCount( 3 );
248+
twIdentifyLayers->setColumnCount( 4 );
249249
twIdentifyLayers->horizontalHeader()->setVisible( true );
250250
twIdentifyLayers->setHorizontalHeaderItem( 0, new QTableWidgetItem( tr( "Layer" ) ) );
251251
twIdentifyLayers->setHorizontalHeaderItem( 1, new QTableWidgetItem( tr( "Type" ) ) );
252252
twIdentifyLayers->setHorizontalHeaderItem( 2, new QTableWidgetItem( tr( "Identifiable" ) ) );
253+
twIdentifyLayers->setHorizontalHeaderItem( 3, new QTableWidgetItem( tr( "Read Only" ) ) );
253254
twIdentifyLayers->setRowCount( mapLayers.size() );
254255
twIdentifyLayers->verticalHeader()->setResizeMode( QHeaderView::ResizeToContents );
255256

@@ -289,9 +290,16 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas* mapCanvas, QWidget *pa
289290
twi->setFlags( twi->flags() & ~Qt::ItemIsEditable );
290291
twIdentifyLayers->setItem( i, 1, twi );
291292

292-
QCheckBox *cb = new QCheckBox();
293-
cb->setChecked( !noIdentifyLayerIdList.contains( currentLayer->id() ) );
294-
twIdentifyLayers->setCellWidget( i, 2, cb );
293+
QCheckBox *cbIdentify = new QCheckBox();
294+
cbIdentify->setChecked( !noIdentifyLayerIdList.contains( currentLayer->id() ) );
295+
twIdentifyLayers->setCellWidget( i, 2, cbIdentify );
296+
297+
twi = new QTableWidgetItem( type );
298+
twi->setFlags( twi->flags() & ~Qt::ItemIsEditable );
299+
QCheckBox *cbReadOnly = new QCheckBox();
300+
cbReadOnly->setChecked( currentLayer->readOnly() );
301+
cbReadOnly->setEnabled( currentLayer->type() == QgsMapLayer::VectorLayer );
302+
twIdentifyLayers->setCellWidget( i, 3, cbReadOnly );
295303
}
296304

297305
grpOWSServiceCapabilities->setChecked( QgsProject::instance()->readBoolEntry( "WMSServiceCapabilities", "/", false ) );
@@ -880,12 +888,17 @@ void QgsProjectProperties::apply()
880888
QStringList noIdentifyLayerList;
881889
for ( int i = 0; i < twIdentifyLayers->rowCount(); i++ )
882890
{
883-
QCheckBox *cb = qobject_cast<QCheckBox *>( twIdentifyLayers->cellWidget( i, 2 ) );
884-
if ( cb && !cb->isChecked() )
891+
QString id = twIdentifyLayers->item( i, 0 )->data( Qt::UserRole ).toString();
892+
893+
QCheckBox *cbIdentify = qobject_cast<QCheckBox *>( twIdentifyLayers->cellWidget( i, 2 ) );
894+
if ( cbIdentify && !cbIdentify->isChecked() )
885895
{
886-
QString id = twIdentifyLayers->item( i, 0 )->data( Qt::UserRole ).toString();
887896
noIdentifyLayerList << id;
888897
}
898+
QCheckBox *cbReadOnly = qobject_cast<QCheckBox *>( twIdentifyLayers->cellWidget( i, 3 ) );
899+
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( id ) );
900+
if ( vl )
901+
vl->setReadOnly( cbReadOnly->checkState() == Qt::Checked );
889902
}
890903

891904
QgsProject::instance()->setNonIdentifiableLayers( noIdentifyLayerList );

src/core/qgsmaplayer.h

+9
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@ class CORE_EXPORT QgsMapLayer : public QObject
231231
/** Returns the current blending mode for a layer */
232232
QPainter::CompositionMode blendMode() const;
233233

234+
/** Returns if this layer is read only. */
235+
bool readOnly() const { return isReadOnly(); }
236+
234237
/** Synchronises with changes in the datasource
235238
*/
236239
virtual void reload() {}
@@ -735,6 +738,12 @@ class CORE_EXPORT QgsMapLayer : public QObject
735738
QgsError mError;
736739

737740
private:
741+
/**
742+
* This method returns true by default but can be overwritten to specify
743+
* that a certain layer is writable.
744+
*/
745+
virtual bool isReadOnly() const { return true; }
746+
738747
/** Layer's spatial reference system.
739748
private to make sure setCrs must be used and layerCrsChanged() is emitted */
740749
QgsCoordinateReferenceSystem* mCRS;

src/core/qgsvectorlayer.h

+9-3
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ struct CORE_EXPORT QgsVectorJoinInfo
379379
*
380380
* Provider to display vector data in a GRASS GIS layer.
381381
*
382-
* TODO QGIS3: Remove virtual from non-inherited methods (like isModified, isReadOnly)
382+
* TODO QGIS3: Remove virtual from non-inherited methods (like isModified)
383383
*/
384384

385385

@@ -1090,8 +1090,14 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
10901090

10911091
virtual bool isSpatial() const override;
10921092

1093-
/** Returns true if the provider is in read-only mode */
1094-
virtual bool isReadOnly() const;
1093+
/**
1094+
* Returns true if the provider is in read-only mode
1095+
*
1096+
* @deprecated Use readOnly() instead. Will be made private with QGIS 3
1097+
*
1098+
* TODO QGIS3: make private
1099+
*/
1100+
Q_DECL_DEPRECATED virtual bool isReadOnly() const override;
10951101

10961102
/** Returns true if the provider has been modified since the last commit */
10971103
virtual bool isModified() const;

src/gui/qgsrelationeditorwidget.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ void QgsRelationEditorWidget::setRelationFeature( const QgsRelation& relation, c
146146
QgsVectorLayer* lyr = relation.referencingLayer();
147147

148148
bool canChangeAttributes = lyr->dataProvider()->capabilities() & QgsVectorDataProvider::ChangeAttributeValues;
149-
if ( canChangeAttributes && !lyr->isReadOnly() )
149+
if ( canChangeAttributes && !lyr->readOnly() )
150150
{
151151
mToggleEditingButton->setEnabled( true );
152152
updateButtons();
@@ -205,7 +205,7 @@ void QgsRelationEditorWidget::setRelations( const QgsRelation& relation, const Q
205205
QgsVectorLayer* lyr = relation.referencingLayer();
206206

207207
bool canChangeAttributes = lyr->dataProvider()->capabilities() & QgsVectorDataProvider::ChangeAttributeValues;
208-
if ( canChangeAttributes && !lyr->isReadOnly() )
208+
if ( canChangeAttributes && !lyr->readOnly() )
209209
{
210210
mToggleEditingButton->setEnabled( true );
211211
updateButtons();

src/ui/qgsprojectpropertiesbase.ui

+16-11
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@
218218
</sizepolicy>
219219
</property>
220220
<property name="currentIndex">
221-
<number>0</number>
221+
<number>2</number>
222222
</property>
223223
<widget class="QWidget" name="mProjOpts_01">
224224
<layout class="QVBoxLayout" name="verticalLayout_6">
@@ -248,7 +248,7 @@
248248
<x>0</x>
249249
<y>0</y>
250250
<width>683</width>
251-
<height>779</height>
251+
<height>776</height>
252252
</rect>
253253
</property>
254254
<layout class="QVBoxLayout" name="verticalLayout_8">
@@ -749,8 +749,8 @@
749749
<rect>
750750
<x>0</x>
751751
<y>0</y>
752-
<width>326</width>
753-
<height>46</height>
752+
<width>683</width>
753+
<height>776</height>
754754
</rect>
755755
</property>
756756
<layout class="QVBoxLayout" name="verticalLayout_7">
@@ -808,8 +808,8 @@
808808
<rect>
809809
<x>0</x>
810810
<y>0</y>
811-
<width>133</width>
812-
<height>100</height>
811+
<width>683</width>
812+
<height>776</height>
813813
</rect>
814814
</property>
815815
<layout class="QVBoxLayout" name="verticalLayout_10">
@@ -851,6 +851,11 @@
851851
<string>Identifiable</string>
852852
</property>
853853
</column>
854+
<column>
855+
<property name="text">
856+
<string>Read Only</string>
857+
</property>
858+
</column>
854859
</widget>
855860
</item>
856861
</layout>
@@ -889,8 +894,8 @@
889894
<rect>
890895
<x>0</x>
891896
<y>0</y>
892-
<width>379</width>
893-
<height>582</height>
897+
<width>381</width>
898+
<height>607</height>
894899
</rect>
895900
</property>
896901
<layout class="QVBoxLayout" name="verticalLayout_12">
@@ -1314,8 +1319,8 @@
13141319
<rect>
13151320
<x>0</x>
13161321
<y>0</y>
1317-
<width>663</width>
1318-
<height>2249</height>
1322+
<width>642</width>
1323+
<height>2374</height>
13191324
</rect>
13201325
</property>
13211326
<layout class="QVBoxLayout" name="verticalLayout_13">
@@ -2380,7 +2385,7 @@
23802385
<rect>
23812386
<x>0</x>
23822387
<y>0</y>
2383-
<width>168</width>
2388+
<width>166</width>
23842389
<height>46</height>
23852390
</rect>
23862391
</property>

0 commit comments

Comments
 (0)