Fix include
[kdeaccessibility.git] / ksayit / src / docbookclasses.cpp
blob9f1572f83a0ef706d2dda56365cabd6be5f0cd41
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
12 // Qt includes
13 #include <qwidget.h>
14 #include <QTextStream>
15 #include <QPixmap>
16 #include <QRegExp>
18 // tests
19 #include <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"
34 //////////////////////////////////////
35 // Interface
36 //////////////////////////////////////
37 ListViewInterface::ListViewInterface(ListViewInterface *parent, QString label)
38 : K3ListViewItem( parent, label )
42 ListViewInterface::ListViewInterface(ListViewInterface *parent, ListViewInterface *after, QString label) : K3ListViewItem( parent, after, label )
46 ListViewInterface::ListViewInterface(K3ListView *lv, QString label)
47 : K3ListViewItem( lv, label )
52 //////////////////////////////////////
53 // Classes for Roberts elements
54 //////////////////////////////////////
55 /**
56 * RobDocument
58 RobDocument::RobDocument(K3ListView *lv, QString label)
59 : ListViewInterface( lv, label )
61 QPixmap pixmap = KIconLoader::global()->loadIcon("help-contents", K3Icon::Small);
62 this->setPixmap(0, pixmap);
63 m_iseditable = false;
66 RobDocument::~RobDocument()
70 QString RobDocument::getRTFHeader() const
72 QString header = "<H1><center>" + this->text(0) + "</center></H1>";
73 return header;
76 QString RobDocument::getHeader() const
78 QString header = this->text(0) + '\n';
79 return header;
84 //////////////////////////////////////
85 // Classes for Docbook elements
86 //////////////////////////////////////
87 /**
88 * Overview
90 Overview::Overview(ListViewInterface *parent, ListViewInterface *after, QString label)
91 : ListViewInterface(parent, after, label)
93 QPixmap pixmap = KIconLoader::global()->loadIcon("document-new", K3Icon::Small);
94 this->setPixmap(0, pixmap);
95 this->setText(3, "0"); // upmost element
96 m_iseditable = false;
99 Overview::~Overview()
103 QString Overview::getRTFHeader() const
105 QString header = "<H1>" + this->text(0) + "</H1>";
106 return header;
109 QString Overview::getHeader() const
111 QString header = this->text(0) + '\n';
112 return header;
117 * Date
119 Date::Date(ListViewInterface *parent, ListViewInterface *after, QString label)
120 : ListViewInterface(parent, after, label)
122 QPixmap pixmap = KIconLoader::global()->loadIcon("document-new", K3Icon::Small);
123 this->setPixmap(0, pixmap);
124 this->setText(3, "00000001");
125 m_node = QDomNode();
126 m_iseditable = false;
129 Date::~Date()
133 void Date::setNodes( const NodeList_type &list )
135 if ( !list.empty() )
136 m_node = list.front();
139 void Date::getNodes( NodeList_type &list ) const
141 list.push_back( m_node );
144 QString Date::getRTFHeader() const
146 QString header = "<B>" + this->text(0) + ": </B>";
147 return header;
150 QString Date::getHeader() const
152 QString header = this->text(0) + ": ";
153 return header;
156 QString Date::getRTFData() const
158 if ( !m_node.isNull() ){
159 QString date = m_node.nodeValue() + "<br>";
160 return date;
161 } else
162 return QString();
165 QString Date::getData() const
167 if ( !m_node.isNull() ){
168 return m_node.nodeValue();
169 } else
170 return QString();
173 QString Date::getRawData() const
175 QString ret;
177 // get content of element
178 QTextStream in(&ret, QIODevice::WriteOnly);
179 in.setEncoding(QTextStream::UnicodeUTF8);
180 in << m_node;
182 return ret;
185 void Date::setData(const QString &data, QDomDocument &)
187 m_node.setNodeValue(data);
188 this->setText(1, data); // update ListView
193 * ReleaseInfo
195 ReleaseInfo::ReleaseInfo(ListViewInterface *parent, ListViewInterface *after, QString label)
196 : ListViewInterface(parent, after, label)
198 QPixmap pixmap = KIconLoader::global()->loadIcon("document-new", K3Icon::Small);
199 this->setPixmap(0, pixmap);
200 this->setText(3, "00000002");
201 m_node = QDomNode();
202 m_iseditable = false;
205 ReleaseInfo::~ReleaseInfo()
209 void ReleaseInfo::setNodes( const NodeList_type &list )
211 if ( !list.empty() )
212 m_node = list.front();
215 void ReleaseInfo::getNodes( NodeList_type &list ) const
217 list.push_back( m_node );
220 QString ReleaseInfo::getRTFHeader() const
222 QString header = "<B>" + this->text(0) + ": </B>";
223 return header;
226 QString ReleaseInfo::getHeader() const
228 QString header = this->text(0) + ": ";
229 return header;
232 QString ReleaseInfo::getRTFData() const
234 if ( !m_node.isNull() ){
235 QString releaseinfo = m_node.nodeValue() + "<br>";
236 return releaseinfo;
237 } else
238 return QString();
241 QString ReleaseInfo::getData() const
243 if ( !m_node.isNull() ){
244 return m_node.nodeValue();
245 } else
246 return QString();
249 QString ReleaseInfo::getRawData() const
251 QString ret;
253 // get content of element
254 QTextStream in(&ret, QIODevice::WriteOnly);
255 in.setEncoding(QTextStream::UnicodeUTF8);
256 in << m_node;
258 return ret;
261 void ReleaseInfo::setData(const QString &data, QDomDocument &)
263 m_node.setNodeValue(data);
264 this->setText(1, data); // update ListView
269 * Authorgroup
271 AuthorGroup::AuthorGroup(ListViewInterface *parent, ListViewInterface *after, QString label)
272 : ListViewInterface(parent, after, label)
274 QPixmap pixmap = KIconLoader::global()->loadIcon("kdmconfig", K3Icon::Small);
275 this->setPixmap(0, pixmap);
276 this->setText(3, "00000003");
277 m_iseditable = false;
280 AuthorGroup::~AuthorGroup()
284 QString AuthorGroup::getRTFHeader() const
286 QString header = "<H2>" + this->text(0) + "</H2>";
287 return header;
290 QString AuthorGroup::getHeader() const
292 QString header = this->text(0) + '\n';
293 return header;
298 * Author
300 Author::Author(ListViewInterface *parent, ListViewInterface *after, QString label)
301 : ListViewInterface(parent, after, label)
303 QPixmap pixmap = KIconLoader::global()->loadIcon("personal", K3Icon::Small);
304 this->setPixmap(0, pixmap);
305 m_node1 = QDomNode();
306 m_node2 = QDomNode();
307 m_iseditable = false;
310 Author::~Author()
314 void Author::setNodes( const NodeList_type &list )
316 NodeList_type l_list = list;
317 NodeList_type::iterator it = l_list.begin();
318 m_node1 = *it;
319 m_node2 = *(++it);
322 void Author::getNodes( NodeList_type &list ) const
324 list.push_back( m_node1 );
325 list.push_back( m_node2 );
329 QString Author::getRTFHeader() const
331 QString header = "<B>" + this->text(0) + ": </B>";
332 return header;
335 QString Author::getHeader() const
337 QString author = this->text(0) + ": ";
338 return author;
341 QString Author::getRTFData() const
343 QString ret = QString();
344 if ( !m_node1.isNull() ){
345 ret = m_node1.nodeValue() + ' ';
348 if ( !m_node2.isNull() ){
349 ret += m_node2.nodeValue() + "<br>";
351 return ret;
354 QString Author::getData() const
356 QString ret = QString();
357 if ( !m_node1.isNull() ){
358 ret = m_node1.nodeValue() + ' ';
361 if ( !m_node2.isNull() ){
362 ret += m_node2.nodeValue();
364 return ret;
367 QString Author::getRawData() const
369 QString ret = QString();
370 if ( !m_node1.isNull() ){
371 ret = m_node1.nodeValue() + ' ';
374 if ( !m_node2.isNull() ){
375 ret += m_node2.nodeValue();
377 return ret;
380 void Author::setData(const QString &data, QDomDocument &)
382 // canonify string
383 QString m_data = data;
384 m_data.replace( QRegExp("\n"), "" ); // remove Newlines
385 m_data.replace( QRegExp(" {2,}"), " " ); // remove multiple spaces
386 m_data.replace( QRegExp("[\t|\r]{1,}"), ""); // remove Tabs
387 // split string "firstname surname"
388 QString firstname = m_data.section(' ', 0, 0);
389 QString surname = m_data.section(' ', 1, 1);
390 // update node elements
391 m_node1.setNodeValue(firstname);
392 m_node2.setNodeValue(surname);
393 // update Listview
394 this->setText(1, firstname + ' ' + surname);
399 * KeywordSet
401 KeywordSet::KeywordSet(ListViewInterface *parent, ListViewInterface *after, QString label)
402 : ListViewInterface(parent, after, label)
404 QPixmap pixmap = KIconLoader::global()->loadIcon("txt", K3Icon::Small);
405 this->setPixmap(0, pixmap);
406 this->setText(3, "00000004");
407 m_iseditable = false;
410 KeywordSet::~KeywordSet()
414 QString KeywordSet::getRTFHeader() const
416 QString header = "<H2>" + this->text(0) + "</H2>";
417 return header;
420 QString KeywordSet::getHeader() const
422 QString header = this->text(0) + '\n';
423 return header;
428 * Keyword
430 Keyword::Keyword(ListViewInterface *parent, ListViewInterface *after, QString label)
431 : ListViewInterface(parent, after, label)
433 m_node = QDomNode();
434 m_iseditable = false;
437 Keyword::~Keyword()
441 void Keyword::setNodes( const NodeList_type &list )
443 if ( !list.empty() )
444 m_node = list.front();
447 void Keyword::getNodes( NodeList_type &list ) const
449 list.push_back( m_node );
452 QString Keyword::getRTFData() const
454 if ( !m_node.isNull() ){
455 QString keyword = m_node.nodeValue() + "<br>";
456 return keyword;
457 } else
458 return QString();
461 QString Keyword::getData() const
463 if ( !m_node.isNull() ){
464 return m_node.nodeValue();
465 } else
466 return QString();
469 QString Keyword::getRawData() const
471 QString ret;
473 // get content of element
474 QTextStream in(&ret, QIODevice::WriteOnly);
475 in.setEncoding(QTextStream::UnicodeUTF8);
476 in << m_node;
478 return ret;
481 void Keyword::setData(const QString &data, QDomDocument &)
483 m_node.setNodeValue(data);
484 this->setText(0, data); // update ListView
489 * Abstract
491 Abstract::Abstract(ListViewInterface *parent, ListViewInterface *after, QString label)
492 : ListViewInterface(parent, after, label)
494 QPixmap pixmap = KIconLoader::global()->loadIcon("document-new", K3Icon::Small);
495 this->setPixmap(0, pixmap);
496 this->setText(3, "00000005");
497 m_iseditable = false;
500 Abstract::~Abstract()
504 QString Abstract::getRTFHeader() const
506 QString header = "<H2>" + this->text(0) + "</H2>";
507 return header;
510 QString Abstract::getHeader() const
512 QString header = this->text(0) + '\n';
513 return header;
518 * Para
520 Para::Para(ListViewInterface *parent, ListViewInterface *after, QString label)
521 : ListViewInterface(parent, after, label)
523 QPixmap pixmap = KIconLoader::global()->loadIcon("leftjust", K3Icon::Small);
524 this->setPixmap(0, pixmap);
525 m_node = QDomNode();
526 m_iseditable = false;
529 Para::~Para()
533 void Para::setNodes( const NodeList_type &list )
535 if ( !list.empty() )
536 m_node = list.front();
539 void Para::getNodes( NodeList_type &list ) const
541 list.push_back( m_node );
544 QString Para::getRTFHeader() const
546 return QString();
549 QString Para::getHeader() const
551 QString header = this->text(0) + '\n';
552 return header;
555 QString Para::getRTFData() const
557 // get content of element
558 QString paragraph;
559 QTextStream in(&paragraph, QIODevice::WriteOnly);
560 in.setEncoding(QTextStream::UnicodeUTF8);
561 in << m_node;
563 // delegate job to SAX-Parser
564 QString data;
565 QString ret;
567 ParaSaxParser *p = ParaSaxParser::Instance();
568 p->setData( paragraph );
569 p->getRTFText( data );
570 ret = "<p>" + data + "</p>";
572 return ret;
575 QString Para::getData() const
577 // get content of element
578 QString paragraph;
579 QTextStream in(&paragraph, QIODevice::WriteOnly);
580 in.setEncoding(QTextStream::UnicodeUTF8);
581 in << m_node;
583 // delegate job to SAX-Parser
584 QString data;
586 ParaSaxParser *p = ParaSaxParser::Instance();
587 p->setData( paragraph );
588 p->getText( data );
590 return data;
593 QString Para::getRawData() const
595 QString ret;
597 // get content of element
598 QTextStream in(&ret, QIODevice::WriteOnly);
599 in.setEncoding(QTextStream::UnicodeUTF8);
600 in << m_node;
601 // remove opening and closing para-tags
602 ret.replace( QRegExp("</?(para|Para|PARA)/?>"),"");
603 ret.replace( QRegExp("^ "),"" );
604 ret.replace( QRegExp("^\n"), "" );
606 return ret;
609 void Para::setData(const QString &data, QDomDocument &)
611 kDebug(100200) << "Para::setData()" << endl;
613 // add tags
614 QString l_data = "<para>\n" + data + "\n</para>";
616 // create a temporary document
617 QString err;
618 QDomDocument tempDoc;
619 if ( !tempDoc.setContent(l_data) ){
620 l_data = "<para><![CDATA[\n" + data + "\n]]></para>";
621 // try again
622 if ( !tempDoc.setContent(l_data) ){
623 err = i18n("Unable to make document persistent.");
624 kDebug() << err << endl;
625 // throw err;
626 return;
628 err = i18n("This paragraph contains mismatching tags. It will will be handled as CDATA.");
629 KMessageBox::information(0, err, i18n("XML-Error"));
632 // get parent
633 QDomNode parent = m_node.parentNode();
634 if ( parent.isNull() ){
635 kDebug(100200) << "Para::setData(): Structural DocBook format error." << endl;
636 return;
639 // replace Node
640 parent.replaceChild(tempDoc, m_node);
641 m_node = tempDoc;
646 * Chapter
648 Chapter::Chapter(ListViewInterface *parent, ListViewInterface *after, QString label)
649 : ListViewInterface(parent, after, label)
651 QPixmap pixmap = KIconLoader::global()->loadIcon("leftjust", K3Icon::Small);
652 this->setPixmap(0, pixmap);
653 m_iseditable = false;
656 Chapter::~Chapter()
660 QString Chapter::getRTFHeader() const
662 QString header = "<H1>" + this->text(0) + "</H1>";
663 return header;
666 QString Chapter::getHeader() const
668 QString header = this->text(0) + '\n';
669 return header;
674 * Sect1
676 Sect1::Sect1(ListViewInterface *parent, ListViewInterface *after, QString label)
677 : ListViewInterface(parent, after, label)
679 QPixmap pixmap = KIconLoader::global()->loadIcon("leftjust", K3Icon::Small);
680 this->setPixmap(0, pixmap);
681 m_iseditable = false;
684 Sect1::~Sect1()
688 QString Sect1::getRTFHeader() const
690 QString header = "<H2>" + this->text(0) + "</H2>";
691 return header;
694 QString Sect1::getHeader() const
696 QString header = this->text(0) + '\n';
697 return header;
702 * Sect2
704 Sect2::Sect2(ListViewInterface *parent, ListViewInterface *after, QString label)
705 : ListViewInterface(parent, after, label)
707 QPixmap pixmap = KIconLoader::global()->loadIcon("leftjust", K3Icon::Small);
708 this->setPixmap(0, pixmap);
709 m_iseditable = false;
712 Sect2::~Sect2()
716 QString Sect2::getRTFHeader() const
718 QString header = "<H3>" + this->text(0) + "</H3>";
719 return header;
722 QString Sect2::getHeader() const
724 QString header = this->text(0) + '\n';
725 return header;
730 * Sect3
732 Sect3::Sect3(ListViewInterface *parent, ListViewInterface *after, QString label)
733 : ListViewInterface(parent, after, label)
735 QPixmap pixmap = KIconLoader::global()->loadIcon("leftjust", K3Icon::Small);
736 this->setPixmap(0, pixmap);
737 m_iseditable = false;
740 Sect3::~Sect3()
744 QString Sect3::getRTFHeader() const
746 QString header = "<B>" + this->text(0) + "</B><br>";
747 return header;
750 QString Sect3::getHeader() const
752 QString header = this->text(0) + '\n';
753 return header;
758 * Sect4
760 Sect4::Sect4(ListViewInterface *parent, ListViewInterface *after, QString label)
761 : ListViewInterface(parent, after, label)
763 QPixmap pixmap = KIconLoader::global()->loadIcon("leftjust", K3Icon::Small);
764 this->setPixmap(0, pixmap);
765 m_iseditable = false;
768 Sect4::~Sect4()
772 QString Sect4::getRTFHeader() const
774 QString header = "<B>" + this->text(0) + "</B><br>";
775 return header;
778 QString Sect4::getHeader() const
780 QString header = this->text(0) + '\n';
781 return header;
786 * Sect5
788 Sect5::Sect5(ListViewInterface *parent, ListViewInterface *after, QString label)
789 : ListViewInterface(parent, after, label)
791 QPixmap pixmap = KIconLoader::global()->loadIcon("leftjust", K3Icon::Small);
792 this->setPixmap(0, pixmap);
793 m_iseditable = false;
796 Sect5::~Sect5()
800 QString Sect5::getRTFHeader() const
802 QString header = "<B>" + this->text(0) + "</B><br>";
803 return header;
806 QString Sect5::getHeader() const
808 QString header = this->text(0) + '\n';
809 return header;