Quantum GIS API Documentation  1.8
src/core/qgslabelattributes.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                          qgslabel.cpp - render vector labels
00003                              -------------------
00004     begin                : August 2004
00005     copyright            : (C) 2004 by Radim Blazek
00006     email                : blazek@itc.it
00007  ***************************************************************************/
00008 /***************************************************************************
00009  *                                                                         *
00010  *   This program is free software; you can redistribute it and/or modify  *
00011  *   it under the terms of the GNU General Public License as published by  *
00012  *   the Free Software Foundation; either version 2 of the License, or     *
00013  *   (at your option) any later version.                                   *
00014  *                                                                         *
00015  ***************************************************************************/
00016 
00017 #include <QApplication>
00018 #include <QString>
00019 #include <QFont>
00020 #include <QColor>
00021 #include <QPen>
00022 #include <QBrush>
00023 
00024 #include "qgslabelattributes.h"
00025 #include "qgslogger.h"
00026 
00027 QgsLabelAttributes::QgsLabelAttributes( bool def )
00028     : mTextIsSet( false )
00029     , mFamilyIsSet( false )
00030     , mBoldIsSet( false )
00031     , mItalicIsSet( false )
00032     , mUnderlineIsSet( false )
00033     , mStrikeOutIsSet( false )
00034     , mSizeType( 0 )
00035     , mSize( 0.0 )
00036     , mSizeIsSet( false )
00037     , mColorIsSet( false )
00038     , mOffsetType( 0 )
00039     , mXOffset( 0 )
00040     , mYOffset( 0 )
00041     , mOffsetIsSet( false )
00042     , mAngle( 0.0 )
00043     , mAngleIsSet( false )
00044     , mAngleIsAuto( false )
00045     , mAlignment( 0 )
00046     , mAlignmentIsSet( false )
00047     , mBufferEnabledFlag( false )
00048     , mBufferSizeType( 0 )
00049     , mBufferSize( 0.0 )
00050     , mBufferSizeIsSet( false )
00051     , mBufferColorIsSet( false )
00052     , mBufferStyleIsSet( false )
00053     , mBorderColorIsSet( false )
00054     , mBorderWidthIsSet( false )
00055     , mBorderStyleIsSet( false )
00056     , mMultilineEnabledFlag( false )
00057     , mSelectedOnly( false )
00058 {
00059 
00060   if ( def )   // set defaults
00061   {
00062     setText( QObject::tr( "Label" ) );
00063 
00064     mFont = QApplication::font();
00065     mFamilyIsSet = true;
00066     mBoldIsSet = true;
00067     mItalicIsSet = true;
00068     mUnderlineIsSet = true;
00069 
00070     setSize( 12.0, PointUnits );
00071 
00072     setOffset( 0, 0, PointUnits );
00073     setAngle( 0 );
00074     setAutoAngle( false );
00075 
00076     setAlignment( Qt::AlignCenter );
00077     setColor( QColor( 0, 0, 0 ) );
00078 
00079     setBufferSize( 1, PointUnits );
00080     setBufferColor( QColor( 255, 255, 255 ) );
00081     setBufferStyle( Qt::NoBrush );
00082 
00083     setBorderWidth( 0 );
00084     setBorderColor( QColor( 0, 0, 0 ) );
00085     setBorderStyle( Qt::NoPen );
00086   }
00087 }
00088 
00089 QgsLabelAttributes::~QgsLabelAttributes()
00090 {
00091 }
00092 /* Text */
00093 void QgsLabelAttributes::setText( const QString & text )
00094 {
00095   mText = text;
00096   mTextIsSet = true;
00097 }
00098 
00099 bool QgsLabelAttributes::textIsSet( void ) const
00100 {
00101   return mTextIsSet;
00102 }
00103 
00104 const QString QgsLabelAttributes::text( void ) const
00105 {
00106   return mText;
00107 }
00108 
00109 
00110 /* Offset */
00111 void QgsLabelAttributes::setOffset( double x, double y, int type )
00112 {
00113   mOffsetType = type;
00114   mXOffset = x;
00115   mYOffset = y;
00116   mOffsetIsSet = true;
00117 }
00118 
00119 bool QgsLabelAttributes::offsetIsSet( void ) const
00120 {
00121   return mOffsetIsSet;
00122 }
00123 
00124 int QgsLabelAttributes::offsetType( void ) const
00125 {
00126   return mOffsetType;
00127 }
00128 
00129 double QgsLabelAttributes::xOffset( void ) const
00130 {
00131   return mXOffset;
00132 }
00133 
00134 double QgsLabelAttributes::yOffset( void ) const
00135 {
00136   return mYOffset;
00137 }
00138 
00139 /* Angle */
00140 void QgsLabelAttributes::setAngle( double angle )
00141 {
00142   mAngle = angle;
00143   mAngleIsSet = true;
00144 }
00145 
00146 bool QgsLabelAttributes::angleIsSet( void ) const
00147 {
00148   return mAngleIsSet;
00149 }
00150 
00151 double QgsLabelAttributes::angle( void ) const
00152 {
00153   return mAngle;
00154 }
00155 
00156 bool QgsLabelAttributes::angleIsAuto() const
00157 {
00158   return mAngleIsAuto;
00159 }
00160 
00161 void QgsLabelAttributes::setAutoAngle( bool state )
00162 {
00163   mAngleIsAuto = state;
00164 }
00165 
00166 /* Alignment */
00167 void QgsLabelAttributes::setAlignment( int alignment )
00168 {
00169   mAlignment = alignment;
00170   mAlignmentIsSet = true;
00171 }
00172 
00173 bool QgsLabelAttributes::alignmentIsSet( void ) const
00174 {
00175   return mAlignmentIsSet;
00176 }
00177 
00178 int QgsLabelAttributes::alignment( void ) const
00179 {
00180   return mAlignment;
00181 }
00182 
00183 /* Font */
00184 void QgsLabelAttributes::setFamily( const QString & family )
00185 {
00186   mFont.setFamily( family );
00187   mFamilyIsSet = true;
00188 }
00189 
00190 bool QgsLabelAttributes::familyIsSet( void ) const
00191 {
00192   return mFamilyIsSet;
00193 }
00194 
00195 const QString QgsLabelAttributes::family( void ) const
00196 {
00197   return mFont.family();
00198 }
00199 
00200 
00201 void QgsLabelAttributes::setBold( bool enable )
00202 {
00203   mFont.setBold( enable );
00204   mBoldIsSet = true;
00205 }
00206 
00207 bool QgsLabelAttributes::boldIsSet( void ) const
00208 {
00209   return mBoldIsSet;
00210 }
00211 
00212 bool QgsLabelAttributes::bold( void ) const
00213 {
00214   return mFont.bold();
00215 }
00216 
00217 
00218 void QgsLabelAttributes::setItalic( bool enable )
00219 {
00220   mFont.setItalic( enable );
00221   mItalicIsSet = true;
00222 }
00223 
00224 bool QgsLabelAttributes::italicIsSet( void ) const
00225 {
00226   return mItalicIsSet;
00227 }
00228 
00229 bool QgsLabelAttributes::italic( void ) const
00230 {
00231   return mFont.italic();
00232 }
00233 
00234 
00235 void QgsLabelAttributes::setUnderline( bool enable )
00236 {
00237   mFont.setUnderline( enable );
00238   mUnderlineIsSet = true;
00239 }
00240 
00241 bool QgsLabelAttributes::underlineIsSet( void ) const
00242 {
00243   return mUnderlineIsSet;
00244 }
00245 
00246 bool QgsLabelAttributes::underline( void ) const
00247 {
00248   return mFont.underline();
00249 }
00250 
00251 void QgsLabelAttributes::setStrikeOut( bool enable )
00252 {
00253   mFont.setStrikeOut( enable );
00254   mStrikeOutIsSet = true;
00255 }
00256 
00257 bool QgsLabelAttributes::strikeOutIsSet( void ) const
00258 {
00259   return mStrikeOutIsSet;
00260 }
00261 
00262 bool QgsLabelAttributes::strikeOut( void ) const
00263 {
00264   return mFont.strikeOut();
00265 }
00266 
00267 
00268 void QgsLabelAttributes::setSize( double size, int type )
00269 {
00270   mSizeType = type;
00271   mSize = size;
00272   mSizeIsSet = true;
00273 }
00274 
00275 bool QgsLabelAttributes::sizeIsSet( void ) const
00276 {
00277   return mSizeIsSet;
00278 }
00279 
00280 int QgsLabelAttributes::sizeType( void ) const
00281 {
00282   return mSizeType;
00283 }
00284 
00285 double QgsLabelAttributes::size( void ) const
00286 {
00287   return mSize;
00288 }
00289 
00290 
00291 void QgsLabelAttributes::setColor( const QColor &color )
00292 {
00293   mColor = color;
00294   mColorIsSet = true;
00295 }
00296 
00297 bool QgsLabelAttributes::colorIsSet( void ) const
00298 {
00299   return mColorIsSet;
00300 }
00301 
00302 const QColor & QgsLabelAttributes::color( void ) const
00303 {
00304   return mColor;
00305 }
00306 
00307 /* Buffer */
00308 bool QgsLabelAttributes::bufferEnabled() const
00309 {
00310   return mBufferEnabledFlag;
00311 }
00312 void QgsLabelAttributes::setBufferEnabled( bool useBufferFlag )
00313 {
00314   mBufferEnabledFlag = useBufferFlag;
00315 }
00316 void QgsLabelAttributes::setBufferSize( double size, int type )
00317 {
00318   mBufferSizeType = type;
00319   mBufferSize = size;
00320   mBufferSizeIsSet = true;
00321 }
00322 
00323 bool QgsLabelAttributes::bufferSizeIsSet( void ) const
00324 {
00325   return mBufferSizeIsSet;
00326 }
00327 
00328 int QgsLabelAttributes::bufferSizeType( void ) const
00329 {
00330   return mBufferSizeType;
00331 }
00332 
00333 double QgsLabelAttributes::bufferSize( void ) const
00334 {
00335   return mBufferSize;
00336 }
00337 
00338 
00339 void QgsLabelAttributes::setBufferColor( const QColor &color )
00340 {
00341   mBufferBrush.setColor( color );
00342   mBufferColorIsSet = true;
00343 }
00344 
00345 bool QgsLabelAttributes::bufferColorIsSet( void ) const
00346 {
00347   return mColorIsSet;
00348 }
00349 
00350 QColor QgsLabelAttributes::bufferColor( void ) const
00351 {
00352   return mBufferBrush.color();
00353 }
00354 
00355 
00356 void QgsLabelAttributes::setBufferStyle( Qt::BrushStyle style )
00357 {
00358   mBufferBrush.setStyle( style );
00359   mBufferStyleIsSet = true;
00360 }
00361 
00362 bool QgsLabelAttributes::bufferStyleIsSet( void ) const
00363 {
00364   return mBufferStyleIsSet;
00365 }
00366 
00367 Qt::BrushStyle QgsLabelAttributes::bufferStyle( void ) const
00368 {
00369   return mBufferBrush.style();
00370 }
00371 
00372 /* Border */
00373 void QgsLabelAttributes::setBorderColor( const QColor &color )
00374 {
00375   mBorderPen.setColor( color );
00376   mBorderColorIsSet = true;
00377 }
00378 
00379 bool QgsLabelAttributes::borderColorIsSet( void ) const
00380 {
00381   return mBorderColorIsSet;
00382 }
00383 
00384 QColor QgsLabelAttributes::borderColor( void ) const
00385 {
00386   return mBorderPen.color();
00387 }
00388 
00389 void QgsLabelAttributes::setBorderWidth( int width )
00390 {
00391   mBorderPen.setWidth( width );
00392   mBorderWidthIsSet = true;
00393 }
00394 
00395 bool QgsLabelAttributes::borderWidthIsSet( void ) const
00396 {
00397   return mBorderWidthIsSet;
00398 }
00399 
00400 int QgsLabelAttributes::borderWidth( void ) const
00401 {
00402   return mBorderPen.width();
00403 }
00404 
00405 
00406 void QgsLabelAttributes::setBorderStyle( Qt::PenStyle style )
00407 {
00408   mBorderPen.setStyle( style );
00409   mBorderStyleIsSet = true;
00410 }
00411 
00412 bool QgsLabelAttributes::borderStyleIsSet( void ) const
00413 {
00414   return mBorderStyleIsSet;
00415 }
00416 
00417 Qt::PenStyle QgsLabelAttributes::borderStyle( void ) const
00418 {
00419   return mBorderPen.style();
00420 }
00421 
00422 /* Multiline */
00423 bool QgsLabelAttributes::multilineEnabled() const
00424 {
00425   return mMultilineEnabledFlag;
00426 }
00427 void QgsLabelAttributes::setMultilineEnabled( bool useMultilineFlag )
00428 {
00429   mMultilineEnabledFlag = useMultilineFlag;
00430 }
00431 
00432 /* selected only */
00433 bool QgsLabelAttributes::selectedOnly() const
00434 {
00435   return mSelectedOnly;
00436 }
00437 void QgsLabelAttributes::setSelectedOnly( bool selectedOnly )
00438 {
00439   mSelectedOnly = selectedOnly;
00440 }
00441 
00442 /* units */
00443 QString QgsLabelAttributes::unitsName( int units )
00444 {
00445   if ( units == MapUnits )
00446   {
00447     return QString( "mu" );
00448   }
00449 
00450   return QString( "pt" );
00451 }
00452 
00453 int QgsLabelAttributes::unitsCode( const QString &name )
00454 {
00455   if ( name.compare( "mu" ) == 0 )
00456   {
00457     return MapUnits;
00458   }
00459 
00460   return PointUnits;
00461 }
00462 
00463 /* alignment */
00464 QString QgsLabelAttributes::alignmentName( int alignment )
00465 {
00466   QgsDebugMsg( QString( "alignment=%1" ).arg( alignment ) );
00467   if ( !alignment )                                       return  QString( "center" );
00468   if ( alignment == ( Qt::AlignRight | Qt::AlignBottom ) ) return  QString( "aboveleft" );
00469   if ( alignment == ( Qt::AlignRight | Qt::AlignTop ) ) return  QString( "belowleft" );
00470   if ( alignment == ( Qt::AlignLeft  | Qt::AlignBottom ) ) return  QString( "aboveright" );
00471   if ( alignment == ( Qt::AlignLeft  | Qt::AlignTop ) ) return  QString( "belowright" );
00472   if ( alignment == ( Qt::AlignRight | Qt::AlignVCenter ) ) return  QString( "left" );
00473   if ( alignment == ( Qt::AlignLeft  | Qt::AlignVCenter ) ) return  QString( "right" );
00474   if ( alignment == ( Qt::AlignBottom | Qt::AlignHCenter ) ) return  QString( "above" );
00475   if ( alignment == ( Qt::AlignTop   | Qt::AlignHCenter ) ) return  QString( "below" );
00476   if ( alignment == ( Qt::AlignCenter ) ) return  QString( "center" );
00477   return QString( "center" );
00478 }
00479 
00480 int QgsLabelAttributes::alignmentCode( const QString &name )
00481 {
00482   QString lname = name.toLower();
00483   if ( lname.compare( "aboveleft" )  == 0 )  return Qt::AlignRight | Qt::AlignBottom     ;
00484   if ( lname.compare( "belowleft" )  == 0 )  return Qt::AlignRight | Qt::AlignTop        ;
00485   if ( lname.compare( "aboveright" )  == 0 ) return Qt::AlignLeft  | Qt::AlignBottom     ;
00486   if ( lname.compare( "belowright" )  == 0 ) return Qt::AlignLeft  | Qt::AlignTop        ;
00487   if ( lname.compare( "left" )  == 0 )       return Qt::AlignRight | Qt::AlignVCenter    ;
00488   if ( lname.compare( "right" )  == 0 )      return Qt::AlignLeft  | Qt::AlignVCenter    ;
00489   if ( lname.compare( "above" )  == 0 )      return Qt::AlignBottom | Qt::AlignHCenter    ;
00490   if ( lname.compare( "below" )  == 0 )      return Qt::AlignTop   | Qt::AlignHCenter    ;
00491   if ( lname.compare( "center" )  == 0 )       return Qt::AlignCenter                      ;
00492 
00493 
00494   return Qt::AlignCenter;
00495 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines