Coin / src / actions / SoSearchAction.cpp

  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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/**************************************************************************\
 * Copyright (c) Kongsberg Oil & Gas Technologies AS
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of the copyright holder nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\**************************************************************************/

/*!
  \class SoSearchAction SoSearchAction.h Inventor/actions/SoSearchAction.h
  \brief The SoSearchAction class provides methods for searching through scene graphs.
  \ingroup actions

  Nodes can be searched for by pointer, type, and name, or a
  combination of those criteria.  Types can be interpreted as exact
  types, or the type can match nodes derived from it.  Every single
  node can be searched, or normal traversal rules can be followed when
  searching (this is especially important to note with regard to
  switch nodes).

  When using more than one of the setNode(), setType() and setName()
  calls, note that the action will search for node(s) with an \c "AND"
  combination of the given search criteria.

  One of the most common pitfalls when using the SoSearchAction class
  is to call the function isFound() after doing a search.  It does not
  return what you would expect it to return if you haven't read the
  documentation for that function.

  Be aware that if you do search operations on an SoSearchAction
  created on the stack, you can get some unfortunate side effects if
  you're not careful. Since SoSearchAction keeps a list of the path(s)
  found in the latest search, the nodes in these paths will be
  unref'ed when the SoSearchAction stack instance is destructed at the
  end of your function. If the root of your scene-graph then has
  ref-count zero (it is often useful to do a unrefNoDelete() before
  returning a node from a function to leave the referencing to the
  caller), the root node will be destructed! It might be better to
  create a heap instance of the search action in those cases, since
  you'll then be able to destruct the search action before calling
  unrefNoDelete(). Another solution would be to call reset() before
  calling unrefNoDelete() on your object, since reset() truncates
  the path list.

  See the documentation of SoTexture2 for a full usage example of
  SoSearchAction.
*/

#include <Inventor/actions/SoSearchAction.h>

#include <Inventor/nodes/SoNode.h>

#include "actions/SoSubActionP.h"

// *************************************************************************

/*!
  \enum SoSearchAction::LookFor

  Specify the search criterion. This can be a bitwise combination of
  the available values.
*/

/*!
  \enum SoSearchAction::Interest

  Values used when specifiying what node(s) we are interested in: the
  first one found, the last one or all of them.
*/

/*!
  \var SbBool SoSearchAction::duringSearchAll

  Obsoleted global flag, only present for compatibility reasons
  with old SGI / TGS Inventor application code.

  It's set to \c TRUE when an SoSearchAction traversal with
  SoSearchAction::isSearchingAll() equal to \c TRUE is started, and is
  reset to \c FALSE again after traversal has finished.

  (The flag is used by SGI / TGS Inventor in SoSwitch::affectsState()
  to know when SoSwitch::whichChild should behave as
  SoSwitch::SO_SWITCH_ALL. We have a better solution for this problem
  in Coin.)
*/

class SoSearchActionP {
public:
};

SO_ACTION_SOURCE(SoSearchAction);

SbBool SoSearchAction::duringSearchAll = FALSE;


// Overridden from parent class.
void
SoSearchAction::initClass(void)
{
  SO_ACTION_INTERNAL_INIT_CLASS(SoSearchAction, SoAction);
  SoSearchAction::duringSearchAll = FALSE;
}


/*!
  Initializes internal settings with default values. With the default
  settings, the SoSearchAction will ignore all nodes.
*/
SoSearchAction::SoSearchAction(void)
  : lookfor(0), interest(FIRST), searchall(FALSE),
    node(NULL), type(SoType::badType()), name(""),
    path(NULL) // paths(0)
{
  SO_ACTION_CONSTRUCTOR(SoSearchAction);
}

/*!
  Destructor.
*/
SoSearchAction::~SoSearchAction()
{
  this->reset(); // clears everything
}

// *************************************************************************

/*!
  Sets the \a node pointer to search for.

  The action will be configured to set the search "interest" to
  LookFor \c NODE, so there is no need to call
  SoSearchAction::setFind().
*/
void
SoSearchAction::setNode(SoNode * const nodeptr)
{
  this->node = nodeptr;
  this->lookfor |= NODE;
}

/*!
  Returns the node the SoSearchAction instance is configured to search
  for.

  Note that this method does not return what was found when you applied the
  action - it only returns what was specifically set by the user with
  setNode().  What the action found is returned by getPath() and getPaths().
*/
SoNode *
SoSearchAction::getNode(void) const
{
  return this->node;
}

/*!
  Configures the SoSearchAction instance to search for nodes of the
  given \a type, and nodes of classes derived from the given \a type
  if \a chkderived is \c TRUE.

  The action will be configured to set the search "interest" to
  LookFor \c TYPE, so there is no need to call
  SoSearchAction::setFind().
*/
void
SoSearchAction::setType(const SoType typearg, const SbBool chkderivedarg)
{
  this->type = typearg;
  this->chkderived = chkderivedarg;
  this->lookfor |= TYPE;
}

/*!
  Returns the node type which is searched for, and whether derived
  classes of that type also returns a match.
*/
SoType
SoSearchAction::getType(SbBool & chkderivedref) const
{
  chkderivedref = this->chkderived;
  return this->type;
}

/*!
  Configures the SoSearchAction instance to search for nodes with the
  given \a name.

  The action will be configured to set the search "interest" to
  LookFor \c NAME, so there is no need to call
  SoSearchAction::setFind().

  \sa SoNode::getByName()
*/
void
SoSearchAction::setName(const SbName namearg)
{
  this->name = namearg;
  this->lookfor |= NAME;
}

