Skip to content

Commit

Permalink
Update map tools to work with auxiliary fields in not editable mode
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed Oct 9, 2017
1 parent 8826a8a commit 283d796
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 19 deletions.
3 changes: 1 addition & 2 deletions src/app/qgisapp.cpp
Expand Up @@ -10993,8 +10993,7 @@ void QgisApp::updateLabelToolButtons()
for ( QMap<QString, QgsMapLayer *>::iterator it = layers.begin(); it != layers.end(); ++it )
{
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( it.value() );
if ( !vlayer || !vlayer->isEditable() ||
( !vlayer->diagramsEnabled() && !vlayer->labelsEnabled() ) )
if ( !vlayer || ( !vlayer->diagramsEnabled() && !vlayer->labelsEnabled() ) )
continue;

int colX, colY, colShow, colAng;
Expand Down
121 changes: 111 additions & 10 deletions src/app/qgsmaptoollabel.cpp
Expand Up @@ -25,6 +25,9 @@
#include "qgsvectorlayerlabeling.h"
#include "qgsdiagramrenderer.h"
#include "qgssettings.h"
#include "qgsvectorlayerjoininfo.h"
#include "qgsvectorlayerjoinbuffer.h"
#include "qgsauxiliarystorage.h"

#include <QMouseEvent>

Expand Down Expand Up @@ -467,7 +470,7 @@ bool QgsMapToolLabel::currentLabelDataDefinedPosition( double &x, bool &xSuccess

bool QgsMapToolLabel::layerIsRotatable( QgsVectorLayer *vlayer, int &rotationCol ) const
{
if ( !vlayer || !vlayer->isEditable() || !vlayer->labeling() )
if ( !vlayer || !vlayer->labeling() )
{
return false;
}
Expand All @@ -485,7 +488,23 @@ bool QgsMapToolLabel::labelIsRotatable( QgsVectorLayer *layer, const QgsPalLayer
{
QString rColName = dataDefinedColumnName( QgsPalLayerSettings::LabelRotation, settings );
rotationCol = layer->fields().lookupField( rColName );
return rotationCol != -1;

if ( rotationCol >= 0 )
{
bool auxiliaryField = isAuxiliaryField( layer, rotationCol );

if ( !auxiliaryField )
{
if ( layer->isEditable() )
return true;
else
return false;
}
else
return true;
}

return false;
}


Expand Down Expand Up @@ -580,15 +599,34 @@ bool QgsMapToolLabel::diagramMoveable( QgsVectorLayer *vlayer, int &xCol, int &y
yCol = vlayer->fields().lookupField( ddY.field() );
}
}
return xCol >= 0 && yCol >= 0;

// diagrams may be moveable even if layer is not editable when data
// defined columns come from auxiliary storage
if ( xCol >= 0 && yCol >= 0 )
{
bool xAuxiliaryField = isAuxiliaryField( vlayer, xCol );
bool yAuxiliaryField = isAuxiliaryField( vlayer, yCol );

if ( ! xAuxiliaryField || ! yAuxiliaryField )
{
if ( vlayer->isEditable() )
return true;
else
return false;
}
else
return true;
}

return false;
}
}
return false;
}

