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