libyui  3.9.3
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 using std::string;
35 
36 
38 {
39  /**
40  * Constructor
41  **/
42  YLabelPrivate( const string & text,
43  bool isHeading,
44  bool isOutputField )
45  : text( text )
46  , isHeading( isHeading )
47  , isOutputField( isOutputField )
48  , useBoldFont( false )
49  {}
50 
51  string text;
52  bool isHeading;
53  bool isOutputField;
54  bool useBoldFont;
55 };
56 
57 
59  const string & text,
60  bool isHeading,
61  bool isOutputField )
62  : YWidget( parent )
63  , priv( new YLabelPrivate( text, isHeading, isOutputField ) )
64 {
65  YUI_CHECK_NEW( priv );
66 }
67 
68 
70 {
71  // NOP
72 }
73 
74 
75 string YLabel::text() const
76 {
77  return priv->text;
78 }
79 
80 
81 void YLabel::setText( const string & newText )
82 {
83  priv->text = newText;
84 }
85 
86 
87 bool YLabel::isHeading() const
88 {
89  return priv->isHeading;
90 }
91 
92 
94 {
95  return priv->isOutputField;
96 }
97 
98 
99 bool YLabel::useBoldFont() const
100 {
101  return priv->useBoldFont;
102 }
103 
104 
105 void YLabel::setUseBoldFont( bool bold )
106 {
107  priv->useBoldFont = bold;
108 }
109 
110 
111 const YPropertySet &
113 {
114  static YPropertySet propSet;
115 
116  if ( propSet.isEmpty() )
117  {
118  /*
119  * @property string Label the label text
120  * @property string Value the label text (alias for Label)
121  * @property string Text the label text (alias for Label)
122  */
123 
124  propSet.add( YProperty( YUIProperty_Label, YStringProperty ) );
125  propSet.add( YProperty( YUIProperty_Value, YStringProperty ) );
126  propSet.add( YProperty( YUIProperty_Text, YStringProperty ) );
127  propSet.add( YWidget::propertySet() );
128  }
129 
130  return propSet;
131 }
132 
133 
134 bool
135 YLabel::setProperty( const string & propertyName, const YPropertyValue & val )
136 {
137  propertySet().check( propertyName, val.type() ); // throws exceptions if not found or type mismatch
138 
139  if ( propertyName == YUIProperty_Label ) setText( val.stringVal() );
140  else if ( propertyName == YUIProperty_Value ) setText( val.stringVal() );
141  else if ( propertyName == YUIProperty_Text ) setText( val.stringVal() );
142  else
143  {
144  return YWidget::setProperty( propertyName, val );
145  }
146 
147  return true; // success -- no special processing necessary
148 }
149 
150 
152 YLabel::getProperty( const string & propertyName )
153 {
154  propertySet().check( propertyName ); // throws exceptions if not found
155 
156  if ( propertyName == YUIProperty_Label ) return YPropertyValue( text() );
157  else if ( propertyName == YUIProperty_Value ) return YPropertyValue( text() );
158  else if ( propertyName == YUIProperty_Text ) return YPropertyValue( text() );
159  else
160  {
161  return YWidget::getProperty( propertyName );
162  }
163 }
164 
165 
166 string YLabel::debugLabel() const
167 {
168  string label = text();
169 
170  if ( label.size() > MAX_DEBUG_LABEL_LEN )
171  {
172  label.resize( MAX_DEBUG_LABEL_LEN );
173  label.append( "..." );
174  }
175 
176  for ( string::size_type i=0; i < label.size(); i++ )
177  {
178  if ( label[i] == '\n' ) label[i] = ' ';
179  if ( label[i] == '\"' ) label[i] = ' ';
180  }
181 
182  return label;
183 }
184 
185 
186 
187 const char *
189 {
190  if ( priv->isHeading ) return "YLabel_Heading";
191  else if ( priv->isOutputField ) return "YLabel_OutputField";
192  else return "YLabel";
193 }
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:93
virtual void setText(const std::string &newText)
Set the text the widget displays.
Definition: YLabel.cc:81
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:135
bool useBoldFont() const
Return &#39;true&#39; if a bold font should be used.
Definition: YLabel.cc:99
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:152
void add(const YProperty &prop)
Add a property to this property set.
Definition: YProperty.cc:146
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
Definition: YWidget.cc:432
A set of properties to check names and types against.
Definition: YProperty.h:197
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:87
virtual const YPropertySet & propertySet()
Return this class&#39;s property set.
Definition: YWidget.cc:395
virtual const YPropertySet & propertySet()
Return this class&#39;s property set.
Definition: YLabel.cc:112
std::string text() const
Return the text the widget displays.
Definition: YLabel.cc:75
virtual std::string debugLabel() const
Returns a descriptive label of this widget instance for debugging.
Definition: YLabel.cc:166
YLabel(YWidget *parent, const std::string &text, bool isHeading=false, bool isOutputField=false)
Constructor.
Definition: YLabel.cc:58
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YWidget.cc:457
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:188
virtual void setUseBoldFont(bool bold=true)
Switch bold font on or off.
Definition: YLabel.cc:105
virtual ~YLabel()
Destructor.
Definition: YLabel.cc:69
YLabelPrivate(const string &text, bool isHeading, bool isOutputField)
Constructor.
Definition: YLabel.cc:42
void check(const std::string &propertyName) const
Check if a property &#39;propertyName&#39; exists in this property set.
Definition: YProperty.cc:88
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