Created
December 11, 2012 19:02
-
-
Save robertknight/4261089 to your computer and use it in GitHub Desktop.
WebLabel src
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "weblabel.h" | |
#include <QtWebKit/QWebFrame> | |
#include <QtWebKit/QWebSettings> | |
#include <QtGui/QPainter> | |
WebKitLabel::WebKitLabel(const QString& text, QWidget* parent) | |
: QFrame(parent) | |
, m_webPage(new QWebPage(this)) | |
{ | |
m_webPage->setView(this); | |
connect(m_webPage, SIGNAL(linkClicked(QUrl)), | |
this, SLOT(handleLinkClick(QUrl))); | |
connect(m_webPage, SIGNAL(linkHovered(QString, QString, QString)), | |
this, SLOT(handleLinkHover(QString))); | |
//m_webPage->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff); | |
m_webPage->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff); | |
updatePalette(); | |
updateFonts(); | |
setText(text); | |
} | |
void WebKitLabel::updatePalette() | |
{ | |
QPalette palette = this->palette(); | |
palette.setBrush(QPalette::Base, Qt::transparent); | |
m_webPage->setPalette(palette); | |
} | |
void WebKitLabel::updateFonts() | |
{ | |
QWebSettings* settings = m_webPage->settings(); | |
settings->setFontFamily(QWebSettings::StandardFont, font().family()); | |
settings->setFontSize(QWebSettings::DefaultFontSize, font().pointSize()); | |
} | |
void WebKitLabel::setText(const QString& text) | |
{ | |
m_webPage->mainFrame()->setHtml(text); | |
m_mainElement = m_webPage->mainFrame()->findFirstElement("body"); | |
m_mainElement.setStyleProperty("margin", "0"); | |
m_mainElement.setStyleProperty("padding", "0"); | |
updateGeometry(); | |
} | |
QString WebKitLabel::text() const | |
{ | |
return m_webPage->mainFrame()->toHtml(); | |
} | |
void WebKitLabel::paintEvent(QPaintEvent* event) | |
{ | |
QFrame::paintEvent(event); | |
QPainter painter(this); | |
m_webPage->mainFrame()->render(&painter); | |
} | |
void WebKitLabel::resizeEvent(QResizeEvent*) | |
{ | |
m_webPage->setViewportSize(size()); | |
} | |
QSize WebKitLabel::sizeHint() const | |
{ | |
return m_mainElement.geometry().size(); | |
} | |
QSize WebKitLabel::minimumSizeHint() const | |
{ | |
return sizeHint(); | |
} | |
void WebKitLabel::handleLinkClick(const QUrl& url) | |
{ | |
emit linkActivated(url.toString()); | |
} | |
void WebKitLabel::handleLinkHover(const QString& link) | |
{ | |
emit linkHovered(link); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <QtGui/QFrame> | |
#include <QtWebKit/QWebElement> | |
#include <QtWebKit/QWebPage> | |
class WebKitLabel : public QFrame | |
{ | |
Q_OBJECT | |
public: | |
WebKitLabel(const QString& text, QWidget* parent = 0); | |
void setText(const QString& text); | |
QString text() const; | |
virtual QSize sizeHint() const; | |
virtual QSize minimumSizeHint() const; | |
Q_SIGNALS: | |
void linkActivated(const QString& link); | |
void linkHovered(const QString& link); | |
protected: | |
virtual void paintEvent(QPaintEvent* event); | |
virtual void resizeEvent(QResizeEvent* event); | |
private Q_SLOTS: | |
void handleLinkClick(const QUrl& url); | |
void handleLinkHover(const QString& link); | |
private: | |
void updatePalette(); | |
void updateFonts(); | |
QWebPage* m_webPage; | |
QWebElement m_mainElement; | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment