1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_SW_INC_SWNUMBERTREE_HXX
21 #define INCLUDED_SW_INC_SWNUMBERTREE_HXX
26 #include <SwNumberTreeTypes.hxx>
28 class SwNumberTreeNode
;
30 bool SwNumberTreeNodeLessThan (const SwNumberTreeNode
* pA
,
31 const SwNumberTreeNode
* pB
);
33 struct compSwNumberTreeNodeLessThan
35 bool operator()(const SwNumberTreeNode
* pA
,
36 const SwNumberTreeNode
* pB
) const
37 { return SwNumberTreeNodeLessThan(pA
, pB
); }
41 A tree of numbered nodes.
64 The root contains the nodes of the first level. Each node A of the
65 first level contains those nodes of the second level that have the
66 same first level number as A and so on for the subsidiary levels.
68 The numbering label of a node A is resolved by concatenating the
69 numbers of the nodes on the path from the root to A.
71 ------------------------------------------
75 A phantom is an auxiliary node that is used to emulate numberings
76 starting with nodes not at top level. The phantom contains the
77 number for the level but is not considered part of the numbering.
79 Constraint 1: A phantom is always the first child node.
80 Constraint 2: At each node there is at most one child that is a phantom.
81 Constraint 3: A phantom is the smallest of all numbering nodes.
90 + 0 (phantom, not counted)
95 The phantom gets numbered with 0. The first non-phantom node gets
96 numbered with the start value.
98 -----------------------------------------
104 6.1. ljdflaksjflkjasflkjsf
107 + 5 (phantom, counted)
110 + 1 ljdflaksjflkjasflkjsf
112 The phantom gets numbered with the start value.
114 class SW_DLLPUBLIC SwNumberTreeNode
117 typedef std::set
<SwNumberTreeNode
*, compSwNumberTreeNodeLessThan
>
118 tSwNumberTreeChildren
;
123 virtual ~SwNumberTreeNode();
128 @param pChild child to add
129 @param nDepth depth in which to add the child
131 void AddChild( SwNumberTreeNode
* pChild
,
132 const int nDepth
= 0 );
137 @param pChild child to be removed
139 void RemoveChild( SwNumberTreeNode
* pChild
);
142 Remove this child from the tree.
147 Returns the parent of this node.
151 inline SwNumberTreeNode
* GetParent() const
157 Returns number of this node.
159 @param bValidate validate the number?
161 @return number of this node
163 SwNumberTree::tSwNumTreeNumber
GetNumber( bool bValidate
= true ) const;
165 bool IsContinueingPreviousSubTree() const { return mbContinueingPreviousSubTree
;}
168 Returns level numbers of this node.
170 @return level numbers of this node
172 SwNumberTree::tNumberVector
GetNumberVector() const;
175 Return if numbering is restartet at this node.
177 virtual bool IsRestart() const = 0;
184 virtual SwNumberTree::tSwNumTreeNumber
GetStartValue() const = 0;
187 Return if this node is counted.
189 @retval true this node is counted
190 @retval false this node is NOT counted
192 virtual bool IsCounted() const;
195 Return if this node is counted continuous.
197 @retval true This node is counted continuous.
200 virtual bool IsContinuous() const = 0;
203 Return if a node is first non-phantom child of this node.
205 @param pNode the node to check
207 @retval true pNode is first child of this node
210 bool IsFirst(const SwNumberTreeNode
* pNode
) const;
213 Return if this node if the first non-phantom node in the tree.
215 @retval true this node is the first non-phantom node in the tree
218 bool IsFirst() const;
221 Return if this node is a phantom.
223 @retval true this node is a phantom
224 @retval false this node is NOT a phantom
226 bool IsPhantom() const { return mbPhantom
;}
228 /** set level of this node
230 precondition: node is already member of a list tree
234 void SetLevelInListTree( const int nLevel
);
237 Return level of this node.
239 The level of this node is the length of the path from the root
242 @return the level of this node
244 int GetLevelInListTree() const;
247 Returns if this node is less than another node.
249 @param rTreeNode node to compare with
251 @attention A phantom node is considered the least element with
254 @retval true this node is less than rTreeNode
257 virtual bool LessThan(const SwNumberTreeNode
& rTreeNode
) const;
260 Invalidate this node and all its descendants.
262 All iterators holding the last valid node in the according list
263 of children are set to the end of this list, thereby stating all
264 children in the list are invalid.
265 #i83479# - made public
267 void InvalidateTree() const;
270 Notifies all invalid children of this node.
271 #i83479# - made public
273 void NotifyInvalidChildren();
278 Calls Invalidate(this) on parent.
285 Validates all nodes in this subtree.
292 Calls Validate(this) on parent.
297 Notifies all invalid siblings of this node.
299 void NotifyInvalidSiblings();
302 notification of all nodes in the list tree on certain list level
304 void NotifyNodesOnListLevel( const int nListLevel
);
306 /** Invalidation and notification of complete numbering tree
309 Usage: on <IsCounted()> state change its needed to invalidate the
310 complete numbering tree due to wide influence of this change.
312 inline void InvalidateAndNotifyTree()
316 GetRoot()->InvalidateTree();
322 Returns the greatest descendant of the root that is smaller than
323 this node, aka the predecessor of this node.
325 @return the predecessor
327 SwNumberTreeNode
* GetPred( bool bSibling
= false ) const;
329 /** determines the node, which is preceding the node
332 The search for the preceding node is performed for the tree below the
333 <this> node. To search the complete tree, the method has been called for
334 the root of the tree.
338 const SwNumberTreeNode
* GetPrecedingNodeOf( const SwNumberTreeNode
& rNode
) const;
340 #ifdef __SW_NUMBER_TREE_SANITY_CHECK
344 @param bRecursive descend to children
346 @retval true the structure of this node is sane
349 bool IsSane(bool bRecursive
) const;
350 #endif // __SW_NUMBER_TREE_SANITY_CHECK
356 tSwNumberTreeChildren mChildren
;
359 Returns the root node of the tree this node is part of.
361 Important note: method call <GetRoot()->GetRoot()> returns NULL.
365 SwNumberTreeNode
* GetRoot() const;
368 Return if the notification is not disabled on global conditions
370 @retval true Notification enabled in general.
373 virtual bool IsNotificationEnabled() const = 0;
376 Returns how many children this node has got.
378 @return number of children
380 tSwNumberTreeChildren::size_type
GetChildCount() const;
382 // #i64010# - made pure virtual
383 virtual bool HasCountedChildren() const = 0;
386 virtual bool IsCountedForNumbering() const = 0;
388 // method called before this tree node has been added to the list tree
389 virtual void PreAdd() = 0;
390 // method called after this tree node has been removed from the list tree
391 virtual void PostRemove() = 0;
393 #ifdef __SW_NUMBER_TREE_SANITY_CHECK
395 Sanity check with loop detection.
397 @param bRecursive descend to children
398 @param rParents vector for recording path
400 @retval true this node is sane
401 @retval false else */
403 (bool bRecursive
, std::vector
<const SwNumberTreeNode
*> rParents
) const;
404 #endif // __SW_NUMBER_TREE_SANITY_CHECK
409 SwNumberTreeNode
* mpParent
;
412 the number of the node
414 mutable SwNumberTree::tSwNumTreeNumber mnNumber
;
416 // boolean indicating, that a node of a not counted parent node is continuing
417 // the numbering of parent's previous node sub tree.
421 // sdfjlksaf <-- not counted parent node
422 // 1.2. lfjlaskf <-- <mbContinueingPreviousSubTree = true>
423 mutable bool mbContinueingPreviousSubTree
;
426 true this node is a phantom
427 false this node is NOT a phantom
432 Iterator to the last valid element. All children that are less
433 than or equal to the referenced child are valid. All children
434 greater than the referenced child are invalid.
436 mutable tSwNumberTreeChildren::const_iterator mItLastValid
;
438 SwNumberTreeNode(const SwNumberTreeNode
& );
439 SwNumberTreeNode
& operator=( const SwNumberTreeNode
& );
442 Calls _GetNumberVector on parent and adds number of this node
445 @param rVector return value
446 @param bValidate validate the number?
448 void _GetNumberVector( SwNumberTree::tNumberVector
& rVector
,
449 bool bValidate
= true ) const;
454 Calls SetLastValid for the preceding sibling of the child and
455 notifies all invalid children.
457 @param pChild the child to invalidate
459 void Invalidate( SwNumberTreeNode
* pChild
);
461 /** Invalidation of all children
463 Usage: on <IsCounted()> state change the children have to be invalidated
465 inline void InvalidateChildren()
467 SetLastValid( mChildren
.end() );
470 /** Invalidation of parent node, if its not counted.
472 Usage: on <IsCounted()> state change the parent have to be invalidated
474 inline void InvalidateNotCountedParent()
476 if ( GetParent() && !GetParent()->IsCountedForNumbering() )
478 GetParent()->InvalidateMe();
483 Set the last valid child of this node.
485 @param aItLastValid iterator pointing to the new last valid child
486 @param bValidating - true always set the last valid node to
488 - false only set if aItLastValid is preceding
489 the current last valid node
491 void SetLastValid(tSwNumberTreeChildren::const_iterator aItLastValid
,
492 bool bValidating
= false) const;
495 Set this node as last valid child of its parent.
497 @param bValidation see aboce
499 void SetLastValid(bool bValidating
) const;
502 Return if this node is notifiable.
504 @attention If a not is not notifiable a notify request is *not*
505 forwarded to its descendants.
507 @retval true This node is notifiable.
510 virtual bool IsNotifiable() const = 0;
515 Called when the number of the node got invalid.
517 virtual void NotifyNode() = 0;
520 Notifies this node (NotifyNode) and all descendants.
524 /** Notification of parent node siblings, if its not counted.
526 Usage: on <IsCounted()> state change the parent node and its siblings
529 inline void NotifyNotCountedParentSiblings()
531 if ( GetParent() && !GetParent()->IsCountedForNumbering() )
533 GetParent()->NotifyInvalidSiblings();
537 /** notification of children nodes on certain depth
541 void NotifyChildrenOnDepth( const int nDepth
);
544 Returns if a child A this node is valid.
546 A is valid if aItLastValid in parent refers to a node
547 greater than of equal to A.
549 @param pChild child to be tested
551 @retval true this node is valid
552 @retval false this node is NOT valid
554 bool IsValid(const SwNumberTreeNode
* pChild
) const;
557 Returns if this node is valid.
559 @retval true this node is valid
562 bool IsValid() const;
567 @param pNode child to be validated
569 @attention All invalid children preceding pNode are validated, too.
571 void Validate(const SwNumberTreeNode
* pNode
) const;
574 Validates a child using hierarchical numbering.
576 @param pNode child to be validated
578 @attention All invalid children preceding pNode are validated, too.
580 void ValidateHierarchical(const SwNumberTreeNode
* pNode
) const;
583 Validates a child using continuous numbering.
585 @param pNode child to be validated
587 @attention All invalid children preceding pNode are validated, too.
589 void ValidateContinuous(const SwNumberTreeNode
* pNode
) const;
592 Creates a new node of the same class.
596 virtual SwNumberTreeNode
* Create() const = 0;
601 @return the created phantom
603 SwNumberTreeNode
* CreatePhantom();
606 Set if this node is a phantom.
608 @param bPhantom - true this node is a phantom
609 - false this node is a phantom
611 void SetPhantom(bool bPhantom
= true);
614 Return if phantoms are counted.
616 @retval true phantoms are counted
619 virtual bool IsCountPhantoms() const = 0;
622 Return if all descendants of this node are phantoms.
624 @retval true all descendants are phantoms
627 bool HasOnlyPhantoms() const;
629 bool HasPhantomCountedParent() const;
632 HB, OD : return node, if it isn't a phantom, otherwise return first
633 non-phantom descendant.
634 Returns the first child of this node that is NOT a phantom.
636 @return the first non phantom child
638 SwNumberTreeNode
* GetFirstNonPhantomChild();
641 Removes recursively phantoms that have no children.
643 The resulting tree has no phantoms that either have no children or
644 whose descendancy consist entirely of phantoms.
646 void ClearObsoletePhantoms();
648 tSwNumberTreeChildren::const_iterator
GetIterator(const SwNumberTreeNode
* pChild
) const;
651 Moves all children to a given destination node.
653 @param pDest the destination node
655 void MoveChildren(SwNumberTreeNode
* pDest
);
657 /** Moves all children of this node that are greater than a given node
658 to the destination node.
660 distinguish between node for comparing, whose children are greater,
661 and the destination node.
664 input parameter - reference to the node, which is used to determine
668 input parameter - reference to the node, which is the destination for
671 void MoveGreaterChildren( SwNumberTreeNode
& _rCompareNode
,
672 SwNumberTreeNode
& _rDestNode
);
675 Returns the last descendant of a node, if it has children.
677 @return last descendant of the node
679 SwNumberTreeNode
* GetLastDescendant() const;
684 Functor. Checks if a certain node is less than the functor's member.
686 struct SwNumberTreeNodeIsLessThan
688 const SwNumberTreeNode
* pNode
;
690 SwNumberTreeNodeIsLessThan(const SwNumberTreeNode
* _pNode
)
693 bool operator()(const SwNumberTreeNode
* _pNode
) const
694 { return SwNumberTreeNodeLessThan(_pNode
, pNode
); }
696 #endif // INCLUDED_SW_INC_SWNUMBERTREE_HXX
698 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */