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

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2008 Fredy Yanardi <fyanardi@gmail.com>
0003  * Copyright (C) 2008-2009 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 Foundation; 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 Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019  */
0020 
0021 #include "KPrNotes.h"
0022 
0023 #include <KoImageCollection.h>
0024 #include <KoImageData.h>
0025 #include <KoShape.h>
0026 #include <KoShapeFactoryBase.h>
0027 #include <KoShapeLayer.h>
0028 #include <KoShapePaintingContext.h>
0029 #include <KoShapeRegistry.h>
0030 #include <KoShapeSavingContext.h>
0031 #include <KoXmlNS.h>
0032 #include <KoXmlWriter.h>
0033 #include <KoPASavingContext.h>
0034 
0035 #include "StageDebug.h"
0036 #include "KPrDocument.h"
0037 #include "KPrPage.h"
0038 
0039 #include <QPainter>
0040 // a helper class to load attributes of the thumbnail shape
0041 class ShapeLoaderHelper : public KoShape
0042 {
0043 public:
0044     ShapeLoaderHelper() { }
0045 
0046     void paint( QPainter &, const KoViewConverter &, KoShapePaintingContext &) override { }
0047 
0048     bool loadOdf( const KoXmlElement & element, KoShapeLoadingContext &context ) override
0049     {
0050         return loadOdfAttributes( element, context, OdfAllAttributes );
0051     }
0052 
0053     void saveOdf( KoShapeSavingContext & ) const override { }
0054 };
0055 
0056 KPrNotes::KPrNotes( KPrPage *page, KPrDocument * document )
0057 : KoPAPageBase()
0058 , m_page( page )
0059 , m_doc( document )
0060 , m_imageCollection( new KoImageCollection() )
0061 {
0062     // add default layer
0063     KoShapeLayer* layer = new KoShapeLayer;
0064     addShape( layer );
0065 
0066     // All sizes and positions are hardcoded for now
0067     KoShapeFactoryBase *factory = KoShapeRegistry::instance()->value("TextShapeID");
0068     Q_ASSERT(factory);
0069     if (factory) {
0070         m_textShape = factory->createDefaultShape(m_doc->resourceManager());
0071         m_textShape->setGeometryProtected(true);
0072         m_textShape->setAdditionalAttribute( "presentation:class", "notes" );
0073         m_textShape->setPosition(QPointF(62.22, 374.46));
0074         m_textShape->setSize(QSizeF(489.57, 356.37));
0075         layer->addShape( m_textShape );
0076     } else {
0077         warnStage << "text shape factory not found";
0078     }
0079 
0080     factory = KoShapeRegistry::instance()->value("PictureShape");
0081     Q_ASSERT(factory);
0082     if (factory) {
0083         m_thumbnailShape = factory->createDefaultShape(m_doc->resourceManager());
0084         m_thumbnailShape->setGeometryProtected(true);
0085         m_thumbnailShape->setAdditionalAttribute( "presentation:class", "page" );
0086         m_thumbnailShape->setPosition(QPointF(108.00, 60.18));
0087         m_thumbnailShape->setSize(QSizeF(396.28, 296.96));
0088         layer->addShape( m_thumbnailShape );
0089     } else {
0090         warnStage << "picture shape factory not found";
0091     }
0092 }
0093 
0094 KPrNotes::~KPrNotes()
0095 {
0096     delete m_imageCollection;
0097 }
0098 
0099 KoShape *KPrNotes::textShape()
0100 {
0101     return m_textShape;
0102 }
0103 
0104 void KPrNotes::saveOdf(KoShapeSavingContext &context) const
0105 {
0106     KoXmlWriter & writer = context.xmlWriter();
0107     writer.startElement("presentation:notes");
0108 
0109     context.addOption( KoShapeSavingContext::PresentationShape );
0110     m_textShape->saveOdf(context);
0111     context.removeOption( KoShapeSavingContext::PresentationShape );
0112     writer.startElement("draw:page-thumbnail");
0113     m_thumbnailShape->saveOdfAttributes( context, OdfAllAttributes );
0114     writer.addAttribute("draw:page-number", static_cast<KoPASavingContext &>(context).page());
0115     writer.endElement(); // draw:page-thumbnail
0116 
0117     KoShapeLayer* layer = static_cast<KoShapeLayer*>( shapes().last() );
0118     foreach ( KoShape *shape, layer->shapes() ) {
0119         if ( shape != m_textShape && shape != m_thumbnailShape ) {
0120             shape->saveOdf( context );
0121         }
0122     }
0123 
0124     writer.endElement(); // presentation:notes
0125 }
0126 
0127 bool KPrNotes::loadOdf(const KoXmlElement &element, KoShapeLoadingContext &context)
0128 {
0129     KoXmlElement child;
0130     KoShapeLayer* layer = static_cast<KoShapeLayer*>( shapes().last() );
0131 
0132     forEachElement( child, element ) {
0133         if ( child.namespaceURI() != KoXmlNS::draw )
0134             continue;
0135 
0136         if ( child.tagName() == "page-thumbnail") {
0137             ShapeLoaderHelper *helper = new ShapeLoaderHelper();
0138             helper->loadOdf( child, context );
0139             m_thumbnailShape->setSize( helper->size() );
0140             m_thumbnailShape->setTransformation( helper->transformation() );
0141             m_thumbnailShape->setPosition( helper->position() );
0142             m_thumbnailShape->setShapeId( helper->shapeId() );
0143             delete helper;
0144         }
0145         else /* if ( child.tagName() == "frame") */ {
0146             KoShape *shape = KoShapeRegistry::instance()->createShapeFromOdf( child, context );
0147             if ( shape ) {
0148                 if ( shape->shapeId() == "TextShapeID" &&
0149                         child.hasAttributeNS( KoXmlNS::presentation, "class" ) ) {
0150                     layer->removeShape( m_textShape );
0151                     delete m_textShape;
0152                     m_textShape = shape;
0153                     m_textShape->setAdditionalAttribute( "presentation:class", "notes" );
0154                     layer->addShape( m_textShape );
0155                 }
0156                 else {
0157                     layer->addShape( shape );
0158                 }
0159             }
0160         }
0161     }
0162 
0163     return true;
0164 }
0165 
0166 void KPrNotes::paintComponent(QPainter& painter, const KoViewConverter& converter, KoShapePaintingContext &paintcontext)
0167 {
0168     Q_UNUSED(painter);
0169     Q_UNUSED(converter);
0170     Q_UNUSED(paintcontext);
0171 }
0172 
0173 KoPageLayout & KPrNotes::pageLayout()
0174 {
0175     return m_pageLayout;
0176 }
0177 
0178 const KoPageLayout & KPrNotes::pageLayout() const
0179 {
0180     return m_pageLayout;
0181 }
0182 
0183 bool KPrNotes::displayMasterShapes()
0184 {
0185     return false;
0186 }
0187 
0188 void KPrNotes::setDisplayMasterShapes( bool )
0189 {
0190 }
0191 
0192 bool KPrNotes::displayShape(KoShape *) const
0193 {
0194     return true;
0195 }
0196 
0197 bool KPrNotes::displayMasterBackground()
0198 {
0199     return false;
0200 }
0201 
0202 void KPrNotes::setDisplayMasterBackground( bool )
0203 {
0204 }
0205 
0206 QImage KPrNotes::thumbImage(const QSize&)
0207 {
0208     Q_ASSERT( 0 );
0209     return QImage();
0210 }
0211 
0212 QPixmap KPrNotes::generateThumbnail( const QSize& )
0213 {
0214     Q_ASSERT( 0 );
0215     return QPixmap();
0216 }
0217 
0218 void KPrNotes::updatePageThumbnail()
0219 {
0220     QSizeF thumbnameSize(m_thumbnailShape->size());
0221 
0222     if (!thumbnameSize.isNull()) {
0223         // set image at least to 150 dpi we might need more when printing
0224         thumbnameSize *= 150 / 72.;
0225         // using KoPADocument::pageThumbnail(...) ensures that the page data is up-to-date
0226         const QImage pageThumbnail = m_doc->pageThumbImage(m_page, thumbnameSize.toSize());
0227         KoImageData *imageData = m_imageCollection->createImageData(pageThumbnail);
0228         m_thumbnailShape->setUserData( imageData );
0229     }
0230 }
0231 
0232 void KPrNotes::paintPage( QPainter & painter, KoZoomHandler & /*zoomHandler*/ )
0233 {
0234     Q_UNUSED(painter);
0235     // TODO implement when printing page with notes
0236     Q_ASSERT( 0 );
0237 }