1/*
2 Project: Graphos
3 GRDrawableObject.m
4
5 Copyright (C) 2008-2018 GNUstep Application Project
6
7 Author: Ing. Riccardo Mottola
8
9 Created: 2008-02-25
10
11 This application is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public
13 License as published by the Free Software Foundation; either
14 version 2 of the License, or (at your option) any later version.
15
16 This application is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 Library General Public License for more details.
20
21 You should have received a copy of the GNU General Public
22 License along with this library; if not, write to the Free
23 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
24 */
25
26#import <AppKit/NSGraphics.h>
27#import "GRDrawableObject.h"
28#import "GRObjectEditor.h"
29
30
31@implementation GRDrawableObject
32
33- (void)dealloc
34{
35    [editor release];
36    [strokeColor release];
37    [fillColor release];
38    [super dealloc];
39}
40
41- (GRObjectEditor *)allocEditor
42{
43  return [[GRObjectEditor alloc] initEditor:self];
44}
45
46/** initializes by using the properties array as defaults */
47- (id)initInView:(GRDocView *)aView
48      zoomFactor:(CGFloat)zf
49      withProperties:(NSDictionary *)properties
50{
51  self = [super init];
52  if(self)
53    {
54      NSColor *newColor;
55      id val;
56
57      docView = aView;
58      zmFactor = zf;
59      stroked = YES;
60      filled = NO;
61      visible = YES;
62      locked = NO;
63      strokeColor = [[[NSColor blackColor] colorUsingColorSpaceName: NSCalibratedRGBColorSpace] retain];
64      fillColor = [[[NSColor whiteColor] colorUsingColorSpaceName: NSCalibratedRGBColorSpace] retain];
65
66      val = [properties objectForKey: @"stroked"];
67      if (val != nil)
68	[self setStroked: [val boolValue]];
69      newColor = (NSColor *)[properties objectForKey: @"strokecolor"];
70      if (newColor != nil)
71	[self setStrokeColor: newColor];
72
73      val = [properties objectForKey: @"filled"];
74      if (val != nil)
75	[self setFilled: [val boolValue]];
76      newColor = (NSColor *)[properties objectForKey: @"fillcolor"];
77      if (newColor != nil)
78	[self setFillColor: newColor];
79
80      val = [properties objectForKey: @"visible"];
81      if (val)
82	visible = [val boolValue];
83
84      val = [properties objectForKey: @"locked"];
85      if (val)
86	locked = [val boolValue];
87
88      editor = [self allocEditor];
89    }
90  return self;
91}
92
93/** initializes all parameters from a description dictionary */
94- (id)initFromData:(NSDictionary *)description
95            inView:(GRDocView *)aView
96        zoomFactor:(CGFloat)zf
97{
98  self = [super init];
99  if(self)
100    {
101      NSArray *linearr;
102      NSString *str;
103      id obj;
104      CGFloat strokeCol[4];
105      CGFloat fillCol[4];
106      CGFloat strokeAlpha;
107      CGFloat fillAlpha;
108
109      editor = [self allocEditor];
110
111      docView = aView;
112      zmFactor = zf;
113
114      obj = [description objectForKey: @"stroked"];
115      if ([obj isKindOfClass:[NSString class]])
116	obj = [NSNumber numberWithInt:[obj intValue]];
117      stroked = [obj boolValue];
118      strokeAlpha = [[description objectForKey: @"strokealpha"] floatValue];
119      str = [description objectForKey: @"strokecolor"];
120      linearr = [str componentsSeparatedByString: @" "];
121      if ([linearr count] == 3)
122	{
123	  strokeCol[0] = [[linearr objectAtIndex: 0] floatValue];
124	  strokeCol[1] = [[linearr objectAtIndex: 1] floatValue];
125	  strokeCol[2] = [[linearr objectAtIndex: 2] floatValue];
126	  strokeColor = [NSColor colorWithCalibratedRed: strokeCol[0]
127						  green: strokeCol[1]
128						   blue: strokeCol[2]
129						  alpha: strokeAlpha];
130	  [strokeColor retain];
131	}
132      else
133	{
134	  strokeCol[0] = [[linearr objectAtIndex: 0] floatValue];
135	  strokeCol[1] = [[linearr objectAtIndex: 1] floatValue];
136	  strokeCol[2] = [[linearr objectAtIndex: 2] floatValue];
137	  strokeCol[3] = [[linearr objectAtIndex: 3] floatValue];
138	  strokeColor = [NSColor colorWithDeviceCyan: strokeCol[0]
139					     magenta: strokeCol[1]
140					      yellow: strokeCol[2]
141					       black: strokeCol[3]
142					       alpha: strokeAlpha];
143	  strokeColor = [[strokeColor colorUsingColorSpaceName: NSCalibratedRGBColorSpace] retain];
144	  }
145      obj = [description objectForKey: @"filled"];
146      if ([obj isKindOfClass:[NSString class]])
147	obj = [NSNumber numberWithInt:[obj intValue]];
148      filled = [obj boolValue];
149      fillAlpha = [[description objectForKey: @"fillalpha"] floatValue];
150      str = [description objectForKey: @"fillcolor"];
151      linearr = [str componentsSeparatedByString: @" "];
152      if ([linearr count] == 3)
153	{
154	  fillCol[0] = [[linearr objectAtIndex: 0] floatValue];
155	  fillCol[1] = [[linearr objectAtIndex: 1] floatValue];
156	  fillCol[2] = [[linearr objectAtIndex: 2] floatValue];
157	  fillColor = [NSColor colorWithCalibratedRed: fillCol[0]
158						green: fillCol[1]
159						 blue: fillCol[2]
160						alpha: fillAlpha];
161	  [fillColor retain];
162	}
163      else
164	{
165	  fillCol[0] = [[linearr objectAtIndex: 0] floatValue];
166	  fillCol[1] = [[linearr objectAtIndex: 1] floatValue];
167	  fillCol[2] = [[linearr objectAtIndex: 2] floatValue];
168	  fillCol[3] = [[linearr objectAtIndex: 3] floatValue];
169	  fillColor = [NSColor colorWithDeviceCyan: fillCol[0]
170					   magenta: fillCol[1]
171					    yellow: fillCol[2]
172					     black: fillCol[3]
173					     alpha: fillAlpha];
174	  fillColor = [[fillColor colorUsingColorSpaceName: NSCalibratedRGBColorSpace] retain];
175	}
176      obj = [description objectForKey: @"visible"];
177      if ([obj isKindOfClass:[NSString class]])
178	obj = [NSNumber numberWithInt:[obj intValue]];
179      visible = [obj boolValue];
180      obj = [description objectForKey: @"locked"];
181      if ([obj isKindOfClass:[NSString class]])
182	obj = [NSNumber numberWithInt:[obj intValue]];
183      locked = [obj boolValue];
184    }
185  return self;
186}
187
188
189- (NSDictionary *)objectDescription
190{
191#ifdef GNUSTEP
192    [self subclassResponsibility: _cmd];
193#endif
194    return nil;
195}
196
197
198- (id)copyWithZone:(NSZone *)zone
199{
200  GRDrawableObject *objCopy;
201  GRObjectEditor *editorCopy;
202
203  objCopy = [[[self class] allocWithZone:zone] init];
204
205  objCopy->visible = visible;
206  objCopy->locked = locked;
207  objCopy->zmFactor = zmFactor;
208  objCopy->stroked = stroked;
209  objCopy->filled = filled;
210
211  editorCopy = [[self editor] copy];
212  [editorCopy setObject: objCopy];
213
214  objCopy->docView = [self view];
215  objCopy->editor = editorCopy;
216
217  objCopy->strokeColor = [[strokeColor colorUsingColorSpaceName: NSCalibratedRGBColorSpace] retain];
218  objCopy->fillColor = [[fillColor colorUsingColorSpaceName: NSCalibratedRGBColorSpace] retain];
219
220  return objCopy;
221}
222
223- (BOOL)objectHitForSelection:(NSPoint)p
224{
225#ifdef GNUSTEP
226    [self subclassResponsibility: _cmd];
227#endif
228    return NO;
229}
230
231- (GRDocView *)view
232{
233    return docView;
234}
235
236- (GRObjectEditor *)editor
237{
238    return editor;
239}
240
241- (BOOL)visible
242{
243    return visible;
244}
245
246- (void)setVisible:(BOOL)value
247{
248    visible = value;
249    if(!visible)
250        [editor unselect];
251}
252
253- (BOOL)locked
254{
255    return locked;
256}
257
258- (void)setLocked:(BOOL)value
259{
260    locked = value;
261}
262
263- (CGFloat)zoomFactor
264{
265  return zmFactor;
266}
267
268- (void)setZoomFactor:(CGFloat)f
269{
270    zmFactor = f;
271}
272
273- (void)setStrokeColor:(NSColor *)c
274{
275  [strokeColor release];
276  strokeColor = [[c colorUsingColorSpaceName: NSCalibratedRGBColorSpace] retain];
277}
278
279- (NSColor *)strokeColor
280{
281    return strokeColor;
282}
283
284- (void)setFillColor:(NSColor *)c
285{
286  [fillColor release];
287  fillColor = [[c colorUsingColorSpaceName: NSCalibratedRGBColorSpace] retain];
288}
289
290- (NSColor *)fillColor
291{
292    return fillColor;
293}
294
295- (void)setFilled:(BOOL)value
296{
297    filled = value;
298}
299
300- (BOOL)isFilled
301{
302    return filled;
303}
304
305- (void)setStroked:(BOOL)value
306{
307    stroked = value;
308}
309
310- (BOOL)isStroked
311{
312    return stroked;
313}
314
315- (void)draw
316{
317#ifdef GNUSTEP
318    [self subclassResponsibility: _cmd];
319#endif
320}
321
322
323@end
324