00001
00002
00003
00004
00005
00006
00007
00008
00009 #import "LLBar.h"
00010
00011
00012 @implementation LLBar
00013
00014 static NSBezierPath *line = nil;
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;
00079 }
00080
00081 - (void) setValue:(float)newValue {
00082 if ((value > max) || (value > tempMax)) {
00083
00084
00085 newValue = max;
00086 }
00087 value = newValue;
00088 }
00089
00090 - (void) setTempMax:(float)newValue {
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];
00117 NSRect barRect = NSInsetRect(aRect, 2, 2);
00118 NSRect disabledBar = barRect;
00119 float maxBarLength = barRect.size.width;
00120 int maxNrOfDescreteRects = max;
00121 int tempMaxNrOfDescreteRects = tempMax;
00122 int nrOfDescreteRects = 0;
00123
00124
00125 [[NSColor whiteColor] set];
00126 NSRectFill(aRect);
00127
00128
00129 [[NSColor blackColor] set];
00130 [line removeAllPoints];
00131 [line appendBezierPathWithRect:aRect];
00132 [line stroke];
00133
00134
00135 if (min < max) {
00136
00137 barRect.size.width = maxBarLength * value / max;
00138 nrOfDescreteRects = value;
00139 if (warning < critical) {
00140 if (value < min) {
00141 barRect.size.width = 0;
00142 } else {
00143 if (value < warning) {
00144
00145 } else {
00146 if (value < critical) {
00147 barColor = [NSColor orangeColor];
00148 } else {
00149 barColor = [NSColor redColor];
00150 }
00151 }
00152 }
00153 } else {
00154 if (value < min) {
00155 barRect.size.width = 0;
00156 } else {
00157 if (value > warning) {
00158
00159 } else {
00160 if (value > critical) {
00161 barColor = [NSColor orangeColor];
00162 } else {
00163 barColor = [NSColor redColor];
00164 }
00165 }
00166 }
00167 }
00168 } else {
00169
00170 }
00171
00172
00173 if (!discrete) {
00174
00175
00176 [barColor set];
00177 NSRectFill(barRect);
00178
00179
00180 [[NSColor blackColor] set];
00181 NSFrameRect(barRect);
00182
00183
00184 if (tempMax < max) {
00185
00186
00187 NSPoint end = barRect.origin;
00188 end.x += maxBarLength;
00189
00190
00191 NSPoint start = barRect.origin;
00192 start.x += maxBarLength * tempMax / max;
00193
00194
00195
00196 start.y -= barRect.size.height / 2;
00197 end.y = start.y;
00198
00199
00200 disabledBar.origin.x = start.x;
00201 disabledBar.size.width = end.x - start.x;
00202 [[NSColor grayColor] set];
00203 NSRectFill(disabledBar);
00204
00205
00206
00207 [[NSColor blackColor] set];
00208 [line removeAllPoints];
00209 [line moveToPoint:start];
00210 [line lineToPoint:end];
00211 [line stroke];
00212 }
00213 } else {
00214
00215
00216
00217
00218
00219
00220 float discreteRectWidth = maxBarLength;
00221
00222 discreteRectWidth -= LLBAR_SEPERATION_BETWEEN_RECTS;
00223
00224 discreteRectWidth /= maxNrOfDescreteRects;
00225
00226 discreteRectWidth -= LLBAR_SEPERATION_BETWEEN_RECTS;
00227
00228 barRect.size.width = discreteRectWidth;
00229
00230 barRect.origin.x += LLBAR_SEPERATION_BETWEEN_RECTS;
00231 for (int i = 0; i < nrOfDescreteRects; i++) {
00232
00233 [barColor set];
00234 NSRectFill(barRect);
00235
00236 [[NSColor blackColor] set];
00237 NSFrameRect(barRect);
00238
00239 barRect.origin.x += (discreteRectWidth + LLBAR_SEPERATION_BETWEEN_RECTS);
00240 }
00241
00242 [[NSColor blackColor] set];
00243
00244 for (int i = nrOfDescreteRects; i < tempMaxNrOfDescreteRects; i++) {
00245
00246 NSFrameRect(barRect);
00247
00248 barRect.origin.x += (discreteRectWidth + LLBAR_SEPERATION_BETWEEN_RECTS);
00249 }
00250
00251
00252 barColor = [[NSColor blackColor] colorWithAlphaComponent:0.5];
00253 for (int i = tempMaxNrOfDescreteRects; i < maxNrOfDescreteRects; i++) {
00254
00255 [barColor set];
00256 NSRectFill(barRect);
00257
00258 [[NSColor blackColor] set];
00259 NSFrameRect(barRect);
00260
00261 barRect.origin.x += (discreteRectWidth + LLBAR_SEPERATION_BETWEEN_RECTS);
00262 }
00263 }
00264
00265
00266
00267 }
00268
00269 @end