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