Exemplo n.º 1
0
void QgsMapToolShowHideLabels::showHideLabels( QMouseEvent *e )
{
  QgsMapLayer *layer = mCanvas->currentLayer();
  QgsVectorLayer *vlayer = dynamic_cast<QgsVectorLayer *>( layer );
  if ( !vlayer )
    return;

  bool doHide = e->modifiers() & Qt::ShiftModifier;
  QString editTxt = doHide ? tr( "Hid labels" ) : tr( "Showed labels" );
  vlayer->beginEditCommand( editTxt );

  bool labelChanged = false;
  if ( doHide )
  {
    QList<QgsLabelPosition> positions;
    if ( selectedLabelFeatures( vlayer, positions ) )
    {
      for ( const QgsLabelPosition &pos : qgis::as_const( positions ) )
      {
        if ( showHide( pos, false ) )
          labelChanged = true;
      }
    }
  }
  else
  {
    QgsFeatureIds fids;
    if ( selectedFeatures( vlayer, fids ) )
    {
      for ( const QgsFeatureId &fid : qgis::as_const( fids ) )
      {
        QgsLabelPosition pos;
        pos.featureId = fid;
        pos.layerID = vlayer->id();

        // we want to show labels...
        pos.isDiagram = false;
        if ( showHide( pos, true ) )
          labelChanged = true;

        // ... and diagrams
        pos.isDiagram = true;
        if ( showHide( pos, true ) )
          labelChanged = true;
      }
    }
  }

  if ( labelChanged )
  {
    vlayer->endEditCommand();
    vlayer->triggerRepaint();
  }
  else
  {
    vlayer->destroyEditCommand();
  }
}
void QgsMapToolShowHideLabels::showHideLabels( QMouseEvent * e )
{

  if ( !mCanvas || mCanvas->isDrawing() )
  {
    QgsDebugMsg( "Canvas not ready" );
    return;
  }

  mRender = mCanvas->mapRenderer();
  if ( !mRender )
  {
    QgsDebugMsg( "Failed to acquire map renderer" );
    return;
  }

  QgsMapLayer* layer = mCanvas->currentLayer();

  QgsVectorLayer* vlayer = dynamic_cast<QgsVectorLayer*>( layer );
  if ( !vlayer )
  {
    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 ? true : false;

  QgsFeatureIds selectedFeatIds;

  if ( !doHide )
  {
    QgsDebugMsg( "Showing labels operation" );

    if ( !selectedFeatures( vlayer, selectedFeatIds ) )
    {
      return;
    }
  }
  else
  {
    QgsDebugMsg( "Hiding labels operation" );

    if ( !selectedLabelFeatures( vlayer, selectedFeatIds ) )
    {
      return;
    }
  }

  QgsDebugMsg( "Number of selected labels or features: " + QString::number( selectedFeatIds.size() ) );


  bool labelChanged = false;

  foreach ( const QgsFeatureId &fid, selectedFeatIds )
  {
    if ( showHideLabel( vlayer, fid, doHide ) )
    {
      // TODO: highlight features (maybe with QTimer?)
      labelChanged = true;
    }
  }

  if ( labelChanged )
  {
    mCanvas->refresh();
  }
}
Exemplo n.º 3
0
void QgsMapToolShowHideLabels::showHideLabels( QMouseEvent * e )
{
  QgsMapLayer* layer = mCanvas->currentLayer();

  QgsVectorLayer* vlayer = dynamic_cast<QgsVectorLayer*>( layer );
  if ( !vlayer )
  {
    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 ? true : false;

  QgsFeatureIds selectedFeatIds;

  if ( !doHide )
  {
    QgsDebugMsg( "Showing labels operation" );

    if ( !selectedFeatures( vlayer, selectedFeatIds ) )
    {
      return;
    }
  }
  else
  {
    QgsDebugMsg( "Hiding labels operation" );

    if ( !selectedLabelFeatures( vlayer, selectedFeatIds ) )
    {
      return;
    }
  }

  QgsDebugMsg( "Number of selected labels or features: " + QString::number( selectedFeatIds.size() ) );

  if ( selectedFeatIds.isEmpty() )
  {
    return;
  }

  bool labelChanged = false;
  QString editTxt = doHide ? tr( "Hid labels" ) : tr( "Showed labels" );

  vlayer->beginEditCommand( editTxt );
  Q_FOREACH ( const QgsFeatureId &fid, selectedFeatIds )
  {
    if ( showHideLabel( vlayer, fid, doHide ) )
    {
      // TODO: highlight features (maybe with QTimer?)
      labelChanged = true;
    }
  }

  if ( labelChanged )
  {
    vlayer->endEditCommand();
    mCanvas->refresh();
  }
  else
  {
    vlayer->destroyEditCommand();
  }
}