libyui  3.3.2
YLabel.cc
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: YLabel.cc
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 #define MAX_DEBUG_LABEL_LEN 32
26 
27 
28 #define YUILogComponent "ui"
29 #include "YUILog.h"
30 
31 #include "YUISymbols.h"
32 #include "YLabel.h"
33 
34 
36 {
37  /**
38  * Constructor
39  **/
40  YLabelPrivate( const std::string & text,
41  bool isHeading,
42  bool isOutputField )
43  : text( text )
44  , isHeading( isHeading )
45  , isOutputField( isOutputField )
46  , useBoldFont( false )
47  {}
48 
49  std::string text;
50  bool isHeading;
51  bool isOutputField;
52  bool useBoldFont;
53 };
54 
55 
57  const std::string & text,
58  bool isHeading,
59  bool isOutputField )
60  : YWidget( parent )
61  , priv( new YLabelPrivate( text, isHeading, isOutputField ) )
62 {
63  YUI_CHECK_NEW( priv );
64 }
65 
66 
68 {
69  // NOP
70 }
71 
72 
73 std::string YLabel::text() const
74 {
75  return priv->text;
76 }
77 
78 
79 void YLabel::setText( const std::string & newText )
80 {
81  priv->text = newText;
82 }
83 
84 
85 bool YLabel::isHeading() const
86 {
87  return priv->isHeading;
88 }
89 
90 
92 {
93  return priv->isOutputField;
94 }
95 
96 
97 bool YLabel::useBoldFont() const
98 {
99  return priv->useBoldFont;
100 }
101 
102 
103 void YLabel::setUseBoldFont( bool bold )
104 {
105  priv->useBoldFont = bold;
106 }
107 
108 
109 const YPropertySet &
111 {
112  static YPropertySet propSet;
113 
114  if ( propSet.isEmpty() )
115  {
116  /*
117  * @property std::string Label the label text
118  * @property std::string Value the label text (alias for Label)
119  * @property std::string Text the label text (alias for Label)
120  */
121 
122  propSet.add( YProperty( YUIProperty_Label, YStringProperty ) );
123  propSet.add( YProperty( YUIProperty_Value, YStringProperty ) );
124  propSet.add( YProperty( YUIProperty_Text, YStringProperty ) );
125  propSet.add( YWidget::propertySet() );
126  }
127 
128  return propSet;
129 }
130 
131 
132 bool
133 YLabel::setProperty( const std::string & propertyName, const YPropertyValue & val )
134 {
135  propertySet().check( propertyName, val.type() ); // throws exceptions if not found or type mismatch
136 
137  if ( propertyName == YUIProperty_Label ) setText( val.stringVal() );
138  else if ( propertyName == YUIProperty_Value ) setText( val.stringVal() );
139  else if ( propertyName == YUIProperty_Text ) setText( val.stringVal() );
140  else
141  {
142  return YWidget::setProperty( propertyName, val );
143  }
144 
145  return true; // success -- no special processing necessary
146 }
147 
148 
150 YLabel::getProperty( const std::string & propertyName )
151 {
152  propertySet().check( propertyName ); // throws exceptions if not found
153 
154  if ( propertyName == YUIProperty_Label ) return YPropertyValue( text() );
155  else if ( propertyName == YUIProperty_Value ) return YPropertyValue( text() );
156  else if ( propertyName == YUIProperty_Text ) return YPropertyValue( text() );
157  else
158  {
159  return YWidget::getProperty( propertyName );
160  }
161 }
162 
163 
164 std::string YLabel::debugLabel() const
165 {
166  std::string label = text();
167 
168  if ( label.size() > MAX_DEBUG_LABEL_LEN )
169  {
170  label.resize( MAX_DEBUG_LABEL_LEN );
171  label.append( "..." );
172  }
173 
174  for ( std::string::size_type i=0; i < label.size(); i++ )
175  {
176  if ( label[i] == '\n' ) label[i] = ' ';
177  if ( label[i] == '\"' ) label[i] = ' ';
178  }
179 
180  return label;
181 }
182 
183 
184 
185 const char *
187 {
188  if ( priv->isHeading ) return "YLabel_Heading";
189  else if ( priv->isOutputField ) return "YLabel_OutputField";
190  else return "YLabel";
191 }
bool isOutputField() const
Return &#39;true&#39; if this is an OutputField widget, i.e., it should display its text similar to an InputF...
Definition: YLabel.cc:91
virtual void setText(const std::string &newText)
Set the text the widget displays.
Definition: YLabel.cc:79
YLabelPrivate(const std::string &text, bool isHeading, bool isOutputField)
Constructor.
Definition: YLabel.cc:40
bool isEmpty() const
Returns &#39;true&#39; if this property set does not contain anything.
Definition: YProperty.h:263
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
Definition: YLabel.cc:133
bool useBoldFont() const
Return &#39;true&#39; if a bold font should be used.
Definition: YLabel.cc:97
Transport class for the value of simple properties.
Definition: YProperty.h:104
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YLabel.cc:150
void add(const YProperty &prop)
Add a property to this property set.
Definition: YProperty.cc:145
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
Definition: YWidget.cc:428
A set of properties to check names and types against.
Definition: YProperty.h:197
std::string text() const
Return the text the widget displays.
Definition: YLabel.cc:73
bool isHeading() const
Return &#39;true&#39; if this is a Heading widget, i.e., it should display its text in a bold and/or larger f...
Definition: YLabel.cc:85
virtual const YPropertySet & propertySet()
Return this class&#39;s property set.
Definition: YWidget.cc:393
virtual const YPropertySet & propertySet()
Return this class&#39;s property set.
Definition: YLabel.cc:110
YLabel(YWidget *parent, const std::string &text, bool isHeading=false, bool isOutputField=false)
Constructor.
Definition: YLabel.cc:56
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YWidget.cc:453
std::string stringVal() const
Methods to get the value of this property.
Definition: YProperty.h:180
Class for widget properties.
Definition: YProperty.h:51
virtual const char * widgetClass() const
Returns a descriptive name of this widget class for logging, debugging etc.
Definition: YLabel.cc:186
virtual void setUseBoldFont(bool bold=true)
Switch bold font on or off.
Definition: YLabel.cc:103
virtual ~YLabel()
Destructor.
Definition: YLabel.cc:67
virtual std::string debugLabel() const
Returns a descriptive label of this widget instance for debugging.
Definition: YLabel.cc:164
void check(const std::string &propertyName) const
Check if a property &#39;propertyName&#39; exists in this property set.
Definition: YProperty.cc:87
Abstract base class of all UI widgets.
Definition: YWidget.h:54
YPropertyType type() const
Returns the type of this property value.
Definition: YProperty.h:169