 |
 |
Overlapping Rects in View: need logic help
|
 |
|
 |
|
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status:
Offline
|
|
Ok, I am working on a small program that has similar features to iCal in that it draws rectangles that represent appointments. The height of the rectangles is proportional to the length of the appointment. I want to make it so that when two appointments overlap, their width gets smaller and one of the overlapping appointments gets shifted to the right, like if you have overlapping events in iCal.
My appointment class is a model class.
I draw my rectangles in a custom NSView. In my drawRect method I cycle through an array that contains appointment objects that should be displayed, for each appointment, I call its accessor methods to retreive the start time and end time and then calculate the size of the rect accordingly. This all works fine. As of now, I can check to see if times are overlapping and then adjust the width for the overlapping appointments, but I don't know how to incrementally shift only *one* appointment over. Can someone give me some help about the best way of doing this.
Here is some of my code:
//this is in the drawRect method
for(i=0; i<[appointmentArray count]; i++) {
NSRect aRect = [self rectForAppointment:[appointmentArray objectAtIndex:i] withNumber:i andRect:rect];
//this is the rect that gets draw.
//rect = the size of the NSView.
The rectForAppointmen:(Appointment *) withNumber:(int) andRect:(NSRect)
method creates and returns and NSRect with the correct size.
My feeling is that I should adjust the rectForAppointment... method to accomodate for overlapping appointments, but I don't know how to shift only one overlapping rect and not its sister overlapping rect. I don't know where to go next...
TIA
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
How is it difficult? Just pick one of the rects and translate it over. I don't see where the problem is....
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status:
Offline
|
|
I know it is not difficult, but I can't figure out the right way to do it.
Here is some more code if it will help:-(NSRect) rectForAppointment:(Appointment *)event withNumber:(int)num andRect:(NSRect)rect{
NSRect rect;
float width=0;
float xOrigin=0;
int widthMod = [self numberOfOverlapsAppointment:event]+1;
if(widthMod>1) {
width = (([self bounds].size.width-27.0)/widthMod)*0.93;
}
else
width = ([self bounds].size.width-27.0)*0.93;
//xOrigin is offset by 28 so that it is not flush on edge
if(widthMod>1)
xOrigin = 28 + (widthMod*width*0.85);
else
xOrigin = 28;
//some other stuff happens here, but it is not important
/*here is the numberOfOverlapsAppointment
-(int) numberOfOverlapsAppointment:(Appointment *)event {
int num = 0;
int i = 0;
for (i=0; i<[appointmentArray count]; i++) {
Appointment *inst = [appointmentArray objectAtIndex:i];
if(![event isEqual:inst] && ![[event overLap] isEqual:inst]) {
if([[event date] timeIntervalSinceDate:[inst endDate]]<0 && [[event endDate] timeIntervalSinceDate:[inst date]]>0) {
num++;
[event setOverLap:inst];
}
else if([[event endDate] timeIntervalSinceDate:[inst date]]>0 && [[event date] timeIntervalSinceDate:[inst date]]<0) {
[event setOverLap:inst];
num++;
}
}
}
return num;
}
//The problem is that if one event overlaps with another, they both get drawn shifted over, not just one. I don't know if having the accessor methods setOverLap and overLap are necessary.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status:
Offline
|
|
The problem as I see it is that if you have three overlapping appointments, the 3rd has to be shifted over by the sum of the second's shift and the third's shift.
What I would do is use an accumulator variable that stores the total number of pixels shifted thus far for a column. Then, get each rect as normal. Figure out if a rect needs to shift, and if so, by how much. Add this to your accumulator, then add the accumulator to the rect's origin.x.
|
Geekspiff - generating spiffdiddlee software since before you began paying attention.
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status:
Offline
|
|
Thanks smeger. It is very close to working now. That is all I needed was someone to tell me to use a variable to keep track of how far things are shifted over.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status:
Offline
|
|
OK, I hate to do it, but I am asking for more help.
Here is my problem lets say there are 5 appointments total: 9-12, 10-12 and 11-12. All three of these overlap and it draws those fine. But lets say that the other two apppointments are at 3-5 and 4-5. These two get drawn shifted too far over because the previous three events increased my "shifter" variable.
So I need to reset my shifter variable for each group of events. I don't know what the most efficient way to do this would be or where to do it. Any more great ideas?
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status:
Offline
|
|
|
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|