summaryrefslogtreecommitdiffabout
path: root/pwmanager/pwmanager/commentbox.cpp
Unidiff
Diffstat (limited to 'pwmanager/pwmanager/commentbox.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--pwmanager/pwmanager/commentbox.cpp238
1 files changed, 238 insertions, 0 deletions
diff --git a/pwmanager/pwmanager/commentbox.cpp b/pwmanager/pwmanager/commentbox.cpp
new file mode 100644
index 0000000..280b139
--- a/dev/null
+++ b/pwmanager/pwmanager/commentbox.cpp
@@ -0,0 +1,238 @@
1/***************************************************************************
2 * *
3 * copyright (C) 2004 by Michael Buesch *
4 * email: mbuesch@freenet.de *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License version 2 *
8 * as published by the Free Software Foundation. *
9 * *
10 ***************************************************************************/
11
12/***************************************************************************
13 * copyright (C) 2004 by Ulf Schenk
14 * This file is originaly based on version 1.0.1 of pwmanager
15 * and was modified to run on embedded devices that run microkde
16 *
17 * $Id$
18 **************************************************************************/
19
20#include "commentbox.h"
21#include "pwmexception.h"
22#include "htmlgen.h"
23
24#include <klocale.h>
25
26#ifndef PWM_EMBEDDED
27#include <khtml_part.h>
28#include <khtmlview.h>
29#include <qtextedit.h>
30#else
31#include <qmultilineedit.h>
32#endif
33
34CommentBox::CommentBox(QWidget *_parentWidget)
35{
36 PWM_ASSERT(_parentWidget);
37 parentWidget = _parentWidget;
38 textDta = 0;
39#ifndef PWM_EMBEDDED
40 htmlDta = 0;
41#endif
42 mode = mode_notSet;
43}
44
45CommentBox::~CommentBox()
46{
47 clearText();
48 clearHtml();
49}
50
51void CommentBox::clear()
52{
53 clearText();
54 clearHtml();
55 mode = mode_notSet;
56}
57
58void CommentBox::clearText()
59{
60 delete_ifnot_null(textDta);
61}
62
63void CommentBox::clearHtml()
64{
65#ifndef PWM_EMBEDDED
66 delete_ifnot_null(htmlDta);
67#endif
68}
69
70void CommentBox::setText(const QString &text)
71{
72 switchTo(mode_text);
73 PWM_ASSERT(textDta);
74 textDta->setText(i18n("Comment") + ": " + text);
75 if (!textDta->isVisible())
76 textDta->show();
77}
78
79bool CommentBox::getText(QString *text)
80{
81 if (mode != mode_text)
82 return false;
83 PWM_ASSERT(text);
84 if (!textDta) {
85 *text = "";
86 return true;
87 }
88 *text = textDta->text();
89 return true;
90}
91
92void CommentBox::setHtml(QString code)
93{
94#ifndef PWM_EMBEDDED
95 switchTo(mode_html);
96 PWM_ASSERT(htmlDta);
97 if (!HtmlGen::replaceSSDummy(&code))
98 printWarn("CommentBox::setHtml(): replaceSSDummy() failed!");
99 htmlDta->begin();
100 htmlDta->write(code);
101 htmlDta->end();
102 htmlDta->show();
103#endif
104}
105
106void CommentBox::setContent(const QString &dta)
107{
108 // if there's no data, hide the comment-box
109 if (dta.isEmpty()) {
110 clear();
111 return;
112 }
113#ifndef PWM_EMBEDDED
114 if (HtmlGen::isHtml(dta)) {
115 setHtml(dta);
116 return;
117 }
118#endif
119 // we assume it's plain text
120 setText(dta);
121}
122
123void CommentBox::switchTo(commentBoxMode newMode)
124{
125 if (newMode == mode)
126 return;
127
128 // cleanup old mode
129 switch (mode) {
130 case mode_text:
131 clearText();
132 break;
133 case mode_html:
134 clearHtml();
135 break;
136 default:
137 break;
138 }
139
140 // setup new mode
141 switch (newMode) {
142 case mode_text:
143#ifndef PWM_EMBEDDED
144 textDta = new QTextEdit(parentWidget);
145 textDta->setTextFormat(Qt::PlainText);
146#else
147 textDta = new QMultiLineEdit(parentWidget);
148#endif
149 textDta->setReadOnly(true);
150 textDta->show();
151 break;
152 case mode_html:
153#ifndef PWM_EMBEDDED
154 htmlDta = new KHTMLPart(parentWidget, 0,
155 parentWidget);
156 htmlDta->show();
157#endif
158 break;
159 default:
160 BUG();
161 break;
162 }
163
164 mode = newMode;
165}
166
167void CommentBox::show()
168{
169 switch (mode) {
170 case mode_text:
171 PWM_ASSERT(textDta);
172 textDta->show();
173 break;
174 case mode_html:
175#ifndef PWM_EMBEDDED
176 PWM_ASSERT(htmlDta);
177 htmlDta->show();
178#endif
179 break;
180 default:
181 break;
182 }
183}
184
185void CommentBox::hide()
186{
187 switch (mode) {
188 case mode_text:
189 PWM_ASSERT(textDta);
190 textDta->hide();
191 break;
192 case mode_html:
193#ifndef PWM_EMBEDDED
194 PWM_ASSERT(htmlDta);
195 htmlDta->hide();
196#endif
197 break;
198 default:
199 break;
200 }
201}
202
203void CommentBox::resize(const QSize &size)
204{
205 switch (mode) {
206 case mode_text:
207 PWM_ASSERT(textDta);
208 textDta->resize(size);
209 break;
210 case mode_html:
211#ifndef PWM_EMBEDDED
212 PWM_ASSERT(htmlDta);
213 htmlDta->view()->resize(size);
214#endif
215 break;
216 default:
217 break;
218 }
219}
220
221QSize CommentBox::size()
222{
223 switch (mode) {
224 case mode_text:
225 PWM_ASSERT(textDta);
226 return textDta->size();
227 break;
228 case mode_html:
229#ifndef PWM_EMBEDDED
230 PWM_ASSERT(htmlDta);
231 return htmlDta->view()->size();
232#endif
233 break;
234 default:
235 break;
236 }
237 return QSize();
238}