1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
/* This file is part of the KDE project
SPDX-FileCopyrightText: 2010 KO GmbH <ben.martin@kogmbh.com>
SPDX-FileCopyrightText: 2012 C. Boemann <cbo@boemann.dk>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "KoTextInlineRdf.h"
// lib
#include <KoAnnotation.h>
#include <KoBookmark.h>
#include <KoTextDocument.h>
#include <KoTextEditor.h>
#include <KoTextMeta.h>
#include <opendocument/KoTextSharedSavingData.h>
#include <styles/KoCharacterStyle.h>
// komain
#include <KoElementReference.h>
#include <KoShapeSavingContext.h>
#include <KoXmlNS.h>
#include <KoXmlWriter.h>
// TODO: the "errorText" define in TextDebug.h results in a build error, if used as second include here
// find out why and perhaps change the rather generic "errorText" & Co defines to something less conflict-prone
#include "TextDebug.h"
// Qt
#include <QTextCursor>
#include <QTextDocument>
#include <QTextTableCell>
#ifdef SHOULD_BUILD_RDF
#include <Soprano/Soprano>
enum Type {
EmptyNode = Soprano::Node::EmptyNode,
ResourceNode = Soprano::Node::ResourceNode,
LiteralNode = Soprano::Node::LiteralNode,
BlankNode = Soprano::Node::BlankNode
};
#else
enum Type {
EmptyNode,
ResourceNode,
LiteralNode,
BlankNode
};
#endif
class Q_DECL_HIDDEN KoTextInlineRdf::Private
{
public:
Private(const QTextDocument *doc, const QTextBlock &b)
: block(b)
, document(doc)
{
isObjectAttributeUsed = false;
sopranoObjectType = LiteralNode;
}
Private(const QTextDocument *doc, KoBookmark *b)
: document(doc)
, bookmark(b)
{
isObjectAttributeUsed = false;
sopranoObjectType = LiteralNode;
}
Private(const QTextDocument *doc, KoAnnotation *b)
: document(doc)
, annotation(b)
{
isObjectAttributeUsed = false;
sopranoObjectType = LiteralNode;
}
Private(const QTextDocument *doc, KoTextMeta *b)
: document(doc)
, kotextmeta(b)
{
isObjectAttributeUsed = false;
sopranoObjectType = LiteralNode;
}
Private(const QTextDocument *doc, const QTextTableCell &c)
: document(doc)
, cell(c)
{
isObjectAttributeUsed = false;
sopranoObjectType = LiteralNode;
}
Private(const QTextDocument *doc, KoSection *s)
: document(doc)
, section(s)
{
isObjectAttributeUsed = false;
sopranoObjectType = LiteralNode;
}
QString id; // original xml:id
// FIXME: design like this seems inapropriate, maybe
// making Interface from KoTextInlineRdf will be better.
// Just my thoughts.
// where we might get the object value from
QTextBlock block;
// or document and one of bookmark, annotation, kotextmeta, ...
QPointer<const QTextDocument> document;
QPointer<KoBookmark> bookmark;
QPointer<KoAnnotation> annotation;
QPointer<KoTextMeta> kotextmeta;
KoSection *section;
QTextTableCell cell;
QString subject;
QString predicate;
int sopranoObjectType;
QString dt;
// if the content="" attribute was used,
// then isObjectAttributeUsed=1 and object=content attribute value.
QString object;
bool isObjectAttributeUsed;
};
KoTextInlineRdf::KoTextInlineRdf(const QTextDocument *doc, const QTextBlock &b)
: QObject(const_cast<QTextDocument *>(doc))
, d(new Private(doc, b))
{
}
KoTextInlineRdf::KoTextInlineRdf(const QTextDocument *doc, KoBookmark *b)
: QObject(const_cast<QTextDocument *>(doc))
, d(new Private(doc, b))
{
}
KoTextInlineRdf::KoTextInlineRdf(const QTextDocument *doc, KoAnnotation *b)
: QObject(const_cast<QTextDocument *>(doc))
, d(new Private(doc, b))
{
}
KoTextInlineRdf::KoTextInlineRdf(const QTextDocument *doc, KoTextMeta *b)
: QObject(const_cast<QTextDocument *>(doc))
, d(new Private(doc, b))
{
}
KoTextInlineRdf::KoTextInlineRdf(const QTextDocument *doc, const QTextTableCell &b)
: QObject(const_cast<QTextDocument *>(doc))
, d(new Private(doc, b))
{
}
KoTextInlineRdf::KoTextInlineRdf(const QTextDocument *doc, KoSection *s)
: QObject(const_cast<QTextDocument *>(doc))
, d(new Private(doc, s))
{
}
KoTextInlineRdf::~KoTextInlineRdf()
{
debugText << " this:" << (void *)this;
delete d;
}
bool KoTextInlineRdf::loadOdf(const KoXmlElement &e)
{
d->id = e.attribute("id", QString());
d->subject = e.attributeNS(KoXmlNS::xhtml, "about");
d->predicate = e.attributeNS(KoXmlNS::xhtml, "property");
d->dt = e.attributeNS(KoXmlNS::xhtml, "datatype");
QString content = e.attributeNS(KoXmlNS::xhtml, "content");
//
// Content / triple object explicitly set through an attribute
//
if (e.hasAttributeNS(KoXmlNS::xhtml, "content")) {
d->isObjectAttributeUsed = true;
d->object = content;
}
return true;
}
bool KoTextInlineRdf::saveOdf(KoShapeSavingContext &context, KoXmlWriter *writer, KoElementReference id) const
{
debugText << " this:" << (void *)this << " xmlid:" << d->id << "passed id" << id.toString();
QString oldID = d->id;
if (!id.isValid()) {
id = KoElementReference();
}
QString newID = id.toString();
if (KoTextSharedSavingData *sharedData = dynamic_cast<KoTextSharedSavingData *>(context.sharedData(KOTEXT_SHARED_SAVING_ID))) {
sharedData->addRdfIdMapping(oldID, newID);
}
debugText << "oldID:" << oldID << " newID:" << newID;
writer->addAttribute("xml:id", newID);
if (!d->subject.isEmpty()) {
writer->addAttribute("xhtml:about", d->subject);
}
if (!d->predicate.isEmpty()) {
writer->addAttribute("xhtml:property", d->predicate);
}
if (!d->dt.isEmpty()) {
writer->addAttribute("xhtml:datatype", d->dt);
}
if (d->isObjectAttributeUsed) {
writer->addAttribute("xhtml:content", d->object);
}
debugText << "done..";
return true;
}
QString KoTextInlineRdf::createXmlId()
{
KoElementReference ref;
return ref.toString();
}
QString KoTextInlineRdf::subject() const
{
return d->subject;
}
QString KoTextInlineRdf::predicate() const
{
return d->predicate;
}
QPair<int, int> KoTextInlineRdf::findExtent() const
{
if (d->bookmark && d->document) {
return QPair<int, int>(d->bookmark.data()->rangeStart(), d->bookmark.data()->rangeEnd());
}
if (d->annotation && d->document) {
return QPair<int, int>(d->annotation.data()->rangeStart(), d->annotation.data()->rangeEnd());
}
// FIXME: We probably have to do something with endAnnotation()
// too, but I don't know exactly what...
if (d->kotextmeta && d->document) {
KoTextMeta *e = d->kotextmeta.data()->endBookmark();
if (!e) {
return QPair<int, int>(0, 0);
}
// debugText << "(Semantic)meta... start:" << d->kotextmeta.data()->position() << " end:" << e->position();
return QPair<int, int>(d->kotextmeta.data()->position(), e->position());
}
if (d->cell.isValid() && d->document) {
QTextCursor b = d->cell.firstCursorPosition();
QTextCursor e = d->cell.lastCursorPosition();
return QPair<int, int>(b.position(), e.position());
}
if (d->section) {
return d->section->bounds();
}
return QPair<int, int>(0, 0);
}
QString KoTextInlineRdf::object() const
{
if (d->isObjectAttributeUsed) {
return d->object;
}
KoTextDocument textDocument(d->document.data());
if (d->bookmark && d->document) {
QString ret = d->bookmark.data()->text();
return ret.remove(QChar::ObjectReplacementCharacter);
} else if (d->kotextmeta && d->document) {
// FIXME: Need to do something with endAnnotation?
KoTextMeta *e = d->kotextmeta.data()->endBookmark();
if (!e) {
debugText << "Broken KoTextMeta, no end tag found!";
return QString();
} else {
KoTextEditor *editor = textDocument.textEditor();
editor->setPosition(d->kotextmeta.data()->position(), QTextCursor::MoveAnchor);
editor->setPosition(e->position(), QTextCursor::KeepAnchor);
QString ret = editor->selectedText();
return ret.remove(QChar::ObjectReplacementCharacter);
}
} else if (d->cell.isValid() && d->document) {
QTextCursor b = d->cell.firstCursorPosition();
b.setPosition(d->cell.lastCursorPosition().position(), QTextCursor::KeepAnchor);
QString ret = b.selectedText();
return ret.remove(QChar::ObjectReplacementCharacter);
}
return d->block.text();
}
int KoTextInlineRdf::sopranoObjectType() const
{
return d->sopranoObjectType;
}
QString KoTextInlineRdf::xmlId() const
{
return d->id;
}
void KoTextInlineRdf::setXmlId(const QString &id)
{
d->id = id;
}
KoTextInlineRdf *KoTextInlineRdf::tryToGetInlineRdf(const QTextFormat &tf)
{
if (!tf.hasProperty(KoCharacterStyle::InlineRdf)) {
return nullptr;
}
QVariant v = tf.property(KoCharacterStyle::InlineRdf);
return v.value<KoTextInlineRdf *>();
}
KoTextInlineRdf *KoTextInlineRdf::tryToGetInlineRdf(QTextCursor &cursor)
{
QTextCharFormat cf = cursor.charFormat();
if (!cf.hasProperty(KoCharacterStyle::InlineRdf)) {
return nullptr;
}
QVariant v = cf.property(KoCharacterStyle::InlineRdf);
return v.value<KoTextInlineRdf *>();
}
KoTextInlineRdf *KoTextInlineRdf::tryToGetInlineRdf(KoTextEditor *handler)
{
QTextCharFormat cf = handler->charFormat();
if (!cf.hasProperty(KoCharacterStyle::InlineRdf)) {
return nullptr;
}
QVariant v = cf.property(KoCharacterStyle::InlineRdf);
return v.value<KoTextInlineRdf *>();
}
void KoTextInlineRdf::attach(KoTextInlineRdf *inlineRdf, QTextCursor &cursor)
{
QTextCharFormat format = cursor.charFormat();
QVariant v = QVariant::fromValue(inlineRdf);
format.setProperty(KoCharacterStyle::InlineRdf, v);
cursor.mergeCharFormat(format);
}
|