2009-03-03 18:38:16 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2009 Marco Martin <notmart@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "textbrowser.h"
|
|
|
|
|
2009-09-03 11:58:01 +02:00
|
|
|
#include <QGraphicsSceneWheelEvent>
|
2009-12-28 20:52:54 +01:00
|
|
|
#include <QMenu>
|
2010-10-22 02:25:17 +02:00
|
|
|
#include <QPainter>
|
|
|
|
#include <QScrollBar>
|
2009-03-03 18:38:16 +01:00
|
|
|
|
|
|
|
#include <ktextbrowser.h>
|
|
|
|
|
2010-10-22 02:25:17 +02:00
|
|
|
#include "svg.h"
|
|
|
|
#include "theme.h"
|
2009-03-03 18:38:16 +01:00
|
|
|
#include "private/style_p.h"
|
2010-10-22 02:25:17 +02:00
|
|
|
#include "private/themedwidgetinterface_p.h"
|
2009-03-03 18:38:16 +01:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2010-10-22 02:25:17 +02:00
|
|
|
class TextBrowserPrivate : public ThemedWidgetInterface<TextBrowser>
|
2009-03-03 18:38:16 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
TextBrowserPrivate(TextBrowser *browser)
|
2010-10-22 18:55:46 +02:00
|
|
|
: ThemedWidgetInterface<TextBrowser>(browser),
|
2009-04-02 17:16:53 +02:00
|
|
|
savedMinimumHeight(0),
|
|
|
|
savedMaximumHeight(QWIDGETSIZE_MAX),
|
2010-10-22 02:25:17 +02:00
|
|
|
wasNotFixed(true)
|
2009-03-03 18:38:16 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void setFixedHeight()
|
|
|
|
{
|
2011-12-04 13:59:54 +01:00
|
|
|
KTextBrowser *native = q->nativeWidget();
|
|
|
|
if (native->document() &&
|
2009-03-03 18:38:16 +01:00
|
|
|
q->sizePolicy().verticalPolicy() == QSizePolicy::Fixed &&
|
|
|
|
native->verticalScrollBarPolicy() == Qt::ScrollBarAlwaysOff) {
|
|
|
|
native->document()->setTextWidth(q->size().width());
|
|
|
|
QSize s = native->document()->size().toSize();
|
2009-04-02 17:16:53 +02:00
|
|
|
if (wasNotFixed) {
|
|
|
|
savedMinimumHeight = q->minimumHeight();
|
|
|
|
savedMaximumHeight = q->maximumHeight();
|
|
|
|
wasNotFixed = false;
|
|
|
|
}
|
2009-03-03 18:38:16 +01:00
|
|
|
q->setMinimumHeight(s.height());
|
|
|
|
q->setMaximumHeight(s.height());
|
2009-04-02 17:16:53 +02:00
|
|
|
} else if (!wasNotFixed) {
|
|
|
|
q->setMinimumHeight(savedMinimumHeight);
|
|
|
|
q->setMaximumHeight(savedMaximumHeight);
|
|
|
|
wasNotFixed = true;
|
2009-03-03 18:38:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
KTextBrowser *native;
|
|
|
|
Plasma::Style::Ptr style;
|
2009-04-02 17:16:53 +02:00
|
|
|
int savedMinimumHeight;
|
|
|
|
int savedMaximumHeight;
|
|
|
|
bool wasNotFixed;
|
2009-03-03 18:38:16 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
TextBrowser::TextBrowser(QGraphicsWidget *parent)
|
|
|
|
: QGraphicsProxyWidget(parent),
|
|
|
|
d(new TextBrowserPrivate(this))
|
|
|
|
{
|
|
|
|
KTextBrowser *native = new KTextBrowser;
|
2009-12-28 20:52:54 +01:00
|
|
|
native->setWindowFlags(native->windowFlags()|Qt::BypassGraphicsProxyWidget);
|
2009-03-03 18:38:16 +01:00
|
|
|
connect(native, SIGNAL(textChanged()), this, SIGNAL(textChanged()));
|
|
|
|
connect(native, SIGNAL(textChanged()), this, SLOT(setFixedHeight()));
|
2010-07-13 23:49:01 +02:00
|
|
|
native->setWindowIcon(QIcon());
|
2011-12-04 13:59:54 +01:00
|
|
|
d->setWidget(native);
|
2009-03-03 18:38:16 +01:00
|
|
|
d->native = native;
|
|
|
|
native->setAttribute(Qt::WA_NoSystemBackground);
|
|
|
|
native->setFrameShape(QFrame::NoFrame);
|
|
|
|
native->setTextBackgroundColor(Qt::transparent);
|
|
|
|
native->viewport()->setAutoFillBackground(false);
|
|
|
|
d->style = Plasma::Style::sharedStyle();
|
|
|
|
native->verticalScrollBar()->setStyle(d->style.data());
|
|
|
|
native->horizontalScrollBar()->setStyle(d->style.data());
|
2010-10-22 02:25:17 +02:00
|
|
|
d->initTheming();
|
2009-03-03 18:38:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TextBrowser::~TextBrowser()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
Plasma::Style::doneWithSharedStyle();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextBrowser::setText(const QString &text)
|
|
|
|
{
|
2009-11-14 04:58:33 +01:00
|
|
|
static_cast<KTextBrowser*>(widget())->setText(text);
|
2009-03-03 18:38:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString TextBrowser::text() const
|
|
|
|
{
|
|
|
|
return static_cast<KTextBrowser*>(widget())->toHtml();
|
|
|
|
}
|
|
|
|
|
2009-05-23 12:23:07 +02:00
|
|
|
void TextBrowser::setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy policy)
|
|
|
|
{
|
2011-12-04 13:59:54 +01:00
|
|
|
nativeWidget()->setHorizontalScrollBarPolicy(policy);
|
2009-05-23 12:23:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextBrowser::setVerticalScrollBarPolicy(Qt::ScrollBarPolicy policy)
|
|
|
|
{
|
2011-12-04 13:59:54 +01:00
|
|
|
nativeWidget()->setVerticalScrollBarPolicy(policy);
|
2009-05-23 12:23:07 +02:00
|
|
|
}
|
|
|
|
|
2009-03-03 18:38:16 +01:00
|
|
|
void TextBrowser::setStyleSheet(const QString &stylesheet)
|
|
|
|
{
|
|
|
|
widget()->setStyleSheet(stylesheet);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString TextBrowser::styleSheet()
|
|
|
|
{
|
|
|
|
return widget()->styleSheet();
|
|
|
|
}
|
|
|
|
|
|
|
|
KTextBrowser *TextBrowser::nativeWidget() const
|
|
|
|
{
|
|
|
|
return static_cast<KTextBrowser*>(widget());
|
|
|
|
}
|
|
|
|
|
2010-01-03 02:49:52 +01:00
|
|
|
void TextBrowser::append(const QString &text)
|
|
|
|
{
|
|
|
|
return nativeWidget()->append(text);
|
|
|
|
}
|
|
|
|
|
2009-03-03 18:38:16 +01:00
|
|
|
void TextBrowser::dataUpdated(const QString &sourceName, const Plasma::DataEngine::Data &data)
|
|
|
|
{
|
|
|
|
Q_UNUSED(sourceName)
|
|
|
|
|
|
|
|
KTextBrowser *te = nativeWidget();
|
|
|
|
te->clear();
|
|
|
|
|
|
|
|
foreach (const QVariant &v, data) {
|
|
|
|
if (v.canConvert(QVariant::String)) {
|
|
|
|
te->append(v.toString() + '\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-28 20:52:54 +01:00
|
|
|
void TextBrowser::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
|
|
|
|
{
|
|
|
|
QMenu *popup = nativeWidget()->createStandardContextMenu(event->screenPos());
|
2010-07-29 16:49:06 +02:00
|
|
|
if (popup) {
|
|
|
|
popup->exec(event->screenPos());
|
|
|
|
delete popup;
|
|
|
|
}
|
2009-12-28 20:52:54 +01:00
|
|
|
}
|
|
|
|
|
2009-03-03 18:38:16 +01:00
|
|
|
void TextBrowser::resizeEvent(QGraphicsSceneResizeEvent *event)
|
|
|
|
{
|
|
|
|
d->setFixedHeight();
|
|
|
|
QGraphicsProxyWidget::resizeEvent(event);
|
|
|
|
}
|
|
|
|
|
2009-09-03 11:58:01 +02:00
|
|
|
void TextBrowser::wheelEvent(QGraphicsSceneWheelEvent *event)
|
|
|
|
{
|
2011-12-04 13:59:54 +01:00
|
|
|
if (nativeWidget()->verticalScrollBarPolicy() == Qt::ScrollBarAlwaysOff &&
|
|
|
|
nativeWidget()->horizontalScrollBarPolicy() == Qt::ScrollBarAlwaysOff) {
|
2009-09-03 11:58:01 +02:00
|
|
|
event->ignore();
|
|
|
|
} else {
|
|
|
|
QGraphicsProxyWidget::wheelEvent(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-13 21:18:29 +02:00
|
|
|
void TextBrowser::changeEvent(QEvent *event)
|
|
|
|
{
|
2010-10-22 02:25:17 +02:00
|
|
|
d->changeEvent(event);
|
2009-09-13 21:18:29 +02:00
|
|
|
QGraphicsProxyWidget::changeEvent(event);
|
|
|
|
}
|
|
|
|
|
2009-03-03 18:38:16 +01:00
|
|
|
} // namespace Plasma
|
|
|
|
|
|
|
|
|
2012-02-08 23:33:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
#include "moc_textbrowser.cpp"
|