QGIS API Documentation  2.15.0-Master (af20121)
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 
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 
89 /* Text */
91 {
92  mText = text;
93  mTextIsSet = true;
94 }
95 
97 {
98  return mTextIsSet;
99 }
100 
102 {
103  return mText;
104 }
105 
106 
107 /* Offset */
108 void QgsLabelAttributes::setOffset( double x, double y, int type )
109 {
110  mOffsetType = type;
111  mXOffset = x;
112  mYOffset = y;
113  mOffsetIsSet = true;
114 }
115 
117 {
118  return mOffsetIsSet;
119 }
120 
122 {
123  return mOffsetType;
124 }
125 
127 {
128  return mXOffset;
129 }
130 
132 {
133  return mYOffset;
134 }
135 
136 /* Angle */
138 {
139  mAngle = angle;
140  mAngleIsSet = true;
141 }
142 
144 {
145  return mAngleIsSet;
146 }
147 
149 {
150  return mAngle;
151 }
152 
154 {
155  return mAngleIsAuto;
156 }
157 
159 {
160  mAngleIsAuto = state;
161 }
162 
163 /* Alignment */
165 {
167  mAlignmentIsSet = true;
168 }
169 
171 {
172  return mAlignmentIsSet;
173 }
174 
176 {
177  return mAlignment;
178 }
179 
180 /* Font */
182 {
183  mFont.setFamily( family );
184  mFamilyIsSet = true;
185 }
186 
188 {
189  return mFamilyIsSet;
190 }
191 
193 {
194  return mFont.family();
195 }
196 
197 
198 void QgsLabelAttributes::setBold( bool enable )
199 {
200  mFont.setBold( enable );
201  mBoldIsSet = true;
202 }
203 
205 {
206  return mBoldIsSet;
207 }
208 
210 {
211  return mFont.bold();
212 }
213 
214 
215 void QgsLabelAttributes::setItalic( bool enable )
216 {
217  mFont.setItalic( enable );
218  mItalicIsSet = true;
219 }
220 
222 {
223  return mItalicIsSet;
224 }
225 
227 {
228  return mFont.italic();
229 }
230 
231 
233 {
234  mFont.setUnderline( enable );
235  mUnderlineIsSet = true;
236 }
237 
239 {
240  return mUnderlineIsSet;
241 }
242 
244 {
245  return mFont.underline();
246 }
247 
249 {
250  mFont.setStrikeOut( enable );
251  mStrikeOutIsSet = true;
252 }
253 
255 {
256  return mStrikeOutIsSet;
257 }
258 
260 {
261  return mFont.strikeOut();
262 }
263 
264 
265 void QgsLabelAttributes::setSize( double size, int type )
266 {
267  mSizeType = type;
268  mSize = size;
269  mSizeIsSet = true;
270 }
271 
273 {
274  return mSizeIsSet;
275 }
276 
278 {
279  return mSizeType;
280 }
281 
283 {
284  return mSize;
285 }
286 
287 
289 {
290  mColor = color;
291  mColorIsSet = true;
292 }
293 
295 {
296  return mColorIsSet;
297 }
298 
300 {
301  return mColor;
302 }
303 
304 /* Buffer */
306 {
307  return mBufferEnabledFlag;
308 }
309 void QgsLabelAttributes::setBufferEnabled( bool useBufferFlag )
310 {
311  mBufferEnabledFlag = useBufferFlag;
312 }
313 void QgsLabelAttributes::setBufferSize( double size, int type )
314 {
315  mBufferSizeType = type;
316  mBufferSize = size;
317  mBufferSizeIsSet = true;
318 }
319 
321 {
322  return mBufferSizeIsSet;
323 }
324 
326 {
327  return mBufferSizeType;
328 }
329 
331 {
332  return mBufferSize;
333 }
334 
335 
337 {
338  mBufferBrush.setColor( color );
339  mBufferColorIsSet = true;
340 }
341 
343 {
344  return mColorIsSet;
345 }
346 
348 {
349  return mBufferBrush.color();
350 }
351 
352 
353 void QgsLabelAttributes::setBufferStyle( Qt::BrushStyle style )
354 {
355  mBufferBrush.setStyle( style );
356  mBufferStyleIsSet = true;
357 }
358 
360 {
361  return mBufferStyleIsSet;
362 }
363 
364 Qt::BrushStyle QgsLabelAttributes::bufferStyle() const
365 {
366  return mBufferBrush.style();
367 }
368 
369 /* Border */
371 {
372  mBorderPen.setColor( color );
373  mBorderColorIsSet = true;
374 }
375 
377 {
378  return mBorderColorIsSet;
379 }
380 
382 {
383  return mBorderPen.color();
384 }
385 
387 {
388  mBorderPen.setWidth( width );
389  mBorderWidthIsSet = true;
390 }
391 
393 {
394  return mBorderWidthIsSet;
395 }
396 
398 {
399  return mBorderPen.width();
400 }
401 
402 
403 void QgsLabelAttributes::setBorderStyle( Qt::PenStyle style )
404 {
405  mBorderPen.setStyle( style );
406  mBorderStyleIsSet = true;
407 }
408 
410 {
411  return mBorderStyleIsSet;
412 }
413 
415 {
416  return mBorderPen.style();
417 }
418 
419 /* Multiline */
421 {
422  return mMultilineEnabledFlag;
423 }
424 void QgsLabelAttributes::setMultilineEnabled( bool useMultilineFlag )
425 {
426  mMultilineEnabledFlag = useMultilineFlag;
427 }
428 
429 /* selected only */
431 {
432  return mSelectedOnly;
433 }
435 {
437 }
438 
439 /* units */
441 {
442  if ( units == MapUnits )
443  {
444  return QString( "mu" );
445  }
446 
447  return QString( "pt" );
448 }
449 
451 {
452  if ( name.compare( "mu" ) == 0 )
453  {
454  return MapUnits;
455  }
456 
457  return PointUnits;
458 }
459 
460 /* alignment */
462 {
463  QgsDebugMsg( QString( "alignment=%1" ).arg( alignment ) );
464  if ( !alignment ) return QString( "center" );
465  if ( alignment == ( Qt::AlignRight | Qt::AlignBottom ) ) return QString( "aboveleft" );
466  if ( alignment == ( Qt::AlignRight | Qt::AlignTop ) ) return QString( "belowleft" );
467  if ( alignment == ( Qt::AlignLeft | Qt::AlignBottom ) ) return QString( "aboveright" );
468  if ( alignment == ( Qt::AlignLeft | Qt::AlignTop ) ) return QString( "belowright" );
469  if ( alignment == ( Qt::AlignRight | Qt::AlignVCenter ) ) return QString( "left" );
470  if ( alignment == ( Qt::AlignLeft | Qt::AlignVCenter ) ) return QString( "right" );
471  if ( alignment == ( Qt::AlignBottom | Qt::AlignHCenter ) ) return QString( "above" );
472  if ( alignment == ( Qt::AlignTop | Qt::AlignHCenter ) ) return QString( "below" );
473  if ( alignment == ( Qt::AlignCenter ) ) return QString( "center" );
474  return QString( "center" );
475 }
476 
478 {
479  QString lname = name.toLower();
480  if ( lname.compare( "aboveleft" ) == 0 ) return Qt::AlignRight | Qt::AlignBottom;
481  if ( lname.compare( "belowleft" ) == 0 ) return Qt::AlignRight | Qt::AlignTop;
482  if ( lname.compare( "aboveright" ) == 0 ) return Qt::AlignLeft | Qt::AlignBottom;
483  if ( lname.compare( "belowright" ) == 0 ) return Qt::AlignLeft | Qt::AlignTop;
484  if ( lname.compare( "left" ) == 0 ) return Qt::AlignRight | Qt::AlignVCenter;
485  if ( lname.compare( "right" ) == 0 ) return Qt::AlignLeft | Qt::AlignVCenter;
486  if ( lname.compare( "above" ) == 0 ) return Qt::AlignBottom | Qt::AlignHCenter;
487  if ( lname.compare( "below" ) == 0 ) return Qt::AlignTop | Qt::AlignHCenter;
488  if ( lname.compare( "center" ) == 0 ) return Qt::AlignCenter;
489 
490 
491  return Qt::AlignCenter;
492 }
void setBufferColor(const QColor &color)
double bufferSize() const
bool borderColorIsSet() const
void setMultilineEnabled(bool useMultiline)
Qt::PenStyle style() const
void setStyle(Qt::PenStyle style)
void setBold(bool enable)
void setSize(double size, int type)
bool mSelectedOnly
Label only selected.
void setUnderline(bool enable)
void setSelectedOnly(bool selectedonly)
static int unitsCode(const QString &name)
void setFamily(const QString &family)
bool alignmentIsSet() const
Qt::BrushStyle style() const
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
static QString alignmentName(int alignment)
void setUnderline(bool enable)
int mAlignment
Alignment.
QBrush mBufferBrush
Buffer brush (color, style)
void setStyle(Qt::BrushStyle style)
QString tr(const char *sourceText, const char *disambiguation, int n)
void setBorderStyle(Qt::PenStyle style)
bool bold() const
bool italic() const
const QColor & color() const
void setAutoAngle(bool state)
void setBold(bool enable)
static QString unitsName(int units)
QColor color() const
QColor bufferColor() const
int mSizeType
Font size, size type.
QFont font()
void setBufferStyle(Qt::BrushStyle style)
static int alignmentCode(const QString &name)
const QColor & color() const
void setBufferSize(double size, int type)
double mAngle
Angle (degrees)
bool borderWidthIsSet() const
bool underline() const
void setBorderColor(const QColor &color)
QgsLabelAttributes(bool def=true)
Constructor.
void setColor(const QColor &color)
void setText(const QString &text)
bool multilineEnabled() const
void setAngle(double angle)
bool mBufferEnabledFlag
Buffer enablement.
bool mMultilineEnabledFlag
Multiline enablement.
bool bufferSizeIsSet() const
QString toLower() const
const QString family() const
void setItalic(bool enable)
void setStrikeOut(bool enable)
Qt::BrushStyle bufferStyle() const
Qt::PenStyle borderStyle() const
QFont mFont
Font (family, weight, italic, underline, strikeout)
void setAlignment(int alignment)
void setOffset(double x, double y, int type)
int width() const
void setWidth(int width)
QString family() const
bool strikeOutIsSet() const
void setFamily(const QString &family)
bool borderStyleIsSet() const
QPen mBorderPen
Border pen (color, width, style)
void setStrikeOut(bool enable)
bool underlineIsSet() const
int mBufferSizeType
Buffer size, size type.
bool strikeOut() const
bool bufferStyleIsSet() const
void setColor(const QColor &color)
QColor borderColor() const
int compare(const QString &other) const
void setColor(const QColor &color)
void setBufferEnabled(bool useBufferFlag)
bool bufferColorIsSet() const
void setBorderWidth(int width)
void setItalic(bool enable)
const QString text() const