Debian Codegraph @

lib/nodekits/src/upgraders/SoV1BaseKit.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 /*
38 * Copyright (C) 1990,91 Silicon Graphics, Inc.
39 *
40 _______________________________________________________________________
41 ______________ S I L I C O N G R A P H I C S I N C . ____________
42 |
43 | $Revision: 1.1.1.1 $
44 |
45 | Classes:
46 | SoV1BaseKit
47 |
48 | Author(s) : Paul Isaacs and Thad Beier
49 |
50 ______________ S I L I C O N G R A P H I C S I N C . ____________
51 _______________________________________________________________________
52 */
53
54
55 #include <Inventor/SoDB.h>
56 #include <Inventor/SoPath.h>
57 #include <Inventor/nodes/SoLabel.h>
58 #include <Inventor/nodes/SoGroup.h>
59 #include <Inventor/nodes/SoSwitch.h>
60 #include <Inventor/nodekits/SoNodeKitListPart.h>
61
62 #include <Inventor/misc/upgraders/SoV1BaseKit.h>
63 #include <Inventor/nodekits/SoBaseKit.h>
64 #include <Inventor/errors/SoDebugError.h>
65
66
67 // Defines for printing out file data for nodekits
68 #define SO_V1_BASEKIT_FILEDATA_HEADER "partName/childNum pairs [ "
69 #define SO_V1_BASEKIT_FILEDATA_FOOTER "]"
70
71 // Define the required type id and name inquiry methods
72 SO_NODE_SOURCE(SoV1BaseKit);
73
74 // Define the required catalog inquiry methods
75 SO_V1_SUBKIT_CATALOG_VARS(SoV1BaseKit);
76 SO_V1_SUBKIT_CATALOG_METHODS(SoV1BaseKit);
77
78
79 ////////////////////////////////////////////////////////////////////////
80 //
81 // Description:
82 // Constructor
83 //
84 // Use: public
85
86 SoV1BaseKit::SoV1BaseKit()
87 //
88 ////////////////////////////////////////////////////////////////////////
89 {
90 SO_NODE_CONSTRUCTOR(SoV1BaseKit);
91
92 if (SO_NODE_IS_FIRST_INSTANCE() ) {
93
94 // Initialize children catalog and add entries to it
95 // NOTE: for all classes other than the SoV1BaseKit class, the
96 // macro SO_V1_SUBKIT_INHERIT_CATALOG is used.
97 // the nodekitCatalog is made from scratch ONLY in the SoV1BaseKit class
98 nodekitCatalog = new SoV1NodekitCatalog();
99 SO_V1_SUBKIT_ADD_CATALOG_ENTRY("this",SoV1BaseKit, "", "", FALSE);
100 SO_V1_SUBKIT_ADD_CATALOG_ENTRY("label",SoLabel, "this", "", TRUE );
101 }
102
103 if ( getNodekitCatalog() != NULL )
104 nodekitPartsList = new SoV1NodekitParts( this );
105 }
106
107 ////////////////////////////////////////////////////////////////////////
108 //
109 // Description:
110 // Destructor (necessary since inline destructor is too complex)
111 //
112 // Use: public
113
114 SoV1BaseKit::~SoV1BaseKit()
115 //
116 ////////////////////////////////////////////////////////////////////////
117 {
118 delete nodekitPartsList;
119 }
120
121 ////////////////////////////////////////////////////////////////////////
122 //
123 // Description:
124 // Return the part with the given name
125 //
126 // Use: protected
127
128 SoNode *
129 SoV1BaseKit::getAnyPart( const SbName &partName, const SoType &partType,
130 SbBool makeIfNeeded, SbBool leafCheck,
131 SbBool publicCheck, SoPath * /* inheritancePath */ )
132 //
133 ////////////////////////////////////////////////////////////////////////
134 {
135 SoNode *theNode = nodekitPartsList->getAnyPart( partName, makeIfNeeded,
136 leafCheck, publicCheck );
137
138 // check that the node we are giving back is derived from
139 // partType, so that it is safe for the recipient of 'theNode'
140 // to treat it as being of that type
141 if ( theNode != NULL
142 && !theNode->isOfType( partType )) {
143 #ifdef DEBUG
144 SoDebugError::post("SoV1BaseKit::getAnyPart",
145 "the node you asked for, %s, is not of the type you asked for",
146 partName.getString() );
147 #endif
148 return NULL;
149 }
150 return theNode;
151 }
152
153 ////////////////////////////////////////////////////////////////////////
154 //
155 // Description:
156 // Return the part with the given name
157 //
158 // Use: public
159
160 SoNode *
161 SoV1BaseKit::getPart( const SbName &partName, const SoType &partType,
162 SbBool makeIfNeeded,
163 SoPath *inheritancePath )
164 //
165 ////////////////////////////////////////////////////////////////////////
166 {
167 // the fourth argument, leafCheck and publicCheck are TRUE, because we
168 // don't ordinarily return parts unless they are public leaves.
169 return ( getAnyPart( partName, partType, makeIfNeeded, TRUE, TRUE,
170 inheritancePath ) );
171 }
172
173 ////////////////////////////////////////////////////////////////////////
174 //
175 // Description:
176 // Return the part with the given name
177 //
178 // Use: protected
179
180 SbBool
181 SoV1BaseKit::setAnyPart( const SbName &partName, SoNode *from, SbBool anyPart )
182 //
183 ////////////////////////////////////////////////////////////////////////
184 {
185 if ( from )
186 from->ref();
187 SbBool answer = nodekitPartsList->setAnyPart( partName, from, anyPart );
188 if ( from )
189 from->unref();
190 return answer;
191 }
192
193 ////////////////////////////////////////////////////////////////////////
194 //
195 // Description:
196 // Check to see if the given node is derived from any of the typeIds
197 // in the given typeList
198 //
199 // Use: public
200
201 SbBool
202 SoV1BaseKit::childOkInList(const SoNode *child,
203 const SbPList &listToCheck ) const
204 //
205 ////////////////////////////////////////////////////////////////////////
206 {
207 for( int i = 0; i < listToCheck.getLength(); i++ ) {
208 if ( child->isOfType( *((SoType *) listToCheck[i] ) ) )
209 return TRUE;
210 }
211
212 return FALSE;
213 }
214
215 #define COMMENT_CHAR '#'
216
217
218 ////////////////////////////////////////////////////////////////////////
219 //
220 // Description:
221 // Creates the nodekitPartsList list.
222 // Should be called once during the constructor for each instance.
223 //
224 //
225 //
226 // Use: protected
227
228 void
229 SoV1BaseKit::createNodekitPartsList()
230 //
231 ////////////////////////////////////////////////////////////////////////
232 {
233 if ( getNodekitCatalog() != NULL ) {
234 SoV1NodekitParts *oldParts = nodekitPartsList;
235 nodekitPartsList = new SoV1NodekitParts( this, oldParts );
236 if (oldParts)
237 delete oldParts;
238 }
239 }
240
241 ////////////////////////////////////////////////////////////////////////
242 //
243 // Description:
244 // Interprets the customData string and checks the structure of the given
245 // node based on those contents.
246 //
247 // Use: private
248
249 void
250 SoV1BaseKit::interpretCustomData(SoV1CustomNode *inst, // Node to interpret for
251 int index ) const // Index into the customData
252 // field for finding the
253 // string to interpret.
254 //
255 ////////////////////////////////////////////////////////////////////////
256 {
257 #ifdef DEBUG
258 if ( !inst->isOfType( SoV1BaseKit::getClassTypeId() )) {
259 SoDebugError::post("SoV1BaseKit::interpretCustomData",
260 "Given node is not a nodekit.");
261 return;
262 }
263 #endif
264
265 // get the catalog and parts list
266 SoV1BaseKit *theKit = (SoV1BaseKit *) inst;
267
268 // only do this if 'index' is for the class SoV1BaseKit...
269 if ( theKit->className.getNum() <= index ||
270 theKit->className[index] != "BaseKit" )
271 return;
272
273 SoV1NodekitParts *nkp = theKit->nodekitPartsList;
274 const SoV1NodekitCatalog *theCat = theKit->getNodekitCatalog();
275
276 #ifdef DEBUG
277 // make sure that 'inst' is listed as the part 'this' in its own
278 // nodekitPartsList list.
279 if ( inst != nkp->nodeList[0] ) {
280 SoDebugError::post("SoV1BaseKit::interpretCustomData",
281 "Given node is not listed in its own partlist!");
282 return;
283 }
284 #endif
285
286 // O.K., we're going to do something a little tricky here. You see, some
287 // of the constructors for nodekits build parts automatically. For example,
288 // an SoV1ConeKit constructor creates a new SoCone and then calls
289 // setPart( "cone", newlyMadeSoCone );
290 // Now, reading in a nodekit follows the following algorithm:
291 // [1] create a new kit
292 // [2] read the kit's fields and children
293 // [3] come into this routine and check the node's structural integrity,
294 // based on the information you find in the field 'customData'
295 // The problem is that steps [1] and [2] can create duplicate children.
296 // Clearly, we want to keep the children described in the file, which
297 // are created/read during step [2] and enumerated in the 'customData'
298 // field.
299 // Hence, the tricky part:
300 // If 'customData' is empty, then keep the default parts made by
301 // the constructor.
302 // If 'customData' is not empty, then we want to get rid of the
303 // parts made in the constructor. These parts will all be registered
304 // in the partsList. So get rid of everything in the partsList (except
305 // for 'this' of course).
306 if ( !inst->customData.isDefault() &&
307 (inst->customData[(int) index] != "") ) {
308 SbName entryName;
309 for ( int entryNum = nkp->numEntries - 1; entryNum > 0; entryNum-- ) {
310 // Work backwards through the parts list. Then we will be
311 // sure to delete children before their parents.
312 // Also, don't get rid of entry 0, since that is 'this'.
313 if ( nkp->nodeList[entryNum] != NULL ) {
314 entryName = theCat->getName( entryNum );
315 nkp->setAnyPart(entryName, NULL, TRUE );
316 }
317 }
318 }
319 // end of tricky part.
320
321 // Get a destroyable copy of the customData string.(strtok will corrupt it).
322 char *fData = strdup( inst->customData[(int) index].getString());
323 // to begin, advance fData to after the HEADER
324 fData += strlen( SO_V1_BASEKIT_FILEDATA_HEADER );
325
326 char *newPartName, *newChildNumString;
327 int newChildNum, partNum, parentPartNum;
328 SoGroup *parentNode;
329 SoNode *childNode;
330 SbBool isOk;
331
332 newPartName = strtok( fData, " \n"); // first string
333 newChildNumString = strtok( NULL, " \n"); // second string
334
335 // as long as we can keep reading in name/number pairs, continue...
336 while ( newPartName && newChildNumString ) {
337
338 isOk = TRUE;
339
340 // for each partName/childNumber pair, find the part,
341 // find that part's parent, and set the properly numbered child
342 // of that parent to be 'partName'
343 sscanf( newChildNumString, "%d", &newChildNum );
344
345 // find and check the partNum for this part
346 partNum = theCat->getPartNumber( newPartName );
347 if ( partNum == SO_V1_CATALOG_NAME_NOT_FOUND ) {
348 #ifdef DEBUG
349 SoDebugError::postWarning("SoV1BaseKit::interpretCustomData",
350 "node type %s wont recognize the part named %s",
351 inst->getTypeId().getName().getString(), newPartName );
352 #endif
353 isOk = FALSE;
354 }
355
356 // find and check the parentPartNum
357 if ( isOk ) {
358 parentPartNum = theCat->getParentPartNumber( newPartName );
359 if ( parentPartNum == SO_V1_CATALOG_NAME_NOT_FOUND ) {
360 #ifdef DEBUG
361 SoDebugError::postWarning("SoV1BaseKit::interpretCustomData",
362 "node type %s has no parent part for part named %s",
363 inst->getTypeId().getName().getString(), newPartName );
364 #endif
365 isOk = FALSE;
366 }
367 }
368
369 // make sure parent is already in the parts list!
370 if ( isOk ) {
371 if (nkp->nodeList[parentPartNum] == NULL) {
372 #ifdef DEBUG
373 SoDebugError::post("SoV1BaseKit::interpretCustomData",
374 "parent of node %s is not an existing node yet.",
375 newPartName );
376 #endif
377 isOk = FALSE;
378 }
379 }
380
381 // make sure parent has enough children to get the one specified
382 if ( isOk ) {
383 parentNode = (SoGroup *) nkp->nodeList[parentPartNum];
384 if ( !parentNode ||
385 newChildNum >= parentNode->getNumChildren() ) {
386 #ifdef DEBUG
387 SoDebugError::post("SoV1BaseKit::interpretCustomData",
388 "parent of node %s only has %d children, but you are asking for child number %d", newPartName, parentNode->getNumChildren(), newChildNum );
389 #endif
390 isOk = FALSE;
391 }
392 }
393
394 // make sure child is of type given by the template
395 if ( isOk ) {
396 childNode = parentNode->getChild( newChildNum );
397 if ( !childNode ||
398 !childNode->isOfType( theCat->getType( partNum ) ) ) {
399 #ifdef DEBUG
400 SoDebugError::post("SoV1BaseKit::interpretCustomData",
401 "given part not of correct type");
402 #endif
403 isOk = FALSE;
404 }
405 }
406
407 // make sure the spot isnt already taken...
408 if ( isOk ) {
409 if ( nkp->nodeList[partNum] != NULL ) {
410 #ifdef DEBUG
411 SoDebugError::post("SoV1BaseKit::interpretCustomData",
412 " given part %s already in parts list", newPartName );
413 #endif
414 isOk = FALSE;
415 }
416 }
417
418 if ( isOk && theCat->isList( partNum ) ) {
419 const SbPList listTypes = theCat->getListItemTypes( partNum );
420 SoType *babyType;
421 SoNode *theBaby;
422 SoGroup *grp = (SoGroup *) childNode;
423
424 for (int babyNum = 0;
425 (babyNum < grp->getNumChildren()) && (isOk == TRUE);
426 babyNum++ ) {
427
428 theBaby = grp->getChild( babyNum );
429 SbBool foundBaby = FALSE;
430
431 for ( int j = 0;
432 (j < listTypes.getLength()) && (foundBaby == FALSE); j++ ) {
433 babyType = (SoType *) listTypes[j];
434
435 if ( theBaby->isOfType( *babyType ) )
436 foundBaby = TRUE;
437 }
438 if ( foundBaby == FALSE ) {
439 #ifdef DEBUG
440 SoDebugError::post("SoV1BaseKit::interpretCustomData",
441 " part %s is list, but children chosen node are not of the list item type", newPartName );
442 #endif
443 isOk = FALSE;
444 }
445 }
446 }
447
448 // passed every test! set that part...
449 if ( isOk ) {
450 nkp->nodeList[ partNum ] = childNode;
451 }
452
453 newPartName = strtok( NULL, " \n"); // first string
454 newChildNumString = strtok( NULL, " \n"); // second string
455 }
456 }
457
458 ////////////////////////////////////////////////////////////////////////
459 //
460 // Description:
461 // Create the V2.0 node to replace this node
462 //
463 // Use: public
464
465 SoNode *
466 SoV1BaseKit::createNewNode()
467 //
468 ////////////////////////////////////////////////////////////////////////
469 {
470 return new SoBaseKit;
471 }
472
473 ////////////////////////////////////////////////////////////////////////
474 //
475 // Description:
476 // Virtual method to setUp the new node.
477 //
478 // Use: public
479
480 void
481 SoV1BaseKit::setUpNewNode(SoNode *newNode)
482 //
483 ////////////////////////////////////////////////////////////////////////
484 {
485 const SoV1NodekitCatalog *cat = getNodekitCatalog();
486
487 SoBaseKit *newKit = (SoBaseKit *) newNode;
488
489 // We're going to be undoing some parent child connecions.
490 // So, just to be safe, ref every non-NULL node in the parts list,
491 // and unref them at the end...
492 int i;
493 for( i = 1; i < cat->getNumEntries(); i++ ) {
494 SoNode *thePart = nodekitPartsList->nodeList[i];
495 if (thePart != NULL)
496 thePart->ref();
497 }
498
499 // Loop through all parts in the parts list.
500 // Start at 1, since part 0 in the list is 'this'
501 for( i = 1; i < cat->getNumEntries(); i++ ) {
502
503 SoNode *thePart = nodekitPartsList->nodeList[i];
504 const SbName &theName = cat->getName( i );
505
506 // If the part is not a leaf part, (and also is non-NULL)
507 // then remove any children it may have.
508 // Why? Well, at this point, we've got all the node pointers
509 // established in the parts list, so we won't lose track of them.
510 // When we call set part in the new node, it will establish the
511 // correct locations for each part in the new kit. But we don't
512 // want to keep the old parent-child relationships that we read from
513 // file.
514 if ( thePart != NULL && !cat->isLeaf(i)) {
515 if (thePart->isOfType( SoGroup::getClassTypeId() )) {
516 SoGroup *grp = (SoGroup *) thePart;
517 while ( grp->getNumChildren() > 0 ) {
518 grp->removeChild(0);
519 }
520 }
521 }
522
523
524 // Attempt to just call setPart with the entry in the list...
525 if ( !tryToSetPartInNewNode( newKit, thePart, theName ) ) {
526
527 // Okay, we couldn't just set the part.
528 // Try to do some of the special case stuff.
529 if ( !dealWithUpgradedPart( newKit, thePart, theName )) {
530 #ifdef DEBUG
531 SoDebugError::post("SoV1BaseKit::setUpNewNode",
532 "Dont know what to do with part named %s. The node is going to be ignored and lost.", theName.getString() );
533 #endif
534 }
535 }
536 }
537
538 // Unref the parts we ref'ed
539 for( i = 1; i < cat->getNumEntries(); i++ ) {
540 SoNode *thePart = nodekitPartsList->nodeList[i];
541 if (thePart != NULL)
542 thePart->unref();
543 }
544
545 }
546
547
548 ////////////////////////////////////////////////////////////////////////
549 //
550 // Description:
551 // Attempt to set the given part in the new node.
552 // This will succeed if the partName exists in the newNode's catalog and
553 // the node that we are attempting to use for that part is of a legal type.
554 //
555 // Use: protected
556 //
557 SbBool
558 SoV1BaseKit::tryToSetPartInNewNode( SoBaseKit *newNode,
559 SoNode *newPart, const SbName &newPartName )
560 //
561 ////////////////////////////////////////////////////////////////////////
562 {
563 const SoNodekitCatalog *cat = newNode->getNodekitCatalog();
564
565 // Does newNode's catalog contain an entry called 'newPartName'?
566 int partNumber = cat->getPartNumber( newPartName );
567 if ( partNumber == SO_CATALOG_NAME_NOT_FOUND ) {
568 if ( newPart != NULL )
569 return FALSE;
570 else
571 // Everythings okay, really, if the part we were going to give it
572 // was just NULL anyway.
573 return TRUE;
574 }
575
576 // Is newPart either NULL or derived from the class given in newNode's
577 // catalog for 'newPartName'
578 const SoType &partType = cat->getType( partNumber );
579 if ( newPart != NULL && newPart->isOfType( partType ) == FALSE )
580 return FALSE;
581
582 // If we got here, we can call setAnyPart!
583 newNode->setAnyPart( newPartName, newPart );
584 return TRUE;
585 }
586
587 ////////////////////////////////////////////////////////////////////////
588 //
589 // Description:
590 // A virtual function that allows classes to treat parts that fail the
591 // tests of 'tryToSetPartInNewNode'
592 //
593 // For SoV1BaseKit, this means dealing with the part 'label' and
594 // dealing with any part that is a list part.
595 //
596 // Subclasses will have to do special work if, for example, a part name
597 // or part type has been changed or if a part has been removed.
598 //
599 // Use: public
600 //
601 SbBool
602 SoV1BaseKit::dealWithUpgradedPart( SoBaseKit *newNode, SoNode *newPart,
603 const SbName &newPartName )
604 //
605 ////////////////////////////////////////////////////////////////////////
606 {
607 // If the part name is "label", let's just name the newKit
608 // "label", since the label part no longer exists and most likely
609 // this is the appropriate functionality.
610 if ( newPartName == "label" ) {
611 if ( !newPart->isOfType( SoLabel::getClassTypeId() ) )
612 return FALSE;
613
614 SoLabel *lbl = (SoLabel *) newPart;
615 newNode->setName(lbl->label.getValue());
616 return TRUE;
617 }
618
619 // If the part doesn't exist in the catalog, there is nothing more
620 // we can do just here.
621 const SoNodekitCatalog *cat = newNode->getNodekitCatalog();
622 int partNumber = cat->getPartNumber( newPartName );
623 if ( partNumber == SO_CATALOG_NAME_NOT_FOUND )
624 return FALSE;
625
626 // If the part was a list part, we've got more work to do...
627 if ( cat->isList( partNumber ) ) {
628 // Old lists used to be just some subclass of group with children
629 // stuck underneath.
630 // New lists are of type SoNodeKitListPart. Within this part is
631 // a container node, and below it are children of a given type.
632
633 // First, get a list from the new node.
634 SoNodeKitListPart *newList
635 = (SoNodeKitListPart *) newNode->getAnyPart( newPartName, TRUE);
636
637 // For each child of the old list, try to set it as a child
638 // in the new list.
639 #ifdef DEBUG
640 if ( !newPart->isOfType( SoGroup::getClassTypeId() ) ) {
641 SoDebugError::post("SoV1BaseKit::dealWithUpgradedPart",
642 "the node for part %s in the old node isn't subclassed from SoGroup", newPartName.getString() );
643 return FALSE;
644 }
645 #endif
646 // Check if all the children of oldList are legal children for newList
647 SoGroup *oldList = (SoGroup *) newPart;
648 SbBool allOkay = TRUE;
649 int i;
650 for ( i = 0; i < oldList->getNumChildren(); i++ ) {
651 SoNode *kid = oldList->getChild(i);
652 if ( newList->isChildPermitted( kid ) == FALSE )
653 allOkay = FALSE;
654 }
655
656 // If the children aren't ALL okay, then don't set any. Just return
657 // FALSE and hope that a subclass will be able to deal with them.
658 if (allOkay == FALSE)
659 return FALSE;
660
661 // Move all the children from oldList to newList...
662 for ( i = 0; i < oldList->getNumChildren(); i++ ) {
663 SoNode *kid = oldList->getChild(i);
664 newList->addChild( kid );
665 }
666 while( oldList->getNumChildren() > 0 )
667 oldList->removeChild(0);
668
669 // If the listContainer type is an SoSwitch, then copy the
670 // whichChild value into the new list.
671 if ( oldList->isOfType( SoSwitch::getClassTypeId() )
672 && newList->getContainerType().isDerivedFrom(
673 SoSwitch::getClassTypeId())) {
674 SoSwitch *sw = (SoSwitch *) oldList;
675 // Send a string that says "whichChild x" to newList...
676 char myBuf[200];
677 sprintf( myBuf, "whichChild %d", sw->whichChild.getValue() );
678 newList->containerSet( myBuf );
679 }
680 return TRUE;
681 }
682
683 return FALSE; // don't know how to do anything yet...
684 }