3 #include <QDesktopServices>
8 #include <QtGui/QX11Info>
10 #include <X11/Xatom.h>
12 #include "contentwindow.h"
14 /* Got ViewportItem and GraphicsView from maemobrowser in the qt examples. The
15 * whole point of this is to get a finger friendly web widget */
17 class ViewportItem : public QGraphicsWidget, public QAbstractKineticScroller {
21 ViewportItem() : QGraphicsWidget(), QAbstractKineticScroller(), m_widget(0), m_ignoreEvents(false) {
22 setFlag(QGraphicsItem::ItemHasNoContents, true);
23 setFlag(QGraphicsItem::ItemClipsChildrenToShape, true);
24 setFlag(QGraphicsItem::ItemClipsToShape, true);
25 setAttribute(Qt::WA_OpaquePaintEvent, true);
26 setFiltersChildEvents(true);
29 void setWidget(QGraphicsWidget *widget) {
31 m_widget->setParentItem(0);
36 m_widget->setParentItem(this);
37 m_widget->setAttribute(Qt::WA_OpaquePaintEvent, true);
39 if (qgraphicsitem_cast<QGraphicsWebView *>(m_widget)) {
40 connect(m_widget, SIGNAL(loadProgress(int)), this, SLOT(resizeWebViewToFrame()));
41 connect(m_widget, SIGNAL(loadFinished(bool)), this, SLOT(resizeWebViewToFrame()));
42 resizeWebViewToFrame();
56 bool sceneEventFilter(QGraphicsItem *i, QEvent *e) {
58 if (i && (i == m_widget) && !m_ignoreEvents && m_widget->isEnabled()) {
60 case QEvent::GraphicsSceneMousePress:
61 case QEvent::GraphicsSceneMouseMove:
62 case QEvent::GraphicsSceneMouseRelease:
63 res = handleMouseEvent(static_cast<QGraphicsSceneMouseEvent *>(e));
69 // prevent text selection and image dragging
70 if (e->type() == QEvent::GraphicsSceneMouseMove)
72 return res ? true : QGraphicsWidget::sceneEventFilter(i, e);
75 void keyPressEvent(QKeyEvent *e) {
80 y = m_widget->y() - 50;
81 h = (m_widget->size() - size()).height();
82 m_widget->setY(y < -h ? -h : y);
86 y = m_widget->y() + 50;
87 m_widget->setY(y > 0 ? 0 : y);
91 x = m_widget->x() - 50;
92 w = (m_widget->size() - size()).width();
93 m_widget->setX(x < -w ? -w : x);
97 x = m_widget->x() + 50;
98 m_widget->setX(x > 0 ? 0 : x);
102 if(qgraphicsitem_cast<QGraphicsWebView *>(m_widget)) {
103 QGraphicsWebView *wv = (QGraphicsWebView *)m_widget;
104 wv->setZoomFactor(wv->zoomFactor() * 1.25);
105 resizeWebViewToFrame();
110 if(qgraphicsitem_cast<QGraphicsWebView *>(m_widget)) {
111 QGraphicsWebView *wv = (QGraphicsWebView *)m_widget;
112 wv->setZoomFactor(wv->zoomFactor() / 1.25);
113 resizeWebViewToFrame();
119 emit showNextEntry();
124 emit showPrevEntry();
128 if(e->modifiers() & Qt::ShiftModifier) {
129 y = m_widget->y() + size().height();
130 m_widget->setY(y > 0 ? 0 : y);
133 y = m_widget->y() - size().height();;
134 h = (m_widget->size() - size()).height();
135 m_widget->setY(y < -h ? -h : y);
140 if(e->modifiers() & Qt::ShiftModifier)
143 emit toggleStarred();
156 QSize viewportSize() const {
157 return size().toSize();
160 QPoint maximumScrollPosition() const {
161 QSizeF s = m_widget ? m_widget->size() - size() : QSize(0, 0);
162 return QPoint(qMax(0, int(s.width())), qMax(0, int(s.height())));
165 QPoint scrollPosition() const {
166 return m_widget ? -m_widget->pos().toPoint() + m_overShoot : QPoint();
169 void setScrollPosition(const QPoint &p, const QPoint &overShoot) {
170 m_overShoot = overShoot;
172 m_widget->setPos(-p + m_overShoot);
175 void sendEvent(QGraphicsItem *i, QEvent *e) {
176 m_ignoreEvents = true;
177 scene()->sendEvent(i, e);
178 m_ignoreEvents = false;
182 void resizeWebViewToFrame() {
183 if (QGraphicsWebView *view = qgraphicsitem_cast<QGraphicsWebView *>(m_widget)) {
184 if (view->page() && view->page()->mainFrame()) {
185 QSizeF s = view->page()->mainFrame()->contentsSize();
186 /* TODO: Figure out the proper way to
187 * get this 800 pixels. */
190 s = s.expandedTo(s2);
191 view->setGeometry(QRectF(view->geometry().topLeft(), s));
197 QGraphicsWidget *m_widget;
202 class GraphicsView : public QGraphicsView {
206 GraphicsView() : QGraphicsView(new QGraphicsScene()), viewport(0) {
207 setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
208 setOptimizationFlags(QGraphicsView::DontSavePainterState);
210 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
211 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
213 setFrameShape(QFrame::NoFrame);
214 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
216 viewport = new ViewportItem();
217 scene()->addItem(viewport);
220 ViewportItem *viewportItem() const {
225 void resizeEvent(QResizeEvent *e) {
226 QGraphicsView::resizeEvent(e);
227 setUpdatesEnabled(false);
232 QRectF rect(QPointF(0, 0), size());
233 scene()->setSceneRect(rect);
235 viewport->setGeometry(rect);
236 setUpdatesEnabled(true);
241 ViewportItem *viewport;
244 ContentWindow::ContentWindow(QWidget *parent, Entry *e) : QMainWindow(parent) {
245 setAttribute(Qt::WA_Maemo5StackedWindow);
247 QWebSettings::globalSettings()->setAttribute(QWebSettings::PluginsEnabled, true);
249 starred = new QAction(tr("Starred"), this);
250 starred->setCheckable(true);
251 menuBar()->addAction(starred);
253 shared = new QAction(tr("Shared"), this);
254 shared->setCheckable(true);
255 menuBar()->addAction(shared);
257 keepUnread = new QAction(tr("Keep unread"), this);
258 keepUnread->setCheckable(true);
259 menuBar()->addAction(keepUnread);
261 menuBar()->addAction(tr("See original"), this, SLOT(seeOriginal()));
263 GraphicsView *gv = new GraphicsView();
264 webview = new QGraphicsWebView();
265 gv->viewportItem()->setWidget(webview);
267 webview->settings()->setFontSize(QWebSettings::MinimumFontSize, 22);
268 webview->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
270 connect(webview, SIGNAL(loadFinished(bool)), SLOT(loadFinished(bool)));
271 connect(webview, SIGNAL(loadStarted()), SLOT(loadStarted()));
272 connect(webview->page(), SIGNAL(linkClicked(const QUrl &)), SLOT(showLink(const QUrl &)));
273 connect(gv->viewportItem(), SIGNAL(showNextEntry()), SIGNAL(showNextEntry()));
274 connect(gv->viewportItem(), SIGNAL(showPrevEntry()), SIGNAL(showPrevEntry()));
275 connect(gv->viewportItem(), SIGNAL(showOriginal()), SLOT(seeOriginal()));
276 connect(gv->viewportItem(), SIGNAL(toggleStarred()), SLOT(toggleStarred()));
277 connect(gv->viewportItem(), SIGNAL(toggleShared()), SLOT(toggleShared()));
278 connect(gv->viewportItem(), SIGNAL(toggleRead()), SLOT(toggleRead()));
280 setCentralWidget(gv);
281 gv->viewportItem()->setFocus();
282 gv->viewportItem()->grabKeyboard();
291 ContentWindow::~ContentWindow() {
295 void ContentWindow::loadStarted() {
296 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
299 void ContentWindow::loadFinished(bool) {
300 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
303 void ContentWindow::showLink(const QUrl &url) {
304 /* Attempt to launch external browser */
305 if(!QDesktopServices::openUrl(url))
306 webview->setUrl(url); /* Failed... Show inline */
309 void ContentWindow::seeOriginal() {
310 showLink(entry->link);
313 void ContentWindow::toggleStarred() {
315 if(starred->isChecked()) {
316 QMaemo5InformationBox::information(this, "Starred",
317 QMaemo5InformationBox::DefaultTimeout);
320 QMaemo5InformationBox::information(this, "Star removed",
321 QMaemo5InformationBox::DefaultTimeout);
325 void ContentWindow::toggleShared() {
327 if(shared->isChecked()) {
328 QMaemo5InformationBox::information(this, "Shared",
329 QMaemo5InformationBox::DefaultTimeout);
332 QMaemo5InformationBox::information(this, "Unshared",
333 QMaemo5InformationBox::DefaultTimeout);
337 void ContentWindow::toggleRead() {
338 if(!keepUnread->isEnabled()) {
339 QMaemo5InformationBox::information(this, "Read state locked",
340 QMaemo5InformationBox::DefaultTimeout);
344 keepUnread->toggle();
345 if(keepUnread->isChecked()) {
346 QMaemo5InformationBox::information(this, "Marked unread",
347 QMaemo5InformationBox::DefaultTimeout);
350 QMaemo5InformationBox::information(this, "Marked read",
351 QMaemo5InformationBox::DefaultTimeout);
355 void ContentWindow::showEntry(Entry *e) {
357 /* Store settings of previously shown entry */
358 entry->markRead(!keepUnread->isChecked());
359 entry->markStar(starred->isChecked());
360 entry->markShared(shared->isChecked());
365 starred->setChecked((entry->flags & ENTRY_FLAG_STARRED));
366 shared->setChecked((entry->flags & ENTRY_FLAG_SHARED));
367 keepUnread->setChecked(false);
368 keepUnread->setEnabled((entry->flags & ENTRY_FLAG_LOCKED) == 0);
370 setWindowTitle(entry->title);
372 webview->setHtml(entry->content);
375 void ContentWindow::closeEvent(QCloseEvent *event) {
377 entry->markRead(!keepUnread->isChecked());
378 entry->markStar(starred->isChecked());
379 entry->markShared(shared->isChecked());
380 QMainWindow::closeEvent(event);
383 void ContentWindow::grabZoomKeys(bool grab) {
387 unsigned long val = (grab) ? 1 : 0;
388 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
392 XChangeProperty (QX11Info::display(), winId(), atom, XA_INTEGER, 32, PropModeReplace,
393 reinterpret_cast<unsigned char *>(&val), 1);
396 #include "contentwindow.moc"