QgsMapSaveDialog::QgsMapSaveDialog( QWidget *parent, QgsMapCanvas *mapCanvas, const QList<QgsDecorationItem *> &decorations, const QList< QgsAnnotation *> &annotations, DialogType type ) : QDialog( parent ) , mDialogType( type ) , mMapCanvas( mapCanvas ) , mAnnotations( annotations ) { setupUi( this ); // Use unrotated visible extent to insure output size and scale matches canvas QgsMapSettings ms = mMapCanvas->mapSettings(); ms.setRotation( 0 ); mExtent = ms.visibleExtent(); mDpi = ms.outputDpi(); mSize = ms.outputSize(); mResolutionSpinBox->setValue( mDpi ); mExtentGroupBox->setOutputCrs( ms.destinationCrs() ); mExtentGroupBox->setCurrentExtent( mExtent, ms.destinationCrs() ); mExtentGroupBox->setOutputExtentFromCurrent(); mExtentGroupBox->setMapCanvas( mapCanvas ); mScaleWidget->setScale( ms.scale() ); mScaleWidget->setMapCanvas( mMapCanvas ); mScaleWidget->setShowCurrentScaleButton( true ); QString activeDecorations; Q_FOREACH ( QgsDecorationItem *decoration, decorations ) { mDecorations << decoration; if ( activeDecorations.isEmpty() ) activeDecorations = decoration->name().toLower(); else activeDecorations += QStringLiteral( ", %1" ).arg( decoration->name().toLower() ); }
QgsRenderContext QgsRenderContext::fromMapSettings( const QgsMapSettings &mapSettings ) { QgsRenderContext ctx; ctx.setMapToPixel( mapSettings.mapToPixel() ); ctx.setExtent( mapSettings.visibleExtent() ); ctx.setFlag( DrawEditingInfo, mapSettings.testFlag( QgsMapSettings::DrawEditingInfo ) ); ctx.setFlag( ForceVectorOutput, mapSettings.testFlag( QgsMapSettings::ForceVectorOutput ) ); ctx.setFlag( UseAdvancedEffects, mapSettings.testFlag( QgsMapSettings::UseAdvancedEffects ) ); ctx.setFlag( UseRenderingOptimization, mapSettings.testFlag( QgsMapSettings::UseRenderingOptimization ) ); ctx.setCoordinateTransform( QgsCoordinateTransform() ); ctx.setSelectionColor( mapSettings.selectionColor() ); ctx.setFlag( DrawSelection, mapSettings.testFlag( QgsMapSettings::DrawSelection ) ); ctx.setFlag( DrawSymbolBounds, mapSettings.testFlag( QgsMapSettings::DrawSymbolBounds ) ); ctx.setFlag( RenderMapTile, mapSettings.testFlag( QgsMapSettings::RenderMapTile ) ); ctx.setFlag( Antialiasing, mapSettings.testFlag( QgsMapSettings::Antialiasing ) ); ctx.setFlag( RenderPartialOutput, mapSettings.testFlag( QgsMapSettings::RenderPartialOutput ) ); ctx.setScaleFactor( mapSettings.outputDpi() / 25.4 ); // = pixels per mm ctx.setRendererScale( mapSettings.scale() ); ctx.setExpressionContext( mapSettings.expressionContext() ); ctx.setSegmentationTolerance( mapSettings.segmentationTolerance() ); ctx.setSegmentationToleranceType( mapSettings.segmentationToleranceType() ); ctx.mDistanceArea.setSourceCrs( mapSettings.destinationCrs() ); ctx.mDistanceArea.setEllipsoid( mapSettings.ellipsoid() ); //this flag is only for stopping during the current rendering progress, //so must be false at every new render operation ctx.setRenderingStopped( false ); return ctx; }
void QgsExtentGroupBox::setOutputExtentFromCurrent() { if ( mCanvas ) { // Use unrotated visible extent to insure output size and scale matches canvas QgsMapSettings ms = mCanvas->mapSettings(); ms.setRotation( 0 ); setOutputExtent( ms.visibleExtent(), ms.destinationCrs(), CurrentExtent ); } else { setOutputExtent( mCurrentExtent, mCurrentCrs, CurrentExtent ); } }
void QgsMapRendererJob::drawOldLabeling( const QgsMapSettings& settings, QgsRenderContext& renderContext ) { // render all labels for vector layers in the stack, starting at the base QListIterator<QString> li( settings.layers() ); li.toBack(); while ( li.hasPrevious() ) { if ( renderContext.renderingStopped() ) { break; } QString layerId = li.previous(); QgsMapLayer *ml = QgsMapLayerRegistry::instance()->mapLayer( layerId ); if ( !ml || ( ml->type() != QgsMapLayer::VectorLayer ) ) continue; // only make labels if the layer is visible // after scale dep viewing settings are checked if ( ml->hasScaleBasedVisibility() && ( settings.scale() < ml->minimumScale() || settings.scale() > ml->maximumScale() ) ) continue; bool split = false; const QgsCoordinateTransform* ct = 0; QgsRectangle r1 = settings.visibleExtent(), r2; if ( settings.hasCrsTransformEnabled() ) { ct = QgsCoordinateTransformCache::instance()->transform( ml->crs().authid(), settings.destinationCrs().authid() ); split = reprojectToLayerExtent( ct, ml->crs().geographicFlag(), r1, r2 ); } renderContext.setCoordinateTransform( ct ); renderContext.setExtent( r1 ); ml->drawLabels( renderContext ); if ( split ) { renderContext.setExtent( r2 ); ml->drawLabels( renderContext ); } } }
void QgsMapCanvas::readProject( const QDomDocument & doc ) { QDomNodeList nodes = doc.elementsByTagName( "mapcanvas" ); if ( nodes.count() ) { QDomNode node = nodes.item( 0 ); QgsMapSettings tmpSettings; tmpSettings.readXML( node ); setMapUnits( tmpSettings.mapUnits() ); setCrsTransformEnabled( tmpSettings.hasCrsTransformEnabled() ); setDestinationCrs( tmpSettings.destinationCrs() ); setExtent( tmpSettings.extent() ); setRotation( tmpSettings.rotation() ); mSettings.datumTransformStore() = tmpSettings.datumTransformStore(); clearExtentHistory(); // clear the extent history on project load } else { QgsDebugMsg( "Couldn't read mapcanvas information from project" ); } }
void QgsDecorationNorthArrow::render( const QgsMapSettings &mapSettings, QgsRenderContext &context ) { //Large IF statement controlled by enable checkbox if ( enabled() ) { QSize size( 64, 64 ); QSvgRenderer svg; const QByteArray &svgContent = QgsApplication::svgCache()->svgContent( QStringLiteral( ":/images/north_arrows/default.svg" ), size.width(), mColor, mOutlineColor, 1.0, 1.0 ); svg.load( svgContent ); if ( svg.isValid() ) { double centerXDouble = size.width() / 2.0; double centerYDouble = size.width() / 2.0; //save the current canvas rotation context.painter()->save(); // //work out how to shift the image so that it rotates // properly about its center //(x cos a + y sin a - x, -x sin a + y cos a - y) // // could move this call to somewhere else so that it is only // called when the projection or map extent changes if ( mAutomatic ) { mRotationInt = QgsBearingUtils:: bearingTrueNorth( mapSettings.destinationCrs(), context.extent().center() ); mRotationInt += mapSettings.rotation(); } double myRadiansDouble = mRotationInt * M_PI / 180.0; int xShift = static_cast<int>( ( ( centerXDouble * cos( myRadiansDouble ) ) + ( centerYDouble * sin( myRadiansDouble ) ) ) - centerXDouble ); int yShift = static_cast<int>( ( ( -centerXDouble * sin( myRadiansDouble ) ) + ( centerYDouble * cos( myRadiansDouble ) ) ) - centerYDouble ); // need width/height of paint device int myHeight = context.painter()->device()->height(); int myWidth = context.painter()->device()->width(); //QgsDebugMsg("Rendering north arrow at " + mPlacementLabels.at(mPlacementIndex)); // Set margin according to selected units int myXOffset = 0; int myYOffset = 0; switch ( mMarginUnit ) { case QgsUnitTypes::RenderMillimeters: { int myPixelsInchX = context.painter()->device()->logicalDpiX(); int myPixelsInchY = context.painter()->device()->logicalDpiY(); myXOffset = myPixelsInchX * INCHES_TO_MM * mMarginHorizontal; myYOffset = myPixelsInchY * INCHES_TO_MM * mMarginVertical; break; } case QgsUnitTypes::RenderPixels: myXOffset = mMarginHorizontal - 5; // Minus 5 to shift tight into corner myYOffset = mMarginVertical - 5; break; case QgsUnitTypes::RenderPercentage: myXOffset = ( ( myWidth - size.width() ) / 100. ) * mMarginHorizontal; myYOffset = ( ( myHeight - size.width() ) / 100. ) * mMarginVertical; break; default: // Use default of top left break; } //Determine placement of label from form combo box switch ( mPlacement ) { case BottomLeft: context.painter()->translate( myXOffset, myHeight - myYOffset - size.width() ); break; case TopLeft: context.painter()->translate( myXOffset, myYOffset ); break; case TopRight: context.painter()->translate( myWidth - myXOffset - size.width(), myYOffset ); break; case BottomRight: context.painter()->translate( myWidth - myXOffset - size.width(), myHeight - myYOffset - size.width() ); break; default: { //QgsDebugMsg("Unable to determine where to put north arrow so defaulting to top left"); } } //rotate the canvas by the north arrow rotation amount context.painter()->rotate( mRotationInt ); //Now we can actually do the drawing, and draw a smooth north arrow even when rotated context.painter()->translate( xShift, yShift ); svg.render( context.painter(), QRectF( 0, 0, size.width(), size.height() ) ); //unrotate the canvas again context.painter()->restore(); } else { QFont myQFont( QStringLiteral( "time" ), 12, QFont::Bold ); context.painter()->setFont( myQFont ); context.painter()->setPen( Qt::black ); context.painter()->drawText( 10, 20, tr( "North arrow pixmap not found" ) ); } } }
void QgsDecorationNorthArrow::render( const QgsMapSettings &mapSettings, QgsRenderContext &context ) { if ( !enabled() ) return; double maxLength = mSize * mapSettings.outputDpi() / 25.4; QSvgRenderer svg; const QByteArray &svgContent = QgsApplication::svgCache()->svgContent( svgPath(), maxLength, mColor, mOutlineColor, 1.0, 1.0 ); svg.load( svgContent ); if ( svg.isValid() ) { QSize size( maxLength, maxLength ); QRectF viewBox = svg.viewBoxF(); if ( viewBox.height() > viewBox.width() ) { size.setWidth( maxLength * viewBox.width() / viewBox.height() ); } else { size.setHeight( maxLength * viewBox.height() / viewBox.width() ); } double centerXDouble = size.width() / 2.0; double centerYDouble = size.height() / 2.0; //save the current canvas rotation context.painter()->save(); // //work out how to shift the image so that it rotates // properly about its center //(x cos a + y sin a - x, -x sin a + y cos a - y) // // could move this call to somewhere else so that it is only // called when the projection or map extent changes if ( mAutomatic ) { try { mRotationInt = QgsBearingUtils:: bearingTrueNorth( mapSettings.destinationCrs(), mapSettings.transformContext(), context.extent().center() ); } catch ( QgsException & ) { mRotationInt = 0.0; } mRotationInt += mapSettings.rotation(); } double radiansDouble = mRotationInt * M_PI / 180.0; int xShift = static_cast<int>( ( ( centerXDouble * std::cos( radiansDouble ) ) + ( centerYDouble * std::sin( radiansDouble ) ) ) - centerXDouble ); int yShift = static_cast<int>( ( ( -centerXDouble * std::sin( radiansDouble ) ) + ( centerYDouble * std::cos( radiansDouble ) ) ) - centerYDouble ); // need width/height of paint device int deviceHeight = context.painter()->device()->height(); int deviceWidth = context.painter()->device()->width(); // Set margin according to selected units int xOffset = 0; int yOffset = 0; switch ( mMarginUnit ) { case QgsUnitTypes::RenderMillimeters: { int pixelsInchX = context.painter()->device()->logicalDpiX(); int pixelsInchY = context.painter()->device()->logicalDpiY(); xOffset = pixelsInchX * INCHES_TO_MM * mMarginHorizontal; yOffset = pixelsInchY * INCHES_TO_MM * mMarginVertical; break; } case QgsUnitTypes::RenderPixels: xOffset = mMarginHorizontal - 5; // Minus 5 to shift tight into corner yOffset = mMarginVertical - 5; break; case QgsUnitTypes::RenderPercentage: xOffset = ( ( deviceWidth - size.width() ) / 100. ) * mMarginHorizontal; yOffset = ( ( deviceHeight - size.width() ) / 100. ) * mMarginVertical; break; case QgsUnitTypes::RenderMapUnits: case QgsUnitTypes::RenderPoints: case QgsUnitTypes::RenderInches: case QgsUnitTypes::RenderUnknownUnit: case QgsUnitTypes::RenderMetersInMapUnits: break; } //Determine placement of label from form combo box switch ( mPlacement ) { case BottomLeft: context.painter()->translate( xOffset, deviceHeight - yOffset - maxLength + ( maxLength - size.height() ) / 2 ); break; case TopLeft: context.painter()->translate( xOffset, yOffset ); break; case TopRight: context.painter()->translate( deviceWidth - xOffset - maxLength + ( maxLength - size.width() ) / 2, yOffset ); break; case BottomRight: context.painter()->translate( deviceWidth - xOffset - maxLength + ( maxLength - size.width() ) / 2, deviceHeight - yOffset - maxLength + ( maxLength - size.height() ) / 2 ); break; } //rotate the canvas by the north arrow rotation amount context.painter()->rotate( mRotationInt ); //Now we can actually do the drawing, and draw a smooth north arrow even when rotated context.painter()->translate( xShift, yShift ); svg.render( context.painter(), QRectF( 0, 0, size.width(), size.height() ) ); //unrotate the canvas again context.painter()->restore(); } }