1 /* This file is part of the KDE project
2  * Copyright (C) 2006-2007,2009,2010 Thomas Zander <zander@kde.org>
3  * Copyright (C) 2007 Jan Hambrecht <jaham@gmx.net>
4  * Copyright (C) 2008 Thorsten Zachmann <zachmann@kde.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB.  If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 #include "TextShapeFactory.h"
22 #include "TextShape.h"
23 
24 #include <KoProperties.h>
25 #include <KoShape.h>
26 #include <KoTextDocument.h>
27 #include <KoTextShapeData.h>
28 #include <KoXmlNS.h>
29 #include <KoStyleManager.h>
30 #include <KoDocumentResourceManager.h>
31 #include <KoInlineTextObjectManager.h>
32 #include <KoTextRangeManager.h>
33 #include <changetracker/KoChangeTracker.h>
34 #include <KoImageCollection.h>
35 #include <KoShapeLoadingContext.h>
36 
37 #include <KoIcon.h>
38 
39 #include <klocalizedstring.h>
40 #include <kundo2stack.h>
41 #include <QTextCursor>
42 
TextShapeFactory()43 TextShapeFactory::TextShapeFactory()
44     : KoShapeFactoryBase(TextShape_SHAPEID, i18n("Text"))
45 {
46     setToolTip(i18n("A shape that shows text"));
47     QList<QPair<QString, QStringList> > odfElements;
48     odfElements.append(QPair<QString, QStringList>(KoXmlNS::draw, QStringList("text-box")));
49     odfElements.append(QPair<QString, QStringList>(KoXmlNS::table, QStringList("table")));
50     setXmlElements(odfElements);
51     setLoadingPriority(1);
52 
53     KoShapeTemplate t;
54     t.name = i18n("Text");
55     t.iconName = koIconName("x-shape-text");
56     t.toolTip = i18n("Text Shape");
57     KoProperties *props = new KoProperties();
58     t.properties = props;
59     props->setProperty("demo", true);
60     addTemplate(t);
61 }
62 
createDefaultShape(KoDocumentResourceManager * documentResources) const63 KoShape *TextShapeFactory::createDefaultShape(KoDocumentResourceManager *documentResources) const
64 {
65     KoInlineTextObjectManager *manager = 0;
66     KoTextRangeManager *locationManager = 0;
67     if (documentResources && documentResources->hasResource(KoText::InlineTextObjectManager)) {
68         QVariant variant = documentResources->resource(KoText::InlineTextObjectManager);
69         if (variant.isValid()) {
70             manager = variant.value<KoInlineTextObjectManager *>();
71         }
72     }
73     if (documentResources && documentResources->hasResource(KoText::TextRangeManager)) {
74         QVariant variant = documentResources->resource(KoText::TextRangeManager);
75         if (variant.isValid()) {
76             locationManager = variant.value<KoTextRangeManager *>();
77         }
78     }
79     if (!manager) {
80         manager = new KoInlineTextObjectManager();
81     }
82     if (!locationManager) {
83         locationManager = new KoTextRangeManager();
84     }
85     TextShape *text = new TextShape(manager, locationManager);
86     if (documentResources) {
87         KoTextDocument document(text->textShapeData()->document());
88 
89         if (documentResources->hasResource(KoText::StyleManager)) {
90             KoStyleManager *styleManager = documentResources->resource(KoText::StyleManager).value<KoStyleManager *>();
91             document.setStyleManager(styleManager);
92         }
93 
94         // this is needed so the shape can reinitialize itself with the stylemanager
95         text->textShapeData()->setDocument(text->textShapeData()->document());
96 
97         document.setUndoStack(documentResources->undoStack());
98 
99         if (documentResources->hasResource(KoText::PageProvider)) {
100             KoPageProvider *pp = static_cast<KoPageProvider *>(documentResources->resource(KoText::PageProvider).value<void *>());
101             text->setPageProvider(pp);
102         }
103         if (documentResources->hasResource(KoText::ChangeTracker)) {
104             KoChangeTracker *changeTracker = documentResources->resource(KoText::ChangeTracker).value<KoChangeTracker *>();
105             document.setChangeTracker(changeTracker);
106         }
107 
108         document.setShapeController(documentResources->globalShapeController());
109 
110         //update the resources of the document
111         text->updateDocumentData();
112 
113         text->setImageCollection(documentResources->imageCollection());
114     }
115 
116     return text;
117 }
118 
createShape(const KoProperties *,KoDocumentResourceManager * documentResources) const119 KoShape *TextShapeFactory::createShape(const KoProperties */*params*/, KoDocumentResourceManager *documentResources) const
120 {
121     TextShape *shape = static_cast<TextShape *>(createDefaultShape(documentResources));
122     shape->textShapeData()->document()->setUndoRedoEnabled(false);
123     shape->setSize(QSizeF(300, 200));
124     /*
125     QString text("text");
126     if (params->contains(text)) {
127         KoTextShapeData *shapeData = qobject_cast<KoTextShapeData*>(shape->userData());
128     }
129     */
130     if (documentResources) {
131         shape->setImageCollection(documentResources->imageCollection());
132     }
133     shape->textShapeData()->document()->setUndoRedoEnabled(true);
134     return shape;
135 }
136 
supports(const KoXmlElement & e,KoShapeLoadingContext & context) const137 bool TextShapeFactory::supports(const KoXmlElement &e, KoShapeLoadingContext &context) const
138 {
139     Q_UNUSED(context);
140     return (e.localName() == "text-box" && e.namespaceURI() == KoXmlNS::draw) ||
141            (e.localName() == "table" && e.namespaceURI() == KoXmlNS::table);
142 }
143 
newDocumentResourceManager(KoDocumentResourceManager * manager) const144 void TextShapeFactory::newDocumentResourceManager(KoDocumentResourceManager *manager) const
145 {
146     QVariant variant;
147     variant.setValue<KoInlineTextObjectManager *>(new KoInlineTextObjectManager(manager));
148     manager->setResource(KoText::InlineTextObjectManager, variant);
149 
150     variant.setValue<KoTextRangeManager *>(new KoTextRangeManager());
151     manager->setResource(KoText::TextRangeManager, variant);
152 
153     if (!manager->hasResource(KoDocumentResourceManager::UndoStack)) {
154 //        qWarning() << "No KUndo2Stack found in the document resource manager, creating a new one";
155         manager->setUndoStack(new KUndo2Stack(manager));
156     }
157     if (!manager->hasResource(KoText::StyleManager)) {
158         variant.setValue(new KoStyleManager(manager));
159         manager->setResource(KoText::StyleManager, variant);
160     }
161     if (!manager->imageCollection()) {
162         manager->setImageCollection(new KoImageCollection(manager));
163     }
164 }
165