bool QgsMapToolLabel::labelMoveable( QgsVectorLayer *vlayer, int &xCol, int &yCol ) const
{
if ( !vlayer || !vlayer->isEditable() || !vlayer->labeling() )
if ( !vlayer || !vlayer->labeling() )
{
return false;
}
Expand All @@ -606,10 +644,30 @@ bool QgsMapToolLabel::labelMoveable( QgsVectorLayer *vlayer, const QgsPalLayerSe
{
QString xColName = dataDefinedColumnName( QgsPalLayerSettings::PositionX, settings );
QString yColName = dataDefinedColumnName( QgsPalLayerSettings::PositionY, settings );

//return !xColName.isEmpty() && !yColName.isEmpty();
xCol = vlayer->fields().lookupField( xColName );
yCol = vlayer->fields().lookupField( yColName );
return ( xCol != -1 && yCol != -1 );

// labels may be moveable even if layer is not editable when data defined
// columns come from auxiliary storage
if ( xCol >= 0 && yCol >= 0 )
{
bool xAuxiliaryField = isAuxiliaryField( vlayer, xCol );
bool yAuxiliaryField = isAuxiliaryField( vlayer, yCol );

if ( ! xAuxiliaryField || ! yAuxiliaryField )
{
if ( vlayer->isEditable() )
return true;
else
return false;
}
else
return true;
}

return false;
}

bool QgsMapToolLabel::layerCanPin( QgsVectorLayer *vlayer, int &xCol, int &yCol ) const
Expand All @@ -621,7 +679,7 @@ bool QgsMapToolLabel::layerCanPin( QgsVectorLayer *vlayer, int &xCol, int &yCol

bool QgsMapToolLabel::labelCanShowHide( QgsVectorLayer *vlayer, int &showCol ) const
{
if ( !vlayer || !vlayer->isEditable() || !vlayer->labeling() )
if ( !vlayer || !vlayer->labeling() )
{
return false;
}
Expand All @@ -631,8 +689,20 @@ bool QgsMapToolLabel::labelCanShowHide( QgsVectorLayer *vlayer, int &showCol ) c
QString fieldname = dataDefinedColumnName( QgsPalLayerSettings::Show,
vlayer->labeling()->settings( providerId ) );
showCol = vlayer->fields().lookupField( fieldname );
if ( showCol != -1 )
return true;
if ( showCol >= 0 )
{
bool auxiliaryField = isAuxiliaryField( vlayer, showCol );

if ( ! auxiliaryField )
{
if ( vlayer->isEditable() )
return true;
else
return false;
}
else
return true;
}
}

return false;
Expand Down Expand Up @@ -665,7 +735,7 @@ bool QgsMapToolLabel::diagramCanShowHide( QgsVectorLayer *vlayer, int &showCol )
{
showCol = -1;

if ( vlayer && vlayer->isEditable() && vlayer->diagramsEnabled() )
if ( vlayer && vlayer->diagramsEnabled() )
{
if ( const QgsDiagramLayerSettings *dls = vlayer->diagramLayerSettings() )
{
Expand All @@ -679,7 +749,22 @@ bool QgsMapToolLabel::diagramCanShowHide( QgsVectorLayer *vlayer, int &showCol )
}
}

return showCol >= 0;
if ( showCol >= 0 )
{
bool auxiliaryField = isAuxiliaryField( vlayer, showCol );

if ( !auxiliaryField )
{
if ( vlayer->isEditable() )
return true;
else
return false;
}
else
return true;
}

return false;
}

//
Expand All @@ -704,3 +789,19 @@ QgsMapToolLabel::LabelDetails::LabelDetails( const QgsLabelPosition &p )
settings = QgsPalLayerSettings();
}
}

bool QgsMapToolLabel::isAuxiliaryField( QgsVectorLayer *layer, int index ) const
{
bool auxiliaryField = false;

if ( index >= 0 && layer->fields().fieldOrigin( index ) == QgsFields::OriginJoin )
{
int srcFieldIndex;
const QgsVectorLayerJoinInfo *info = layer->joinBuffer()->joinForFieldIndex( index, layer->fields(), srcFieldIndex );

if ( info && info->joinLayerId() == layer->auxiliaryLayer()->id() )
auxiliaryField = true;
}

return auxiliaryField;
}
2 changes: 2 additions & 0 deletions src/app/qgsmaptoollabel.h
Expand Up @@ -175,6 +175,8 @@ class APP_EXPORT QgsMapToolLabel: public QgsMapTool
\since QGIS 2.16
*/
bool isPinned();

bool isAuxiliaryField( QgsVectorLayer *layer, int index ) const;
};

#endif // QGSMAPTOOLLABEL_H
4 changes: 2 additions & 2 deletions src/app/qgsmaptoolmovelabel.cpp
Expand Up @@ -43,7 +43,7 @@ void QgsMapToolMoveLabel::canvasPressEvent( QgsMapMouseEvent *e )
mCurrentLabel = LabelDetails( labelPos );

QgsVectorLayer *vlayer = mCurrentLabel.layer;
if ( !vlayer || !vlayer->isEditable() )
if ( !vlayer )
{
return;
}
Expand Down Expand Up @@ -91,7 +91,7 @@ void QgsMapToolMoveLabel::canvasReleaseEvent( QgsMapMouseEvent *e )
deleteRubberBands();

QgsVectorLayer *vlayer = mCurrentLabel.layer;
if ( !vlayer || !vlayer->isEditable() )
if ( !vlayer )
{
return;
}
Expand Down
5 changes: 0 additions & 5 deletions src/app/qgsmaptoolshowhidelabels.cpp
Expand Up @@ -114,11 +114,6 @@ void QgsMapToolShowHideLabels::showHideLabels( QMouseEvent *e )
QgsDebugMsg( "Failed to cast label layer to vector layer" );
return;
}
if ( !vlayer->isEditable() )
{
QgsDebugMsg( "Vector layer not editable, skipping label" );
return;
}

bool doHide = e->modifiers() & Qt::ShiftModifier;
bool labelChanged = false;
Expand Down

0 comments on commit 283d796

Please sign in to comment.