libyui  3.10.0
YBarGraph.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: YBarGraph.cc
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 
26 #include <stdio.h>
27 #include <vector>
28 
29 #define YUILogComponent "ui"
30 #include "YUILog.h"
31 
32 #include "YUISymbols.h"
33 #include "YBarGraph.h"
34 
35 using std::string;
36 
37 
38 #define CHECK_INDEX(index) \
39  do \
40  { \
41  if ( (index) < 0 || \
42  (index) >= (int) priv->segments.size() ) \
43  { \
44  YUI_THROW( YUIIndexOutOfRangeException( \
45  (index), /* current */ \
46  0, /* min */ \
47  (int) priv->segments.size() - 1 ) ); /* max */ \
48  } \
49  } while( 0 )
50 
51 
52 
54 {
56  : updatesPending( false )
57  , postponeUpdates( false )
58  {}
59 
60  std::vector<YBarGraphSegment> segments;
61  bool updatesPending;
62  bool postponeUpdates;
63 };
64 
65 
66 
67 
69  : YWidget( parent )
70  , priv( new YBarGraphPrivate() )
71 {
72  YUI_CHECK_NEW( priv );
73  setDefaultStretchable( YD_HORIZ, true );
74 }
75 
76 
78 {
79  // NOP
80 }
81 
82 
83 void
84 YBarGraph::updateDisplay()
85 {
86  priv->updatesPending = true;
87 
88  if ( ! priv->postponeUpdates )
89  {
90  doUpdate();
91  priv->updatesPending = false;
92  }
93 }
94 
95 
96 void
98 {
99  priv->segments.push_back( segment );
100  updateDisplay();
101 }
102 
103 
104 void
106 {
107  priv->segments.clear();
108  updateDisplay();
109 }
110 
111 
112 const YBarGraphSegment &
113 YBarGraph::segment( int segmentIndex ) const
114 {
115  CHECK_INDEX( segmentIndex );
116 
117  return priv->segments[ segmentIndex ];
118 }
119 
120 
121 int
123 {
124  return (int) priv->segments.size();
125 }
126 
127 
128 void
129 YBarGraph::setValue( int segmentIndex, int newValue )
130 {
131  CHECK_INDEX( segmentIndex );
132 
133  priv->segments[ segmentIndex ].setValue( newValue );
134  updateDisplay();
135 }
136 
137 
138 void
139 YBarGraph::setLabel( int segmentIndex, const string & newLabel )
140 {
141  CHECK_INDEX( segmentIndex );
142 
143  priv->segments[ segmentIndex ].setLabel( newLabel );
144  updateDisplay();
145 }
146 
147 
148 void
149 YBarGraph::setSegmentColor( int segmentIndex, const YColor & color )
150 {
151  CHECK_INDEX( segmentIndex );
152 
153  if ( color.isUndefined() )
154  YUI_THROW( YUIException( "Invalid YColor" ) );
155 
156  priv->segments[ segmentIndex ].setSegmentColor( color );
157  updateDisplay();
158 }
159 
160 
161 void
162 YBarGraph::setTextColor( int segmentIndex, const YColor & color )
163 {
164  CHECK_INDEX( segmentIndex );
165 
166  if ( color.isUndefined() )
167  YUI_THROW( YUIException( "Invalid YColor" ) );
168 
169  priv->segments[ segmentIndex ].setTextColor( color );
170  updateDisplay();
171 }
172 
173 
174 const YPropertySet &
176 {
177  static YPropertySet propSet;
178 
179  if ( propSet.isEmpty() )
180  {
181  /*
182  * @property list<integer> Values The numerical value for each segment.
183  * @property list<string> Labels Text label for each segment ('\n' allowed).
184  * Use %1 as a placeholder for the current value.
185  */
186  propSet.add( YProperty( YUIProperty_Values, YOtherProperty ) );
187  propSet.add( YProperty( YUIProperty_Labels, YOtherProperty ) );
188  propSet.add( YWidget::propertySet() );
189  }
190 
191  return propSet;
192 }
193 
194 
195 bool
196 YBarGraph::setProperty( const string & propertyName, const YPropertyValue & val )
197 {
198  propertySet().check( propertyName, val.type() ); // throws exceptions if not found or type mismatch
199 
200  if ( propertyName == YUIProperty_Values ) return false; // Needs special handling
201  else if ( propertyName == YUIProperty_Labels ) return false; // Needs special handling
202  else
203  {
204  YWidget::setProperty( propertyName, val );
205  }
206 
207  return true; // success -- no special handling necessary
208 }
209 
210 
212 YBarGraph::getProperty( const string & propertyName )
213 {
214  propertySet().check( propertyName ); // throws exceptions if not found
215 
216  if ( propertyName == YUIProperty_Values ) return YPropertyValue( YOtherProperty );
217  else if ( propertyName == YUIProperty_Labels ) return YPropertyValue( YOtherProperty );
218  else
219  {
220  return YWidget::getProperty( propertyName );
221  }
222 }
223 
224 
225 
226 
228  : _barGraph ( barGraph )
229 {
230  YUI_CHECK_PTR( barGraph );
231 
232  _barGraph->priv->postponeUpdates = true;
233 }
234 
235 
237 {
238  _barGraph->priv->postponeUpdates = false;
239 
240  if ( _barGraph->priv->updatesPending )
241  _barGraph->updateDisplay();
242 }
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
YBarGraph::addSegment
void addSegment(const YBarGraphSegment &segment)
Add one segment.
Definition: YBarGraph.cc:97
YBarGraph::setProperty
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
Definition: YBarGraph.cc:196
YBarGraph::setLabel
void setLabel(int segmentIndex, const std::string &newLabel)
Set the label of the segment with the specified index (from 0 on).
Definition: YBarGraph.cc:139
YBarGraph::segment
const YBarGraphSegment & segment(int segmentIndex) const
Return the segment with the specified index (from 0 on).
Definition: YBarGraph.cc:113
YBarGraph::deleteAllSegments
void deleteAllSegments()
Delete all segments.
Definition: YBarGraph.cc:105
YBarGraph::setValue
void setValue(int segmentIndex, int newValue)
Set the value of the segment with the specifie index (from 0 on).
Definition: YBarGraph.cc:129
YBarGraph::setTextColor
void setTextColor(int segmentIndex, const YColor &color)
Set the text color of the segment with the specified index (from 0 on).
Definition: YBarGraph.cc:162
YColor
Helper class to define an RGB color.
Definition: YColor.h:34
YPropertySet
A set of properties to check names and types against.
Definition: YProperty.h:197
YBarGraph::doUpdate
virtual void doUpdate()=0
Perform a display update after any change to any of the segments.
YPropertySet::isEmpty
bool isEmpty() const
Returns 'true' if this property set does not contain anything.
Definition: YProperty.h:263
YBarGraph
A graph showing partitioning of a whole.
Definition: YBarGraph.h:40
YBarGraph::YBarGraph
YBarGraph(YWidget *parent)
Constructor.
Definition: YBarGraph.cc:68
YBarGraph::getProperty
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YBarGraph.cc:212
YPropertyValue::type
YPropertyType type() const
Returns the type of this property value.
Definition: YProperty.h:169
YWidget::getProperty
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YWidget.cc:457
YBarGraphPrivate
Definition: YBarGraph.cc:53
YProperty
Class for widget properties.
Definition: YProperty.h:51
YBarGraphMultiUpdate::YBarGraphMultiUpdate
YBarGraphMultiUpdate(YBarGraph *barGraph)
Constructor.
Definition: YBarGraph.cc:227
YBarGraph::segments
int segments()
Return the current number of segments.
Definition: YBarGraph.cc:122
YBarGraph::~YBarGraph
virtual ~YBarGraph()
Destructor.
Definition: YBarGraph.cc:77
YColor::isUndefined
bool isUndefined() const
Return 'true' if this color is undefined.
Definition: YColor.h:73
YPropertySet::check
void check(const std::string &propertyName) const
Check if a property 'propertyName' exists in this property set.
Definition: YProperty.cc:88
YPropertyValue
Transport class for the value of simple properties.
Definition: YProperty.h:104
YWidget::setDefaultStretchable
void setDefaultStretchable(YUIDimension dim, bool newStretch)
Set the stretchable state to "newStretch".
Definition: YWidget.cc:566
YBarGraphSegment
One segment of a YBarGraph.
Definition: YBarGraph.h:186
YBarGraph::propertySet
virtual const YPropertySet & propertySet()
Return this class's property set.
Definition: YBarGraph.cc:175
YWidget::setProperty
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
Definition: YWidget.cc:432
YBarGraph::setSegmentColor
void setSegmentColor(int segmentIndex, const YColor &color)
Set the background color of the segment with the specified index (from 0 on).
Definition: YBarGraph.cc:149
YUIException
Base class for UI Exceptions.
Definition: YUIException.h:297
YBarGraphMultiUpdate::~YBarGraphMultiUpdate
~YBarGraphMultiUpdate()
Destructor.
Definition: YBarGraph.cc:236
YWidget::propertySet
virtual const YPropertySet & propertySet()
Return this class's property set.
Definition: YWidget.cc:395