• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

Plasma

  • plasma
  • widgets
textbrowser.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2009 Marco Martin <notmart@gmail.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License as
6 * published by the Free Software Foundation; either version 2, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20#include "textbrowser.h"
21
22#include <QGraphicsSceneWheelEvent>
23#include <QMenu>
24#include <QPainter>
25#include <QScrollBar>
26
27#include <kmimetype.h>
28#include <ktextbrowser.h>
29
30#include "svg.h"
31#include "theme.h"
32#include "private/style_p.h"
33#include "private/themedwidgetinterface_p.h"
34
35namespace Plasma
36{
37
38class TextBrowserPrivate : public ThemedWidgetInterface<TextBrowser>
39{
40public:
41 TextBrowserPrivate(TextBrowser *browser)
42 : ThemedWidgetInterface<TextBrowser>(browser),
43 savedMinimumHeight(0),
44 savedMaximumHeight(QWIDGETSIZE_MAX),
45 wasNotFixed(true)
46 {
47 }
48
49 void setFixedHeight()
50 {
51 KTextBrowser *native = q->nativeWidget();
52 if (native->document() &&
53 q->sizePolicy().verticalPolicy() == QSizePolicy::Fixed &&
54 native->verticalScrollBarPolicy() == Qt::ScrollBarAlwaysOff) {
55 native->document()->setTextWidth(q->size().width());
56 QSize s = native->document()->size().toSize();
57 if (wasNotFixed) {
58 savedMinimumHeight = q->minimumHeight();
59 savedMaximumHeight = q->maximumHeight();
60 wasNotFixed = false;
61 }
62 q->setMinimumHeight(s.height());
63 q->setMaximumHeight(s.height());
64 } else if (!wasNotFixed) {
65 q->setMinimumHeight(savedMinimumHeight);
66 q->setMaximumHeight(savedMaximumHeight);
67 wasNotFixed = true;
68 }
69 }
70
71 KTextBrowser *native;
72 Plasma::Style::Ptr style;
73 int savedMinimumHeight;
74 int savedMaximumHeight;
75 bool wasNotFixed;
76};
77
78TextBrowser::TextBrowser(QGraphicsWidget *parent)
79 : QGraphicsProxyWidget(parent),
80 d(new TextBrowserPrivate(this))
81{
82 KTextBrowser *native = new KTextBrowser;
83 native->setWindowFlags(native->windowFlags()|Qt::BypassGraphicsProxyWidget);
84 connect(native, SIGNAL(textChanged()), this, SIGNAL(textChanged()));
85 connect(native, SIGNAL(textChanged()), this, SLOT(setFixedHeight()));
86 native->setWindowIcon(QIcon());
87 d->setWidget(native);
88 d->native = native;
89 native->setAttribute(Qt::WA_NoSystemBackground);
90 native->setFrameShape(QFrame::NoFrame);
91 native->setTextBackgroundColor(Qt::transparent);
92 native->viewport()->setAutoFillBackground(false);
93 d->style = Plasma::Style::sharedStyle();
94 native->verticalScrollBar()->setStyle(d->style.data());
95 native->horizontalScrollBar()->setStyle(d->style.data());
96 d->initTheming();
97}
98
99TextBrowser::~TextBrowser()
100{
101 delete d;
102 Plasma::Style::doneWithSharedStyle();
103}
104
105void TextBrowser::setText(const QString &text)
106{
107 static_cast<KTextBrowser*>(widget())->setText(text);
108}
109
110QString TextBrowser::text() const
111{
112 return static_cast<KTextBrowser*>(widget())->toHtml();
113}
114
115void TextBrowser::setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy policy)
116{
117 nativeWidget()->setHorizontalScrollBarPolicy(policy);
118}
119
120void TextBrowser::setVerticalScrollBarPolicy(Qt::ScrollBarPolicy policy)
121{
122 nativeWidget()->setVerticalScrollBarPolicy(policy);
123}
124
125void TextBrowser::setStyleSheet(const QString &stylesheet)
126{
127 widget()->setStyleSheet(stylesheet);
128}
129
130QString TextBrowser::styleSheet()
131{
132 return widget()->styleSheet();
133}
134
135KTextBrowser *TextBrowser::nativeWidget() const
136{
137 return static_cast<KTextBrowser*>(widget());
138}
139
140void TextBrowser::append(const QString &text)
141{
142 return nativeWidget()->append(text);
143}
144
145void TextBrowser::dataUpdated(const QString &sourceName, const Plasma::DataEngine::Data &data)
146{
147 Q_UNUSED(sourceName)
148
149 KTextBrowser *te = nativeWidget();
150 te->clear();
151
152 foreach (const QVariant &v, data) {
153 if (v.canConvert(QVariant::String)) {
154 te->append(v.toString() + '\n');
155 }
156 }
157}
158
159void TextBrowser::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
160{
161 QMenu *popup = nativeWidget()->createStandardContextMenu(event->screenPos());
162 if (popup) {
163 popup->exec(event->screenPos());
164 delete popup;
165 }
166}
167
168void TextBrowser::resizeEvent(QGraphicsSceneResizeEvent *event)
169{
170 d->setFixedHeight();
171 QGraphicsProxyWidget::resizeEvent(event);
172}
173
174void TextBrowser::wheelEvent(QGraphicsSceneWheelEvent *event)
175{
176 if (nativeWidget()->verticalScrollBarPolicy() == Qt::ScrollBarAlwaysOff &&
177 nativeWidget()->horizontalScrollBarPolicy() == Qt::ScrollBarAlwaysOff) {
178 event->ignore();
179 } else {
180 QGraphicsProxyWidget::wheelEvent(event);
181 }
182}
183
184void TextBrowser::changeEvent(QEvent *event)
185{
186 d->changeEvent(event);
187 QGraphicsProxyWidget::changeEvent(event);
188}
189
190} // namespace Plasma
191
192#include <textbrowser.moc>
193
Plasma::DataEngine::Data
QHash< QString, QVariant > Data
Definition: dataengine.h:68
Plasma::TextBrowser::setText
void setText(const QString &text)
Sets the display text for this TextBrowser.
Definition: textbrowser.cpp:105
Plasma::TextBrowser::textChanged
void textChanged()
Plasma::TextBrowser::nativeWidget
KTextBrowser * nativeWidget
Definition: textbrowser.h:49
Plasma::TextBrowser::text
QString text
Definition: textbrowser.h:47
QGraphicsProxyWidget
QGraphicsWidget
Plasma
Namespace for everything in libplasma.
Definition: abstractdialogmanager.cpp:25
svg.h
textbrowser.h
theme.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal