1 /* This file is part of the KDE project
2    Copyright (C) 2010 KO GmbH <ben.martin@kogmbh.com>
3 
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public
6    License as published by the Free Software Foundation; either
7    version 2 of the License, or (at your option) any later version.
8 
9    This library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Library General Public License for more details.
13 
14    You should have received a copy of the GNU Library General Public License
15    along with this library; see the file COPYING.LIB.  If not, write to
16    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18 */
19 
20 #include "KoRdfSemanticTreeWidgetItem.h"
21 
22 #include "KoDocumentRdf.h"
23 // main
24 #include <KoCanvasBase.h>
25 #include <KoToolProxy.h>
26 #include <KoDocumentResourceManager.h>
27 #include <KoTextEditor.h>
28 // KF5
29 #include <kactionmenu.h>
30 #include <kpagedialog.h>
31 #include <kdebug.h>
32 #include <klocalizedstring.h>
33 // Qt
34 #include <QVBoxLayout>
35 
36 using namespace Soprano;
37 
38 class RdfSemanticTreeWidgetApplyStylesheet : public RdfSemanticTreeWidgetAction
39 {
40     hKoRdfSemanticItem si;
41     hKoSemanticStylesheet ss;
42 
43 public:
44 
45     RdfSemanticTreeWidgetApplyStylesheet(QWidget *parent, KoCanvasBase *canvas,
46                                          const QString &name,
47                                          hKoRdfSemanticItem si,
48                                          hKoSemanticStylesheet ss = hKoSemanticStylesheet(0));
49     virtual ~RdfSemanticTreeWidgetApplyStylesheet();
50     virtual void activated();
51 };
52 
RdfSemanticTreeWidgetApplyStylesheet(QWidget * parent,KoCanvasBase * canvas,const QString & name,hKoRdfSemanticItem si,hKoSemanticStylesheet ss)53 RdfSemanticTreeWidgetApplyStylesheet::RdfSemanticTreeWidgetApplyStylesheet(QWidget *parent,
54         KoCanvasBase *canvas, const QString &name, hKoRdfSemanticItem si, hKoSemanticStylesheet ss )
55             : RdfSemanticTreeWidgetAction(parent, canvas, name)
56             , si(si)
57             , ss(ss)
58 {
59 }
60 
~RdfSemanticTreeWidgetApplyStylesheet()61 RdfSemanticTreeWidgetApplyStylesheet::~RdfSemanticTreeWidgetApplyStylesheet()
62 {
63 }
64 
activated()65 void RdfSemanticTreeWidgetApplyStylesheet::activated()
66 {
67     kDebug(30015) << "apply selected stylesheet for semantic item...";
68     KoTextEditor *editor = KoTextEditor::getTextEditorFromCanvas(m_canvas);
69     const KoDocumentRdf *rdf = si->documentRdf();
70     QString xmlid = rdf->findXmlId(editor);
71     kDebug(30015) << "semItem:" << si->name() << "xmlid:" << xmlid;
72     KoRdfSemanticItemViewSite vs(si, xmlid);
73     if (ss) {
74         kDebug(30015) << "apply stylesheet, format(), sheet:" << ss->name()
75                 << " xmlid:" << xmlid;
76         vs.applyStylesheet(editor, ss);
77     } else {
78         vs.disassociateStylesheet();
79     }
80 }
81 
82 
KoRdfSemanticTreeWidgetItem(QTreeWidgetItem * parent)83 KoRdfSemanticTreeWidgetItem::KoRdfSemanticTreeWidgetItem(QTreeWidgetItem *parent)
84     : QTreeWidgetItem(parent)
85 {
86 }
87 
~KoRdfSemanticTreeWidgetItem()88 KoRdfSemanticTreeWidgetItem::~KoRdfSemanticTreeWidgetItem()
89 {
90 }
91 
createAction(QWidget * parent,KoCanvasBase * host,const QString & text)92 QAction *KoRdfSemanticTreeWidgetItem::createAction(QWidget *parent, KoCanvasBase *host, const QString  &text)
93 {
94     return new RdfSemanticTreeWidgetAction(parent, host, text);
95 }
96 
addApplyStylesheetActions(QWidget * parent,QList<QAction * > & actions,KoCanvasBase * host)97 void KoRdfSemanticTreeWidgetItem::addApplyStylesheetActions(QWidget *parent,
98         QList<QAction *> &actions, KoCanvasBase *host)
99 {
100     if (!host) {
101         return;
102     }
103     KoTextEditor *editor = KoTextEditor::getTextEditorFromCanvas(host);
104     kDebug(30015) << " semanticItem:" << semanticItem();
105     kDebug(30015) << " semanticItem.name:" << semanticItem()->name();
106     if (!editor) {
107         return;
108     }
109     QString xmlid = semanticItem()->documentRdf()->findXmlId(editor);
110     if (!xmlid.size()) {
111         return;
112     }
113     kDebug(30015) << "xmlid:" << xmlid;
114     KActionMenu *topMenu = new KActionMenu(i18n("Apply Stylesheet"), parent);
115     actions.append(topMenu);
116     KActionMenu *subMenu = new KActionMenu(i18n("System"), topMenu);
117     topMenu->addAction(subMenu);
118     foreach (hKoSemanticStylesheet ss, semanticItem()->stylesheets()) {
119         kDebug(30015) << "format(), sheet:" << ss->name() << " xmlid:" << xmlid;
120         QAction * action = new RdfSemanticTreeWidgetApplyStylesheet(parent,
121                                                                    host, ss->name(),
122                                                                    semanticItem(), ss);
123         subMenu->addAction(action);
124     }
125     subMenu = new KActionMenu(i18n("User"), topMenu);
126     topMenu->addAction(subMenu);
127     foreach (hKoSemanticStylesheet ss, semanticItem()->userStylesheets()) {
128         kDebug(30015) << "format(), sheet:" << ss->name() << " xmlid:" << xmlid;
129         QAction *action = new RdfSemanticTreeWidgetApplyStylesheet(parent,
130                                                                    host, ss->name(),
131                                                                    semanticItem(), ss);
132         subMenu->addAction(action);
133     }
134     // add reapply current sheet option
135     topMenu->addSeparator();
136     KoRdfSemanticItemViewSite vs(semanticItem(), xmlid);
137     if (hKoSemanticStylesheet ss = vs.stylesheet()) {
138         QAction *action = new RdfSemanticTreeWidgetApplyStylesheet(parent,
139                                                                    host, i18n("Reapply Current"),
140                                                                    semanticItem(), ss);
141         topMenu->addAction(action);
142     }
143     QAction *action = new RdfSemanticTreeWidgetApplyStylesheet(parent,
144                                                                host, i18n("Disassociate"),
145                                                                semanticItem(), hKoSemanticStylesheet(0));
146     topMenu->addAction(action);
147 }
148 
actions(QWidget * parent,KoCanvasBase * host)149 QList<QAction *> KoRdfSemanticTreeWidgetItem::actions(QWidget *parent, KoCanvasBase *host)
150 {
151     Q_UNUSED(parent);
152     Q_UNUSED(host);
153     return QList<QAction *>();
154 }
155 
insert(KoCanvasBase * host)156 void KoRdfSemanticTreeWidgetItem::insert(KoCanvasBase *host)
157 {
158     Q_UNUSED(host);
159     kDebug(30015) << "KoRdfSemanticTreeWidgetItem::insert";
160 }
161 
edit()162 void KoRdfSemanticTreeWidgetItem::edit()
163 {
164     QString caption = i18n("Edit %1",uIObjectName());
165     QWidget *widget = new QWidget();
166     QVBoxLayout *lay = new QVBoxLayout(widget);
167     widget->setLayout(lay);
168     lay->setMargin(0);
169     QWidget *w = semanticItem()->createEditor(widget);
170     lay->addWidget(w);
171     KPageDialog dialog;
172     dialog.setWindowTitle(caption);
173     dialog.addPage(widget, QString());
174     if (dialog.exec() == KPageDialog::Accepted) {
175         kDebug(30015) << "KoRdfSemanticTreeWidgetItem::edit() accepted...";
176         semanticItem()->updateFromEditorData();
177     }
178 }
179