File: DiagramView.m

package info (click to toggle)
gnustep-dl2 0.12.0-16
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 8,076 kB
  • sloc: objc: 61,761; makefile: 33
file content (420 lines) | stat: -rw-r--r-- 14,681 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/**
    DiagramView.m

    Author: Matt Rice <ratmice@gmail.com>
    Date: Oct 2006

    This file is part of DBModeler.

    <license>
    DBModeler is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    DBModeler is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DBModeler; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    </license>
**/


#include "DiagramView.h"
#include "EntityView.h"
#include "AttributeCell.h"

#ifdef NeXT_Foundation_LIBRARY
#include <Foundation/Foundation.h>
#else
#include <Foundation/NSSet.h>
#endif

#ifdef NeXT_GUI_LIBRARY
#include <AppKit/AppKit.h>
#else
#include <AppKit/NSColor.h>
#include <AppKit/NSScrollView.h>
#include <AppKit/NSEvent.h>
#include <AppKit/NSGraphics.h>
#include <AppKit/NSBezierPath.h>
#include <AppKit/NSPasteboard.h>
#include <AppKit/NSDragging.h>
#endif

#include <EOAccess/EOModel.h>
#include <EOAccess/EOAttribute.h>
#include <EOAccess/EORelationship.h>
#include <EOModeler/EOModelExtensions.h>

#include <GNUstepBase/GNUstep.h>

@implementation DiagramView
- (id) initWithFrame:(NSRect)frameRect
{
  self = [super initWithFrame:frameRect];
  _shownEntities = [NSMutableDictionary new];
  _relationships = [NSMutableArray new];
  _subview_order = [NSMutableArray new];
  _bgColor = RETAIN([NSColor colorWithCalibratedRed:0.881437 green:0.941223 blue:1.0 alpha:1.0]);
  [self registerForDraggedTypes:[NSArray arrayWithObject:NSColorPboardType]];
  return self;
}

- (void) dealloc
{
  RELEASE(_shownEntities);
  RELEASE(_relationships);
  RELEASE(_subview_order);
  RELEASE(_bgColor);
  [super dealloc];
}

- (BOOL) prepareForDragOperation:(id <NSDraggingInfo>)sender
{
  return YES;
}
- (NSDragOperation) draggingEntered:(id <NSDraggingInfo>)sender
{
  return [sender draggingSourceOperationMask];
}

- (BOOL) performDragOperation:(id <NSDraggingInfo>)sender
{
  NSPasteboard *pb = [sender draggingPasteboard];
  NSColor *color = [NSColor colorFromPasteboard:pb];

  ASSIGN(_bgColor, color);
  [self setNeedsDisplay:YES];
  return YES;
}

- (BOOL) isFlipped
{
  return YES;
}

- (void) setModel:(EOModel *)model
{
  ASSIGN(_model, model);
}

- (void) showEntity:(NSString *)name
{
  EntityView *ev;

  if (!(ev = [_shownEntities objectForKey:name]))
    {
      EOEntity *entity = [_model entityNamed:name];
      NSArray *attribs = [entity attributes];
      NSRect vis = [[self enclosingScrollView] documentVisibleRect];
      NSRect evFrame;
      NSPoint toPoint;
      int i, c = [attribs count];
   
      ev = [[EntityView alloc] initWithFrame:NSMakeRect(0,0,1,1)];
      [ev setTitle:[entity name]];
      [ev setNumberOfAttributes:c];

      for (i = 0; i < c; i++)
        {
          EOAttribute *attrib = [attribs objectAtIndex:i];
          AttributeCell *cell = [ev cellAtRow:i];

          [cell setStringValue:[attrib name]];
          [cell setLock:[[entity attributesUsedForLocking] containsObject:attrib]];
          [cell setProp:[[entity classProperties] containsObject:attrib]];
          [cell setKey:[[entity primaryKeyAttributes] containsObject:attrib]];
        }
 
      [ev sizeToFit];
      evFrame = [ev frame];
  
      /* this "layout mechanism" is pure evil... */
      toPoint.x = vis.origin.x  + ((vis.size.width - evFrame.size.width) * rand()/(RAND_MAX + vis.origin.x));
      toPoint.y = vis.origin.y + ((vis.size.height - evFrame.size.height) * rand()/(RAND_MAX + vis.origin.y));
      
      [ev setFrameOrigin:toPoint];
      [_shownEntities setObject:ev forKey:[entity name]];
  
      [self addSubview:ev];
      [_subview_order addObject:ev];
      [self setupRelationships];
    }
  else
    {
      EOEntity *entity = [_model entityNamed:name];
      NSArray *attribs = [entity attributes];
      int i, c = [attribs count];

      [ev setTitle:[entity name]];
      [ev setNumberOfAttributes:c];
      
      for (i = 0; i < c; i++)
        {
          EOAttribute *attrib = [attribs objectAtIndex:i];
          AttributeCell *cell = [ev cellAtRow:i];

          [cell setStringValue:[attrib name]];
          [cell setLock:[[entity attributesUsedForLocking] containsObject:attrib]];
          [cell setProp:[[entity classProperties] containsObject:attrib]];
          [cell setKey:[[entity primaryKeyAttributes] containsObject:attrib]];
        }
      [ev sizeToFit];
      [ev setNeedsDisplay:YES];
     
    }
}

