Plasma
textbrowser.cpp
Go to the documentation of this file.
00001 /* 00002 * Copyright 2009 Marco Martin <notmart@gmail.com> 00003 * 00004 * This program is free software; you can redistribute it and/or modify 00005 * it under the terms of the GNU Library General Public License as 00006 * published by the Free Software Foundation; either version 2, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details 00013 * 00014 * You should have received a copy of the GNU Library General Public 00015 * License along with this program; if not, write to the 00016 * Free Software Foundation, Inc., 00017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "textbrowser.h" 00021 00022 #include <QGraphicsSceneWheelEvent> 00023 #include <QMenu> 00024 #include <QPainter> 00025 #include <QScrollBar> 00026 00027 #include <kmimetype.h> 00028 #include <ktextbrowser.h> 00029 00030 #include "svg.h" 00031 #include "theme.h" 00032 #include "private/style_p.h" 00033 #include "private/themedwidgetinterface_p.h" 00034 00035 namespace Plasma 00036 { 00037 00038 class TextBrowserPrivate : public ThemedWidgetInterface<TextBrowser> 00039 { 00040 public: 00041 TextBrowserPrivate(TextBrowser *browser) 00042 : ThemedWidgetInterface<TextBrowser>(browser), 00043 native(0), 00044 savedMinimumHeight(0), 00045 savedMaximumHeight(QWIDGETSIZE_MAX), 00046 wasNotFixed(true) 00047 { 00048 } 00049 00050 void setFixedHeight() 00051 { 00052 if (native && native->document() && 00053 q->sizePolicy().verticalPolicy() == QSizePolicy::Fixed && 00054 native->verticalScrollBarPolicy() == Qt::ScrollBarAlwaysOff) { 00055 native->document()->setTextWidth(q->size().width()); 00056 QSize s = native->document()->size().toSize(); 00057 if (wasNotFixed) { 00058 savedMinimumHeight = q->minimumHeight(); 00059 savedMaximumHeight = q->maximumHeight(); 00060 wasNotFixed = false; 00061 } 00062 q->setMinimumHeight(s.height()); 00063 q->setMaximumHeight(s.height()); 00064 } else if (!wasNotFixed) { 00065 q->setMinimumHeight(savedMinimumHeight); 00066 q->setMaximumHeight(savedMaximumHeight); 00067 wasNotFixed = true; 00068 } 00069 } 00070 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 native->setWindowFlags(native->windowFlags()|Qt::BypassGraphicsProxyWidget); 00084 connect(native, SIGNAL(textChanged()), this, SIGNAL(textChanged())); 00085 connect(native, SIGNAL(textChanged()), this, SLOT(setFixedHeight())); 00086 native->setWindowIcon(QIcon()); 00087 setWidget(native); 00088 d->native = native; 00089 native->setAttribute(Qt::WA_NoSystemBackground); 00090 native->setFrameShape(QFrame::NoFrame); 00091 native->setTextBackgroundColor(Qt::transparent); 00092 native->viewport()->setAutoFillBackground(false); 00093 d->style = Plasma::Style::sharedStyle(); 00094 native->verticalScrollBar()->setStyle(d->style.data()); 00095 native->horizontalScrollBar()->setStyle(d->style.data()); 00096 d->initTheming(); 00097 } 00098 00099 TextBrowser::~TextBrowser() 00100 { 00101 delete d; 00102 Plasma::Style::doneWithSharedStyle(); 00103 } 00104 00105 void TextBrowser::setText(const QString &text) 00106 { 00107 static_cast<KTextBrowser*>(widget())->setText(text); 00108 } 00109 00110 QString TextBrowser::text() const 00111 { 00112 return static_cast<KTextBrowser*>(widget())->toHtml(); 00113 } 00114 00115 void TextBrowser::setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy policy) 00116 { 00117 d->native->setHorizontalScrollBarPolicy(policy); 00118 } 00119 00120 void TextBrowser::setVerticalScrollBarPolicy(Qt::ScrollBarPolicy policy) 00121 { 00122 d->native->setVerticalScrollBarPolicy(policy); 00123 } 00124 00125 void TextBrowser::setStyleSheet(const QString &stylesheet) 00126 { 00127 widget()->setStyleSheet(stylesheet); 00128 } 00129 00130 QString TextBrowser::styleSheet() 00131 { 00132 return widget()->styleSheet(); 00133 } 00134 00135 KTextBrowser *TextBrowser::nativeWidget() const 00136 { 00137 return static_cast<KTextBrowser*>(widget()); 00138 } 00139 00140 void TextBrowser::append(const QString &text) 00141 { 00142 return nativeWidget()->append(text); 00143 } 00144 00145 void TextBrowser::dataUpdated(const QString &sourceName, const Plasma::DataEngine::Data &data) 00146 { 00147 Q_UNUSED(sourceName) 00148 00149 KTextBrowser *te = nativeWidget(); 00150 te->clear(); 00151 00152 foreach (const QVariant &v, data) { 00153 if (v.canConvert(QVariant::String)) { 00154 te->append(v.toString() + '\n'); 00155 } 00156 } 00157 } 00158 00159 void TextBrowser::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) 00160 { 00161 QMenu *popup = nativeWidget()->createStandardContextMenu(event->screenPos()); 00162 if (popup) { 00163 popup->exec(event->screenPos()); 00164 delete popup; 00165 } 00166 } 00167 00168 void TextBrowser::resizeEvent(QGraphicsSceneResizeEvent *event) 00169 { 00170 d->setFixedHeight(); 00171 QGraphicsProxyWidget::resizeEvent(event); 00172 } 00173 00174 void TextBrowser::wheelEvent(QGraphicsSceneWheelEvent *event) 00175 { 00176 if (d->native->verticalScrollBarPolicy() == Qt::ScrollBarAlwaysOff && 00177 d->native->horizontalScrollBarPolicy() == Qt::ScrollBarAlwaysOff) { 00178 event->ignore(); 00179 } else { 00180 QGraphicsProxyWidget::wheelEvent(event); 00181 } 00182 } 00183 00184 void TextBrowser::changeEvent(QEvent *event) 00185 { 00186 d->changeEvent(event); 00187 QGraphicsProxyWidget::changeEvent(event); 00188 } 00189 00190 } // namespace Plasma 00191 00192 #include <textbrowser.moc> 00193
KDE 4.6 API Reference