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