QGIS API Documentation  master-3f58142
src/core/raster/qgsrasterrenderer.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                          qgsrasterrenderer.cpp
00003                          ---------------------
00004     begin                : December 2011
00005     copyright            : (C) 2011 by Marco Hugentobler
00006     email                : marco at sourcepole dot ch
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include "qgsrasterrenderer.h"
00019 #include "qgsrastertransparency.h"
00020 
00021 #include <QCoreApplication>
00022 #include <QDomDocument>
00023 #include <QDomElement>
00024 #include <QImage>
00025 #include <QPainter>
00026 
00027 #define tr( sourceText ) QCoreApplication::translate ( "QgsRasterRenderer", sourceText )
00028 
00029 // Changing RGB components of NODATA_COLOR may break tests
00030 const QRgb QgsRasterRenderer::NODATA_COLOR = qRgba( 0, 0, 0, 0 );
00031 
00032 QgsRasterRenderer::QgsRasterRenderer( QgsRasterInterface* input, const QString& type )
00033     : QgsRasterInterface( input )
00034     , mType( type ), mOpacity( 1.0 ), mRasterTransparency( 0 )
00035     , mAlphaBand( -1 ) //, mInvertColor( false )
00036 {
00037 }
00038 
00039 QgsRasterRenderer::~QgsRasterRenderer()
00040 {
00041 }
00042 
00043 int QgsRasterRenderer::bandCount() const
00044 {
00045   if ( mOn ) return 1;
00046 
00047   if ( mInput ) return mInput->bandCount();
00048 
00049   return 0;
00050 }
00051 
00052 QGis::DataType QgsRasterRenderer::dataType( int bandNo ) const
00053 {
00054   QgsDebugMsg( "Entered" );
00055 
00056   if ( mOn ) return QGis::ARGB32_Premultiplied;
00057 
00058   if ( mInput ) return mInput->dataType( bandNo );
00059 
00060   return QGis::UnknownDataType;
00061 }
00062 
00063 bool QgsRasterRenderer::setInput( QgsRasterInterface* input )
00064 {
00065   // Renderer can only work with numerical values in at least 1 band
00066   if ( !input ) return false;
00067 
00068   if ( !mOn )
00069   {
00070     // In off mode we can connect to anything
00071     mInput = input;
00072     return true;
00073   }
00074 
00075   for ( int i = 1; i <= input->bandCount(); i++ )
00076   {
00077     if ( !QgsRasterBlock::typeIsNumeric( input->dataType( i ) ) )
00078     {
00079       return false;
00080     }
00081   }
00082   mInput = input;
00083   return true;
00084 }
00085 
00086 bool QgsRasterRenderer::usesTransparency( ) const
00087 {
00088   if ( !mInput )
00089   {
00090     return true;
00091   }
00092   return ( mAlphaBand > 0 || ( mRasterTransparency && !mRasterTransparency->isEmpty() ) || !qgsDoubleNear( mOpacity, 1.0 ) );
00093 }
00094 
00095 void QgsRasterRenderer::setRasterTransparency( QgsRasterTransparency* t )
00096 {
00097   delete mRasterTransparency;
00098   mRasterTransparency = t;
00099 }
00100 
00101 void QgsRasterRenderer::_writeXML( QDomDocument& doc, QDomElement& rasterRendererElem ) const
00102 {
00103   if ( rasterRendererElem.isNull() )
00104   {
00105     return;
00106   }
00107 
00108   rasterRendererElem.setAttribute( "type", mType );
00109   rasterRendererElem.setAttribute( "opacity", QString::number( mOpacity ) );
00110   rasterRendererElem.setAttribute( "alphaBand", mAlphaBand );
00111 
00112   if ( mRasterTransparency )
00113   {
00114     mRasterTransparency->writeXML( doc, rasterRendererElem );
00115   }
00116 }
00117 
00118 void QgsRasterRenderer::readXML( const QDomElement& rendererElem )
00119 {
00120   if ( rendererElem.isNull() )
00121   {
00122     return;
00123   }
00124 
00125   mType = rendererElem.attribute( "type" );
00126   mOpacity = rendererElem.attribute( "opacity", "1.0" ).toDouble();
00127   mAlphaBand = rendererElem.attribute( "alphaBand", "-1" ).toInt();
00128 
00129   QDomElement rasterTransparencyElem = rendererElem.firstChildElement( "rasterTransparency" );
00130   if ( !rasterTransparencyElem.isNull() )
00131   {
00132     delete mRasterTransparency;
00133     mRasterTransparency = new QgsRasterTransparency();
00134     mRasterTransparency->readXML( rasterTransparencyElem );
00135   }
00136 }
00137 
00138 QString QgsRasterRenderer::minMaxOriginName( int theOrigin )
00139 {
00140   if ( theOrigin == MinMaxUnknown )
00141   {
00142     return "Unknown";
00143   }
00144   else if ( theOrigin == MinMaxUser )
00145   {
00146     return "User";
00147   }
00148 
00149   QString name;
00150   if ( theOrigin & MinMaxMinMax )
00151   {
00152     name += "MinMax";
00153   }
00154   else if ( theOrigin & MinMaxCumulativeCut )
00155   {
00156     name += "CumulativeCut";
00157   }
00158   else if ( theOrigin & MinMaxStdDev )
00159   {
00160     name += "StdDev";
00161   }
00162 
00163   if ( theOrigin & MinMaxFullExtent )
00164   {
00165     name += "FullExtent";
00166   }
00167   else if ( theOrigin & MinMaxSubExtent )
00168   {
00169     name += "SubExtent";
00170   }
00171 
00172   if ( theOrigin & MinMaxEstimated )
00173   {
00174     name += "Estimated";
00175   }
00176   else if ( theOrigin & MinMaxExact )
00177   {
00178     name += "Exact";
00179   }
00180   return name;
00181 }
00182 
00183 QString QgsRasterRenderer::minMaxOriginLabel( int theOrigin )
00184 {
00185   if ( theOrigin == MinMaxUnknown )
00186   {
00187     return tr( "Unknown" );
00188   }
00189   else if ( theOrigin == MinMaxUser )
00190   {
00191     return tr( "User defined" );
00192   }
00193 
00194   QString name;
00195   if ( theOrigin & MinMaxEstimated )
00196   {
00197     name += tr( "Estimated" );
00198   }
00199   else if ( theOrigin & MinMaxExact )
00200   {
00201     name += tr( "Exact" );
00202   }
00203 
00204   name += " ";
00205 
00206   if ( theOrigin & MinMaxMinMax )
00207   {
00208     name += tr( "min / max" );
00209   }
00210   else if ( theOrigin & MinMaxCumulativeCut )
00211   {
00212     name += "cumulative cut";
00213   }
00214   else if ( theOrigin & MinMaxStdDev )
00215   {
00216     name += "standard deviation";
00217   }
00218 
00219   name += " " + tr( " of " ) + " ";
00220 
00221   if ( theOrigin & MinMaxFullExtent )
00222   {
00223     name += "full extent";
00224   }
00225   else if ( theOrigin & MinMaxSubExtent )
00226   {
00227     name += "sub extent";
00228   }
00229 
00230   name += ".";
00231 
00232   return name;
00233 }
00234 
00235 int QgsRasterRenderer::minMaxOriginFromName( QString theName )
00236 {
00237   if ( theName.contains( "Unknown" ) )
00238   {
00239     return MinMaxUnknown;
00240   }
00241   else if ( theName.contains( "User" ) )
00242   {
00243     return MinMaxUser;
00244   }
00245 
00246   int origin = 0;
00247 
00248   if ( theName.contains( "MinMax" ) )
00249   {
00250     origin |= MinMaxMinMax;
00251   }
00252   else if ( theName.contains( "CumulativeCut" ) )
00253   {
00254     origin |= MinMaxCumulativeCut;
00255   }
00256   else if ( theName.contains( "StdDev" ) )
00257   {
00258     origin |= MinMaxStdDev;
00259   }
00260 
00261   if ( theName.contains( "FullExtent" ) )
00262   {
00263     origin |= MinMaxFullExtent;
00264   }
00265   else if ( theName.contains( "SubExtent" ) )
00266   {
00267     origin |= MinMaxSubExtent;
00268   }
00269 
00270   if ( theName.contains( "Estimated" ) )
00271   {
00272     origin |= MinMaxEstimated;
00273   }
00274   else if ( theName.contains( "Exact" ) )
00275   {
00276     origin |= MinMaxExact;
00277   }
00278   return origin;
00279 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines