Warning, file /office/calligra/libs/pageapp/KoShapeTraversal.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002    Copyright (C) 2008 Thorsten Zachmann <zachmann@kde.org>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "KoShapeTraversal.h"
0021 
0022 #include <PageAppDebug.h>
0023 
0024 #include <KoShape.h>
0025 #include <KoShapeContainer.h>
0026 
0027 #include <algorithm>
0028 
0029 KoShape * KoShapeTraversal::nextShape( const KoShape * current )
0030 {
0031     return nextShapeStep( current, 0 );
0032 }
0033 
0034 KoShape * KoShapeTraversal::nextShape( const KoShape * current, const QString & shapeId )
0035 {
0036     KoShape * next = nextShapeStep( current, 0 );
0037 
0038     while ( next != 0 && next->shapeId() != shapeId ) {
0039         next = nextShapeStep( next, 0 );
0040     }
0041 
0042     return next;
0043 }
0044 
0045 KoShape * KoShapeTraversal::previousShape( const KoShape * current )
0046 {
0047     return previousShapeStep( current, 0 );
0048 }
0049 
0050 KoShape * KoShapeTraversal::previousShape( const KoShape * current, const QString & shapeId )
0051 {
0052     KoShape * previous = previousShapeStep( current, 0 );
0053 
0054     while ( previous != 0 && previous->shapeId() != shapeId ) {
0055         previous = previousShapeStep( previous, 0 );
0056     }
0057 
0058     return previous;
0059 }
0060 
0061 KoShape * KoShapeTraversal::last( KoShape * current )
0062 {
0063     KoShape * last = current;
0064     while ( const KoShapeContainer * container = dynamic_cast<const KoShapeContainer *>( last ) ) {
0065         QList<KoShape*> shapes = container->shapes();
0066         if ( !shapes.isEmpty() ) {
0067             last = shapes.last();
0068         }
0069         else {
0070             break;
0071         }
0072     }
0073     return last;
0074 }
0075 
0076 KoShape * KoShapeTraversal::nextShapeStep( const KoShape * current, const KoShapeContainer * parent )
0077 {
0078     Q_ASSERT( current );
0079     if ( !current ) {
0080         return 0;
0081     }
0082 
0083     KoShape * next = 0;
0084 
0085     if ( parent ) {
0086         const QList<KoShape*> shapes = parent->shapes();
0087         QList<KoShape*>::const_iterator it( std::find( shapes.begin(), shapes.end(), current ) );
0088         Q_ASSERT( it != shapes.end() );
0089 
0090         if ( it == shapes.end() ) {
0091             warnPageApp << "the shape is not in the list of children of his parent";
0092             return 0;
0093         }
0094 
0095         ++it;
0096         if ( it != shapes.end() ) {
0097             next = *it;
0098         }
0099         else {
0100             KoShapeContainer * currentParent = parent->parent();
0101             next = currentParent ? nextShapeStep( parent, currentParent ) : 0;
0102         }
0103     }
0104     else {
0105         if ( const KoShapeContainer * container = dynamic_cast<const KoShapeContainer *>( current ) ) {
0106             QList<KoShape*> shapes = container->shapes();
0107             if ( !shapes.isEmpty() ) {
0108                 next = shapes[0];
0109             }
0110         }
0111 
0112         if ( next == 0 ) {
0113             KoShapeContainer * currentParent = current->parent();
0114             next = currentParent ? nextShapeStep( current, currentParent ) : 0;
0115         }
0116     }
0117 
0118     return next;
0119 }
0120 
0121 KoShape * KoShapeTraversal::previousShapeStep( const KoShape * current, const KoShapeContainer * parent )
0122 {
0123     Q_ASSERT( current );
0124     if ( !current ) {
0125         return 0;
0126     }
0127 
0128     KoShape * previous = 0;
0129 
0130     if ( parent ) {
0131         if ( previous == 0 ) {
0132             const QList<KoShape*> shapes = parent->shapes();
0133             QList<KoShape*>::const_iterator it( std::find( shapes.begin(), shapes.end(), current ) );
0134             Q_ASSERT( it != shapes.end() );
0135 
0136             if ( it == shapes.end() ) {
0137                 warnPageApp << "the shape is not in the list of children of his parent";
0138                 return 0;
0139             }
0140 
0141             if ( it != shapes.begin() ) {
0142                 --it;
0143                 previous = last( *it );
0144             }
0145             else {
0146                 previous = current->parent();
0147             }
0148         }
0149     }
0150     else {
0151         KoShapeContainer * currentParent = current->parent();
0152         previous = currentParent ? previousShapeStep( current, currentParent ) : 0;
0153     }
0154 
0155     return previous;
0156 }