QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgslabelattributes.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslabel.cpp - render vector labels
3  -------------------
4  begin : August 2004
5  copyright : (C) 2004 by Radim Blazek
6  email : [email protected]
7  ***************************************************************************/
8 /***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #include <QApplication>
18 #include <QString>
19 #include <QFont>
20 #include <QColor>
21 #include <QPen>
22 #include <QBrush>
23 
24 #include "qgslabelattributes.h"
25 #include "qgslogger.h"
26 
28  : mTextIsSet( false )
29  , mFamilyIsSet( false )
30  , mBoldIsSet( false )
31  , mItalicIsSet( false )
32  , mUnderlineIsSet( false )
33  , mStrikeOutIsSet( false )
34  , mSizeType( 0 )
35  , mSize( 0.0 )
36  , mSizeIsSet( false )
37  , mColorIsSet( false )
38  , mOffsetType( 0 )
39  , mXOffset( 0 )
40  , mYOffset( 0 )
41  , mOffsetIsSet( false )
42  , mAngle( 0.0 )
43  , mAngleIsSet( false )
44  , mAngleIsAuto( false )
45  , mAlignment( 0 )
46  , mAlignmentIsSet( false )
47  , mBufferEnabledFlag( false )
48  , mBufferSizeType( 0 )
49  , mBufferSize( 0.0 )
50  , mBufferSizeIsSet( false )
51  , mBufferColorIsSet( false )
52  , mBufferStyleIsSet( false )
53  , mBorderColorIsSet( false )
54  , mBorderWidthIsSet( false )
55  , mBorderStyleIsSet( false )
56  , mMultilineEnabledFlag( false )
57  , mSelectedOnly( false )
58 {
59 
60  if ( def ) // set defaults
61  {
62  setText( QObject::tr( "Label" ) );
63 
64  mFont = QApplication::font();
65  mFamilyIsSet = true;
66  mBoldIsSet = true;
67  mItalicIsSet = true;
68  mUnderlineIsSet = true;
69 
70  setSize( 12.0, PointUnits );
71 
72  setOffset( 0, 0, PointUnits );
73  setAngle( 0 );
74  setAutoAngle( false );
75 
76  setAlignment( Qt::AlignCenter );
77  setColor( QColor( 0, 0, 0 ) );
78 
80  setBufferColor( QColor( 255, 255, 255 ) );
81  setBufferStyle( Qt::NoBrush );
82 
83  setBorderWidth( 0 );
84  setBorderColor( QColor( 0, 0, 0 ) );
85  setBorderStyle( Qt::NoPen );
86  }
87 }
88 
90 {
91 }
92 /* Text */
93 void QgsLabelAttributes::setText( const QString & text )
94 {
95  mText = text;
96  mTextIsSet = true;
97 }
98 
100 {
101  return mTextIsSet;
102 }
103 
104 const QString QgsLabelAttributes::text( void ) const
105 {
106  return mText;
107 }
108 
109 
110 /* Offset */
111 void QgsLabelAttributes::setOffset( double x, double y, int type )
112 {
113  mOffsetType = type;
114  mXOffset = x;
115  mYOffset = y;
116  mOffsetIsSet = true;
117 }
118 
120 {
121  return mOffsetIsSet;
122 }
123 
125 {
126  return mOffsetType;
127 }
128 
129 double QgsLabelAttributes::xOffset( void ) const
130 {
131  return mXOffset;
132 }
133 
134 double QgsLabelAttributes::yOffset( void ) const
135 {
136  return mYOffset;
137 }
138 
139 /* Angle */
141 {
142  mAngle = angle;
143  mAngleIsSet = true;
144 }
145 
147 {
148  return mAngleIsSet;
149 }
150 
151 double QgsLabelAttributes::angle( void ) const
152 {
153  return mAngle;
154 }
155 
157 {
158  return mAngleIsAuto;
159 }
160 
162 {
163  mAngleIsAuto = state;
164 }
165 
166 /* Alignment */
167 void QgsLabelAttributes::setAlignment( int alignment )
168 {
170  mAlignmentIsSet = true;
171 }
172 
174 {
175  return mAlignmentIsSet;
176 }
177 
179 {
180  return mAlignment;
181 }
182 
183 /* Font */
184 void QgsLabelAttributes::setFamily( const QString & family )
185 {
186  mFont.setFamily( family );
187  mFamilyIsSet = true;
188 }
189 
191 {
192  return mFamilyIsSet;
193 }
194 
195 const QString QgsLabelAttributes::family( void ) const
196 {
197  return mFont.family();
198 }
199 
200 
201 void QgsLabelAttributes::setBold( bool enable )
202 {
203  mFont.setBold( enable );
204  mBoldIsSet = true;
205 }
206 
208 {
209  return mBoldIsSet;
210 }
211 
212 bool QgsLabelAttributes::bold( void ) const
213 {
214  return mFont.bold();
215 }
216 
217 
218 void QgsLabelAttributes::setItalic( bool enable )
219 {
220  mFont.setItalic( enable );
221  mItalicIsSet = true;
222 }
223 
225 {
226  return mItalicIsSet;
227 }
228 
229 bool QgsLabelAttributes::italic( void ) const
230 {
231  return mFont.italic();
232 }
233 
234 
236 {
237  mFont.setUnderline( enable );
238  mUnderlineIsSet = true;
239 }
240 
242 {
243  return mUnderlineIsSet;
244 }
245 
247 {
248  return mFont.underline();
249 }
250 
252 {
253  mFont.setStrikeOut( enable );
254  mStrikeOutIsSet = true;
255 }
256 
258 {
259  return mStrikeOutIsSet;
260 }
261 
263 {
264  return mFont.strikeOut();
265 }
266 
267 
268 void QgsLabelAttributes::setSize( double size, int type )
269 {
270  mSizeType = type;
271  mSize = size;
272  mSizeIsSet = true;
273 }
274 
276 {
277  return mSizeIsSet;
278 }
279 
281 {
282  return mSizeType;
283 }
284 
285 double QgsLabelAttributes::size( void ) const
286 {
287  return mSize;
288 }
289 
290 
291 void QgsLabelAttributes::setColor( const QColor &color )
292 {
293  mColor = color;
294  mColorIsSet = true;
295 }
296 
298 {
299  return mColorIsSet;
300 }
301 
302 const QColor & QgsLabelAttributes::color( void ) const
303 {
304  return mColor;
305 }
306 
307 /* Buffer */
309 {
310  return mBufferEnabledFlag;
311 }
312 void QgsLabelAttributes::setBufferEnabled( bool useBufferFlag )
313 {
314  mBufferEnabledFlag = useBufferFlag;
315 }
316 void QgsLabelAttributes::setBufferSize( double size, int type )
317 {
318  mBufferSizeType = type;
319  mBufferSize = size;
320  mBufferSizeIsSet = true;
321 }
322 
324 {
325  return mBufferSizeIsSet;
326 }
327 
329 {
330  return mBufferSizeType;
331 }
332 
333 double QgsLabelAttributes::bufferSize( void ) const
334 {
335  return mBufferSize;
336 }
337 
338 
339 void QgsLabelAttributes::setBufferColor( const QColor &color )
340 {
341  mBufferBrush.setColor( color );
342  mBufferColorIsSet = true;
343 }
344 
346 {
347  return mColorIsSet;
348 }
349 
351 {
352  return mBufferBrush.color();
353 }
354 
355 
356 void QgsLabelAttributes::setBufferStyle( Qt::BrushStyle style )
357 {
358  mBufferBrush.setStyle( style );
359  mBufferStyleIsSet = true;
360 }
361 
363 {
364  return mBufferStyleIsSet;
365 }
366 
367 Qt::BrushStyle QgsLabelAttributes::bufferStyle( void ) const
368 {
369  return mBufferBrush.style();
370 }
371 
372 /* Border */
373 void QgsLabelAttributes::setBorderColor( const QColor &color )
374 {
375  mBorderPen.setColor( color );
376  mBorderColorIsSet = true;
377 }
378 
380 {
381  return mBorderColorIsSet;
382 }
383 
385 {
386  return mBorderPen.color();
387 }
388 
390 {
391  mBorderPen.setWidth( width );
392  mBorderWidthIsSet = true;
393 }
394 
396 {
397  return mBorderWidthIsSet;
398 }
399 
401 {
402  return mBorderPen.width();
403 }
404 
405 
406 void QgsLabelAttributes::setBorderStyle( Qt::PenStyle style )
407 {
408  mBorderPen.setStyle( style );
409  mBorderStyleIsSet = true;
410 }
411 
413 {
414  return mBorderStyleIsSet;
415 }
416 
417 Qt::PenStyle QgsLabelAttributes::borderStyle( void ) const
418 {
419  return mBorderPen.style();
420 }
421 
422 /* Multiline */
424 {
425  return mMultilineEnabledFlag;
426 }
427 void QgsLabelAttributes::setMultilineEnabled( bool useMultilineFlag )
428 {
429  mMultilineEnabledFlag = useMultilineFlag;
430 }
431 
432 /* selected only */
434 {
435  return mSelectedOnly;
436 }
437 void QgsLabelAttributes::setSelectedOnly( bool selectedOnly )
438 {
440 }
441 
442 /* units */
443 QString QgsLabelAttributes::unitsName( int units )
444 {
445  if ( units == MapUnits )
446  {
447  return QString( "mu" );
448  }
449 
450  return QString( "pt" );
451 }
452 
453 int QgsLabelAttributes::unitsCode( const QString &name )
454 {
455  if ( name.compare( "mu" ) == 0 )
456  {
457  return MapUnits;
458  }
459 
460  return PointUnits;
461 }
462 
463 /* alignment */
464 QString QgsLabelAttributes::alignmentName( int alignment )
465 {
466  QgsDebugMsg( QString( "alignment=%1" ).arg( alignment ) );
467  if ( !alignment ) return QString( "center" );
468  if ( alignment == ( Qt::AlignRight | Qt::AlignBottom ) ) return QString( "aboveleft" );
469  if ( alignment == ( Qt::AlignRight | Qt::AlignTop ) ) return QString( "belowleft" );
470  if ( alignment == ( Qt::AlignLeft | Qt::AlignBottom ) ) return QString( "aboveright" );
471  if ( alignment == ( Qt::AlignLeft | Qt::AlignTop ) ) return QString( "belowright" );
472  if ( alignment == ( Qt::AlignRight | Qt::AlignVCenter ) ) return QString( "left" );
473  if ( alignment == ( Qt::AlignLeft | Qt::AlignVCenter ) ) return QString( "right" );
474  if ( alignment == ( Qt::AlignBottom | Qt::AlignHCenter ) ) return QString( "above" );
475  if ( alignment == ( Qt::AlignTop | Qt::AlignHCenter ) ) return QString( "below" );
476  if ( alignment == ( Qt::AlignCenter ) ) return QString( "center" );
477  return QString( "center" );
478 }
479 
480 int QgsLabelAttributes::alignmentCode( const QString &name )
481 {
482  QString lname = name.toLower();
483  if ( lname.compare( "aboveleft" ) == 0 ) return Qt::AlignRight | Qt::AlignBottom ;
484  if ( lname.compare( "belowleft" ) == 0 ) return Qt::AlignRight | Qt::AlignTop ;
485  if ( lname.compare( "aboveright" ) == 0 ) return Qt::AlignLeft | Qt::AlignBottom ;
486  if ( lname.compare( "belowright" ) == 0 ) return Qt::AlignLeft | Qt::AlignTop ;
487  if ( lname.compare( "left" ) == 0 ) return Qt::AlignRight | Qt::AlignVCenter ;
488  if ( lname.compare( "right" ) == 0 ) return Qt::AlignLeft | Qt::AlignVCenter ;
489  if ( lname.compare( "above" ) == 0 ) return Qt::AlignBottom | Qt::AlignHCenter ;
490  if ( lname.compare( "below" ) == 0 ) return Qt::AlignTop | Qt::AlignHCenter ;
491  if ( lname.compare( "center" ) == 0 ) return Qt::AlignCenter ;
492 
493 
494  return Qt::AlignCenter;
495 }