Luky/Gui/LLBar.m

00001 //
00002 //  LLBar.m
00003 //  MacTrek
00004 //
00005 //  Created by Aqua on 27/04/2006.
00006 //  Copyright 2006 Luky Soft. All rights reserved.
00007 //
00008 
00009 #import "LLBar.h"
00010 
00011 
00012 @implementation LLBar
00013 
00014 static NSBezierPath *line = nil; // this makes us fast, but not rentrant
00015 
00016 - (id) init {
00017     self = [super init];
00018     if (self != nil) {
00019         min = 0.0;
00020         max = 1.0;
00021         value = max;
00022         tempMax = max;
00023         discrete = NO;
00024         line = [[NSBezierPath alloc] init];    
00025         name = [[NSString stringWithString:@"not set"] retain];
00026     }
00027     return self;
00028 }
00029 
00030 - (NSString *)name {
00031     return name;
00032 }
00033 
00034 - (float) min {
00035         return min;
00036 }
00037 
00038 - (float) max {
00039         return max;
00040 }
00041 
00042 - (float) value {
00043         return value;
00044 }
00045 
00046 - (float) tempMax {
00047         return tempMax;
00048 }
00049 
00050 - (float) warning {
00051         return warning;
00052 }
00053 
00054 - (bool) discrete {
00055         return discrete;
00056 }
00057 
00058 - (float) critical {
00059         return critical;
00060 }
00061 
00062 - (void) setName:(NSString *) newValue {
00063     [name release];
00064     name = newValue;
00065     [name retain];
00066 }
00067 
00068 - (void) setDiscrete:(bool)newValue {
00069         discrete = newValue;
00070 }
00071 
00072 - (void) setMin:(float)newValue {
00073         min = newValue;
00074 }
00075 
00076 - (void) setMax:(float)newValue {
00077         max = newValue;
00078     tempMax = newValue; // assume temp max increases as well
00079 }
00080 
00081 - (void) setValue:(float)newValue {
00082         if ((value > max) || (value > tempMax)) {
00083                 //NSLog(@"LLBar.setValue too large");
00084                 //return;
00085         newValue = max;
00086         }
00087         value = newValue;
00088 }
00089 
00090 - (void) setTempMax:(float)newValue { // explicitly reduce the max, temporarly
00091         if (newValue > max) {
00092                 NSLog(@"LLBar.setTempMax tempmax <= max");
00093                 return;
00094         }
00095         tempMax = newValue;
00096 }
00097 
00098 - (void) setWarning:(float)newValue {
00099         if (newValue > max) {
00100                 NSLog(@"LLBar.setWarning <= max");
00101                 return;
00102         }
00103         warning = newValue;
00104 }
00105 
00106 - (void) setCritical:(float)newValue {
00107         if (newValue > max) {
00108                 NSLog(@"LLBar.setCritical <= max");
00109                 return;
00110         }
00111         critical = newValue;
00112 }
00113 
00114 - (void) drawRect:(NSRect)aRect {
00115     
00116         NSColor *barColor = [NSColor greenColor]; // assume ok
00117         NSRect  barRect = NSInsetRect(aRect, 2, 2); // assume 100% full
00118     NSRect  disabledBar = barRect;
00119     float maxBarLength = barRect.size.width;    
00120     int maxNrOfDescreteRects = max;
00121     int tempMaxNrOfDescreteRects = tempMax;
00122     int nrOfDescreteRects = 0; // assume empty
00123     
00124     // draw the background
00125     [[NSColor whiteColor] set];
00126     NSRectFill(aRect);
00127     
00128         // draw the border
00129         [[NSColor blackColor] set];
00130     [line removeAllPoints];
00131     [line appendBezierPathWithRect:aRect];
00132     [line stroke];
00133     
00134         // determine the bar color and the barRect
00135         if (min < max) {// normal situation
00136                     // bar is relative to the max
00137                 barRect.size.width = maxBarLength * value / max;
00138         nrOfDescreteRects = value;
00139                 if (warning < critical) { // near max becomes crit
00140                         if (value < min) { // empty bar
00141                                 barRect.size.width = 0; // do not create negative bar
00142                         } else {
00143                                 if (value < warning) { // small green bar
00144                                        // default
00145                                 } else {
00146                                         if (value < critical) { // warning, but not crit
00147                                                 barColor = [NSColor orangeColor];
00148                                         } else {
00149                                                 barColor = [NSColor redColor];
00150                                         }
00151                                 }
00152                         }
00153                 } else { // near min becommes crit
00154                         if (value < min) { // empty bar
00155                                 barRect.size.width = 0; // do not create negative bar
00156                         } else {
00157                                 if (value > warning) { // large green bar
00158                                        // default
00159                                 } else {
00160                                         if (value > critical) { // warning, but not crit
00161                                                 barColor = [NSColor orangeColor];
00162                                         } else {
00163                                                 barColor = [NSColor redColor];
00164                                         }
00165                                 }
00166                         }
00167                 }
00168         } else {
00169                 //don't support this situation yet
00170         }
00171     
00172         // draw the bar
00173     if (!discrete) {
00174         
00175         // fill inside
00176         [barColor set];
00177         NSRectFill(barRect); 
00178         
00179         // stroke the outline with black
00180         [[NSColor blackColor] set];
00181         NSFrameRect(barRect); 
00182         
00183         // reduce the bar if needed
00184         if (tempMax < max) { // assumes min < max
00185             
00186             // the end point we know
00187             NSPoint end = barRect.origin;
00188             end.x += maxBarLength;
00189             
00190             // it starts at tempmax
00191             NSPoint start = barRect.origin;
00192             start.x += maxBarLength * tempMax / max;
00193             
00194             // draw a line to strike out ay half height
00195             // draw a straight line to strike out the disabled part
00196             start.y -= barRect.size.height / 2; // assumes flipped...
00197             end.y = start.y;
00198             
00199             // first draw the disabled bar in grey
00200             disabledBar.origin.x = start.x;
00201             disabledBar.size.width = end.x - start.x;
00202             [[NSColor grayColor] set];
00203             NSRectFill(disabledBar);
00204             
00205             // draw the line
00206             // $$ for some reason we are not seeing this line...
00207             [[NSColor blackColor] set];
00208             [line removeAllPoints];
00209             [line moveToPoint:start];
00210             [line lineToPoint:end];
00211             [line stroke];
00212         }
00213     } else {
00214         
00215         // a discrete bar is filled with rectangles
00216         // we first draw nrOfDescreteRects for max maxNrOfDescreteRects
00217         //NSLog(@"LLBar.drawRect max %f tempMax %f value %f", max, tempMax, value);
00218         //NSLog(@"LLBar.drawRect maxNrOfDescreteRects %d tempMaxNrOfDescreteRects %d nrOfDescreteRects %d ", maxNrOfDescreteRects, tempMaxNrOfDescreteRects, nrOfDescreteRects);
00219 
00220         float discreteRectWidth = maxBarLength; // should hold nrOfDescreteRects
00221         //NSLog(@"LLBar.drawRect 1 width %f",discreteRectWidth);
00222         discreteRectWidth -= LLBAR_SEPERATION_BETWEEN_RECTS; // remove left seperator
00223         //NSLog(@"LLBar.drawRect 2 width %f",discreteRectWidth);
00224         discreteRectWidth /= maxNrOfDescreteRects; // with for rect+right seperator
00225         //NSLog(@"LLBar.drawRect 3 width %f",discreteRectWidth);
00226         discreteRectWidth -= LLBAR_SEPERATION_BETWEEN_RECTS; // width of single rect
00227         //NSLog(@"LLBar.drawRect 4 width %f",discreteRectWidth);
00228         barRect.size.width = discreteRectWidth;
00229                
00230         barRect.origin.x += LLBAR_SEPERATION_BETWEEN_RECTS; // left seperator
00231         for (int i = 0; i < nrOfDescreteRects; i++) {
00232             // fill inside
00233             [barColor set];
00234             NSRectFill(barRect); 
00235             // stroke the outline with black
00236             [[NSColor blackColor] set];
00237             NSFrameRect(barRect); 
00238             // shift to next
00239             barRect.origin.x += (discreteRectWidth + LLBAR_SEPERATION_BETWEEN_RECTS);
00240         }
00241         
00242         [[NSColor blackColor] set];
00243         // draw the remainder as empty squares
00244         for (int i = nrOfDescreteRects; i < tempMaxNrOfDescreteRects; i++) {
00245             // stroke the outline with black            
00246             NSFrameRect(barRect); 
00247             // shift to next
00248             barRect.origin.x += (discreteRectWidth + LLBAR_SEPERATION_BETWEEN_RECTS);
00249         }
00250         
00251         // and the disabled ones
00252         barColor = [[NSColor blackColor] colorWithAlphaComponent:0.5];
00253         for (int i = tempMaxNrOfDescreteRects; i < maxNrOfDescreteRects; i++) {
00254             // fill inside 50% black
00255             [barColor set];
00256             NSRectFill(barRect); 
00257             // stroke the outline with black
00258             [[NSColor blackColor] set];
00259             NSFrameRect(barRect); 
00260             // shift to next
00261             barRect.origin.x += (discreteRectWidth + LLBAR_SEPERATION_BETWEEN_RECTS);
00262         }
00263     }
00264     
00265 
00266         
00267 }
00268 
00269 @end

Generated on Sat Aug 26 21:14:16 2006 for MacTrek by  doxygen 1.4.7