libyui
3.0.10
|
00001 /* 00002 Copyright (C) 2000-2012 Novell, Inc 00003 This library is free software; you can redistribute it and/or modify 00004 it under the terms of the GNU Lesser General Public License as 00005 published by the Free Software Foundation; either version 2.1 of the 00006 License, or (at your option) version 3.0 of the License. This library 00007 is distributed in the hope that it will be useful, but WITHOUT ANY 00008 WARRANTY; without even the implied warranty of MERCHANTABILITY or 00009 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 00010 License for more details. You should have received a copy of the GNU 00011 Lesser General Public License along with this library; if not, write 00012 to the Free Software Foundation, Inc., 51 Franklin Street, Fifth 00013 Floor, Boston, MA 02110-1301 USA 00014 */ 00015 00016 00017 /*-/ 00018 00019 File: YIntField.cc 00020 00021 Author: Stefan Hundhammer <sh@suse.de> 00022 00023 /-*/ 00024 00025 00026 #define YUILogComponent "ui" 00027 #include "YUILog.h" 00028 00029 #include "YIntField.h" 00030 00031 00032 struct YIntFieldPrivate 00033 { 00034 YIntFieldPrivate( const std::string & label, 00035 int minValue, 00036 int maxValue ) 00037 : label( label ) 00038 , minValue( minValue ) 00039 , maxValue( maxValue ) 00040 {} 00041 00042 std::string label; 00043 int minValue; 00044 int maxValue; 00045 }; 00046 00047 00048 00049 00050 YIntField::YIntField( YWidget * parent, 00051 const std::string & label, 00052 int minValue, 00053 int maxValue ) 00054 : YWidget( parent ) 00055 , priv( new YIntFieldPrivate( label, minValue, maxValue ) ) 00056 { 00057 YUI_CHECK_NEW( priv ); 00058 00059 setDefaultStretchable( YD_HORIZ, true ); 00060 setStretchable( YD_VERT, false ); 00061 } 00062 00063 00064 YIntField::~YIntField() 00065 { 00066 // NOP 00067 } 00068 00069 00070 int 00071 YIntField::enforceRange( int val ) const 00072 { 00073 if ( val < priv->minValue ) 00074 val = priv->minValue; 00075 00076 if ( val > priv->maxValue ) 00077 val = priv->maxValue; 00078 00079 return val; 00080 } 00081 00082 00083 int 00084 YIntField::minValue() const 00085 { 00086 return priv->minValue; 00087 } 00088 00089 00090 void 00091 YIntField::setMinValue( int val ) 00092 { 00093 priv->minValue = val; 00094 00095 int oldValue = value(); 00096 int newValue = enforceRange ( oldValue ); 00097 00098 if ( oldValue != newValue ) 00099 setValue( newValue ); // This might be expensive 00100 } 00101 00102 00103 int 00104 YIntField::maxValue() const 00105 { 00106 return priv->maxValue; 00107 } 00108 00109 00110 void 00111 YIntField::setMaxValue( int val ) 00112 { 00113 priv->maxValue = val; 00114 00115 int oldValue = value(); 00116 int newValue = enforceRange ( oldValue ); 00117 00118 if ( oldValue != newValue ) 00119 setValue( newValue ); // This might be expensive 00120 } 00121 00122 00123 std::string 00124 YIntField::label() const 00125 { 00126 return priv->label; 00127 } 00128 00129 00130 void 00131 YIntField::setLabel( const std::string & label ) 00132 { 00133 priv->label = label; 00134 } 00135 00136 00137 00138 const YPropertySet & 00139 YIntField::propertySet() 00140 { 00141 static YPropertySet propSet; 00142 00143 if ( propSet.isEmpty() ) 00144 { 00145 /* 00146 * @property integer Value the field's contents (the user input) 00147 * @property integer MinValue the minimum value 00148 * @property integer MaxValue the maximum value 00149 * @property std::string Label caption above the field 00150 */ 00151 propSet.add( YProperty( YUIProperty_Value, YIntegerProperty ) ); 00152 propSet.add( YProperty( YUIProperty_MinValue, YIntegerProperty ) ); 00153 propSet.add( YProperty( YUIProperty_MaxValue, YIntegerProperty ) ); 00154 propSet.add( YProperty( YUIProperty_Label, YStringProperty ) ); 00155 propSet.add( YWidget::propertySet() ); 00156 } 00157 00158 return propSet; 00159 } 00160 00161 00162 bool 00163 YIntField::setProperty( const std::string & propertyName, const YPropertyValue & val ) 00164 { 00165 propertySet().check( propertyName, val.type() ); // throws exceptions if not found or type mismatch 00166 00167 if ( propertyName == YUIProperty_Value ) setValue ( val.integerVal() ); 00168 else if ( propertyName == YUIProperty_MinValue ) setMinValue( val.integerVal() ); 00169 else if ( propertyName == YUIProperty_MaxValue ) setMaxValue( val.integerVal() ); 00170 else if ( propertyName == YUIProperty_Label ) setLabel( val.stringVal() ); 00171 else 00172 { 00173 return YWidget::setProperty( propertyName, val ); 00174 } 00175 00176 return true; // success -- no special processing necessary 00177 } 00178 00179 00180 YPropertyValue 00181 YIntField::getProperty( const std::string & propertyName ) 00182 { 00183 propertySet().check( propertyName ); // throws exceptions if not found 00184 00185 if ( propertyName == YUIProperty_Value ) return YPropertyValue( value() ); 00186 if ( propertyName == YUIProperty_MinValue ) return YPropertyValue( minValue() ); 00187 if ( propertyName == YUIProperty_MaxValue ) return YPropertyValue( maxValue() ); 00188 else if ( propertyName == YUIProperty_Label ) return YPropertyValue( label() ); 00189 else 00190 { 00191 return YWidget::getProperty( propertyName ); 00192 } 00193 }