examplewidget.h Example File
maemo5/webview/examplewidget.h
#ifndef EXAMPLEWIDGET_H
#define EXAMPLEWIDGET_H
#include "qwebviewselectionsuppressor.h"
#include <QtGui/qwidget.h>
#include <QtGui/qradiobutton.h>
#include <QtGui/qbuttongroup.h>
#include <QtGui/qabstractkineticscroller.h>
#include <QtGui/qlayout.h>
#include <QtWebKit/qwebview.h>
#include <QApplication>
class ExampleWidget : public QWidget
{
Q_OBJECT
public:
ExampleWidget()
{
const char exampleText[] =
"<html>"
"<body>"
"<h1>WebKit Example</h1>"
"<p>"
"By default, a mouse move on the screen selects text. However, while "
"finger scrolling, the view should just scroll, not select text. This example "
"shows how to achieve the two different modes - either selecting text while "
"swiping, or scrolling while swiping."
"</p>"
"<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />"
"Some dummy text."
"<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />"
"End of page.";
QVBoxLayout *layout = new QVBoxLayout(this);
view = new QWebView;
suppressor = new QWebViewSelectionSuppressor(view);
QUrl url = QUrl::fromUserInput(QApplication::arguments().value(1));
if (!url.isValid()) {
view->setHtml(QLatin1String(exampleText));
} else {
view->settings()->setAttribute(QWebSettings::PluginsEnabled, true);
view->load(url);
}
layout->addWidget(view);
scrollButton = new QRadioButton("Scrolling Mode");
scrollButton->setChecked(true);
textButton = new QRadioButton("Text Selection Mode");
QButtonGroup *mode = new QButtonGroup(this);
mode->addButton(scrollButton);
mode->addButton(textButton);
QHBoxLayout *modeLayout = new QHBoxLayout();
modeLayout->setSpacing(0);
modeLayout->addWidget(scrollButton);
modeLayout->addWidget(textButton);
layout->addLayout(modeLayout);
connect(mode, SIGNAL(buttonClicked(QAbstractButton*)), SLOT(buttonClicked(QAbstractButton*)));
}
private Q_SLOTS:
void buttonClicked(QAbstractButton *button)
{
Q_ASSERT(view->property("kineticScroller").value<QAbstractKineticScroller *>());
if (button == scrollButton) {
suppressor->enable();
view->property("kineticScroller").value<QAbstractKineticScroller *>()->setEnabled(true);
}
if (button == textButton) {
suppressor->disable();
view->property("kineticScroller").value<QAbstractKineticScroller *>()->setEnabled(false);
}
}
private:
QWebView *view;
QWebViewSelectionSuppressor *suppressor;
QRadioButton *scrollButton;
QRadioButton *textButton;
};
#endif