Changeset View
Changeset View
Standalone View
Standalone View
src/kdeeduglossary.cpp
Show All 16 Lines | |||||
17 | * Free Software Foundation, Inc., * | 17 | * Free Software Foundation, Inc., * | ||
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * | 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * | ||
19 | ***************************************************************************/ | 19 | ***************************************************************************/ | ||
20 | 20 | | |||
21 | #include "kdeeduglossary.h" | 21 | #include "kdeeduglossary.h" | ||
22 | 22 | | |||
23 | #include <KActionCollection> | 23 | #include <KActionCollection> | ||
24 | #include <KConfigGroup> | 24 | #include <KConfigGroup> | ||
25 | #include <KHTMLPart> | | |||
26 | #include <KHTMLView> | | |||
27 | #include <KLocalizedString> | 25 | #include <KLocalizedString> | ||
28 | #include <KTreeWidgetSearchLine> | 26 | #include <KTreeWidgetSearchLine> | ||
29 | 27 | | |||
30 | #include <QDebug> | 28 | #include <QDebug> | ||
31 | #include <QDialogButtonBox> | 29 | #include <QDialogButtonBox> | ||
32 | #include <QDomDocument> | 30 | #include <QDomDocument> | ||
33 | #include <QEvent> | 31 | #include <QEvent> | ||
34 | #include <QFile> | 32 | #include <QFile> | ||
35 | #include <QHeaderView> | 33 | #include <QHeaderView> | ||
36 | #include <QLabel> | 34 | #include <QLabel> | ||
37 | #include <QLayout> | 35 | #include <QLayout> | ||
38 | #include <QList> | 36 | #include <QList> | ||
39 | #include <QPushButton> | 37 | #include <QPushButton> | ||
40 | #include <QRegExp> | 38 | #include <QRegExp> | ||
41 | #include <QSplitter> | 39 | #include <QSplitter> | ||
42 | #include <QStringList> | 40 | #include <QStringList> | ||
41 | #include <QTextBrowser> | ||||
43 | #include <QTreeWidget> | 42 | #include <QTreeWidget> | ||
44 | #include <QVBoxLayout> | 43 | #include <QVBoxLayout> | ||
45 | 44 | | |||
46 | static const int FirstLetterRole = 0x00b00a00; | 45 | static const int FirstLetterRole = 0x00b00a00; | ||
47 | 46 | | |||
48 | static const int GlossaryTreeItemType = QTreeWidgetItem::UserType + 1; | 47 | static const int GlossaryTreeItemType = QTreeWidgetItem::UserType + 1; | ||
49 | 48 | | |||
50 | class GlossaryTreeItem : public QTreeWidgetItem | 49 | class GlossaryTreeItem : public QTreeWidgetItem | ||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Line(s) | 88 | public: | |||
103 | } | 102 | } | ||
104 | 103 | | |||
105 | void rebuildTree(); | 104 | void rebuildTree(); | ||
106 | QTreeWidgetItem* createItem(const GlossaryInfo& gi) const; | 105 | QTreeWidgetItem* createItem(const GlossaryInfo& gi) const; | ||
107 | QTreeWidgetItem* findTreeWithLetter(const QChar& l, QTreeWidgetItem* item) const; | 106 | QTreeWidgetItem* findTreeWithLetter(const QChar& l, QTreeWidgetItem* item) const; | ||
108 | 107 | | |||
109 | // slots | 108 | // slots | ||
110 | void itemActivated(QTreeWidgetItem * item, int column); | 109 | void itemActivated(QTreeWidgetItem * item, int column); | ||
111 | // The user clicked on a href. Find and display the right item | | |||
112 | void displayItem(const QUrl &url, const KParts::OpenUrlArguments& arguments, | | |||
113 | const KParts::BrowserArguments &browserArguments); | | |||
114 | 110 | | |||
115 | GlossaryDialog *q; | 111 | GlossaryDialog *q; | ||
116 | 112 | | |||
117 | QList< GlossaryInfo > m_glossaries; | 113 | QList< GlossaryInfo > m_glossaries; | ||
118 | 114 | | |||
119 | KHTMLPart *m_htmlpart; | 115 | QTextBrowser *m_htmlpart; | ||
120 | QTreeWidget *m_glosstree; | 116 | QTreeWidget *m_glosstree; | ||
121 | KTreeWidgetSearchLine *m_search; | 117 | KTreeWidgetSearchLine *m_search; | ||
122 | QString m_htmlbasestring; | 118 | QString m_htmlbasestring; | ||
123 | 119 | | |||
124 | KActionCollection* m_actionCollection; | 120 | KActionCollection* m_actionCollection; | ||
125 | }; | 121 | }; | ||
126 | 122 | | |||
127 | 123 | | |||
▲ Show 20 Lines • Show All 109 Lines • ▼ Show 20 Line(s) | 226 | { | |||
237 | 233 | | |||
238 | itemList = itemDocument.elementsByTagName(QStringLiteral("item")); | 234 | itemList = itemDocument.elementsByTagName(QStringLiteral("item")); | ||
239 | 235 | | |||
240 | const uint num = itemList.count(); | 236 | const uint num = itemList.count(); | ||
241 | for (uint i = 0; i < num; ++i) { | 237 | for (uint i = 0; i < num; ++i) { | ||
242 | reflist.clear(); | 238 | reflist.clear(); | ||
243 | GlossaryItem *item = new GlossaryItem(); | 239 | GlossaryItem *item = new GlossaryItem(); | ||
244 | 240 | | |||
245 | itemElement = (const QDomElement&) itemList.item(i).toElement(); | 241 | itemElement = itemList.item(i).toElement(); | ||
246 | 242 | | |||
247 | QDomNode nameNode = itemElement.namedItem(QStringLiteral("name")); | 243 | QDomNode nameNode = itemElement.namedItem(QStringLiteral("name")); | ||
248 | QDomNode descNode = itemElement.namedItem(QStringLiteral("desc")); | 244 | QDomNode descNode = itemElement.namedItem(QStringLiteral("desc")); | ||
249 | 245 | | |||
250 | QString picName = itemElement.namedItem(QStringLiteral("picture")).toElement().text(); | 246 | QString picName = itemElement.namedItem(QStringLiteral("picture")).toElement().text(); | ||
251 | QDomElement refNode = (const QDomElement&) itemElement.namedItem(QStringLiteral("references")).toElement(); | 247 | QDomElement refNode = itemElement.namedItem(QStringLiteral("references")).toElement(); | ||
252 | 248 | | |||
253 | QString desc = i18n(descNode.toElement().text().toUtf8().constData()); | 249 | QString desc = i18n(descNode.toElement().text().toUtf8().constData()); | ||
254 | if (!picName.isEmpty()) { | 250 | if (!picName.isEmpty()) { | ||
255 | desc.prepend("[img]" + picName + "[/img][brclear][br]"); | 251 | desc.prepend("[br][img]" + picName + "[/img][brclear][br]"); | ||
256 | } | 252 | } | ||
257 | 253 | | |||
258 | item->setName(i18n(nameNode.toElement().text().toUtf8().constData())); | 254 | item->setName(i18n(nameNode.toElement().text().toUtf8().constData())); | ||
259 | 255 | | |||
260 | desc = desc.replace(QLatin1String("[b]"), QLatin1String("<b>")); | 256 | desc = desc.replace(QLatin1String("[b]"), QLatin1String("<b>")); | ||
261 | desc = desc.replace(QLatin1String("[/b]"), QLatin1String("</b>")); | 257 | desc = desc.replace(QLatin1String("[/b]"), QLatin1String("</b>")); | ||
262 | desc = desc.replace(QLatin1String("[i]"), QLatin1String("<i>")); | 258 | desc = desc.replace(QLatin1String("[i]"), QLatin1String("<i>")); | ||
263 | desc = desc.replace(QLatin1String("[/i]"), QLatin1String("</i>")); | 259 | desc = desc.replace(QLatin1String("[/i]"), QLatin1String("</i>")); | ||
▲ Show 20 Lines • Show All 80 Lines • ▼ Show 20 Line(s) | 317 | { | |||
344 | d->m_glosstree = new QTreeWidget(vs); | 340 | d->m_glosstree = new QTreeWidget(vs); | ||
345 | d->m_glosstree->setObjectName(QStringLiteral("treeview")); | 341 | d->m_glosstree->setObjectName(QStringLiteral("treeview")); | ||
346 | d->m_glosstree->setHeaderLabel(QStringLiteral("entries")); | 342 | d->m_glosstree->setHeaderLabel(QStringLiteral("entries")); | ||
347 | d->m_glosstree->header()->hide(); | 343 | d->m_glosstree->header()->hide(); | ||
348 | d->m_glosstree->setRootIsDecorated(true); | 344 | d->m_glosstree->setRootIsDecorated(true); | ||
349 | 345 | | |||
350 | d->m_search->addTreeWidget(d->m_glosstree); | 346 | d->m_search->addTreeWidget(d->m_glosstree); | ||
351 | 347 | | |||
352 | d->m_htmlpart = new KHTMLPart(vs); | 348 | d->m_htmlpart = new QTextBrowser(vs); | ||
349 | d->m_htmlpart->setOpenLinks(false); | ||||
353 | 350 | | |||
354 | connect(d->m_htmlpart->browserExtension(), SIGNAL(openUrlRequestDelayed(QUrl,KParts::OpenUrlArguments,KParts::BrowserArguments)), | | |||
355 | this, SLOT(displayItem(QUrl,KParts::OpenUrlArguments,KParts::BrowserArguments))); | | |||
356 | connect(d->m_glosstree, SIGNAL(itemActivated(QTreeWidgetItem*,int)), | 351 | connect(d->m_glosstree, SIGNAL(itemActivated(QTreeWidgetItem*,int)), | ||
357 | this, SLOT(itemActivated(QTreeWidgetItem*,int))); | 352 | this, SLOT(itemActivated(QTreeWidgetItem*,int))); | ||
353 | connect(d->m_htmlpart, &QTextBrowser::anchorClicked, this, [=](const QUrl &link){ | ||||
354 | // using the "path" part of a qurl as reference | ||||
355 | QString myurl = link.path().toLower(); | ||||
356 | QTreeWidgetItemIterator it(this->d->m_glosstree); | ||||
357 | while (*it) { | ||||
358 | if ((*it)->type() == GlossaryTreeItemType && (*it)->text(0).toLower() == myurl) { | ||||
359 | // force the item to be selected | ||||
360 | this->d->m_glosstree->setCurrentItem(*it); | ||||
361 | // display its content | ||||
362 | emit this->d->itemActivated((*it), 0); | ||||
363 | break; | ||||
364 | } else { | ||||
365 | ++it; | ||||
366 | } | ||||
367 | } | ||||
368 | }); | ||||
358 | 369 | | |||
359 | QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Close); | 370 | QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Close); | ||
360 | connect(buttonBox, &QDialogButtonBox::rejected, this, &GlossaryDialog::reject); | 371 | connect(buttonBox, &QDialogButtonBox::rejected, this, &GlossaryDialog::reject); | ||
361 | buttonBox->button(QDialogButtonBox::Close)->setDefault(true); | 372 | buttonBox->button(QDialogButtonBox::Close)->setDefault(true); | ||
362 | vbox->addWidget(buttonBox); | 373 | vbox->addWidget(buttonBox); | ||
363 | 374 | | |||
364 | resize(800, 400); | 375 | resize(800, 400); | ||
365 | } | 376 | } | ||
366 | 377 | | |||
367 | GlossaryDialog::~GlossaryDialog() | 378 | GlossaryDialog::~GlossaryDialog() | ||
368 | { | 379 | { | ||
369 | delete d; | 380 | delete d; | ||
370 | } | 381 | } | ||
371 | 382 | | |||
372 | void GlossaryDialog::keyPressEvent(QKeyEvent* e) | 383 | void GlossaryDialog::keyPressEvent(QKeyEvent* e) | ||
373 | { | 384 | { | ||
374 | if (e->key() == Qt::Key_Return) { | 385 | if (e->key() == Qt::Key_Return) { | ||
375 | e->ignore(); | 386 | e->ignore(); | ||
376 | } | 387 | } | ||
377 | QDialog::keyPressEvent(e); | 388 | QDialog::keyPressEvent(e); | ||
378 | } | 389 | } | ||
379 | 390 | | |||
380 | void GlossaryDialog::Private::displayItem(const QUrl &url, const KParts::OpenUrlArguments &, const KParts::BrowserArguments &) | | |||
381 | { | | |||
382 | // using the "path" part of a qurl as reference | | |||
383 | QString myurl = url.path().toLower(); | | |||
384 | | ||||
385 | QTreeWidgetItemIterator it(m_glosstree); | | |||
386 | while (*it) { | | |||
387 | if ((*it)->type() == GlossaryTreeItemType && (*it)->text(0).toLower() == myurl) { | | |||
388 | // force the item to be selected | | |||
389 | m_glosstree->setCurrentItem(*it); | | |||
390 | // display its content | | |||
391 | itemActivated((*it), 0); | | |||
392 | break; | | |||
393 | } else { | | |||
394 | ++it; | | |||
395 | } | | |||
396 | } | | |||
397 | } | | |||
398 | | ||||
399 | void GlossaryDialog::Private::rebuildTree() | 391 | void GlossaryDialog::Private::rebuildTree() | ||
400 | { | 392 | { | ||
401 | m_glosstree->clear(); | 393 | m_glosstree->clear(); | ||
402 | 394 | | |||
403 | QList< GlossaryInfo >::ConstIterator it = m_glossaries.constBegin(); | 395 | QList< GlossaryInfo >::ConstIterator it = m_glossaries.constBegin(); | ||
404 | QList< GlossaryInfo >::ConstIterator itEnd = m_glossaries.constEnd(); | 396 | QList< GlossaryInfo >::ConstIterator itEnd = m_glossaries.constEnd(); | ||
405 | for (; it != itEnd; ++it) { | 397 | for (; it != itEnd; ++it) { | ||
406 | m_glosstree->addTopLevelItem(createItem(*it)); | 398 | m_glosstree->addTopLevelItem(createItem(*it)); | ||
▲ Show 20 Lines • Show All 65 Lines • ▼ Show 20 Line(s) | 455 | { | |||
472 | QString bg_picture = glosstreeitem->glossary()->backgroundPicture(); | 464 | QString bg_picture = glosstreeitem->glossary()->backgroundPicture(); | ||
473 | if (!bg_picture.isEmpty()) { | 465 | if (!bg_picture.isEmpty()) { | ||
474 | html = " background=\"file://" + bg_picture + "\""; | 466 | html = " background=\"file://" + bg_picture + "\""; | ||
475 | } | 467 | } | ||
476 | 468 | | |||
477 | html = m_htmlbasestring.arg(html); | 469 | html = m_htmlbasestring.arg(html); | ||
478 | html += glossitem->toHtml() + "</body></html>"; | 470 | html += glossitem->toHtml() + "</body></html>"; | ||
479 | 471 | | |||
480 | m_htmlpart->begin(); | 472 | m_htmlpart->setHtml(html); | ||
481 | m_htmlpart->write(html); | | |||
482 | m_htmlpart->end(); | | |||
483 | } | 473 | } | ||
484 | 474 | | |||
485 | void GlossaryItem::setRef(const QStringList& s) | 475 | void GlossaryItem::setRef(const QStringList& s) | ||
486 | { | 476 | { | ||
487 | m_ref = s; | 477 | m_ref = s; | ||
488 | m_ref.sort(); | 478 | m_ref.sort(); | ||
489 | } | 479 | } | ||
490 | 480 | | |||
▲ Show 20 Lines • Show All 60 Lines • Show Last 20 Lines |