Warning, file /office/calligra/libs/pageapp/KoPAPage.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) 2006-2009 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 "KoPAPage.h"
0021 
0022 #include <QPainter>
0023 #include <PageAppDebug.h>
0024 
0025 #include <KoShapePainter.h>
0026 #include <KoShapeSavingContext.h>
0027 #include <KoOdfLoadingContext.h>
0028 #include <KoStyleStack.h>
0029 #include <KoXmlWriter.h>
0030 #include <KoXmlNS.h>
0031 #include <KoZoomHandler.h>
0032 #include <KoShapePaintingContext.h>
0033 
0034 #include "KoPAMasterPage.h"
0035 #include "KoPASavingContext.h"
0036 #include "KoPALoadingContext.h"
0037 
0038 
0039 KoPAPage::KoPAPage( KoPAMasterPage * masterPage )
0040 : KoPAPageBase()
0041 , m_masterPage( masterPage )
0042 , m_pageProperties( UseMasterBackground | DisplayMasterBackground | DisplayMasterShapes )
0043 {
0044     Q_ASSERT(masterPage);
0045 }
0046 
0047 KoPAPage::~KoPAPage()
0048 {
0049 }
0050 
0051 void KoPAPage::saveOdf( KoShapeSavingContext & context ) const
0052 {
0053     KoPASavingContext &paContext = static_cast<KoPASavingContext&>( context );
0054     paContext.xmlWriter().startElement( "draw:page" );
0055     paContext.xmlWriter().addAttribute( "draw:name", paContext.pageName( this ) );
0056     if (!name().isEmpty() && name() != paContext.pageName( this )) {
0057         paContext.xmlWriter().addAttribute( "calligra:name", name() );
0058     }
0059     paContext.xmlWriter().addAttribute( "draw:id", "page" + QString::number( paContext.page() ) );
0060     paContext.xmlWriter().addAttribute( "xml:id", "page" + QString::number( paContext.page() ) );
0061     paContext.xmlWriter().addAttribute( "draw:master-page-name", paContext.masterPageName( m_masterPage ) );
0062     paContext.xmlWriter().addAttribute( "draw:style-name", saveOdfPageStyle( paContext ) );
0063 
0064     saveOdfPageContent( paContext );
0065 
0066     paContext.xmlWriter().endElement();
0067 }
0068 
0069 KoPageLayout & KoPAPage::pageLayout()
0070 {
0071     Q_ASSERT( m_masterPage );
0072 
0073     return m_masterPage->pageLayout();
0074 }
0075 
0076 const KoPageLayout & KoPAPage::pageLayout() const
0077 {
0078     Q_ASSERT( m_masterPage );
0079 
0080     return m_masterPage->pageLayout();
0081 }
0082 
0083 void KoPAPage::loadOdfPageTag( const KoXmlElement &element, KoPALoadingContext &loadingContext )
0084 {
0085     QString master = element.attributeNS (KoXmlNS::draw, "master-page-name" );
0086     KoPAMasterPage *masterPage = loadingContext.masterPageByName(master);
0087     if (masterPage)
0088         setMasterPage(masterPage);
0089 #ifndef NDEBUG
0090     else
0091         warnPageApp << "Loading didn't provide a page under name; " << master;
0092 #endif
0093     KoStyleStack& styleStack = loadingContext.odfLoadingContext().styleStack();
0094     int pageProperties = UseMasterBackground | DisplayMasterShapes | DisplayMasterBackground;
0095     if ( styleStack.hasProperty( KoXmlNS::draw, "fill" ) ) {
0096         KoPAPageBase::loadOdfPageTag( element, loadingContext );
0097         pageProperties = DisplayMasterShapes;
0098     }
0099     m_pageProperties = pageProperties;
0100     QString name;
0101     if ( element.hasAttributeNS( KoXmlNS::draw, "name" ) ) {
0102         name = element.attributeNS( KoXmlNS::draw, "name" );
0103         loadingContext.addPage( name, this );
0104     }
0105     if ( element.hasAttributeNS( KoXmlNS::calligra, "name" ) ) {
0106         name = element.attributeNS( KoXmlNS::calligra, "name" );
0107     }
0108     setName( name );
0109 }
0110 
0111 void KoPAPage::setMasterPage( KoPAMasterPage * masterPage )
0112 {
0113     Q_ASSERT(masterPage);
0114     m_masterPage = masterPage;
0115     pageUpdated();
0116 }
0117 
0118 void KoPAPage::paintBackground( QPainter & painter, const KoViewConverter & converter, KoShapePaintingContext &paintContext )
0119 {
0120     if ( m_pageProperties & UseMasterBackground ) {
0121         if ( m_pageProperties & DisplayMasterBackground ) {
0122             Q_ASSERT( m_masterPage );
0123             m_masterPage->paintBackground( painter, converter, paintContext );
0124         }
0125     }
0126     else {
0127         KoPAPageBase::paintBackground( painter, converter, paintContext );
0128     }
0129 }
0130 
0131 bool KoPAPage::displayMasterShapes()
0132 {
0133     return m_pageProperties & DisplayMasterShapes;
0134 }
0135 
0136 void KoPAPage::setDisplayMasterShapes( bool display )
0137 {
0138     if ( display ) {
0139         m_pageProperties |= DisplayMasterShapes;
0140     }
0141     else {
0142         m_pageProperties &= ~DisplayMasterShapes;
0143     }
0144 }
0145 
0146 bool KoPAPage::displayMasterBackground()
0147 {
0148     return m_pageProperties & UseMasterBackground;
0149 }
0150 
0151 void KoPAPage::setDisplayMasterBackground( bool display )
0152 {
0153     if ( display ) {
0154         m_pageProperties |= UseMasterBackground;
0155     }
0156     else {
0157         m_pageProperties &= ~UseMasterBackground;
0158     }
0159 }
0160 
0161 bool KoPAPage::displayShape(KoShape *shape) const
0162 {
0163     Q_UNUSED(shape);
0164     return true;
0165 }
0166 
0167 void KoPAPage::paintPage( QPainter & painter, KoZoomHandler & zoomHandler )
0168 {
0169     KoShapePaintingContext context;
0170     paintBackground( painter, zoomHandler, context );
0171 
0172     KoShapePainter shapePainter( getPaintingStrategy() );
0173     if ( displayMasterShapes() ) {
0174         shapePainter.setShapes( masterPage()->shapes() );
0175         shapePainter.paint(painter, zoomHandler);
0176     }
0177     shapePainter.setShapes( shapes() );
0178     shapePainter.paint(painter, zoomHandler);
0179 }
0180 
0181 void KoPAPage::saveOdfPageStyleData( KoGenStyle &style, KoPASavingContext &paContext ) const
0182 {
0183     if ( ( m_pageProperties & UseMasterBackground ) == 0 ) {
0184         KoPAPageBase::saveOdfPageStyleData( style, paContext );
0185     }
0186 }