Plasma
textbrowser.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "textbrowser.h"
00021
00022 #include <QPainter>
00023 #include <QScrollBar>
00024 #include <QGraphicsSceneWheelEvent>
00025
00026 #include <kmimetype.h>
00027 #include <ktextbrowser.h>
00028
00029 #include "plasma/theme.h"
00030 #include "plasma/svg.h"
00031 #include "private/style_p.h"
00032
00033 namespace Plasma
00034 {
00035
00036 class TextBrowserPrivate
00037 {
00038 public:
00039 TextBrowserPrivate(TextBrowser *browser)
00040 : q(browser),
00041 native(0),
00042 savedMinimumHeight(0),
00043 savedMaximumHeight(QWIDGETSIZE_MAX),
00044 wasNotFixed(true)
00045 {
00046 }
00047
00048 void setFixedHeight()
00049 {
00050 if (native && native->document() &&
00051 q->sizePolicy().verticalPolicy() == QSizePolicy::Fixed &&
00052 native->verticalScrollBarPolicy() == Qt::ScrollBarAlwaysOff) {
00053 native->document()->setTextWidth(q->size().width());
00054 QSize s = native->document()->size().toSize();
00055 if (wasNotFixed) {
00056 savedMinimumHeight = q->minimumHeight();
00057 savedMaximumHeight = q->maximumHeight();
00058 wasNotFixed = false;
00059 }
00060 q->setMinimumHeight(s.height());
00061 q->setMaximumHeight(s.height());
00062 } else if (!wasNotFixed) {
00063 q->setMinimumHeight(savedMinimumHeight);
00064 q->setMaximumHeight(savedMaximumHeight);
00065 wasNotFixed = true;
00066 }
00067 }
00068
00069
00070 TextBrowser *q;
00071 KTextBrowser *native;
00072 Plasma::Style::Ptr style;
00073 int savedMinimumHeight;
00074 int savedMaximumHeight;
00075 bool wasNotFixed;
00076 };
00077
00078 TextBrowser::TextBrowser(QGraphicsWidget *parent)
00079 : QGraphicsProxyWidget(parent),
00080 d(new TextBrowserPrivate(this))
00081 {
00082 KTextBrowser *native = new KTextBrowser;
00083 connect(native, SIGNAL(textChanged()), this, SIGNAL(textChanged()));
00084 connect(native, SIGNAL(textChanged()), this, SLOT(setFixedHeight()));
00085 setWidget(native);
00086 d->native = native;
00087 native->setAttribute(Qt::WA_NoSystemBackground);
00088 native->setFrameShape(QFrame::NoFrame);
00089 native->setTextBackgroundColor(Qt::transparent);
00090 native->viewport()->setAutoFillBackground(false);
00091 d->style = Plasma::Style::sharedStyle();
00092 native->verticalScrollBar()->setStyle(d->style.data());
00093 native->horizontalScrollBar()->setStyle(d->style.data());
00094 }
00095
00096 TextBrowser::~TextBrowser()
00097 {
00098 delete d;
00099 Plasma::Style::doneWithSharedStyle();
00100 }
00101
00102 void TextBrowser::setText(const QString &text)
00103 {
00104
00105 static_cast<KTextBrowser*>(widget())->setHtml(text);
00106 }
00107
00108 QString TextBrowser::text() const
00109 {
00110 return static_cast<KTextBrowser*>(widget())->toHtml();
00111 }
00112
00113 void TextBrowser::setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy policy)
00114 {
00115 d->native->setHorizontalScrollBarPolicy(policy);
00116 }
00117
00118 void TextBrowser::setVerticalScrollBarPolicy(Qt::ScrollBarPolicy policy)
00119 {
00120 d->native->setVerticalScrollBarPolicy(policy);
00121 }
00122
00123 void TextBrowser::setStyleSheet(const QString &stylesheet)
00124 {
00125 widget()->setStyleSheet(stylesheet);
00126 }
00127
00128 QString TextBrowser::styleSheet()
00129 {
00130 return widget()->styleSheet();
00131 }
00132
00133 KTextBrowser *TextBrowser::nativeWidget() const
00134 {
00135 return static_cast<KTextBrowser*>(widget());
00136 }
00137
00138 void TextBrowser::dataUpdated(const QString &sourceName, const Plasma::DataEngine::Data &data)
00139 {
00140 Q_UNUSED(sourceName)
00141
00142 KTextBrowser *te = nativeWidget();
00143 te->clear();
00144
00145 foreach (const QVariant &v, data) {
00146 if (v.canConvert(QVariant::String)) {
00147 te->append(v.toString() + '\n');
00148 }
00149 }
00150 }
00151
00152 void TextBrowser::resizeEvent(QGraphicsSceneResizeEvent *event)
00153 {
00154 d->setFixedHeight();
00155 QGraphicsProxyWidget::resizeEvent(event);
00156 }
00157
00158 void TextBrowser::wheelEvent(QGraphicsSceneWheelEvent *event)
00159 {
00160 if (d->native->verticalScrollBarPolicy() == Qt::ScrollBarAlwaysOff &&
00161 d->native->horizontalScrollBarPolicy() == Qt::ScrollBarAlwaysOff) {
00162 event->ignore();
00163 } else {
00164 QGraphicsProxyWidget::wheelEvent(event);
00165 }
00166 }
00167
00168 }
00169
00170 #include <textbrowser.moc>
00171