/*!
  Returns the name the SoSearchAction instance is configured to search
  for.
*/
SbName
SoSearchAction::getName(void) const
{
  return this->name;
}

/*!
  Configures what to search for in the scene graph.  \a what is a
  bitmask of LookFor flags.

  Default find configuration is to ignore all nodes, but the setFind()
  configuration is updated automatically when any one of
  SoSearchAction::setNode(), SoSearchAction::setType() or
  SoSearchAction::setName() is called.
*/
void
SoSearchAction::setFind(const int what)
{
  this->lookfor = what;
}

/*!
  Returns the search configuration of the action instance.
*/
int
SoSearchAction::getFind(void) const
{
  return this->lookfor;
}

/*!
  Configures whether only the first, the last, or all the searching
  matches are of interest.  Default configuration is \c FIRST.
*/
void
SoSearchAction::setInterest(const Interest interestarg)
{
  this->interest = interestarg;
}

/*!
  Returns whether only the first, the last, or all the searching
  matches will be saved.
*/
SoSearchAction::Interest
SoSearchAction::getInterest(void) const
{
  return this->interest;
}

/*!
  Specifies whether normal graph traversal should be done (\a
  searchall is \c FALSE, which is the default setting), or if every
  single node should be searched (\a searchall is \c TRUE).

  If the \a searchall flag is \c TRUE, even nodes considered "hidden"
  by other actions are searched (like for instance the disabled
  children of SoSwitch nodes).

  SoBaseKit::setSearchingChildren() must be used to search for nodes
  under node kits.

*/
void
SoSearchAction::setSearchingAll(const SbBool searchallarg)
{
  this->searchall = searchallarg;
}

/*!
  Returns the traversal method configuration of the action.
*/
SbBool
SoSearchAction::isSearchingAll(void) const
{
  return this->searchall;
}

/*!
  Returns the path to the node of interest that matched the search
  criterions. If no match was found, \c NULL is returned.

  Note that if \c ALL matches are of interest, the result of a search
  action must be fetched through SoSearchAction::getPaths().


  There is one frequently asked question about the paths that are
  returned from either this method or the getPaths() method below:
  "why am I not getting the complete path as expected?"

  Well, then you probably have to cast the path to a SoFullPath, since
  certain nodes (nodekits, many VRML97 nodes) have hidden
  children. SoPath::getTail() will return the first node that has
  hidden children, or the tail if none of the nodes have hidden
  children. SoFullPath::getTail() will always return the actual
  tail. Just do like this:
 
  \code
    SoFullPath * path = (SoFullPath *) searchaction->getPath();
    SoVRMLCoordinate * vrmlcord = (SoVRMLCoordinate *) path->getTail();
  \endcode
*/
SoPath *
SoSearchAction::getPath(void) const
{
  return this->path;
}

/*!
  Returns a pathlist of all nodes that matched the search criterions.

  Note that if interest were only \c FIRST or \c LAST,
  SoSearchAction::getPath() should be used instead of this method.

  \sa getPath()
*/
SoPathList &
SoSearchAction::getPaths(void)
{
  return this->paths;
}

/*!
  Resets all the SoSearchAction internals back to their default
  values.
*/
void
SoSearchAction::reset(void)
{
  this->lookfor = 0;
  this->interest = SoSearchAction::FIRST;
  this->searchall = FALSE;
  this->chkderived = TRUE;
  this->node = NULL;
  this->type = SoType::badType();
  this->name = SbName::empty();
  if (this->path) this->path->unref();
  this->path = NULL;
  this->paths.truncate(0);
}

/*!
  \COININTERNAL

  Marks the SoSearchAction instance as terminated.
*/
void
SoSearchAction::setFound(void)
{
  this->setTerminated(TRUE);
}

/*!
  \COININTERNAL

  Returns whether the search action was terminated.

  Note that this value does not reflect whether the node(s) that
  was searched for was found or not.  Use the result of getPath()
  / getPaths() if that is what you really are looking for.
*/
SbBool
SoSearchAction::isFound(void) const
{
  return this->hasTerminated();
}

/*!
  \COININTERNAL

  Sets the path, or adds the path to the path list, depending on the
  interest configuration.  The path is not copied, so it can not be
  modified after being added without side effects.
*/
void
SoSearchAction::addPath(SoPath * const pathptr)
{
  assert(! this->isFound()); // shouldn't try to add path if found

  switch (this->interest) {
  case FIRST:
    assert(! this->path); // should be NULL
    this->path = pathptr;
    this->path->ref();
    this->setFound();
    break;

  case LAST:
    if (this->path)
      this->path->unref(); // should delete it if possible
    this->path = pathptr;
    this->path->ref();
    break;

  case ALL:
    this->paths.append(pathptr);
    break;

  default:
    assert(FALSE && "Interest setting is invalid");
    break;
  }
}

// *************************************************************************

// Documented in superclass. Overridden from superclass to initialize
// internal data.
void
SoSearchAction::beginTraversal(SoNode * nodeptr)
{
  this->paths.truncate(0);
  if (this->path) this->path->unref();
  this->path = NULL;

  // For compatibility with older application code which is using the
  // now obsoleted 'duringSearchAll' flag.
  SoSearchAction::duringSearchAll = this->isSearchingAll();

  this->traverse(nodeptr); // begin traversal at root node

  SoSearchAction::duringSearchAll = FALSE;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.