File indexing completed on 2024-05-12 16:36:42

0001 /* This file is part of the KDE project
0002    Copyright (C) 2007-2008      Carlos Licea <carlos.licea@kdemail.org>
0003    Copyright (C) 2008 Thorsten Zachmann <zachmann@kde.org>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software itation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This library is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this library; see the file COPYING.LIB.  If not, write to
0017    the Free Software itation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #include "KPrCustomSlideShows.h"
0022 
0023 //Calligra includes
0024 #include <KoPAPageBase.h>
0025 #include <KoPAPage.h>
0026 #include <KoPALoadingContext.h>
0027 #include <KoPASavingContext.h>
0028 #include <KoXmlNS.h>
0029 #include <KoXmlWriter.h>
0030 #include <KoXmlReader.h>
0031 
0032 //Stage includes
0033 #include <KPrDocument.h>
0034 #include "StageDebug.h"
0035 
0036 KPrCustomSlideShows::KPrCustomSlideShows()
0037 {
0038 }
0039 
0040 KPrCustomSlideShows::~KPrCustomSlideShows()
0041 {
0042 }
0043 
0044 void KPrCustomSlideShows::insert( const QString &name, const QList<KoPAPageBase*> &slideShow )
0045 {
0046     QMap< QString, QList<KoPAPageBase*> >::iterator it = m_customSlideShows.find( name );
0047     Q_ASSERT( it == m_customSlideShows.end() );
0048     Q_UNUSED( it ); // only used in the above Q_ASSERT.
0049     m_customSlideShows.insert( name, slideShow );
0050 }
0051 
0052 void KPrCustomSlideShows::remove( const QString &name )
0053 {
0054     QMap< QString, QList<KoPAPageBase*> >::iterator it = m_customSlideShows.find( name );
0055     Q_ASSERT( it != m_customSlideShows.end() );
0056     m_customSlideShows.erase( it );
0057 }
0058 
0059 void KPrCustomSlideShows::update( const QString &name, const QList<KoPAPageBase*> &slideShow )
0060 {
0061     QMap< QString, QList<KoPAPageBase*> >::const_iterator it = m_customSlideShows.constFind( name );
0062     Q_ASSERT( it != m_customSlideShows.constEnd() );
0063     Q_UNUSED( it ); // only used in the above Q_ASSERT.
0064     m_customSlideShows.insert( name, slideShow );
0065     emit updated();
0066 }
0067 void KPrCustomSlideShows::rename( const QString &oldName, const QString &newName )
0068 {
0069     QMap< QString, QList<KoPAPageBase*> >::const_iterator it = m_customSlideShows.constFind( oldName );
0070     Q_ASSERT( it !=  m_customSlideShows.constEnd() );
0071     QList<KoPAPageBase*> value( it.value() );
0072     remove( oldName );
0073     insert( newName, value );
0074 }
0075 
0076 const QList<QString> KPrCustomSlideShows::names() const
0077 {
0078     return m_customSlideShows.keys();
0079 }
0080 
0081 QList<KoPAPageBase*> KPrCustomSlideShows::getByName( const QString &name ) const
0082 {
0083     QMap< QString, QList<KoPAPageBase*> >::const_iterator it = m_customSlideShows.constFind( name );
0084     Q_ASSERT( it !=  m_customSlideShows.constEnd() );
0085 //     if( it == m_customSlideShows.constEnd() ) {
0086 //         return QList<KoPAPageBase*>();
0087 //     }
0088     return it.value();
0089 }
0090 
0091 KoPAPageBase *KPrCustomSlideShows::pageByIndex(const QString &name, int index) const
0092 {
0093     QList<KoPAPageBase*> pages = getByName(name);
0094     return pages.value(index);
0095 }
0096 
0097 
0098 int KPrCustomSlideShows::indexByPage(const QString &name, KoPAPageBase *page) const
0099 {
0100     QList<KoPAPageBase*> pages = getByName(name);
0101     return pages.indexOf(page);
0102 }
0103 
0104 void KPrCustomSlideShows::addSlideToAll( KoPAPageBase *page, unsigned int position )
0105 {
0106     QMap< QString, QList<KoPAPageBase*> >::iterator it = m_customSlideShows.begin();
0107     //FIXME: should we allow negative index?
0108     //if( position < 0 ) return;
0109     while( it != m_customSlideShows.end() ) {
0110         uint size = it.value().size();
0111         it.value().insert( (position<=size)? position : size, page );
0112         ++it;
0113     }
0114     emit updated();
0115 }
0116 
0117 void KPrCustomSlideShows::addSlidesToAll( const QList<KoPAPageBase*> &slideShow, unsigned int position )
0118 {
0119     //FIXME: should we allow negative index?
0120     //if( position < 0 ) return;
0121     for( int i=0; i < slideShow.size(); ++i ) {
0122         addSlideToAll( slideShow[i], position + i );
0123     }
0124 }
0125 
0126 void KPrCustomSlideShows::removeSlideFromAll( KoPAPageBase* page )
0127 {
0128     QMap< QString, QList<KoPAPageBase*> >::iterator it = m_customSlideShows.begin();
0129     while( it != m_customSlideShows.end() ) {
0130         it.value().removeAll( page );
0131         ++it;
0132     }
0133     emit updated();
0134 }
0135 
0136 void KPrCustomSlideShows::removeSlidesFromAll( const QList<KoPAPageBase*> &slideShow )
0137 {
0138     for( int i=0; i < slideShow.size(); ++i ) {
0139         removeSlideFromAll( slideShow[i] );
0140     }
0141 }
0142 
0143 void KPrCustomSlideShows::saveOdf( KoPASavingContext & context )
0144 {
0145     QMap<QString, QList<KoPAPageBase*> >::ConstIterator it = m_customSlideShows.constBegin();
0146     const QMap<QString, QList<KoPAPageBase*> >::ConstIterator end = m_customSlideShows.constEnd();
0147     for (; it != end; ++it) {
0148         const QString &name = it.key();
0149         const QList<KoPAPageBase*> &slideList = it.value();
0150 
0151         context.xmlWriter().startElement( "presentation:show" );
0152         context.xmlWriter().addAttribute( "presentation:name", name );
0153         QString pages;
0154         foreach( KoPAPageBase* page, slideList ) {
0155             KoPAPage * p = dynamic_cast<KoPAPage *>( page );
0156             if ( p ) {
0157                 pages += context.pageName( p ) + ',';
0158             }
0159         }
0160         if( !slideList.isEmpty() ) {
0161             pages.chop(1); //remove the last comma
0162         }
0163         context.xmlWriter().addAttribute( "presentation:pages", pages );
0164         context.xmlWriter().endElement();//presentation:show
0165     }
0166 }
0167 
0168 void KPrCustomSlideShows::loadOdf( const KoXmlElement & presentationSettings, KoPALoadingContext & context )
0169 {
0170     m_customSlideShows.clear();
0171 
0172     KoXmlElement element;
0173     forEachElement( element, presentationSettings ) {
0174         if ( element.tagName() == "show" && element.namespaceURI() == KoXmlNS::presentation ) {
0175             if ( element.hasAttributeNS( KoXmlNS::presentation, "name" ) && element.hasAttributeNS( KoXmlNS::presentation, "pages" ) ) {
0176                 QString name = element.attributeNS( KoXmlNS::presentation, "name" );
0177                 QString pages = element.attributeNS( KoXmlNS::presentation, "pages" );
0178 
0179                 QStringList splitedPages = pages.split( ',' );
0180                 QList<KoPAPageBase*> slideShow;
0181                 foreach (const QString &pageName, splitedPages) {
0182                     KoPAPage * page = context.pageByName( pageName );
0183                     if ( page ) {
0184                         slideShow.append( page );
0185                     }
0186                     else {
0187                         warnStage << "missing attributes is presentation:show";
0188                     }
0189                 }
0190                 if ( !m_customSlideShows.contains( name ) ) {
0191                     m_customSlideShows.insert( name, slideShow );
0192                 }
0193                 else {
0194                     warnStage << "slide show with name" << name << "already existing. It will not be inserted.";
0195                 }
0196             }
0197             else {
0198                 warnStage << "missing attributes is presentation:show";
0199             }
0200         }
0201     }
0202 }
0203 
0204 QStringList KPrCustomSlideShows::namesByPage(KoPAPageBase *page) const
0205 {
0206     QMap< QString, QList<KoPAPageBase*> >::ConstIterator it = m_customSlideShows.begin();
0207     QStringList names;
0208     while(it != m_customSlideShows.end()) {
0209         if (it.value().contains(page)) {
0210             names.append(it.key());
0211         }
0212         ++it;
0213     }
0214     return names;
0215 }