summaryrefslogtreecommitdiffabout
path: root/pwmanager/pwmanager/commentbox.cpp
blob: 280b139a80433400c4c5199e6a792853d95a2fee (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/***************************************************************************
 *                                                                         *
 *   copyright (C) 2004 by Michael Buesch                                  *
 *   email: mbuesch@freenet.de                                             *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License version 2        *
 *   as published by the Free Software Foundation.                         *
 *                                                                         *
 ***************************************************************************/

/***************************************************************************
 * copyright (C) 2004 by Ulf Schenk
 * This file is originaly based on version 1.0.1 of pwmanager
 * and was modified to run on embedded devices that run microkde
 *
 * $Id$
 **************************************************************************/  

#include "commentbox.h"
#include "pwmexception.h"
#include "htmlgen.h"

#include <klocale.h>

#ifndef PWM_EMBEDDED
#include <khtml_part.h>
#include <khtmlview.h>
#include <qtextedit.h>
#else
#include <qmultilineedit.h>
#endif

CommentBox::CommentBox(QWidget *_parentWidget)
{
	PWM_ASSERT(_parentWidget);
	parentWidget = _parentWidget;
	textDta = 0;
#ifndef PWM_EMBEDDED
	htmlDta = 0;
#endif
	mode = mode_notSet;
}

CommentBox::~CommentBox()
{
	clearText();
	clearHtml();
}

void CommentBox::clear()
{
	clearText();
	clearHtml();
	mode = mode_notSet;
}

void CommentBox::clearText()
{
	delete_ifnot_null(textDta);
}

void CommentBox::clearHtml()
{
#ifndef PWM_EMBEDDED
  delete_ifnot_null(htmlDta);
#endif
}

void CommentBox::setText(const QString &text)
{
	switchTo(mode_text);
	PWM_ASSERT(textDta);
	textDta->setText(i18n("Comment") + ":  " + text);
	if (!textDta->isVisible())
		textDta->show();
}

bool CommentBox::getText(QString *text)
{
	if (mode != mode_text)
		return false;
	PWM_ASSERT(text);
	if (!textDta) {
		*text = "";
		return true;
	}
	*text = textDta->text();
	return true;
}

void CommentBox::setHtml(QString code)
{
#ifndef PWM_EMBEDDED
	switchTo(mode_html);
	PWM_ASSERT(htmlDta);
	if (!HtmlGen::replaceSSDummy(&code))
		printWarn("CommentBox::setHtml(): replaceSSDummy() failed!");
	htmlDta->begin();
	htmlDta->write(code);
	htmlDta->end();
	htmlDta->show();
#endif
}

void CommentBox::setContent(const QString &dta)
{
	// if there's no data, hide the comment-box
	if (dta.isEmpty()) {
		clear();
		return;
	}
#ifndef PWM_EMBEDDED
	if (HtmlGen::isHtml(dta)) {
		setHtml(dta);
		return;
	}
#endif
	// we assume it's plain text
	setText(dta);
}

void CommentBox::switchTo(commentBoxMode newMode)
{
	if (newMode == mode)
		return;

	// cleanup old mode
	switch (mode) {
	case mode_text:
		clearText();
		break;
	case mode_html:
		clearHtml();
		break;
	default:
		break;
	}

	// setup new mode
	switch (newMode) {
	case mode_text:
#ifndef PWM_EMBEDDED
		textDta = new QTextEdit(parentWidget);
		textDta->setTextFormat(Qt::PlainText);
#else
		textDta = new QMultiLineEdit(parentWidget);
#endif
		textDta->setReadOnly(true);
		textDta->show();
		break;
	case mode_html:
#ifndef PWM_EMBEDDED
		htmlDta = new KHTMLPart(parentWidget, 0,
					parentWidget);
		htmlDta->show();
#endif
		break;
	default:
		BUG();
		break;
	}

	mode = newMode;
}

void CommentBox::show()
{
	switch (mode) {
	case mode_text:
		PWM_ASSERT(textDta);
		textDta->show();
		break;
	case mode_html:
#ifndef PWM_EMBEDDED
		PWM_ASSERT(htmlDta);
		htmlDta->show();
#endif
		break;
	default:
		break;
	}
}

void CommentBox::hide()
{
	switch (mode) {
	case mode_text:
		PWM_ASSERT(textDta);
		textDta->hide();
		break;
	case mode_html:
#ifndef PWM_EMBEDDED
		PWM_ASSERT(htmlDta);
		htmlDta->hide();
#endif
		break;
	default:
		break;
	}
}

void CommentBox::resize(const QSize &size)
{
	switch (mode) {
	case mode_text:
		PWM_ASSERT(textDta);
		textDta->resize(size);
		break;
	case mode_html:
#ifndef PWM_EMBEDDED
 		PWM_ASSERT(htmlDta);
		htmlDta->view()->resize(size);
#endif
		break;
	default:
		break;
	}
}

QSize CommentBox::size()
{
	switch (mode) {
	case mode_text:
		PWM_ASSERT(textDta);
		return textDta->size();
		break;
	case mode_html:
#ifndef PWM_EMBEDDED
		PWM_ASSERT(htmlDta);
		return htmlDta->view()->size();
#endif
		break;
	default:
		break;
	}
	return QSize();
}