Skip to content

Commit

Permalink
Moved some legend actions to QgisApp, added actions to layer tree view.
Browse files Browse the repository at this point in the history
Still need to adjust action implementations in QgisApp to use layer tree view
  • Loading branch information
wonder-sk committed May 21, 2014
1 parent f2b69e9 commit ca97459
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 163 deletions.
123 changes: 1 addition & 122 deletions src/app/legend/qgslegend.cpp
Expand Up @@ -885,7 +885,7 @@ void QgsLegend::handleRightClickEvent( QTreeWidgetItem* item, const QPoint& posi
theMenu.addAction( QgsApplication::getThemeIcon( "/mActionRemoveLayer.svg" ), tr( "&Remove" ), QgisApp::instance(), SLOT( removeLayer() ) );

theMenu.addAction( QgsApplication::getThemeIcon( "/mActionSetCRS.png" ),
tr( "&Set Group CRS" ), this, SLOT( legendGroupSetCRS() ) );
tr( "&Set Group CRS" ), QgisApp::instance(), SLOT( legendGroupSetCRS() ) );
}

if (( li->type() == QgsLegendItem::LEGEND_LAYER || li->type() == QgsLegendItem::LEGEND_GROUP ) && !groupEmbedded( li ) && !parentGroupEmbedded( li ) )
Expand Down Expand Up @@ -1621,29 +1621,6 @@ void QgsLegend::legendGroupRemove()
mMapCanvas->freeze( false );
}

void QgsLegend::legendGroupSetCRS()
{
if ( !mMapCanvas )
{
return;
}

QgsGenericProjectionSelector * mySelector = new QgsGenericProjectionSelector( this );
mySelector->setMessage();
if ( mySelector->exec() )
{
QgsCoordinateReferenceSystem crs( mySelector->selectedCrsId(), QgsCoordinateReferenceSystem::InternalCrsId );

QgsLegendGroup* lg = dynamic_cast<QgsLegendGroup *>( currentItem() );
setGroupCRS( lg, crs );
}
else
{
QApplication::restoreOverrideCursor();
}

delete mySelector;
}

void QgsLegend::removeGroup( QgsLegendGroup *lg )
{
Expand Down Expand Up @@ -1675,22 +1652,6 @@ void QgsLegend::removeGroup( QgsLegendGroup *lg )
adjustIconSize();
}

void QgsLegend::setGroupCRS( QgsLegendGroup *lg, const QgsCoordinateReferenceSystem &crs )
{
if ( !mMapCanvas )
{
return;
}

foreach ( QgsLegendLayer *cl, lg->legendLayers() )
{
if ( cl )
{
cl->layer()->setCrs( crs );
}
}
}

void QgsLegend::moveLayer( QgsMapLayer *ml, int groupIndex )
{
if ( !ml )
Expand Down Expand Up @@ -2834,62 +2795,6 @@ void QgsLegend::legendLayerZoom()
mMapCanvas->refresh();
}

void QgsLegend::legendLayerZoomNative()
{
//find current Layer
QgsLegendLayer* currentLayer = dynamic_cast<QgsLegendLayer *>( currentItem() );
if ( !currentLayer )
return;

QgsRasterLayer *layer = qobject_cast<QgsRasterLayer *>( currentLayer->layer() );
if ( layer )
{
QgsDebugMsg( "Raster units per pixel : " + QString::number( layer->rasterUnitsPerPixelX() ) );
QgsDebugMsg( "MapUnitsPerPixel before : " + QString::number( mMapCanvas->mapUnitsPerPixel() ) );

if ( mMapCanvas->hasCrsTransformEnabled() )
{
// get legth of central canvas pixel width in source raster crs
QgsRectangle e = mMapCanvas->extent();
QSize s = mMapCanvas->mapSettings().outputSize();
QgsPoint p1( e.center().x(), e.center().y() );
QgsPoint p2( e.center().x() + e.width() / s.width(), e.center().y() + e.height() / s.height() );
QgsCoordinateTransform ct( mMapCanvas->mapSettings().destinationCrs(), layer->crs() );
p1 = ct.transform( p1 );
p2 = ct.transform( p2 );
double width = sqrt( p1.sqrDist( p2 ) ); // width of reprojected pixel
// This is not perfect of course, we use the resolution in just one direction
mMapCanvas->zoomByFactor( qAbs( layer->rasterUnitsPerPixelX() / width ) );
}
else
{
mMapCanvas->zoomByFactor( qAbs( layer->rasterUnitsPerPixelX() / mMapCanvas->mapUnitsPerPixel() ) );
}
mMapCanvas->refresh();
QgsDebugMsg( "MapUnitsPerPixel after : " + QString::number( mMapCanvas->mapUnitsPerPixel() ) );
}
}

