Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make readOnly mode of vector layers configurable
  • Loading branch information
m-kuhn committed Mar 29, 2016
1 parent 1c59eff commit 93e73ac
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 28 deletions.
3 changes: 3 additions & 0 deletions python/core/qgsmaplayer.sip
Expand Up @@ -215,6 +215,9 @@ class QgsMapLayer : QObject
/** Returns the current blending mode for a layer */
QPainter::CompositionMode blendMode() const;

/** Returns if this layer is read only. */
bool readOnly() const;

/** Synchronises with changes in the datasource
*/
virtual void reload();
Expand Down
4 changes: 2 additions & 2 deletions src/app/qgisapp.cpp
Expand Up @@ -7247,7 +7247,7 @@ bool QgisApp::toggleEditing( QgsMapLayer *layer, bool allowCancel )
if ( vlayer->isModified() || ( tg && tg->layers().contains( vlayer ) && tg->modified() ) )
isModified = true;

if ( !vlayer->isEditable() && !vlayer->isReadOnly() )
if ( !vlayer->isEditable() && !vlayer->readOnly() )
{
if ( !( vlayer->dataProvider()->capabilities() & QgsVectorDataProvider::EditingCapabilities ) )
{
Expand Down Expand Up @@ -10011,7 +10011,7 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer* layer )

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

mActionToggleEditing->setEnabled( canSupportEditing && !vlayer->isReadOnly() );
mActionToggleEditing->setEnabled( canSupportEditing && !vlayer->readOnly() );
mActionToggleEditing->setChecked( canSupportEditing && isEditable );
mActionSaveLayerEdits->setEnabled( canSupportEditing && isEditable && vlayer->isModified() );
mUndoWidget->dockContents()->setEnabled( canSupportEditing && isEditable );
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsattributetabledialog.cpp
Expand Up @@ -193,7 +193,7 @@ QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *theLayer, QWid
mToggleEditingButton->blockSignals( true );
mToggleEditingButton->setCheckable( true );
mToggleEditingButton->setChecked( mLayer->isEditable() );
mToggleEditingButton->setEnabled(( canChangeAttributes || canDeleteFeatures || canAddAttributes || canDeleteAttributes || canAddFeatures ) && !mLayer->isReadOnly() );
mToggleEditingButton->setEnabled(( canChangeAttributes || canDeleteFeatures || canAddAttributes || canDeleteAttributes || canAddFeatures ) && !mLayer->readOnly() );
mToggleEditingButton->blockSignals( false );

mSaveEditsButton->setEnabled( mToggleEditingButton->isEnabled() && mLayer->isEditable() );
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsfieldsproperties.cpp
Expand Up @@ -672,7 +672,7 @@ void QgsFieldsProperties::updateButtons()
{
int cap = mLayer->dataProvider()->capabilities();

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

if ( mLayer->isEditable() )
{
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsguivectorlayertools.cpp
Expand Up @@ -56,7 +56,7 @@ bool QgsGuiVectorLayerTools::startEditing( QgsVectorLayer* layer ) const

bool res = true;

if ( !layer->isEditable() && !layer->isReadOnly() )
if ( !layer->isEditable() && !layer->readOnly() )
{
if ( !( layer->dataProvider()->capabilities() & QgsVectorDataProvider::EditingCapabilities ) )
{
Expand Down
27 changes: 20 additions & 7 deletions src/app/qgsprojectproperties.cpp
Expand Up @@ -245,11 +245,12 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas* mapCanvas, QWidget *pa
mLayerSrsId = mProjectSrsId;
}

twIdentifyLayers->setColumnCount( 3 );
twIdentifyLayers->setColumnCount( 4 );
twIdentifyLayers->horizontalHeader()->setVisible( true );
twIdentifyLayers->setHorizontalHeaderItem( 0, new QTableWidgetItem( tr( "Layer" ) ) );
twIdentifyLayers->setHorizontalHeaderItem( 1, new QTableWidgetItem( tr( "Type" ) ) );
twIdentifyLayers->setHorizontalHeaderItem( 2, new QTableWidgetItem( tr( "Identifiable" ) ) );
twIdentifyLayers->setHorizontalHeaderItem( 3, new QTableWidgetItem( tr( "Read Only" ) ) );
twIdentifyLayers->setRowCount( mapLayers.size() );
twIdentifyLayers->verticalHeader()->setResizeMode( QHeaderView::ResizeToContents );

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

QCheckBox *cb = new QCheckBox();
cb->setChecked( !noIdentifyLayerIdList.contains( currentLayer->id() ) );
twIdentifyLayers->setCellWidget( i, 2, cb );
QCheckBox *cbIdentify = new QCheckBox();
cbIdentify->setChecked( !noIdentifyLayerIdList.contains( currentLayer->id() ) );
twIdentifyLayers->setCellWidget( i, 2, cbIdentify );

twi = new QTableWidgetItem( type );
twi->setFlags( twi->flags() & ~Qt::ItemIsEditable );
QCheckBox *cbReadOnly = new QCheckBox();
cbReadOnly->setChecked( currentLayer->readOnly() );
cbReadOnly->setEnabled( currentLayer->type() == QgsMapLayer::VectorLayer );
twIdentifyLayers->setCellWidget( i, 3, cbReadOnly );
}

grpOWSServiceCapabilities->setChecked( QgsProject::instance()->readBoolEntry( "WMSServiceCapabilities", "/", false ) );
Expand Down Expand Up @@ -880,12 +888,17 @@ void QgsProjectProperties::apply()
QStringList noIdentifyLayerList;
for ( int i = 0; i < twIdentifyLayers->rowCount(); i++ )
{
QCheckBox *cb = qobject_cast<QCheckBox *>( twIdentifyLayers->cellWidget( i, 2 ) );
if ( cb && !cb->isChecked() )
QString id = twIdentifyLayers->item( i, 0 )->data( Qt::UserRole ).toString();

QCheckBox *cbIdentify = qobject_cast<QCheckBox *>( twIdentifyLayers->cellWidget( i, 2 ) );
if ( cbIdentify && !cbIdentify->isChecked() )
{
QString id = twIdentifyLayers->item( i, 0 )->data( Qt::UserRole ).toString();
noIdentifyLayerList << id;
}
QCheckBox *cbReadOnly = qobject_cast<QCheckBox *>( twIdentifyLayers->cellWidget( i, 3 ) );
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( id ) );
if ( vl )
vl->setReadOnly( cbReadOnly->checkState() == Qt::Checked );
}

QgsProject::instance()->setNonIdentifiableLayers( noIdentifyLayerList );
Expand Down
9 changes: 9 additions & 0 deletions src/core/qgsmaplayer.h
Expand Up @@ -231,6 +231,9 @@ class CORE_EXPORT QgsMapLayer : public QObject
/** Returns the current blending mode for a layer */
QPainter::CompositionMode blendMode() const;

/** Returns if this layer is read only. */
bool readOnly() const { return isReadOnly(); }

/** Synchronises with changes in the datasource
*/
virtual void reload() {}
Expand Down Expand Up @@ -735,6 +738,12 @@ class CORE_EXPORT QgsMapLayer : public QObject
QgsError mError;

private:
/**
* This method returns true by default but can be overwritten to specify
* that a certain layer is writable.
*/
virtual bool isReadOnly() const { return true; }

/** Layer's spatial reference system.
private to make sure setCrs must be used and layerCrsChanged() is emitted */
QgsCoordinateReferenceSystem* mCRS;
Expand Down
12 changes: 9 additions & 3 deletions src/core/qgsvectorlayer.h
Expand Up @@ -379,7 +379,7 @@ struct CORE_EXPORT QgsVectorJoinInfo
*
* Provider to display vector data in a GRASS GIS layer.
*
* TODO QGIS3: Remove virtual from non-inherited methods (like isModified, isReadOnly)
* TODO QGIS3: Remove virtual from non-inherited methods (like isModified)
*/


Expand Down Expand Up @@ -1090,8 +1090,14 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer

virtual bool isSpatial() const override;

/** Returns true if the provider is in read-only mode */
virtual bool isReadOnly() const;
/**
* Returns true if the provider is in read-only mode
*
* @deprecated Use readOnly() instead. Will be made private with QGIS 3
*
* TODO QGIS3: make private
*/
Q_DECL_DEPRECATED virtual bool isReadOnly() const override;

/** Returns true if the provider has been modified since the last commit */
virtual bool isModified() const;
Expand Down
4 changes: 2 additions & 2 deletions src/gui/qgsrelationeditorwidget.cpp
Expand Up @@ -146,7 +146,7 @@ void QgsRelationEditorWidget::setRelationFeature( const QgsRelation& relation, c
QgsVectorLayer* lyr = relation.referencingLayer();

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

bool canChangeAttributes = lyr->dataProvider()->capabilities() & QgsVectorDataProvider::ChangeAttributeValues;
if ( canChangeAttributes && !lyr->isReadOnly() )
if ( canChangeAttributes && !lyr->readOnly() )
{
mToggleEditingButton->setEnabled( true );
updateButtons();
Expand Down
27 changes: 16 additions & 11 deletions src/ui/qgsprojectpropertiesbase.ui
Expand Up @@ -218,7 +218,7 @@
</sizepolicy>
</property>
<property name="currentIndex">
<number>0</number>
<number>2</number>
</property>
<widget class="QWidget" name="mProjOpts_01">
<layout class="QVBoxLayout" name="verticalLayout_6">
Expand Down Expand Up @@ -248,7 +248,7 @@
<x>0</x>
<y>0</y>
<width>683</width>
<height>779</height>
<height>776</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_8">
Expand Down Expand Up @@ -749,8 +749,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>326</width>
<height>46</height>
<width>683</width>
<height>776</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_7">
Expand Down Expand Up @@ -808,8 +808,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>133</width>
<height>100</height>
<width>683</width>
<height>776</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_10">
Expand Down Expand Up @@ -851,6 +851,11 @@
<string>Identifiable</string>
</property>
</column>
<column>
<property name="text">
<string>Read Only</string>
</property>
</column>
</widget>
</item>
</layout>
Expand Down Expand Up @@ -889,8 +894,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>379</width>
<height>582</height>
<width>381</width>
<height>607</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_12">
Expand Down Expand Up @@ -1314,8 +1319,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>663</width>
<height>2249</height>
<width>642</width>
<height>2374</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_13">
Expand Down Expand Up @@ -2380,7 +2385,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>168</width>
<width>166</width>
<height>46</height>
</rect>
</property>
Expand Down

0 comments on commit 93e73ac

Please sign in to comment.