File indexing completed on 2024-05-12 16:34:57

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2013 Mojtaba Shahi Senobari <mojtaba.shahi3000@gmail.com>
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 "AnnotationTextShapeFactory.h"
0021 #include "AnnotationTextShape.h"
0022 
0023 #include <KoProperties.h>
0024 #include <KoShape.h>
0025 #include <KoTextDocument.h>
0026 #include <KoTextShapeData.h>
0027 #include <KoXmlNS.h>
0028 #include <KoStyleManager.h>
0029 #include <KoDocumentResourceManager.h>
0030 #include <KoInlineTextObjectManager.h>
0031 #include <KoTextRangeManager.h>
0032 #include <changetracker/KoChangeTracker.h>
0033 #include <KoImageCollection.h>
0034 #include <KoShapeLoadingContext.h>
0035 
0036 #include <KoIcon.h>
0037 
0038 #include <klocalizedstring.h>
0039 #include <QDebug>
0040 #include <kundo2stack.h>
0041 #include <QTextCursor>
0042 
0043 AnnotationTextShapeFactory::AnnotationTextShapeFactory() :
0044     KoShapeFactoryBase(AnnotationShape_SHAPEID, i18n("Annotation"))
0045 {
0046     setToolTip(i18n("Annotation shape to show annotation content"));
0047     QList<QPair<QString, QStringList> > odfElements;
0048     odfElements.append(QPair<QString, QStringList>(KoXmlNS::office, QStringList("annotation")));
0049     setXmlElements(odfElements);
0050 
0051     KoShapeTemplate t;
0052     t.name = i18n("Annotation");
0053     t.iconName = koIconName("x-shape-text"); // Any icon for now :)
0054     t.toolTip = i18n("Annotation Shape");
0055     KoProperties *props = new KoProperties();
0056     t.properties = props;
0057     props->setProperty("demo", true);
0058     addTemplate(t);
0059 }
0060 
0061 KoShape *AnnotationTextShapeFactory::createDefaultShape(KoDocumentResourceManager *documentResources) const
0062 {
0063     KoInlineTextObjectManager *manager = 0;
0064     KoTextRangeManager *locationManager = 0;
0065     if (documentResources && documentResources->hasResource(KoText::InlineTextObjectManager)) {
0066         QVariant variant = documentResources->resource(KoText::InlineTextObjectManager);
0067         if (variant.isValid()) {
0068             manager = variant.value<KoInlineTextObjectManager *>();
0069         }
0070     }
0071     if (documentResources && documentResources->hasResource(KoText::TextRangeManager)) {
0072         QVariant variant = documentResources->resource(KoText::TextRangeManager);
0073         if (variant.isValid()) {
0074             locationManager = variant.value<KoTextRangeManager *>();
0075         }
0076     }
0077     if (!manager) {
0078         manager = new KoInlineTextObjectManager();
0079     }
0080     if (!locationManager) {
0081         locationManager = new KoTextRangeManager();
0082     }
0083     AnnotationTextShape *annotation = new AnnotationTextShape(manager, locationManager);
0084     if (documentResources) {
0085         KoTextDocument document(annotation->textShapeData()->document());
0086 
0087         if (documentResources->hasResource(KoText::StyleManager)) {
0088             KoStyleManager *styleManager = documentResources->resource(KoText::StyleManager).value<KoStyleManager*>();
0089             document.setStyleManager(styleManager);
0090         }
0091 
0092         // this is needed so the shape can reinitialize itself with the stylemanager
0093         annotation->textShapeData()->setDocument(annotation->textShapeData()->document(), true);
0094 
0095         document.setUndoStack(documentResources->undoStack());
0096 
0097         if (documentResources->hasResource(KoText::PageProvider)) {
0098             KoPageProvider *pp = static_cast<KoPageProvider *>(documentResources->resource(KoText::PageProvider).value<void*>());
0099             annotation->setPageProvider(pp);
0100         }
0101         if (documentResources->hasResource(KoText::ChangeTracker)) {
0102             KoChangeTracker *changeTracker = documentResources->resource(KoText::ChangeTracker).value<KoChangeTracker*>();
0103             document.setChangeTracker(changeTracker);
0104         }
0105 
0106         document.setShapeController(documentResources->shapeController());
0107 
0108         //update the resources of the document
0109         annotation->updateDocumentData();
0110         annotation->setImageCollection(documentResources->imageCollection());
0111     }
0112 
0113     // Should set if we don't set id it will set to TextShapeID.
0114     annotation->setShapeId(AnnotationShape_SHAPEID);
0115 
0116     annotation->setAnnotationTextData(annotation->textShapeData());
0117     return annotation;
0118 }
0119 
0120 KoShape *AnnotationTextShapeFactory::createShape(const KoProperties *params, KoDocumentResourceManager *documentResources) const
0121 {
0122     Q_UNUSED(params);
0123     AnnotationTextShape *shape = static_cast<AnnotationTextShape*>(createDefaultShape(documentResources));
0124     shape->textShapeData()->document()->setUndoRedoEnabled(false);
0125 
0126     if (documentResources) {
0127         shape->setImageCollection(documentResources->imageCollection());
0128     }
0129     shape->textShapeData()->document()->setUndoRedoEnabled(true);
0130     return shape;
0131 }
0132 
0133 bool AnnotationTextShapeFactory::supports(const KoXmlElement &e, KoShapeLoadingContext &context) const
0134 {
0135     Q_UNUSED(context);
0136     return (e.localName() == "annotation" && e.namespaceURI() == KoXmlNS::office);
0137 }