/Volumes/Plantain/MyDocuments/Projects/MacTrek/MacTrek/Luky/Gui/LLBar.m

00001 //
00002 //  LLBar.m
00003 //  MacTrek
00004 //
00005 //  Created by Aqua on 27/04/2006.
00006 //  Copyright 2006 __MyCompanyName__. 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 nrOfDescreteRects = 0; // assume empty
00122     
00123     // draw the background
00124     [[NSColor whiteColor] set];
00125     NSRectFill(aRect);
00126     
00127         // draw the border
00128         [[NSColor blackColor] set];
00129     [line removeAllPoints];
00130     [line appendBezierPathWithRect:aRect];
00131     [line stroke];
00132     
00133         // determine the bar color and the barRect
00134         if (min < max) {// normal situation
00135                     // bar is relative to the max
00136                 barRect.size.width = maxBarLength * value / max;
00137         nrOfDescreteRects = value;
00138                 if (warning < critical) { // near max becomes crit
00139                         if (value < min) { // empty bar
00140                                 barRect.size.width = 0; // do not create negative bar
00141                         } else {
00142                                 if (value < warning) { // small green bar
00143                                        // default
00144                                 } else {
00145                                         if (value < critical) { // warning, but not crit
00146                                                 barColor = [NSColor orangeColor];
00147                                         } else {
00148                                                 barColor = [NSColor redColor];
00149                                         }
00150                                 }
00151                         }
00152                 } else { // near min becommes crit
00153                         if (value < min) { // empty bar
00154                                 barRect.size.width = 0; // do not create negative bar
00155                         } else {
00156                                 if (value > warning) { // large green bar
00157                                        // default
00158                                 } else {
00159                                         if (value > critical) { // warning, but not crit
00160                                                 barColor = [NSColor orangeColor];
00161                                         } else {
00162                                                 barColor = [NSColor redColor];
00163                                         }
00164                                 }
00165                         }
00166                 }
00167         } else {
00168                 //don't support this situation yet
00169         }
00170     
00171         // draw the bar
00172     if (!discrete) {
00173         // fill inside
00174         [barColor set];
00175         NSRectFill(barRect); 
00176         // stroke the outline with black
00177         [[NSColor blackColor] set];
00178         NSFrameRect(barRect);       
00179     } else {
00180         // a discrete bar is filled with rectangles
00181         // we draw nrOfDescreteRects for max maxNrOfDescreteRects
00182         float discreteRectWidth = barRect.size.width; // should hold nrOfDescreteRects
00183         discreteRectWidth -= LLBAR_SEPERATION_BETWEEN_RECTS; // remove left seperator
00184         discreteRectWidth /= nrOfDescreteRects; // with for rect+right seperator
00185         discreteRectWidth -= LLBAR_SEPERATION_BETWEEN_RECTS; // width of single rect
00186         barRect.size.width = discreteRectWidth;
00187         
00188         barRect.origin.x += LLBAR_SEPERATION_BETWEEN_RECTS; // left seperator
00189         for (int i = 0; i < nrOfDescreteRects; i++) {
00190             // fill inside
00191             [barColor set];
00192             NSRectFill(barRect); 
00193             // stroke the outline with black
00194             [[NSColor blackColor] set];
00195             NSFrameRect(barRect); 
00196             // shift to next
00197             barRect.origin.x += (discreteRectWidth + LLBAR_SEPERATION_BETWEEN_RECTS);
00198         }
00199     }
00200     
00201         // reduce the bar if needed
00202         if (tempMax < max) { // assumes min < max
00203        
00204         // the end point we know
00205         NSPoint end = barRect.origin;
00206         end.x += maxBarLength;
00207         
00208         // it starts at tempmax
00209         NSPoint start = barRect.origin;
00210         start.x += maxBarLength * tempMax / max;
00211         
00212         // draw a line to strike out ay half height
00213         // draw a straight line to strike out the disabled part
00214         start.y -= barRect.size.height / 2;     // assumes flipped...
00215         end.y = start.y;
00216         
00217         // first draw the disabled bar in grey
00218         disabledBar.origin.x = start.x;
00219         disabledBar.size.width = end.x - start.x;
00220         [[NSColor grayColor] set];
00221         NSRectFill(disabledBar);
00222         
00223         // draw the line
00224         // $$ for some reason we are not seeing this line...
00225                 [[NSColor blackColor] set];
00226                 [line removeAllPoints];
00227         [line moveToPoint:start];
00228                 [line lineToPoint:end];
00229                 [line stroke];
00230         }
00231         
00232 }
00233 
00234 @end

Generated on Fri Jul 28 19:15:22 2006 for MacTrek by  doxygen 1.4.7