summaryrefslogtreecommitdiffstats
path: root/ksig/siglistviewitem.cpp
blob: 637bf3df7aa9da002576f664942a22439d05b415 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/***************************************************************************
                          siglistviewitem.cpp  -  description
                             -------------------
    begin                : Fri Jul 12 2002
    copyright            : (C) 2002 by Scott Wheeler
    email                : wheeler@kde.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "siglistviewitem.h"

#include <tdelocale.h>
#include <kdebug.h>

#include <tqregexp.h>

////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////

SigListViewItem::~SigListViewItem()
{
    // remove the element from the tree
    element.parentNode().removeChild(element);
}

TQString SigListViewItem::text() const
{
    return(elementText);
}

void SigListViewItem::setText(const TQString &t)
{
    if(t != elementText) {
	elementText = t;
	dirty = true;
	refreshText();
    }
}

void SigListViewItem::refreshText()
{
    if(!text().isEmpty())
	TDEListViewItem::setText(0, text().simplifyWhiteSpace());
    else
	TDEListViewItem::setText(0, emptySigString);
}

void SigListViewItem::nodeToText(const TQDomNode &n, TQString &s)
{
    TQDomNodeList children = n.childNodes();

    for(uint i = 0; i < children.count(); i++) {
	if(children.item(i).isText())
	    s.append(children.item(i).toText().data());
	else {
	    nodeToText(children.item(i), s);
	    if(children.item(i).isElement() && children.item(i).toElement().tagName() == "p") {
		s.append("\n");
	    }
	}
    }
}

////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

SigListViewItem::SigListViewItem(TQListView *parent, TQDomDocument document, TQDomElement signatureElement) : TDEListViewItem(parent)
{
    emptySigString = i18n("<empty signature>");

    doc = document;
    element = signatureElement;
    nodeToText(element, elementText);
    elementText.replace(TQRegExp("\n$"), "");
    
    dirty = false;
    refreshText();
}

void SigListViewItem::render()
{
    if(dirty) {
	TQDomNodeList children = element.childNodes();

	while(!element.firstChild().isNull()) 
	    element.removeChild(element.firstChild());

	// create new children
	TQStringList lines = TQStringList::split("\n", elementText, true);
	
	for(TQStringList::Iterator it = lines.begin(); it != lines.end(); it++) {
	    TQDomElement p = doc.createElement("p");
	    element.appendChild(p);
	    p.appendChild(doc.createTextNode(*it));
	}
	dirty = false;
    }
}