Warning, file /office/calligra/libs/pageapp/KoPAPastePage.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) 2007-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 "KoPAPastePage.h" 0021 0022 #include <QBuffer> 0023 #include <QString> 0024 #include <KoGenStyles.h> 0025 #include <KoOdfReadStore.h> 0026 #include <KoXmlWriter.h> 0027 #include <KoOdfLoadingContext.h> 0028 #include <KoEmbeddedDocumentSaver.h> 0029 #include "KoPALoadingContext.h" 0030 #include "KoPADocument.h" 0031 #include "KoPAPage.h" 0032 #include "KoPAMasterPage.h" 0033 #include "KoPASavingContext.h" 0034 #include "commands/KoPAPageInsertCommand.h" 0035 0036 KoPAPastePage::KoPAPastePage( KoPADocument * doc, KoPAPageBase * activePage ) 0037 : m_doc( doc ) 0038 , m_activePage( activePage ) 0039 { 0040 } 0041 0042 bool KoPAPastePage::process( const KoXmlElement & body, KoOdfReadStore & odfStore ) 0043 { 0044 KoOdfLoadingContext loadingContext( odfStore.styles(), odfStore.store(), m_doc->defaultStylesResourcePath() ); 0045 KoPALoadingContext paContext(loadingContext, m_doc->resourceManager()); 0046 0047 QList<KoPAPageBase *> newMasterPages( m_doc->loadOdfMasterPages( odfStore.styles().masterPages(), paContext ) ); 0048 QList<KoPAPageBase *> newPages( m_doc->loadOdfPages( body, paContext ) ); 0049 0050 // Check where to start inserting pages 0051 KoPAPageBase * insertAfterPage = 0; 0052 KoPAPageBase * insertAfterMasterPage = 0; 0053 if ( dynamic_cast<KoPAMasterPage *>( m_activePage ) || ( m_activePage == 0 && newPages.empty() ) ) { 0054 insertAfterMasterPage = m_activePage; 0055 insertAfterPage = m_doc->pages( false ).last(); 0056 } 0057 else { 0058 insertAfterPage = m_activePage; 0059 insertAfterMasterPage = m_doc->pages( true ).last(); 0060 } 0061 0062 if ( !newPages.empty() ) { 0063 KoGenStyles mainStyles; 0064 QBuffer buffer; 0065 buffer.open( QIODevice::WriteOnly ); 0066 KoXmlWriter xmlWriter( &buffer ); 0067 KoEmbeddedDocumentSaver embeddedSaver; 0068 KoPASavingContext savingContext(xmlWriter, mainStyles, embeddedSaver, 1); 0069 savingContext.addOption( KoShapeSavingContext::UniqueMasterPages ); 0070 QList<KoPAPageBase*> emptyList; 0071 QList<KoPAPageBase*> existingMasterPages = m_doc->pages( true ); 0072 savingContext.setClearDrawIds( true ); 0073 m_doc->saveOdfPages( savingContext, emptyList, existingMasterPages ); 0074 0075 QMap<QString, KoPAMasterPage*> masterPageNames; 0076 0077 foreach ( KoPAPageBase * page, existingMasterPages ) 0078 { 0079 KoPAMasterPage * masterPage = dynamic_cast<KoPAMasterPage*>( page ); 0080 Q_ASSERT( masterPage ); 0081 if ( masterPage ) { 0082 QString masterPageName( savingContext.masterPageName( masterPage ) ); 0083 if ( !masterPageNames.contains( masterPageName ) ) { 0084 masterPageNames.insert( masterPageName, masterPage ); 0085 } 0086 } 0087 0088 } 0089 0090 m_doc->saveOdfPages( savingContext, emptyList, newMasterPages ); 0091 0092 QMap<KoPAMasterPage*, KoPAMasterPage*> masterPagesToUpdate; 0093 foreach ( KoPAPageBase * page, newMasterPages ) 0094 { 0095 KoPAMasterPage * masterPage = dynamic_cast<KoPAMasterPage*>( page ); 0096 Q_ASSERT( masterPage ); 0097 if ( masterPage ) { 0098 QString masterPageName( savingContext.masterPageName( masterPage ) ); 0099 QMap<QString, KoPAMasterPage*>::const_iterator existingMasterPage( masterPageNames.constFind( masterPageName ) ); 0100 if ( existingMasterPage != masterPageNames.constEnd() ) { 0101 masterPagesToUpdate.insert( masterPage, existingMasterPage.value() ); 0102 } 0103 } 0104 } 0105 0106 // update pages which have a duplicate master page 0107 foreach ( KoPAPageBase * page, newPages ) 0108 { 0109 KoPAPage * p = dynamic_cast<KoPAPage*>( page ); 0110 Q_ASSERT( p ); 0111 if ( p ) { 0112 KoPAMasterPage * masterPage( p->masterPage() ); 0113 QMap<KoPAMasterPage*, KoPAMasterPage*>::const_iterator pageIt( masterPagesToUpdate.constFind( masterPage ) ); 0114 if ( pageIt != masterPagesToUpdate.constEnd() ) { 0115 p->setMasterPage( pageIt.value() ); 0116 } 0117 } 0118 } 0119 0120 // delete duplicate master pages; 0121 QMap<KoPAMasterPage*, KoPAMasterPage*>::const_iterator pageIt( masterPagesToUpdate.constBegin() ); 0122 for ( ; pageIt != masterPagesToUpdate.constEnd(); ++pageIt ) 0123 { 0124 newMasterPages.removeAll( pageIt.key() ); 0125 delete pageIt.key(); 0126 } 0127 } 0128 0129 KUndo2Command * cmd = 0; 0130 if ( m_doc->pageType() == KoPageApp::Slide ) { 0131 cmd = new KUndo2Command( kundo2_i18np( "Paste Slide", "Paste Slides", qMax( newMasterPages.size(), newPages.size() ) ) ); 0132 } 0133 else { 0134 cmd = new KUndo2Command( kundo2_i18np( "Paste Page", "Paste Pages", qMax( newMasterPages.size(), newPages.size() ) ) ); 0135 } 0136 0137 foreach( KoPAPageBase * masterPage, newMasterPages ) 0138 { 0139 new KoPAPageInsertCommand( m_doc, masterPage, insertAfterMasterPage, cmd ); 0140 insertAfterMasterPage = masterPage; 0141 } 0142 0143 foreach( KoPAPageBase * page, newPages ) 0144 { 0145 new KoPAPageInsertCommand( m_doc, page, insertAfterPage, cmd ); 0146 insertAfterPage = page; 0147 } 0148 0149 m_doc->addCommand( cmd ); 0150 0151 return true; 0152 }