/*************************************************************************** * * * 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 #ifndef PWM_EMBEDDED #include #include #include #else #include #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(); }