libyui  3.0.10
/usr/src/RPM/BUILD/libyui-3.0.10/src/YBarGraph.cc
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:         YBarGraph.cc
00020 
00021   Author:       Stefan Hundhammer <sh@suse.de>
00022 
00023 /-*/
00024 
00025 
00026 #include <stdio.h>
00027 #include <vector>
00028 
00029 #define YUILogComponent "ui"
00030 #include "YUILog.h"
00031 
00032 #include "YUISymbols.h"
00033 #include "YBarGraph.h"
00034 
00035 
00036 
00037 #define CHECK_INDEX(index)                                              \
00038     do                                                                  \
00039     {                                                                   \
00040         if ( (index) < 0  ||                                            \
00041              (index) >= (int) priv->segments.size() )                   \
00042         {                                                               \
00043             YUI_THROW( YUIIndexOutOfRangeException(                     \
00044                            (index), /* current */                       \
00045                            0,       /* min */                           \
00046                            (int) priv->segments.size() - 1  ) ); /* max */ \
00047         }                                                               \
00048     } while( 0 )
00049 
00050 
00051 
00052 struct YBarGraphPrivate
00053 {
00054     YBarGraphPrivate()
00055         : updatesPending( false )
00056         , postponeUpdates( false )
00057         {}
00058 
00059     std::vector<YBarGraphSegment>       segments;
00060     bool                                updatesPending;
00061     bool                                postponeUpdates;
00062 };
00063 
00064 
00065 
00066 
00067 YBarGraph::YBarGraph( YWidget * parent )
00068     : YWidget( parent )
00069     , priv( new YBarGraphPrivate() )
00070 {
00071     YUI_CHECK_NEW( priv );
00072     setDefaultStretchable( YD_HORIZ, true );
00073 }
00074 
00075 
00076 YBarGraph::~YBarGraph()
00077 {
00078     // NOP
00079 }
00080 
00081 
00082 void
00083 YBarGraph::updateDisplay()
00084 {
00085     priv->updatesPending = true;
00086 
00087     if ( ! priv->postponeUpdates )
00088     {
00089         doUpdate();
00090         priv->updatesPending = false;
00091     }
00092 }
00093 
00094 
00095 void
00096 YBarGraph::addSegment( const YBarGraphSegment & segment )
00097 {
00098     priv->segments.push_back( segment );
00099     updateDisplay();
00100 }
00101 
00102 
00103 void
00104 YBarGraph::deleteAllSegments()
00105 {
00106     priv->segments.clear();
00107     updateDisplay();
00108 }
00109 
00110 
00111 const YBarGraphSegment &
00112 YBarGraph::segment( int segmentIndex ) const
00113 {
00114     CHECK_INDEX( segmentIndex );
00115 
00116     return priv->segments[ segmentIndex ];
00117 }
00118 
00119 
00120 int
00121 YBarGraph::segments()
00122 {
00123     return (int) priv->segments.size();
00124 }
00125 
00126 
00127 void
00128 YBarGraph::setValue( int segmentIndex, int newValue )
00129 {
00130     CHECK_INDEX( segmentIndex );
00131 
00132     priv->segments[ segmentIndex ].setValue( newValue );
00133     updateDisplay();
00134 }
00135 
00136 
00137 void
00138 YBarGraph::setLabel( int segmentIndex, const std::string & newLabel )
00139 {
00140     CHECK_INDEX( segmentIndex );
00141 
00142     priv->segments[ segmentIndex ].setLabel( newLabel );
00143     updateDisplay();
00144 }
00145 
00146 
00147 void
00148 YBarGraph::setSegmentColor( int segmentIndex, const YColor & color )
00149 {
00150     CHECK_INDEX( segmentIndex );
00151 
00152     if ( color.isUndefined() )
00153         YUI_THROW( YUIException( "Invalid YColor" ) );
00154 
00155     priv->segments[ segmentIndex ].setSegmentColor( color );
00156     updateDisplay();
00157 }
00158 
00159 
00160 void
00161 YBarGraph::setTextColor( int segmentIndex, const YColor & color )
00162 {
00163     CHECK_INDEX( segmentIndex );
00164 
00165     if ( color.isUndefined() )
00166         YUI_THROW( YUIException( "Invalid YColor" ) );
00167 
00168     priv->segments[ segmentIndex ].setTextColor( color );
00169     updateDisplay();
00170 }
00171 
00172 
00173 const YPropertySet &
00174 YBarGraph::propertySet()
00175 {
00176     static YPropertySet propSet;
00177 
00178     if ( propSet.isEmpty() )
00179     {
00180         /*
00181          * @property list<integer> Values       The numerical value for each segment.
00182          * @property list<std::string>  Labels  Text label for each segment ('\n' allowed).
00183          *                                      Use %1 as a placeholder for the current value.
00184          */
00185         propSet.add( YProperty( YUIProperty_Values,             YOtherProperty   ) );
00186         propSet.add( YProperty( YUIProperty_Labels,             YOtherProperty   ) );
00187         propSet.add( YWidget::propertySet() );
00188     }
00189 
00190     return propSet;
00191 }
00192 
00193 
00194 bool
00195 YBarGraph::setProperty( const std::string & propertyName, const YPropertyValue & val )
00196 {
00197     propertySet().check( propertyName, val.type() ); // throws exceptions if not found or type mismatch
00198 
00199     if      ( propertyName == YUIProperty_Values )      return false; // Needs special handling
00200     else if ( propertyName == YUIProperty_Labels )      return false; // Needs special handling
00201     else
00202     {
00203         YWidget::setProperty( propertyName, val );
00204     }
00205 
00206     return true; // success -- no special handling necessary
00207 }
00208 
00209 
00210 YPropertyValue
00211 YBarGraph::getProperty( const std::string & propertyName )
00212 {
00213     propertySet().check( propertyName ); // throws exceptions if not found
00214 
00215     if      ( propertyName == YUIProperty_Values        )       return YPropertyValue( YOtherProperty );
00216     else if ( propertyName == YUIProperty_Labels        )       return YPropertyValue( YOtherProperty );
00217     else
00218     {
00219         return YWidget::getProperty( propertyName );
00220     }
00221 }
00222 
00223 
00224 
00225 
00226 YBarGraphMultiUpdate::YBarGraphMultiUpdate( YBarGraph * barGraph )
00227     : _barGraph ( barGraph )
00228 {
00229     YUI_CHECK_PTR( barGraph );
00230 
00231     _barGraph->priv->postponeUpdates = true;
00232 }
00233 
00234 
00235 YBarGraphMultiUpdate::~YBarGraphMultiUpdate()
00236 {
00237     _barGraph->priv->postponeUpdates = false;
00238 
00239     if ( _barGraph->priv->updatesPending )
00240         _barGraph->updateDisplay();
00241 }
 All Classes Functions Variables Enumerations Friends