Warning, file /office/calligra/libs/rdf/KoSemanticStylesheetsEditor.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 "KoSemanticStylesheetsEditor.h" 0021 0022 #include "ui_KoSemanticStylesheetsEditor.h" 0023 0024 #include "KoRdfSemanticItem.h" 0025 #include "KoRdfSemanticItemRegistry.h" 0026 #include "KoDocumentRdf.h" 0027 0028 #include <kdebug.h> 0029 0030 class Q_DECL_HIDDEN KoSemanticStylesheetsEditor::Private 0031 { 0032 public: 0033 Ui::KoSemanticStylesheetsEditor *m_ui; 0034 KoDocumentRdf *m_rdf; 0035 QTreeWidgetItem *m_systemSheetsParentItem; 0036 QTreeWidgetItem *m_userSheetsParentItem; 0037 QMap<QString, QTreeWidgetItem*> m_systemSheetsItems; 0038 QMap<QString, QTreeWidgetItem*> m_userSheetsItems; 0039 Private() 0040 : m_systemSheetsParentItem(0) 0041 , m_userSheetsParentItem(0) {} 0042 ~Private() { 0043 delete m_ui; 0044 } 0045 }; 0046 0047 enum { 0048 ColName = 0, 0049 ColSemobj = 1, 0050 ColVar = 0, 0051 ColDesc = 1 0052 }; 0053 0054 class KoSemanticStylesheetWidgetItem : public QTreeWidgetItem 0055 { 0056 public: 0057 KoDocumentRdf *m_rdf; 0058 hKoSemanticStylesheet m_ss; 0059 hKoRdfSemanticItem m_si; 0060 KoSemanticStylesheetWidgetItem(KoDocumentRdf *rdf, 0061 hKoSemanticStylesheet ss, 0062 hKoRdfSemanticItem si, 0063 QTreeWidgetItem *parent, 0064 int type = Type) 0065 : QTreeWidgetItem(parent, type) 0066 , m_rdf(rdf) 0067 , m_ss(ss) 0068 , m_si(si) 0069 { 0070 } 0071 KoSemanticStylesheetWidgetItem(KoDocumentRdf *rdf, 0072 hKoRdfSemanticItem si, 0073 QTreeWidgetItem *parent, 0074 int type = Type) 0075 : QTreeWidgetItem(parent, type) 0076 , m_rdf(rdf) 0077 , m_ss(0) 0078 , m_si(si) 0079 { 0080 } 0081 0082 virtual void setData(int column, int role, const QVariant &value) 0083 { 0084 if (m_ss && m_ss->isMutable() && column == ColName) { 0085 QString newName = value.toString(); 0086 kDebug(30015) << "update to value:" << newName; 0087 m_ss->name(newName); 0088 QVariant v = m_ss->name(); 0089 QTreeWidgetItem::setData(column, role, v); 0090 } else { 0091 QTreeWidgetItem::setData(column, role, value); 0092 } 0093 } 0094 }; 0095 0096 0097 KoSemanticStylesheetsEditor::KoSemanticStylesheetsEditor(QWidget *parent, KoDocumentRdf *rdf) 0098 : KoDialog(parent), 0099 d(new Private()) 0100 { 0101 d->m_rdf = rdf; 0102 QWidget *widget = new QWidget(this); 0103 setMainWidget(widget); 0104 d->m_ui = new Ui::KoSemanticStylesheetsEditor(); 0105 d->m_ui->setupUi(widget); 0106 d->m_ui->newStylesheet->setEnabled(false); 0107 d->m_ui->deleteStylesheet->setEnabled(false); 0108 connect(d->m_ui->newStylesheet, SIGNAL(clicked()), 0109 this, SLOT(newStylesheet())); 0110 connect(d->m_ui->deleteStylesheet, SIGNAL(clicked()), 0111 this, SLOT(deleteStylesheet())); 0112 connect(d->m_ui->stylesheets, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)), 0113 this, SLOT(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*))); 0114 d->m_ui->stylesheets->setSelectionBehavior(QAbstractItemView::SelectRows); 0115 d->m_ui->stylesheets->sortItems(ColName, Qt::DescendingOrder); 0116 connect(d->m_ui->definition, SIGNAL(textChanged()), 0117 this, SLOT(definitionChanged())); 0118 // setup system/user parent treewidgets 0119 d->m_systemSheetsParentItem = new QTreeWidgetItem(d->m_ui->stylesheets); 0120 d->m_systemSheetsParentItem->setText(ColName, i18n("System")); 0121 d->m_systemSheetsParentItem->setText(ColSemobj, ""); 0122 d->m_ui->stylesheets->expandItem(d->m_systemSheetsParentItem); 0123 0124 QTreeWidgetItem *treewidget = 0; 0125 treewidget = d->m_systemSheetsParentItem; 0126 const QStringList classNames = KoRdfSemanticItemRegistry::instance()->classNames(); 0127 foreach (const QString &semanticClass, classNames) { 0128 hKoRdfSemanticItem si(static_cast<KoRdfSemanticItem *>( 0129 rdf->createSemanticItem(semanticClass, this).data())); 0130 KoSemanticStylesheetWidgetItem *item = new KoSemanticStylesheetWidgetItem(rdf, si, treewidget); 0131 item->setText(ColName, semanticClass); 0132 d->m_systemSheetsItems[semanticClass] = item; 0133 d->m_ui->stylesheets->expandItem(item); 0134 } 0135 d->m_userSheetsParentItem = new QTreeWidgetItem(d->m_ui->stylesheets); 0136 d->m_userSheetsParentItem->setText(ColName, i18n("User")); 0137 d->m_userSheetsParentItem->setText(ColSemobj, ""); 0138 d->m_ui->stylesheets->expandItem(d->m_userSheetsParentItem); 0139 0140 treewidget = d->m_userSheetsParentItem; 0141 foreach (const QString &semanticClass, classNames) { 0142 hKoRdfSemanticItem si(static_cast<KoRdfSemanticItem *>( 0143 rdf->createSemanticItem(semanticClass, this).data())); 0144 KoSemanticStylesheetWidgetItem* item = new KoSemanticStylesheetWidgetItem(rdf, si, treewidget); 0145 item->setText(ColName, semanticClass); 0146 d->m_userSheetsItems[semanticClass] = item; 0147 d->m_ui->stylesheets->expandItem(item); 0148 } 0149 0150 // initialize stylesheets tree 0151 foreach (const QString &semanticClass, classNames) { 0152 kDebug(30015) << "semanticClass:" << semanticClass; 0153 hKoRdfSemanticItem p(static_cast<KoRdfSemanticItem *>( 0154 rdf->createSemanticItem(semanticClass, this).data())); 0155 setupStylesheetsItems(semanticClass, p, p->stylesheets(), d->m_systemSheetsItems); 0156 setupStylesheetsItems(semanticClass, p, p->userStylesheets(), d->m_userSheetsItems, true); 0157 } 0158 connect(d->m_ui->variables, SIGNAL(itemActivated(QTableWidgetItem*)), 0159 this, SLOT(onVariableActivated(QTableWidgetItem*))); 0160 d->m_ui->variables->horizontalHeader()->setResizeMode(ColVar, QHeaderView::ResizeToContents); 0161 d->m_ui->variables->horizontalHeader()->setResizeMode(ColDesc, QHeaderView::ResizeToContents); 0162 } 0163 0164 KoSemanticStylesheetsEditor::~KoSemanticStylesheetsEditor() 0165 { 0166 } 0167 0168 void KoSemanticStylesheetsEditor::setupStylesheetsItems(const QString& semanticClass, 0169 hKoRdfSemanticItem si, 0170 const QList<hKoSemanticStylesheet> &ssl, 0171 const QMap<QString, QTreeWidgetItem*> &m, 0172 bool editable) 0173 { 0174 foreach (hKoSemanticStylesheet ss, ssl) { 0175 kDebug(30015) << "ss:" << ss->name(); 0176 QTreeWidgetItem *parent = m[semanticClass]; 0177 KoSemanticStylesheetWidgetItem *item = new KoSemanticStylesheetWidgetItem(d->m_rdf, ss, si, parent); 0178 item->setText(ColName, ss->name()); 0179 item->setText(ColSemobj, semanticClass); 0180 if (editable) { 0181 item->setFlags(item->flags() | Qt::ItemIsEditable); 0182 } 0183 } 0184 } 0185 0186 void KoSemanticStylesheetsEditor::slotOk() 0187 { 0188 } 0189 0190 void KoSemanticStylesheetsEditor::maskButtonsDependingOnCurrentItem(QTreeWidgetItem *current) 0191 { 0192 bool newEnabled = true; 0193 bool delEnabled = true; 0194 for (QMap< QString, QTreeWidgetItem* >::iterator iter = d->m_userSheetsItems.begin(); 0195 iter != d->m_userSheetsItems.end(); ++iter) { 0196 if (iter.value() == current) { 0197 delEnabled = false; 0198 } 0199 } 0200 if (d->m_userSheetsParentItem == current) { 0201 newEnabled = false; 0202 delEnabled = false; 0203 } else { 0204 while (current) { 0205 kDebug(30015) << "current:" << current->text(ColName); 0206 if (d->m_systemSheetsParentItem == current) { 0207 newEnabled = false; 0208 delEnabled = false; 0209 break; 0210 } 0211 current = current->parent(); 0212 } 0213 } 0214 d->m_ui->newStylesheet->setEnabled(newEnabled); 0215 d->m_ui->deleteStylesheet->setEnabled(delEnabled); 0216 } 0217 0218 void KoSemanticStylesheetsEditor::definitionChanged() 0219 { 0220 if (KoSemanticStylesheetWidgetItem *ssitem 0221 = dynamic_cast<KoSemanticStylesheetWidgetItem*>(d->m_ui->stylesheets->currentItem())) { 0222 if (!ssitem->m_ss) { 0223 return; 0224 } 0225 QString desc = d->m_ui->definition->toPlainText(); 0226 kDebug(30015) << "ss:" << ssitem->m_ss->name() << " desc:" << desc; 0227 ssitem->m_ss->templateString(desc); 0228 } 0229 } 0230 0231 struct SignalBlockerRAII { 0232 QObject *obj; 0233 bool oldval; 0234 SignalBlockerRAII(QObject *o) 0235 : obj(o) { 0236 oldval = obj->blockSignals(true); 0237 } 0238 ~SignalBlockerRAII() { 0239 obj->blockSignals(oldval); 0240 } 0241 }; 0242 0243 0244 void KoSemanticStylesheetsEditor::currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous) 0245 { 0246 SignalBlockerRAII _blocker1(d->m_ui->definition); 0247 kDebug(30015) << "current:" << current; 0248 kDebug(30015) << "previous:" << previous; 0249 maskButtonsDependingOnCurrentItem(current); 0250 if (previous) { 0251 QString desc = d->m_ui->definition->toPlainText(); 0252 kDebug(30015) << "desc:" << desc; 0253 if (KoSemanticStylesheetWidgetItem *ssitem 0254 = dynamic_cast<KoSemanticStylesheetWidgetItem*>(previous)) { 0255 kDebug(30015) << "ssitem, ss?:" << ssitem->m_ss; 0256 0257 if (ssitem->m_ss) { 0258 ssitem->m_ss->templateString(desc); 0259 } 0260 } 0261 } 0262 if (KoSemanticStylesheetWidgetItem *ssitem 0263 = dynamic_cast<KoSemanticStylesheetWidgetItem*>(current)) { 0264 d->m_ui->definition->setPlainText(QString()); 0265 d->m_ui->variables->clear(); 0266 if (!ssitem->m_ss) { 0267 return; 0268 } 0269 d->m_ui->definition->setPlainText(ssitem->m_ss->templateString()); 0270 // update the list of available variables 0271 QTableWidget *v = d->m_ui->variables; 0272 v->clear(); 0273 int row = 0; 0274 QMap<QString, QString> m; 0275 ssitem->m_si->setupStylesheetReplacementMapping(m); 0276 QMap< QString, QString >::const_iterator mi = m.constBegin(); 0277 QMap< QString, QString >::const_iterator me = m.constEnd(); 0278 for (; mi != me; ++mi) { 0279 QTableWidgetItem *item = 0; 0280 QString varName = mi.key(); 0281 QString desc("t"); 0282 v->setRowCount(row + 1); 0283 item = new QTableWidgetItem(varName); 0284 v->setItem(row, ColVar, item); 0285 item = new QTableWidgetItem(desc); 0286 v->setItem(row, ColDesc, item); 0287 ++row; 0288 } 0289 } 0290 } 0291 0292 void KoSemanticStylesheetsEditor::newStylesheet() 0293 { 0294 kDebug(30015); 0295 if (KoSemanticStylesheetWidgetItem *ssitem 0296 = dynamic_cast<KoSemanticStylesheetWidgetItem*>(d->m_ui->stylesheets->currentItem())) { 0297 if (ssitem->m_ss) { 0298 ssitem = dynamic_cast<KoSemanticStylesheetWidgetItem*>(ssitem->parent()); 0299 if (!ssitem) { 0300 return; 0301 } 0302 } 0303 kDebug(30015) << ssitem; 0304 hKoRdfSemanticItem si = ssitem->m_si; 0305 hKoSemanticStylesheet ss = si->createUserStylesheet( 0306 QString("new stylesheet %1").arg(QDateTime::currentDateTime().toTime_t()), ""); 0307 QTreeWidgetItem *parent = ssitem; 0308 KoSemanticStylesheetWidgetItem* item = 0309 new KoSemanticStylesheetWidgetItem(d->m_rdf, ss, si, parent); 0310 item->setText(ColName, ss->name()); 0311 item->setFlags(item->flags() | Qt::ItemIsEditable); 0312 0313 d->m_ui->stylesheets->setCurrentItem(item); 0314 d->m_ui->stylesheets->scrollToItem(item); 0315 } 0316 } 0317 0318 void KoSemanticStylesheetsEditor::deleteStylesheet() 0319 { 0320 kDebug(30015); 0321 if (KoSemanticStylesheetWidgetItem *ssitem 0322 = dynamic_cast<KoSemanticStylesheetWidgetItem*>(d->m_ui->stylesheets->currentItem())) { 0323 if (!ssitem->m_ss) { 0324 return; 0325 } 0326 hKoSemanticStylesheet sheet = ssitem->m_ss; 0327 ssitem->m_ss = 0; 0328 ssitem->m_si->destroyUserStylesheet(sheet); 0329 ssitem->parent()->removeChild(ssitem); 0330 } 0331 } 0332 0333 void KoSemanticStylesheetsEditor::onVariableActivated(QTableWidgetItem* item) 0334 { 0335 QTableWidgetItem *vitem = d->m_ui->variables->item(item->row(), ColVar); 0336 QString vtext = vitem->text(); 0337 QTextCursor cursor(d->m_ui->definition->textCursor()); 0338 cursor.insertText(vtext); 0339 } 0340 0341 #include <KoSemanticStylesheetsEditor.moc>