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

0001 /*
0002  *  Copyright (c) 2009 Cyrille Berger <cberger@cberger.net>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation;
0007  * either version 2, or (at your option) any later version of the License.
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  * Lesser General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Lesser General Public License
0015  * along with this library; see the file COPYING.  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 "SectionContainer.h"
0021 
0022 #include <QMimeData>
0023 
0024 
0025 #include <KoDrag.h>
0026 #include <KoOdf.h>
0027 #include <KoOdfPaste.h>
0028 #include <KoShapeOdfSaveHelper.h>
0029 #include <KoShapeLayer.h>
0030 #include <KoShapeRegistry.h>
0031 #include <KoShapeSavingContext.h>
0032 #include <KoXmlWriter.h>
0033 #include <KoOdfLoadingContext.h>
0034 #include <KoOdfReadStore.h>
0035 #include <KoShapeLoadingContext.h>
0036 #include <KoDocumentResourceManager.h>
0037 
0038 #include "SectionShapeContainerModel.h"
0039 #include "Utils.h"
0040 #include "Layout.h"
0041 #include "RootSection.h"
0042 #include "ViewManager.h"
0043 
0044 #include <algorithm>
0045 
0046 SectionContainer::SectionContainer(Section* section, RootSection* _rootSection) : m_section(0), m_layer(0), m_rootSection(0), m_sectionModel(0)
0047 {
0048     initContainer(section, _rootSection);
0049 }
0050 
0051 SectionContainer::SectionContainer(const SectionContainer& _rhs)
0052 {
0053     Q_UNUSED(_rhs);
0054     qFatal("Can't copy");
0055 }
0056 
0057 class SectionContainerShapePaste : public KoOdfPaste
0058 {
0059 public:
0060     SectionContainerShapePaste(SectionContainer* _container, KoShapeLayer* _layer, Layout* _layout) : m_container(_container), m_layer(_layer), m_layout(_layout) {}
0061     virtual ~SectionContainerShapePaste() {}
0062     virtual bool process(const KoXmlElement & body, KoOdfReadStore & odfStore) {
0063         KoOdfLoadingContext loadingContext(odfStore.styles(), odfStore.store());
0064         KoShapeLoadingContext context(loadingContext, m_container->resourceManager());
0065         QList<KoShape*> shapes;
0066         m_container->loadOdf(body, context, shapes);
0067         m_layout->addShapes(shapes);
0068         return true;
0069     }
0070 private:
0071     SectionContainer* m_container;
0072     KoShapeLayer* m_layer;
0073     Layout* m_layout;
0074 };
0075 
0076 SectionContainer::SectionContainer(const SectionContainer& _rhs, Section* _section) : m_section(0), m_layer(0), m_rootSection(0), m_sectionModel(0)
0077 {
0078     initContainer(_section, _rhs.m_rootSection);
0079     KoShapeOdfSaveHelper saveHelper(_rhs.m_layer->shapes());
0080     KoDrag drag;
0081     drag.setOdf(KoOdf::mimeType(KoOdf::Text), saveHelper);
0082     QMimeData* mimeData = drag.mimeData();
0083 
0084     Q_ASSERT(mimeData->hasFormat(KoOdf::mimeType(KoOdf::Text)));
0085 
0086     SectionContainerShapePaste paste(this, m_layer, _section->layout());
0087     bool success = paste.paste(KoOdf::Text, mimeData);
0088     Q_ASSERT(success); Q_UNUSED(success)
0089 
0090     delete mimeData;
0091 }
0092 
0093 void SectionContainer::initContainer(Section* _section, RootSection* _rootSection)
0094 {
0095     m_rootSection = _rootSection;
0096     m_section = _section;
0097     m_sectionModel = new SectionShapeContainerModel(m_section);
0098     m_layer = new KoShapeLayer(m_sectionModel);
0099     resourceManager()->setUndoStack(_rootSection->undoStack());
0100 }
0101 
0102 void SectionContainer::addShape(KoShape* shape)
0103 {
0104     m_rootSection->viewManager()->addShape(m_section, shape);
0105 }
0106 
0107 void SectionContainer::removeShape(KoShape* shape)
0108 {
0109     m_rootSection->viewManager()->removeShape(m_section, shape);
0110 }
0111 
0112 Section* SectionContainer::section()
0113 {
0114     return m_section;
0115 }
0116 
0117 KoShapeLayer* SectionContainer::layer()
0118 {
0119     return m_layer;
0120 }
0121 
0122 bool SectionContainer::loadOdf(const KoXmlElement & element, KoShapeLoadingContext &context, QList<KoShape*>& shapes)
0123 {
0124     m_sectionModel->setUpdateLayout(false);
0125     KoXmlElement child;
0126     forEachElement(child, element) {
0127         KoShape * shape = KoShapeRegistry::instance()->createShapeFromOdf(child, context);
0128         if(shape) {
0129             m_layer->addShape(shape);
0130             shapes.push_back(shape);
0131         }
0132     }
0133     m_sectionModel->setUpdateLayout(true);
0134     return true;
0135 }
0136 void SectionContainer::saveOdf(KoShapeSavingContext & context) const
0137 {
0138     context.xmlWriter().startElement("braindump:section");
0139 
0140     QList<KoShape*> shapes = m_layer->shapes();
0141     std::sort(shapes.begin(), shapes.end(), KoShape::compareShapeZIndex);
0142 
0143     foreach(KoShape * shape, shapes) {
0144         shape->saveOdf(context);
0145     }
0146 
0147     context.xmlWriter().endElement();
0148 }
0149 
0150 QRectF SectionContainer::containerBound() const
0151 {
0152     QRectF b;
0153     Utils::containerBoundRec(m_layer->shapes(), b);
0154     return b;
0155 }