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

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2006-2007,2009,2010 Thomas Zander <zander@kde.org>
0003  * Copyright (C) 2007 Jan Hambrecht <jaham@gmx.net>
0004  * Copyright (C) 2008 Thorsten Zachmann <zachmann@kde.org>
0005  *
0006  * This library is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU Library General Public
0008  * License as published by the Free Software Foundation; either
0009  * version 2 of the License, or (at your option) any later version.
0010  *
0011  * This library is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014  * Library General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU Library General Public License
0017  * along with this library; see the file COPYING.LIB.  If not, write to
0018  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019  * Boston, MA 02110-1301, USA.
0020  */
0021 #include "TextShapeFactory.h"
0022 #include "TextShape.h"
0023 
0024 #include <KoProperties.h>
0025 #include <KoShape.h>
0026 #include <KoTextDocument.h>
0027 #include <KoTextShapeData.h>
0028 #include <KoXmlNS.h>
0029 #include <KoStyleManager.h>
0030 #include <KoDocumentResourceManager.h>
0031 #include <KoInlineTextObjectManager.h>
0032 #include <KoTextRangeManager.h>
0033 #include <changetracker/KoChangeTracker.h>
0034 #include <KoImageCollection.h>
0035 #include <KoShapeLoadingContext.h>
0036 
0037 #include <KoIcon.h>
0038 
0039 #include <klocalizedstring.h>
0040 #include <kundo2stack.h>
0041 #include <QTextCursor>
0042 
0043 TextShapeFactory::TextShapeFactory()
0044         : KoShapeFactoryBase(TextShape_SHAPEID, i18n("Text"))
0045 {
0046     setToolTip(i18n("A shape that shows text"));
0047     QList<QPair<QString, QStringList> > odfElements;
0048     odfElements.append(QPair<QString, QStringList>(KoXmlNS::draw, QStringList("text-box")));
0049     odfElements.append(QPair<QString, QStringList>(KoXmlNS::table, QStringList("table")));
0050     setXmlElements(odfElements);
0051     setLoadingPriority(1);
0052 
0053     KoShapeTemplate t;
0054     t.name = i18n("Text");
0055     t.iconName = koIconName("x-shape-text");
0056     t.toolTip = i18n("Text Shape");
0057     KoProperties *props = new KoProperties();
0058     t.properties = props;
0059     props->setProperty("demo", true);
0060     addTemplate(t);
0061 }
0062 
0063 KoShape *TextShapeFactory::createDefaultShape(KoDocumentResourceManager *documentResources) const
0064 {
0065     KoInlineTextObjectManager *manager = 0;
0066     KoTextRangeManager *locationManager = 0;
0067     if (documentResources && documentResources->hasResource(KoText::InlineTextObjectManager)) {
0068         QVariant variant = documentResources->resource(KoText::InlineTextObjectManager);
0069         if (variant.isValid()) {
0070             manager = variant.value<KoInlineTextObjectManager *>();
0071         }
0072     }
0073     if (documentResources && documentResources->hasResource(KoText::TextRangeManager)) {
0074         QVariant variant = documentResources->resource(KoText::TextRangeManager);
0075         if (variant.isValid()) {
0076             locationManager = variant.value<KoTextRangeManager *>();
0077         }
0078     }
0079     if (!manager) {
0080         manager = new KoInlineTextObjectManager();
0081     }
0082     if (!locationManager) {
0083         locationManager = new KoTextRangeManager();
0084     }
0085     TextShape *text = new TextShape(manager, locationManager);
0086     if (documentResources) {
0087         KoTextDocument document(text->textShapeData()->document());
0088 
0089         if (documentResources->hasResource(KoText::StyleManager)) {
0090             KoStyleManager *styleManager = documentResources->resource(KoText::StyleManager).value<KoStyleManager*>();
0091             document.setStyleManager(styleManager);
0092         }
0093 
0094         // this is needed so the shape can reinitialize itself with the stylemanager
0095         text->textShapeData()->setDocument(text->textShapeData()->document(), true);
0096 
0097         document.setUndoStack(documentResources->undoStack());
0098 
0099         if (documentResources->hasResource(KoText::PageProvider)) {
0100             KoPageProvider *pp = static_cast<KoPageProvider *>(documentResources->resource(KoText::PageProvider).value<void*>());
0101             text->setPageProvider(pp);
0102         }
0103         if (documentResources->hasResource(KoText::ChangeTracker)) {
0104             KoChangeTracker *changeTracker = documentResources->resource(KoText::ChangeTracker).value<KoChangeTracker*>();
0105             document.setChangeTracker(changeTracker);
0106         }
0107 
0108         document.setShapeController(documentResources->shapeController());
0109 
0110         //update the resources of the document
0111         text->updateDocumentData();
0112 
0113         text->setImageCollection(documentResources->imageCollection());
0114     }
0115 
0116     return text;
0117 }
0118 
0119 KoShape *TextShapeFactory::createShape(const KoProperties */*params*/, KoDocumentResourceManager *documentResources) const
0120 {
0121     TextShape *shape = static_cast<TextShape*>(createDefaultShape(documentResources));
0122     shape->textShapeData()->document()->setUndoRedoEnabled(false);
0123     shape->setSize(QSizeF(300, 200));
0124     /*
0125     QString text("text");
0126     if (params->contains(text)) {
0127         KoTextShapeData *shapeData = qobject_cast<KoTextShapeData*>(shape->userData());
0128     }
0129     */
0130     if (documentResources) {
0131         shape->setImageCollection(documentResources->imageCollection());
0132     }
0133     shape->textShapeData()->document()->setUndoRedoEnabled(true);
0134     return shape;
0135 }
0136 
0137 bool TextShapeFactory::supports(const KoXmlElement & e, KoShapeLoadingContext &context) const
0138 {
0139     Q_UNUSED(context);
0140     return (e.localName() == "text-box" && e.namespaceURI() == KoXmlNS::draw) ||
0141         (e.localName() == "table" && e.namespaceURI() == KoXmlNS::table);
0142 }
0143 
0144 void TextShapeFactory::newDocumentResourceManager(KoDocumentResourceManager *manager) const
0145 {
0146     QVariant variant;
0147     variant.setValue<KoInlineTextObjectManager*>(new KoInlineTextObjectManager(manager));
0148     manager->setResource(KoText::InlineTextObjectManager, variant);
0149 
0150     variant.setValue<KoTextRangeManager *>(new KoTextRangeManager());
0151     manager->setResource(KoText::TextRangeManager, variant);
0152 
0153     if (!manager->hasResource(KoDocumentResourceManager::UndoStack)) {
0154 //        qWarning() << "No KUndo2Stack found in the document resource manager, creating a new one";
0155         manager->setUndoStack(new KUndo2Stack(manager));
0156     }
0157     if (!manager->hasResource(KoText::StyleManager)) {
0158         variant.setValue(new KoStyleManager(manager));
0159         manager->setResource(KoText::StyleManager, variant);
0160     }
0161     if (!manager->imageCollection())
0162         manager->setImageCollection(new KoImageCollection(manager));
0163 }