Remove this line
[kdeaccessibility.git] / ksayit / src / docbookclasses.cpp
blob75a75fe7c7d2e91faea4e9ae73cd9d0b0d4971b1
1 //
2 // C++ Implementation: docbookclasses
3 //
4 // Description:
5 //
6 //
7 // Author: Robert Vogl <voglrobe@lapislazuli>, (C) 2004
8 //
9 // Copyright: See COPYING file that comes with this distribution
13 // Qt includes
14 #include <QtGui/QWidget>
15 #include <QtCore/QTextStream>
16 #include <QtGui/QPixmap>
17 #include <QtCore/QRegExp>
19 // tests
20 #include <QtCore/QFile>
22 //KDE includes
23 #include <kdebug.h>
24 #include <kglobal.h>
25 #include <klocale.h>
26 #include <kiconloader.h>
27 #include <kmessagebox.h>
29 // App specific includes
30 #include "parasaxparser.h"
31 #include "docbookclasses.h"
33 //////////////////////////////////////
34 // Interface
35 //////////////////////////////////////
36 ListViewInterface::ListViewInterface(ListViewInterface *parent, QString label)
37 : K3ListViewItem( parent, label )
41 ListViewInterface::ListViewInterface(ListViewInterface *parent, ListViewInterface *after, QString label) : K3ListViewItem( parent, after, label )
45 ListViewInterface::ListViewInterface(K3ListView *lv, QString label)
46 : K3ListViewItem( lv, label )
51 //////////////////////////////////////
52 // Classes for Roberts elements
53 //////////////////////////////////////
54 /**
55 * RobDocument
57 RobDocument::RobDocument(K3ListView *lv, QString label)
58 : ListViewInterface( lv, label )
60 QPixmap pixmap = KIconLoader::global()->loadIcon("help-contents", K3Icon::Small);
61 this->setPixmap(0, pixmap);
62 m_iseditable = false;
65 RobDocument::~RobDocument()
69 QString RobDocument::getRTFHeader() const
71 QString header = "<H1><center>" + this->text(0) + "</center></H1>";
72 return header;
75 QString RobDocument::getHeader() const
77 QString header = this->text(0) + '\n';
78 return header;
83 //////////////////////////////////////
84 // Classes for Docbook elements
85 //////////////////////////////////////
86 /**
87 * Overview
89 Overview::Overview(ListViewInterface *parent, ListViewInterface *after, QString label)
90 : ListViewInterface(parent, after, label)
92 QPixmap pixmap = KIconLoader::global()->loadIcon("document-new", K3Icon::Small);
93 this->setPixmap(0, pixmap);
94 this->setText(3, "0"); // upmost element
95 m_iseditable = false;
98 Overview::~Overview()
102 QString Overview::getRTFHeader() const
104 QString header = "<H1>" + this->text(0) + "</H1>";
105 return header;
108 QString Overview::getHeader() const
110 QString header = this->text(0) + '\n';
111 return header;
116 * Date
118 Date::Date(ListViewInterface *parent, ListViewInterface *after, QString label)
119 : ListViewInterface(parent, after, label)
121 QPixmap pixmap = KIconLoader::global()->loadIcon("document-new", K3Icon::Small);
122 this->setPixmap(0, pixmap);
123 this->setText(3, "00000001");
124 m_node = QDomNode();
125 m_iseditable = false;
128 Date::~Date()
132 void Date::setNodes( const NodeList_type &list )
134 if ( !list.empty() )
135 m_node = list.front();
138 void Date::getNodes( NodeList_type &list ) const
140 list.push_back( m_node );
143 QString Date::getRTFHeader() const
145 QString header = "<B>" + this->text(0) + ": </B>";
146 return header;
149 QString Date::getHeader() const
151 QString header = this->text(0) + ": ";
152 return header;
155 QString Date::getRTFData() const
157 if ( !m_node.isNull() ){
158 QString date = m_node.nodeValue() + "<br>";
159 return date;
160 } else
161 return QString();
164 QString Date::getData() const
166 if ( !m_node.isNull() ){
167 return m_node.nodeValue();
168 } else
169 return QString();
172 QString Date::getRawData() const
174 QString ret;
176 // get content of element
177 QTextStream in(&ret, QIODevice::WriteOnly);
178 in.setEncoding(QTextStream::UnicodeUTF8);
179 in << m_node;
181 return ret;
184 void Date::setData(const QString &data, QDomDocument &)
186 m_node.setNodeValue(data);
187 this->setText(1, data); // update ListView
192 * ReleaseInfo
194 ReleaseInfo::ReleaseInfo(ListViewInterface *parent, ListViewInterface *after, QString label)
195 : ListViewInterface(parent, after, label)
197 QPixmap pixmap = KIconLoader::global()->loadIcon("document-new", K3Icon::Small);
198 this->setPixmap(0, pixmap);
199 this->setText(3, "00000002");
200 m_node = QDomNode();
201 m_iseditable = false;
204 ReleaseInfo::~ReleaseInfo()
208 void ReleaseInfo::setNodes( const NodeList_type &list )
210 if ( !list.empty() )
211 m_node = list.front();
214 void ReleaseInfo::getNodes( NodeList_type &list ) const
216 list.push_back( m_node );
219 QString ReleaseInfo::getRTFHeader() const
221 QString header = "<B>" + this->text(0) + ": </B>";
222 return header;
225 QString ReleaseInfo::getHeader() const
227 QString header = this->text(0) + ": ";
228 return header;
231 QString ReleaseInfo::getRTFData() const
233 if ( !m_node.isNull() ){
234 QString releaseinfo = m_node.nodeValue() + "<br>";
235 return releaseinfo;
236 } else
237 return QString();
240 QString ReleaseInfo::getData() const
242 if ( !m_node.isNull() ){
243 return m_node.nodeValue();
244 } else
245 return QString();
248 QString ReleaseInfo::getRawData() const
250 QString ret;
252 // get content of element
253 QTextStream in(&ret, QIODevice::WriteOnly);
254 in.setEncoding(QTextStream::UnicodeUTF8);
255 in << m_node;
257 return ret;
260 void ReleaseInfo::setData(const QString &data, QDomDocument &)
262 m_node.setNodeValue(data);
263 this->setText(1, data); // update ListView
268 * Authorgroup
270 AuthorGroup::AuthorGroup(ListViewInterface *parent, ListViewInterface *after, QString label)
271 : ListViewInterface(parent, after, label)
273 QPixmap pixmap = KIconLoader::global()->loadIcon("kdmconfig", K3Icon::Small);
274 this->setPixmap(0, pixmap);
275 this->setText(3, "00000003");
276 m_iseditable = false;
279 AuthorGroup::~AuthorGroup()
283 QString AuthorGroup::getRTFHeader() const
285 QString header = "<H2>" + this->text(0) + "</H2>";
286 return header;
289 QString AuthorGroup::getHeader() const
291 QString header = this->text(0) + '\n';
292 return header;
297 * Author
299 Author::Author(ListViewInterface *parent, ListViewInterface *after, QString label)
300 : ListViewInterface(parent, after, label)
302 QPixmap pixmap = KIconLoader::global()->loadIcon("personal", K3Icon::Small);
303 this->setPixmap(0, pixmap);
304 m_node1 = QDomNode();
305 m_node2 = QDomNode();
306 m_iseditable = false;
309 Author::~Author()
313 void Author::setNodes( const NodeList_type &list )
315 NodeList_type l_list = list;
316 NodeList_type::iterator it = l_list.begin();
317 m_node1 = *it;
318 m_node2 = *(++it);
321 void Author::getNodes( NodeList_type &list ) const
323 list.push_back( m_node1 );
324 list.push_back( m_node2 );
328 QString Author::getRTFHeader() const
330 QString header = "<B>" + this->text(0) + ": </B>";
331 return header;
334 QString Author::getHeader() const
336 QString author = this->text(0) + ": ";
337 return author;
340 QString Author::getRTFData() const
342 QString ret = QString();
343 if ( !m_node1.isNull() ){
344 ret = m_node1.nodeValue() + ' ';
347 if ( !m_node2.isNull() ){
348 ret += m_node2.nodeValue() + "<br>";
350 return ret;
353 QString Author::getData() const
355 QString ret = QString();
356 if ( !m_node1.isNull() ){
357 ret = m_node1.nodeValue() + ' ';
360 if ( !m_node2.isNull() ){
361 ret += m_node2.nodeValue();
363 return ret;
366 QString Author::getRawData() const
368 QString ret = QString();
369 if ( !m_node1.isNull() ){
370 ret = m_node1.nodeValue() + ' ';
373 if ( !m_node2.isNull() ){
374 ret += m_node2.nodeValue();
376 return ret;
379 void Author::setData(const QString &data, QDomDocument &)
381 // canonify string
382 QString m_data = data;
383 m_data.replace( QRegExp("\n"), "" ); // remove Newlines
384 m_data.replace( QRegExp(" {2,}"), " " ); // remove multiple spaces
385 m_data.replace( QRegExp("[\t|\r]{1,}"), ""); // remove Tabs
386 // split string "firstname surname"
387 QString firstname = m_data.section(' ', 0, 0);
388 QString surname = m_data.section(' ', 1, 1);
389 // update node elements
390 m_node1.setNodeValue(firstname);
391 m_node2.setNodeValue(surname);
392 // update Listview
393 this->setText(1, firstname + ' ' + surname);
398 * KeywordSet
400 KeywordSet::KeywordSet(ListViewInterface *parent, ListViewInterface *after, QString label)
401 : ListViewInterface(parent, after, label)
403 QPixmap pixmap = KIconLoader::global()->loadIcon("txt", K3Icon::Small);
404 this->setPixmap(0, pixmap);
405 this->setText(3, "00000004");
406 m_iseditable = false;
409 KeywordSet::~KeywordSet()
413 QString KeywordSet::getRTFHeader() const
415 QString header = "<H2>" + this->text(0) + "</H2>";
416 return header;
419 QString KeywordSet::getHeader() const
421 QString header = this->text(0) + '\n';
422 return header;
427 * Keyword
429 Keyword::Keyword(ListViewInterface *parent, ListViewInterface *after, QString label)
430 : ListViewInterface(parent, after, label)
432 m_node = QDomNode();
433 m_iseditable = false;
436 Keyword::~Keyword()
440 void Keyword::setNodes( const NodeList_type &list )
442 if ( !list.empty() )
443 m_node = list.front();
446 void Keyword::getNodes( NodeList_type &list ) const
448 list.push_back( m_node );
451 QString Keyword::getRTFData() const
453 if ( !m_node.isNull() ){
454 QString keyword = m_node.nodeValue() + "<br>";
455 return keyword;
456 } else
457 return QString();
460 QString Keyword::getData() const
462 if ( !m_node.isNull() ){
463 return m_node.nodeValue();
464 } else
465 return QString();
468 QString Keyword::getRawData() const
470 QString ret;
472 // get content of element
473 QTextStream in(&ret, QIODevice::WriteOnly);
474 in.setEncoding(QTextStream::UnicodeUTF8);
475 in << m_node;
477 return ret;
480 void Keyword::setData(const QString &data, QDomDocument &)
482 m_node.setNodeValue(data);
483 this->setText(0, data); // update ListView
488 * Abstract
490 Abstract::Abstract(ListViewInterface *parent, ListViewInterface *after, QString label)
491 : ListViewInterface(parent, after, label)
493 QPixmap pixmap = KIconLoader::global()->loadIcon("document-new", K3Icon::Small);
494 this->setPixmap(0, pixmap);
495 this->setText(3, "00000005");
496 m_iseditable = false;
499 Abstract::~Abstract()
503 QString Abstract::getRTFHeader() const
505 QString header = "<H2>" + this->text(0) + "</H2>";
506 return header;
509 QString Abstract::getHeader() const
511 QString header = this->text(0) + '\n';
512 return header;
517 * Para
519 Para::Para(ListViewInterface *parent, ListViewInterface *after, QString label)
520 : ListViewInterface(parent, after, label)
522 QPixmap pixmap = KIconLoader::global()->loadIcon("leftjust", K3Icon::Small);
523 this->setPixmap(0, pixmap);
524 m_node = QDomNode();
525 m_iseditable = false;
528 Para::~Para()
532 void Para::setNodes( const NodeList_type &list )
534 if ( !list.empty() )
535 m_node = list.front();
538 void Para::getNodes( NodeList_type &list ) const
540 list.push_back( m_node );
543 QString Para::getRTFHeader() const
545 return QString();
548 QString Para::getHeader() const
550 QString header = this->text(0) + '\n';
551 return header;
554 QString Para::getRTFData() const
556 // get content of element
557 QString paragraph;
558 QTextStream in(&paragraph, QIODevice::WriteOnly);
559 in.setEncoding(QTextStream::UnicodeUTF8);
560 in << m_node;
562 // delegate job to SAX-Parser
563 QString data;
564 QString ret;
566 ParaSaxParser *p = ParaSaxParser::Instance();
567 p->setData( paragraph );
568 p->getRTFText( data );
569 ret = "<p>" + data + "</p>";
571 return ret;
574 QString Para::getData() const
576 // get content of element
577 QString paragraph;
578 QTextStream in(&paragraph, QIODevice::WriteOnly);
579 in.setEncoding(QTextStream::UnicodeUTF8);
580 in << m_node;
582 // delegate job to SAX-Parser
583 QString data;
585 ParaSaxParser *p = ParaSaxParser::Instance();
586 p->setData( paragraph );
587 p->getText( data );
589 return data;
592 QString Para::getRawData() const
594 QString ret;
596 // get content of element
597 QTextStream in(&ret, QIODevice::WriteOnly);
598 in.setEncoding(QTextStream::UnicodeUTF8);
599 in << m_node;
600 // remove opening and closing para-tags
601 ret.replace( QRegExp("</?(para|Para|PARA)/?>"),"");
602 ret.replace( QRegExp("^ "),"" );
603 ret.replace( QRegExp("^\n"), "" );
605 return ret;
608 void Para::setData(const QString &data, QDomDocument &)
610 kDebug(100200) << "Para::setData()";
612 // add tags
613 QString l_data = "<para>\n" + data + "\n</para>";
615 // create a temporary document
616 QString err;
617 QDomDocument tempDoc;
618 if ( !tempDoc.setContent(l_data) ){
619 l_data = "<para><![CDATA[\n" + data + "\n]]></para>";
620 // try again
621 if ( !tempDoc.setContent(l_data) ){
622 err = i18n("Unable to make document persistent.");
623 kDebug() << err;
624 // throw err;
625 return;
627 err = i18n("This paragraph contains mismatching tags. It will will be handled as CDATA.");
628 KMessageBox::information(0, err, i18n("XML-Error"));
631 // get parent
632 QDomNode parent = m_node.parentNode();
633 if ( parent.isNull() ){
634 kDebug(100200) << "Para::setData(): Structural DocBook format error.";
635 return;
638 // replace Node
639 parent.replaceChild(tempDoc, m_node);
640 m_node = tempDoc;
645 * Chapter
647 Chapter::Chapter(ListViewInterface *parent, ListViewInterface *after, QString label)
648 : ListViewInterface(parent, after, label)
650 QPixmap pixmap = KIconLoader::global()->loadIcon("leftjust", K3Icon::Small);
651 this->setPixmap(0, pixmap);
652 m_iseditable = false;
655 Chapter::~Chapter()
659 QString Chapter::getRTFHeader() const
661 QString header = "<H1>" + this->text(0) + "</H1>";
662 return header;
665 QString Chapter::getHeader() const
667 QString header = this->text(0) + '\n';
668 return header;
673 * Sect1
675 Sect1::Sect1(ListViewInterface *parent, ListViewInterface *after, QString label)
676 : ListViewInterface(parent, after, label)
678 QPixmap pixmap = KIconLoader::global()->loadIcon("leftjust", K3Icon::Small);
679 this->setPixmap(0, pixmap);
680 m_iseditable = false;
683 Sect1::~Sect1()
687 QString Sect1::getRTFHeader() const
689 QString header = "<H2>" + this->text(0) + "</H2>";
690 return header;
693 QString Sect1::getHeader() const
695 QString header = this->text(0) + '\n';
696 return header;
701 * Sect2
703 Sect2::Sect2(ListViewInterface *parent, ListViewInterface *after, QString label)
704 : ListViewInterface(parent, after, label)
706 QPixmap pixmap = KIconLoader::global()->loadIcon("leftjust", K3Icon::Small);
707 this->setPixmap(0, pixmap);
708 m_iseditable = false;
711 Sect2::~Sect2()
715 QString Sect2::getRTFHeader() const
717 QString header = "<H3>" + this->text(0) + "</H3>";
718 return header;
721 QString Sect2::getHeader() const
723 QString header = this->text(0) + '\n';
724 return header;
729 * Sect3
731 Sect3::Sect3(ListViewInterface *parent, ListViewInterface *after, QString label)
732 : ListViewInterface(parent, after, label)
734 QPixmap pixmap = KIconLoader::global()->loadIcon("leftjust", K3Icon::Small);
735 this->setPixmap(0, pixmap);
736 m_iseditable = false;
739 Sect3::~Sect3()
743 QString Sect3::getRTFHeader() const
745 QString header = "<B>" + this->text(0) + "</B><br>";
746 return header;
749 QString Sect3::getHeader() const
751 QString header = this->text(0) + '\n';
752 return header;
757 * Sect4
759 Sect4::Sect4(ListViewInterface *parent, ListViewInterface *after, QString label)
760 : ListViewInterface(parent, after, label)
762 QPixmap pixmap = KIconLoader::global()->loadIcon("leftjust", K3Icon::Small);
763 this->setPixmap(0, pixmap);
764 m_iseditable = false;
767 Sect4::~Sect4()
771 QString Sect4::getRTFHeader() const
773 QString header = "<B>" + this->text(0) + "</B><br>";
774 return header;
777 QString Sect4::getHeader() const
779 QString header = this->text(0) + '\n';
780 return header;
785 * Sect5
787 Sect5::Sect5(ListViewInterface *parent, ListViewInterface *after, QString label)
788 : ListViewInterface(parent, after, label)
790 QPixmap pixmap = KIconLoader::global()->loadIcon("leftjust", K3Icon::Small);
791 this->setPixmap(0, pixmap);
792 m_iseditable = false;
795 Sect5::~Sect5()
799 QString Sect5::getRTFHeader() const
801 QString header = "<B>" + this->text(0) + "</B><br>";
802 return header;
805 QString Sect5::getHeader() const
807 QString header = this->text(0) + '\n';
808 return header;