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

0001 /* This file is part of the KDE project
0002    Copyright (C) 2010 KO GmbH <ben.martin@kogmbh.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 "KoRdfSemanticItemViewSite.h"
0021 #include "KoDocumentRdf.h"
0022 // main
0023 #include <KoCanvasBase.h>
0024 #include <KoCanvasResourceManager.h>
0025 #include <KoToolProxy.h>
0026 #include <KoText.h>
0027 #include <KoTextEditor.h>
0028 // KF5
0029 #include <kdebug.h>
0030 
0031 using namespace Soprano;
0032 
0033 class KoRdfSemanticItemViewSitePrivate
0034 {
0035 public:
0036     QString m_xmlid;
0037     hKoRdfSemanticItem m_semItem;
0038     KoRdfSemanticItemViewSitePrivate(hKoRdfSemanticItem si, const QString &xmlid)
0039         : m_xmlid(xmlid)
0040         , m_semItem(si)
0041         {
0042         }
0043 };
0044 
0045 
0046 KoRdfSemanticItemViewSite::KoRdfSemanticItemViewSite(hKoRdfSemanticItem si, const QString &xmlid)
0047     :
0048     d (new KoRdfSemanticItemViewSitePrivate(si,xmlid))
0049 {
0050 }
0051 
0052 KoRdfSemanticItemViewSite::~KoRdfSemanticItemViewSite()
0053 {
0054     delete d;
0055 }
0056 
0057 Soprano::Node KoRdfSemanticItemViewSite::linkingSubject() const
0058 {
0059     const KoDocumentRdf *documentRdf = d->m_semItem->documentRdf();
0060     QSharedPointer<Soprano::Model> m = documentRdf->model();
0061     Node pred(QUrl("http://calligra.org/rdf/site/package/common#idref"));
0062     Node obj = Node::createLiteralNode(d->m_xmlid);
0063     Node context = documentRdf->manifestRdfNode();
0064     // try to find it if it already exists
0065     StatementIterator it = m->listStatements(Node(), pred, obj, context);
0066     QList<Statement> allStatements = it.allElements();
0067     foreach (Soprano::Statement s, allStatements) {
0068         return s.subject();
0069     }
0070     Node ret = m->createBlankNode();
0071     m->addStatement(ret, pred, obj, context);
0072     return ret;
0073 }
0074 
0075 QString KoRdfSemanticItemViewSite::getProperty(const QString &prop, const QString &defval) const
0076 {
0077     Soprano::Node ls = linkingSubject();
0078     QString fqprop = "http://calligra.org/rdf/site#" + prop;
0079     const KoDocumentRdf *rdf = d->m_semItem->documentRdf();
0080     QSharedPointer<Soprano::Model> m = rdf->model();
0081     StatementIterator it = m->listStatements(ls, Node::createResourceNode(QUrl(fqprop)),
0082                                Node(), rdf->manifestRdfNode());
0083     QList<Statement> allStatements = it.allElements();
0084     foreach (Soprano::Statement s, allStatements) {
0085         return s.object().toString();
0086     }
0087     return defval;
0088 }
0089 
0090 void KoRdfSemanticItemViewSite::setProperty(const QString &prop, const QString &v)
0091 {
0092     QString fqprop = "http://calligra.org/rdf/site#" + prop;
0093     const KoDocumentRdf *documentRdf = d->m_semItem->documentRdf();
0094     QSharedPointer<Soprano::Model> m = documentRdf->model();
0095     Soprano::Node ls = linkingSubject();
0096     Soprano::Node pred = Node::createResourceNode(QUrl(fqprop));
0097     m->removeAllStatements(Statement(ls, pred, Node()));
0098     m->addStatement(ls, pred,Node::createLiteralNode(v), documentRdf->manifestRdfNode());
0099 }
0100 
0101 hKoSemanticStylesheet KoRdfSemanticItemViewSite::stylesheet() const
0102 {
0103     QString name = getProperty("stylesheet", "name");
0104     QString type = getProperty("stylesheet-type", KoSemanticStylesheet::stylesheetTypeSystem());
0105     QString uuid = getProperty("stylesheet-uuid", "");
0106     kDebug(30015) << "stylesheet at site, format(), xmlid:" << d->m_xmlid;
0107     kDebug(30015) << " sheet:" << name << " type:" << type;
0108     hKoSemanticStylesheet ret(0);
0109     if (!uuid.isEmpty()) {
0110         ret = d->m_semItem->findStylesheetByUuid(uuid);
0111     }
0112     if (!ret) {
0113         ret = d->m_semItem->findStylesheetByName(type, name);
0114     }
0115     if (!ret) {
0116         // safety first, there will always be a default stylesheet
0117         ret = d->m_semItem->defaultStylesheet();
0118     }
0119     Q_ASSERT(ret);
0120     return ret;
0121 }
0122 
0123 void KoRdfSemanticItemViewSite::applyStylesheet(KoTextEditor *editor, hKoSemanticStylesheet ss)
0124 {
0125     // Save the stylesheet property and cause a reflow.
0126     kDebug(30015) << "apply stylesheet at site. format(), xmlid:" << d->m_xmlid << " sheet:" << ss->name();
0127     setStylesheetWithoutReflow(ss);
0128     reflowUsingCurrentStylesheet(editor);
0129 }
0130 
0131 void KoRdfSemanticItemViewSite::disassociateStylesheet()
0132 {
0133     kDebug(30015) << "stylesheet at site. xmlid:" << d->m_xmlid;
0134     setProperty("stylesheet", "");
0135     setProperty("stylesheet-type", "");
0136     setProperty("stylesheet-uuid", "");
0137 }
0138 
0139 void KoRdfSemanticItemViewSite::setStylesheetWithoutReflow(hKoSemanticStylesheet ss)
0140 {
0141     // Save the stylesheet property
0142     kDebug(30015) << "apply stylesheet at site. format(), xmlid:" << d->m_xmlid << " sheet:" << ss->name();
0143     setProperty("stylesheet", ss->name());
0144     setProperty("stylesheet-type", ss->type());
0145     setProperty("stylesheet-uuid", ss->uuid());
0146 }
0147 
0148 void KoRdfSemanticItemViewSite::reflowUsingCurrentStylesheet(KoTextEditor *editor)
0149 {
0150     hKoSemanticStylesheet ss = stylesheet();
0151     if (ss) {
0152         ss->format(d->m_semItem, editor, d->m_xmlid);
0153     }
0154 }
0155 
0156 void KoRdfSemanticItemViewSite::selectRange(KoCanvasResourceManager *provider, int startpos, int endpos)
0157 {
0158     kDebug(30015) << " startpos:" << startpos << " endpos:" << endpos;
0159     if (endpos > startpos) {
0160         provider->setResource(KoText::CurrentTextPosition, startpos);
0161         provider->setResource(KoText::CurrentTextAnchor, endpos + 1);
0162         provider->clearResource(KoText::SelectedTextPosition);
0163         provider->clearResource(KoText::SelectedTextAnchor);
0164     } else {
0165         provider->setResource(KoText::CurrentTextPosition, startpos);
0166     }
0167 }
0168 
0169 void KoRdfSemanticItemViewSite::select(KoCanvasBase *host)
0170 {
0171     Q_ASSERT(d->m_semItem);
0172     Q_ASSERT(d->m_semItem->documentRdf());
0173     Q_ASSERT(host);
0174     KoTextEditor *editor = KoTextEditor::getTextEditorFromCanvas(host);
0175     KoCanvasResourceManager *provider = host->resourceManager();
0176     const KoDocumentRdf *rdf = d->m_semItem->documentRdf();
0177     QPair<int, int> p = rdf->findExtent(d->m_xmlid);
0178     int startpos = p.first;
0179     int endpos = p.second + 1;
0180     if (!endpos) {
0181         kDebug(30015) << "No end point found for semantic item:" << d->m_semItem->name();
0182         kDebug(30015) << "xmlid:" << d->m_xmlid;
0183         return;
0184     }
0185     kDebug(30015) << "xmlid:" << d->m_xmlid;
0186     kDebug(30015) << "start:" << startpos << " endpos:" << endpos;
0187     selectRange(provider, startpos, endpos);
0188     kDebug(30015) << "selected text:" << editor->selectedText();
0189 }