int sortSubviews(id view1, id view2, void *context)
{
  DiagramView *self = context;
  unsigned idx1, idx2;

  idx1 = [self->_subview_order indexOfObject:view1];
  idx2 = [self->_subview_order indexOfObject:view2];
  
  return (idx1 < idx2) ? NSOrderedDescending : NSOrderedAscending;
}

- (void) orderViewFront:(NSView *)v
{
  int idx = [_subview_order indexOfObject:v];
  RETAIN(v);
  [_subview_order removeObjectAtIndex:idx];
  [_subview_order insertObject:v atIndex:0];
  RELEASE(v);
  [self sortSubviewsUsingFunction:(int (*)(id, id, void *))sortSubviews context:self];
  [self setNeedsDisplay:YES];
}

- (void) setupRelationships
{
  int i,c;
  NSArray *stuff = [_shownEntities allKeys];
  [_relationships removeAllObjects];
  for (i = 0, c = [stuff count]; i < c; i++)
     {
       int j, d;
       NSString *entName = [stuff objectAtIndex:i];
       EOEntity *ent = [_model entityNamed:entName];
       NSArray *rels = [ent relationships];
       
       for (j = 0, d = [rels count]; j < d; j++)
         {
           EORelationship *rel = [rels objectAtIndex:j];
           EOEntity *dest = [rel destinationEntity];
           id srcName = [ent name];
           id destName = [dest name]; 
           EntityView *from = [_shownEntities objectForKey:srcName];
           EntityView *to = [_shownEntities objectForKey:destName];
           NSArray *srcAttribs = [rel sourceAttributes]; 
           NSArray *destAttribs = [rel destinationAttributes]; 
           int k, e;
           for (k = 0, e = [srcAttribs count]; k < e; k++)
             {
               id sAttrib = [srcAttribs objectAtIndex:k];
               id dAttrib = [destAttribs objectAtIndex:k];
               int sIdx = [[ent attributes] indexOfObject:sAttrib];
               int dIdx = [[dest attributes] indexOfObject:dAttrib];
               NSRect fromRect = [from attributeRectAtRow:sIdx];
               NSRect toRect = [to attributeRectAtRow:dIdx];
               NSRect fromViewFrame = [from frame];
               NSRect toViewFrame = [to frame];
               NSPoint midPoint;
               NSPoint tmp; 
               float arrowOffset; 
                NSBezierPath *path = [NSBezierPath bezierPath];
               BOOL fromRight;
               BOOL toRight;

               [path setLineWidth:2];

               fromRect.origin.y += fromViewFrame.origin.y; 
               toRect.origin.y += toViewFrame.origin.y; 
              
               /* which side of the EntityView the arrow line will be connecting
                * to, for the source and destination entities */
               fromRight = (fromViewFrame.origin.x - 40 < toViewFrame.origin.x + toViewFrame.size.width);
               toRight = (toViewFrame.origin.x - 40 < fromViewFrame.origin.x + fromViewFrame.size.width);
               
               if (fromRight)
                 {
                   fromRect.origin.x = fromViewFrame.origin.x + fromViewFrame.size.width + 5;
                 }
               else
                 {
                   fromRect.origin.x = fromViewFrame.origin.x - 5;
                 }
               
               if (toRight)
                 {
                   toRect.origin.x = toViewFrame.origin.x + toViewFrame.size.width;
                   toRect.origin.x += 5;
                   /* <- */
                   arrowOffset = -5.0;
                 }
               else
                 {
                   toRect.origin.x = toViewFrame.origin.x;
                   toRect.origin.x -= 5;
                   /* -> */
                   arrowOffset = 5.0;
                 }

               fromRect.origin.y = NSMidY(fromRect);
               toRect.origin.y = NSMidY(toRect);
               
               /* every line segment is drawn forwards and backwards so we dont
                * end up with lightning bolts when filling the arrow */

               /* a recursive relationship... 
                * Don't think they are particularly useful but... */
               if (fromRect.origin.y == toRect.origin.y
                   && fromRect.origin.x == toRect.origin.x)
                 {  
                   [path moveToPoint:NSMakePoint(toRect.origin.x + 15, toRect.origin.y)];
                   [path lineToPoint:NSMakePoint(toRect.origin.x + 15, toRect.origin.y + 5)];
                   [path lineToPoint:NSMakePoint(toRect.origin.x + 15, toRect.origin.y)];

                   [path moveToPoint:NSMakePoint(toRect.origin.x + 15, toRect.origin.y + 5)];
                   [path lineToPoint:NSMakePoint(toRect.origin.x + 20, toRect.origin.y + 5)];
                   [path lineToPoint:NSMakePoint(toRect.origin.x + 15, toRect.origin.y + 5)];
                   
                   [path moveToPoint:NSMakePoint(toRect.origin.x + 20, toRect.origin.y + 5)];
                   [path lineToPoint:NSMakePoint(toRect.origin.x + 20, toRect.origin.y)];
                   [path lineToPoint:NSMakePoint(toRect.origin.x + 20, toRect.origin.y + 5)];
                 }
               
               if ((fromRight || toRight) && !(fromRight && toRight))
                 {
                   /* a line like...   +-----
                    *                  |   
                    *             -----+ 
                    *  (from the right side, to the left side or vice versa)
                    */           
                   [path moveToPoint:fromRect.origin];
               
                   midPoint.x = (fromRect.origin.x + toRect.origin.x) / 2;
                   midPoint.y = fromRect.origin.y;
                   [path lineToPoint:midPoint];
               
                   [path lineToPoint:fromRect.origin];
                    [path moveToPoint:midPoint]; 
                   tmp = midPoint;
                   midPoint.x = (fromRect.origin.x + toRect.origin.x) / 2;
                   midPoint.y = toRect.origin.y;
                   [path lineToPoint:midPoint];
                   [path lineToPoint:tmp];

                   [path moveToPoint:midPoint];
                   [path lineToPoint:toRect.origin];
                   [path lineToPoint:midPoint];
                 }
               else if (fromRight && toRight)
                 {
                   /*  need to       --+   or -------+ <- joint end or start.
                     * make a line      |             |
                    * like...   -------+           --+ <- joint start or end.
                    * from the right side to the right side.
                    */
                         
                   NSPoint jointStart;
                   NSPoint jointEnd;
                   
                   if (toRect.origin.x + toRect.size.width < fromRect.origin.x + fromRect.size.width)
                     {
                       jointStart = NSMakePoint(fromRect.origin.x + 20, fromRect.origin.y);
                       jointEnd = NSMakePoint(fromRect.origin.x + 20, toRect.origin.y);
                     }
                   else
                     {
                       jointStart = NSMakePoint(toRect.origin.x + 20, fromRect.origin.y);
                       jointEnd = NSMakePoint(toRect.origin.x + 20, toRect.origin.y);
                     } 
                   [path moveToPoint:fromRect.origin];
                   [path lineToPoint:jointStart];
                   [path lineToPoint:fromRect.origin];
                   
                   [path moveToPoint:jointStart];
                   [path lineToPoint:jointEnd];
                   [path lineToPoint:jointStart];
                   
                   [path moveToPoint:jointEnd]; 
                   [path lineToPoint:toRect.origin];
                   [path lineToPoint:jointEnd];
                 }

               /* draw arrows.. */ 
               if ([rel isToMany]) 
                 {
                   [path moveToPoint:toRect.origin]; 
                   [path lineToPoint:NSMakePoint(toRect.origin.x - arrowOffset, toRect.origin.y + arrowOffset)];
                   [path lineToPoint:NSMakePoint(toRect.origin.x - arrowOffset, toRect.origin.y - arrowOffset)]; 
                   [path lineToPoint:toRect.origin]; 
                   toRect.origin.x -= arrowOffset;
                   [path moveToPoint:toRect.origin]; 
                   [path lineToPoint:NSMakePoint(toRect.origin.x - arrowOffset, toRect.origin.y + arrowOffset)];
                   [path lineToPoint:NSMakePoint(toRect.origin.x - arrowOffset, toRect.origin.y - arrowOffset)]; 
                   [path lineToPoint:toRect.origin]; 
                 }
               else
                 {
                   [path moveToPoint:toRect.origin]; 
                   [path lineToPoint:NSMakePoint(toRect.origin.x - arrowOffset, toRect.origin.y + arrowOffset)];
                   [path lineToPoint:NSMakePoint(toRect.origin.x - arrowOffset, toRect.origin.y - arrowOffset)]; 
                   [path lineToPoint:toRect.origin]; 
                 }

               [path closePath];
               [_relationships addObject:path];
             }

         }
     }
}

- (void) mouseDown:(NSEvent *)ev
{
  /* create new relationshp??*/
}

- (BOOL) autoscroll:(NSEvent *)ev
{
  NSPoint pt = [self convertPoint:[ev locationInWindow] fromView:nil];
  NSRect r = _frame;
  BOOL flag;

  if (!NSPointInRect(pt, r))
    {
      if (pt.x > r.size.width)
        r.size.width = pt.x; 
      if (pt.y > r.size.height)
        r.size.height = pt.y;
      [self setFrameSize:r.size];
    }
  flag = [super autoscroll:ev];
  return flag;
}

- (void) drawRect:(NSRect)aRect
{
  int i, c;
  [_bgColor set];
  NSRectFill([self frame]);
  [self setupRelationships];
  
  for (i = 0, c = [_relationships count]; i < c; i++)
    {
      [[NSColor blackColor] set];
      NSBezierPath *path = [_relationships objectAtIndex:i];
      [path stroke];
      [path fill];
    }
}

@end