void QgsLegend::legendLayerStretchUsingCurrentExtent()
{
//find current Layer
QgsLegendLayer* currentLayer = dynamic_cast<QgsLegendLayer *>( currentItem() );
if ( !currentLayer )
return;

QgsRasterLayer *layer = qobject_cast<QgsRasterLayer *>( currentLayer->layer() );
if ( layer )
{
QgsContrastEnhancement::ContrastEnhancementAlgorithm contrastEnhancementAlgorithm = QgsContrastEnhancement::StretchToMinimumMaximum;

QgsRectangle myRectangle;
myRectangle = mMapCanvas->mapSettings().outputExtentToLayerExtent( layer, mMapCanvas->extent() );
layer->setContrastEnhancement( contrastEnhancementAlgorithm, QgsRaster::ContrastEnhancementMinMax, myRectangle );

refreshLayerSymbology( layer->id() );
mMapCanvas->refresh();
}
}

void QgsLegend::readProject( const QDomDocument & doc )
{
Expand Down Expand Up @@ -3012,32 +2917,6 @@ void QgsLegend::removeSelectedLayers()
mMapCanvas->freeze( false );
}

void QgsLegend::setCRSForSelectedLayers( const QgsCoordinateReferenceSystem &crs )
{
// Turn off rendering to improve speed.
mMapCanvas->freeze();

foreach ( QTreeWidgetItem * item, selectedItems() )
{
QgsLegendGroup* lg = dynamic_cast<QgsLegendGroup *>( item );
if ( lg )
{
setGroupCRS( lg, crs );
continue;
}

QgsLegendLayer *ll = dynamic_cast<QgsLegendLayer *>( item );
if ( ll && ll->layer() )
{
ll->layer()->setCrs( crs );
continue;
}
}

// Turn on rendering (if it was on previously)
mMapCanvas->freeze( false );
}

bool QgsLegend::parentGroupEmbedded( QTreeWidgetItem* item ) const
{
if ( !item )
Expand Down
15 changes: 0 additions & 15 deletions src/app/legend/qgslegend.h
Expand Up @@ -339,23 +339,12 @@ class QgsLegend : public QTreeWidget
legend layer files*/
void legendLayerZoom();

/**Zooms so that the pixels of the raster layer occupies exactly one screen pixel.
Only works on raster layers*/
void legendLayerZoomNative();

/**Stretches the raster layer, if stretching is active, based on the min and max of the current extent.
Only workds on raster layers*/
void legendLayerStretchUsingCurrentExtent();

/**Updates check states when the map canvas layer set is changed */
void refreshCheckStates();

/** Remove selected layers */
void removeSelectedLayers();

/** Set CRS for selected layers */
void setCRSForSelectedLayers( const QgsCoordinateReferenceSystem &crs );

/** Update drawing order */
bool updateDrawingOrder();

Expand Down Expand Up @@ -483,12 +472,8 @@ class QgsLegend : public QTreeWidget
void handleRightClickEvent( QTreeWidgetItem* item, const QPoint& position );
/**Removes the current legend group*/
void legendGroupRemove();
/**Set the CRS of the current legend group*/
void legendGroupSetCRS();
/**Removes a legend group and its layers*/
void removeGroup( QgsLegendGroup * lg );
/**Removes a legend group and its layers*/
void setGroupCRS( QgsLegendGroup * lg, const QgsCoordinateReferenceSystem &crs );
/**Sets all listview items to open*/
void expandAll();
/**Sets all listview items to closed*/
Expand Down
4 changes: 2 additions & 2 deletions src/app/legend/qgslegendlayer.cpp
Expand Up @@ -369,12 +369,12 @@ void QgsLegendLayer::addToPopupMenu( QMenu& theMenu )
tr( "&Zoom to Layer Extent" ), legend(), SLOT( legendLayerZoom() ) );
if ( lyr->type() == QgsMapLayer::RasterLayer )
{
theMenu.addAction( tr( "&Zoom to Best Scale (100%)" ), legend(), SLOT( legendLayerZoomNative() ) );
theMenu.addAction( tr( "&Zoom to Best Scale (100%)" ), QgisApp::instance(), SLOT( legendLayerZoomNative() ) );

QgsRasterLayer *rasterLayer = qobject_cast<QgsRasterLayer *>( lyr );
if ( rasterLayer && rasterLayer->rasterType() != QgsRasterLayer::Palette )
{
theMenu.addAction( tr( "&Stretch Using Current Extent" ), legend(), SLOT( legendLayerStretchUsingCurrentExtent() ) );
theMenu.addAction( tr( "&Stretch Using Current Extent" ), QgisApp::instance(), SLOT( legendLayerStretchUsingCurrentExtent() ) );
}
}

Expand Down

0 comments on commit ca97459

Please sign in to comment.