libyui  3.10.0
YIntField.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: YIntField.cc
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 
26 #define YUILogComponent "ui"
27 #include "YUILog.h"
28 
29 #include "YIntField.h"
30 
31 using std::string;
32 
33 
35 {
36  YIntFieldPrivate( const string & label,
37  int minValue,
38  int maxValue )
39  : label( label )
40  , minValue( minValue )
41  , maxValue( maxValue )
42  {}
43 
44  string label;
45  int minValue;
46  int maxValue;
47 };
48 
49 
50 
51 
53  const string & label,
54  int minValue,
55  int maxValue )
56  : YWidget( parent )
57  , priv( new YIntFieldPrivate( label, minValue, maxValue ) )
58 {
59  YUI_CHECK_NEW( priv );
60 
61  setDefaultStretchable( YD_HORIZ, true );
62  setStretchable( YD_VERT, false );
63 }
64 
65 
67 {
68  // NOP
69 }
70 
71 
72 int
73 YIntField::enforceRange( int val ) const
74 {
75  if ( val < priv->minValue )
76  val = priv->minValue;
77 
78  if ( val > priv->maxValue )
79  val = priv->maxValue;
80 
81  return val;
82 }
83 
84 
85 int
87 {
88  return priv->minValue;
89 }
90 
91 
92 void
94 {
95  priv->minValue = val;
96 
97  int oldValue = value();
98  int newValue = enforceRange ( oldValue );
99 
100  if ( oldValue != newValue )
101  setValue( newValue ); // This might be expensive
102 }
103 
104 
105 int
107 {
108  return priv->maxValue;
109 }
110 
111 
112 void
114 {
115  priv->maxValue = val;
116 
117  int oldValue = value();
118  int newValue = enforceRange ( oldValue );
119 
120  if ( oldValue != newValue )
121  setValue( newValue ); // This might be expensive
122 }
123 
124 
125 string
127 {
128  return priv->label;
129 }
130 
131 
132 void
133 YIntField::setLabel( const string & label )
134 {
135  priv->label = label;
136 }
137 
138 
139 
140 const YPropertySet &
142 {
143  static YPropertySet propSet;
144 
145  if ( propSet.isEmpty() )
146  {
147  /*
148  * @property integer Value the field's contents (the user input)
149  * @property integer MinValue the minimum value
150  * @property integer MaxValue the maximum value
151  * @property string Label caption above the field
152  */
153  propSet.add( YProperty( YUIProperty_Value, YIntegerProperty ) );
154  propSet.add( YProperty( YUIProperty_MinValue, YIntegerProperty ) );
155  propSet.add( YProperty( YUIProperty_MaxValue, YIntegerProperty ) );
156  propSet.add( YProperty( YUIProperty_Label, YStringProperty ) );
157  propSet.add( YWidget::propertySet() );
158  }
159 
160  return propSet;
161 }
162 
163 
164 bool
165 YIntField::setProperty( const string & propertyName, const YPropertyValue & val )
166 {
167  propertySet().check( propertyName, val.type() ); // throws exceptions if not found or type mismatch
168 
169  if ( propertyName == YUIProperty_Value ) setValue ( val.integerVal() );
170  else if ( propertyName == YUIProperty_MinValue ) setMinValue( val.integerVal() );
171  else if ( propertyName == YUIProperty_MaxValue ) setMaxValue( val.integerVal() );
172  else if ( propertyName == YUIProperty_Label ) setLabel( val.stringVal() );
173  else
174  {
175  return YWidget::setProperty( propertyName, val );
176  }
177 
178  return true; // success -- no special processing necessary
179 }
180 
181 
183 YIntField::getProperty( const string & propertyName )
184 {
185  propertySet().check( propertyName ); // throws exceptions if not found
186 
187  if ( propertyName == YUIProperty_Value ) return YPropertyValue( value() );
188  if ( propertyName == YUIProperty_MinValue ) return YPropertyValue( minValue() );
189  if ( propertyName == YUIProperty_MaxValue ) return YPropertyValue( maxValue() );
190  else if ( propertyName == YUIProperty_Label ) return YPropertyValue( label() );
191  else
192  {
193  return YWidget::getProperty( propertyName );
194  }
195 }
YPropertySet::add
void add(const YProperty &prop)
Add a property to this property set.
Definition: YProperty.cc:146
YWidget
Abstract base class of all UI widgets.
Definition: YWidget.h:54
YIntField::propertySet
virtual const YPropertySet & propertySet()
Return this class's property set.
Definition: YIntField.cc:141
YIntField::setProperty
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
Definition: YIntField.cc:165
YIntField::setMaxValue
void setMaxValue(int val)
Set a new maximum value.
Definition: YIntField.cc:113
YPropertySet
A set of properties to check names and types against.
Definition: YProperty.h:197
YPropertySet::isEmpty
bool isEmpty() const
Returns 'true' if this property set does not contain anything.
Definition: YProperty.h:263
YIntFieldPrivate
Definition: YIntField.cc:34
YPropertyValue::stringVal
std::string stringVal() const
Methods to get the value of this property.
Definition: YProperty.h:180
YPropertyValue::type
YPropertyType type() const
Returns the type of this property value.
Definition: YProperty.h:169
YIntField::getProperty
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YIntField.cc:183
YIntField::label
std::string label() const
Get the label (the caption above the input field).
Definition: YIntField.cc:126
YIntField::YIntField
YIntField(YWidget *parent, const std::string &label, int minValue, int maxValue)
Constructor.
Definition: YIntField.cc:52
YWidget::getProperty
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YWidget.cc:457
YProperty
Class for widget properties.
Definition: YProperty.h:51
YWidget::setStretchable
void setStretchable(YUIDimension dim, bool newStretch)
Set the stretchable state to "newStretch" regardless of any hstretch or vstretch options.
Definition: YWidget.cc:560
YIntField::~YIntField
virtual ~YIntField()
Destructor.
Definition: YIntField.cc:66
YIntField::maxValue
int maxValue() const
Return the maximum value.
Definition: YIntField.cc:106
YIntField::minValue
int minValue() const
Return the minimum value.
Definition: YIntField.cc:86
YPropertySet::check
void check(const std::string &propertyName) const
Check if a property 'propertyName' exists in this property set.
Definition: YProperty.cc:88
YIntField::setMinValue
void setMinValue(int val)
Set a new minimum value.
Definition: YIntField.cc:93
YPropertyValue
Transport class for the value of simple properties.
Definition: YProperty.h:104
YIntField::setValue
void setValue(int val)
Set the current value (the number entered by the user or set from the outside) of this IntField.
Definition: YIntField.h:81
YWidget::setDefaultStretchable
void setDefaultStretchable(YUIDimension dim, bool newStretch)
Set the stretchable state to "newStretch".
Definition: YWidget.cc:566
YIntField::setLabel
virtual void setLabel(const std::string &label)
Set the label (the caption above the input field).
Definition: YIntField.cc:133
YIntField::enforceRange
int enforceRange(int val) const
Enforce 'val' to be between minValue and maxValue.
Definition: YIntField.cc:73
YIntField::value
virtual int value()=0
Get the current value (the number entered by the user or set from the outside) of this IntField.
YWidget::setProperty
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
Definition: YWidget.cc:432
YWidget::propertySet
virtual const YPropertySet & propertySet()
Return this class's property set.
Definition: YWidget.cc:395