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

Plasma

  • sources
  • kde-4.12
  • kdelibs
  • 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 
35 namespace Plasma
36 {
37 
38 class TextBrowserPrivate : public ThemedWidgetInterface<TextBrowser>
39 {
40 public:
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 
78 TextBrowser::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 
99 TextBrowser::~TextBrowser()
100 {
101  delete d;
102  Plasma::Style::doneWithSharedStyle();
103 }
104 
105 void TextBrowser::setText(const QString &text)
106 {
107  static_cast<KTextBrowser*>(widget())->setText(text);
108 }
109 
110 QString TextBrowser::text() const
111 {
112  return static_cast<KTextBrowser*>(widget())->toHtml();
113 }
114 
115 void TextBrowser::setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy policy)
116 {
117  nativeWidget()->setHorizontalScrollBarPolicy(policy);
118 }
119 
120 void TextBrowser::setVerticalScrollBarPolicy(Qt::ScrollBarPolicy policy)
121 {
122  nativeWidget()->setVerticalScrollBarPolicy(policy);
123 }
124 
125 void TextBrowser::setStyleSheet(const QString &stylesheet)
126 {
127  widget()->setStyleSheet(stylesheet);
128 }
129 
130 QString TextBrowser::styleSheet()
131 {
132  return widget()->styleSheet();
133 }
134 
135 KTextBrowser *TextBrowser::nativeWidget() const
136 {
137  return static_cast<KTextBrowser*>(widget());
138 }
139 
140 void TextBrowser::append(const QString &text)
141 {
142  return nativeWidget()->append(text);
143 }
144 
145 void 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 
159 void 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 
168 void TextBrowser::resizeEvent(QGraphicsSceneResizeEvent *event)
169 {
170  d->setFixedHeight();
171  QGraphicsProxyWidget::resizeEvent(event);
172 }
173 
174 void 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 
184 void 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::TextBrowser::setHorizontalScrollBarPolicy
void setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy policy)
Sets the policy used to show/hide the horizontal scrollbar.
Definition: textbrowser.cpp:115
textbrowser.h
Plasma::TextBrowser::TextBrowser
TextBrowser(QGraphicsWidget *parent=0)
Definition: textbrowser.cpp:78
Plasma::TextBrowser::dataUpdated
void dataUpdated(const QString &sourceName, const Plasma::DataEngine::Data &data)
Definition: textbrowser.cpp:145
theme.h
Plasma::TextBrowser::wheelEvent
void wheelEvent(QGraphicsSceneWheelEvent *event)
Definition: textbrowser.cpp:174
Plasma::DataEngine::Data
QHash< QString, QVariant > Data
Definition: dataengine.h:68
Plasma::TextBrowser::setVerticalScrollBarPolicy
void setVerticalScrollBarPolicy(Qt::ScrollBarPolicy policy)
Sets the policy used to show/hide the vertical scrollbar.
Definition: textbrowser.cpp:120
Plasma::TextBrowser::styleSheet
QString styleSheet()
Plasma::TextBrowser::append
void append(const QString &text)
Allows appending text to the text browser.
Definition: textbrowser.cpp:140
Plasma::TextBrowser::~TextBrowser
~TextBrowser()
Definition: textbrowser.cpp:99
Plasma::TextBrowser::contextMenuEvent
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
Definition: textbrowser.cpp:159
Plasma::TextBrowser::setText
void setText(const QString &text)
Sets the display text for this TextBrowser.
Definition: textbrowser.cpp:105
QGraphicsProxyWidget
Plasma::TextBrowser::setStyleSheet
void setStyleSheet(const QString &stylesheet)
Sets the stylesheet used to control the visual display of this TextBrowser.
Definition: textbrowser.cpp:125
Plasma::TextBrowser::nativeWidget
KTextBrowser * nativeWidget() const
Plasma::TextBrowser::textChanged
void textChanged()
Plasma::TextBrowser::resizeEvent
void resizeEvent(QGraphicsSceneResizeEvent *event)
Definition: textbrowser.cpp:168
Plasma::TextBrowser::changeEvent
void changeEvent(QEvent *event)
Definition: textbrowser.cpp:184
Plasma::TextBrowser::text
QString text() const
svg.h
QGraphicsWidget
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:34 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



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