summaryrefslogtreecommitdiffstats
path: root/flickcharm
diff options
context:
space:
mode:
authorAriya Hidayat <ariya.hidayat@nokia.com>2008-11-14 21:11:53 +0000
committerDavid Boddie <dboddie@trolltech.com>2008-11-14 21:11:53 +0000
commit50fd7301e4731c55ee95dcb83fbd7fd387d24a16 (patch)
tree3fa4d9e5c68e963cb1a0f06559758b14c42b34b9 /flickcharm
parent0bbc0a3fe177aecc0d76d7773dfd73523b6f4b77 (diff)
Add the Flick Charm.
Diffstat (limited to 'flickcharm')
-rw-r--r--flickcharm/flickcharm.cpp307
-rw-r--r--flickcharm/flickcharm.h49
-rw-r--r--flickcharm/flickcharm.pro8
-rw-r--r--flickcharm/main.cpp134
4 files changed, 498 insertions, 0 deletions
diff --git a/flickcharm/flickcharm.cpp b/flickcharm/flickcharm.cpp
new file mode 100644
index 0000000..0aec724
--- /dev/null
+++ b/flickcharm/flickcharm.cpp
@@ -0,0 +1,307 @@
+/****************************************************************************
+**
+** Copyright (C) 2008-2008 Trolltech ASA. All rights reserved.
+**
+** This file is part of the Graphics Dojo project on Trolltech Labs.
+**
+** This file may be used under the terms of the GNU General Public
+** License version 2.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of
+** this file. Please review the following information to ensure GNU
+** General Public Licensing requirements will be met:
+** http://www.trolltech.com/products/qt/opensource.html
+**
+** If you are unsure which license is appropriate for your use, please
+** review the following information:
+** http://www.trolltech.com/products/qt/licensing.html or contact the
+** sales department at sales@trolltech.com.
+**
+** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+**
+****************************************************************************/
+
+#include "flickcharm.h"
+
+#include <QAbstractScrollArea>
+#include <QApplication>
+#include <QBasicTimer>
+#include <QEvent>
+#include <QHash>
+#include <QList>
+#include <QMouseEvent>
+#include <QScrollBar>
+#include <QWebFrame>
+#include <QWebView>
+
+#include <QDebug>
+
+struct FlickData {
+ typedef enum { Steady, Pressed, ManualScroll, AutoScroll, Stop } State;
+ State state;
+ QWidget *widget;
+ QPoint pressPos;
+ QPoint offset;
+ QPoint dragPos;
+ QPoint speed;
+ QList<QEvent*> ignored;
+};
+
+class FlickCharmPrivate
+{
+public:
+ QHash<QWidget*, FlickData*> flickData;
+ QBasicTimer ticker;
+};
+
+FlickCharm::FlickCharm(QObject *parent): QObject(parent)
+{
+ d = new FlickCharmPrivate;
+}
+
+FlickCharm::~FlickCharm()
+{
+ delete d;
+}
+
+void FlickCharm::activateOn(QWidget *widget)
+{
+ QAbstractScrollArea *scrollArea = dynamic_cast<QAbstractScrollArea*>(widget);
+ if (scrollArea) {
+ scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+ scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+
+ QWidget *viewport = scrollArea->viewport();
+
+ viewport->installEventFilter(this);
+ scrollArea->installEventFilter(this);
+
+ d->flickData.remove(viewport);
+ d->flickData[viewport] = new FlickData;
+ d->flickData[viewport]->widget = widget;
+ d->flickData[viewport]->state = FlickData::Steady;
+
+ return;
+ }
+
+ QWebView *webView = dynamic_cast<QWebView*>(widget);
+ if (webView) {
+ QWebFrame *frame = webView->page()->mainFrame();
+ frame->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
+ frame->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
+
+ webView->installEventFilter(this);
+
+ d->flickData.remove(webView);
+ d->flickData[webView] = new FlickData;
+ d->flickData[webView]->widget = webView;
+ d->flickData[webView]->state = FlickData::Steady;
+
+ return;
+ }
+
+ qWarning() << "FlickCharm only works on QAbstractScrollArea (and derived classes)";
+ qWarning() << "or QWebView (and derived classes)";
+}
+
+void FlickCharm::deactivateFrom(QWidget *widget)
+{
+ QAbstractScrollArea *scrollArea = dynamic_cast<QAbstractScrollArea*>(widget);
+ if (scrollArea) {
+ QWidget *viewport = scrollArea->viewport();
+
+ viewport->removeEventFilter(this);
+ scrollArea->removeEventFilter(this);
+
+ delete d->flickData[viewport];
+ d->flickData.remove(viewport);
+
+ return;
+ }
+
+ QWebView *webView = dynamic_cast<QWebView*>(widget);
+ if (webView) {
+ webView->removeEventFilter(this);
+
+ delete d->flickData[webView];
+ d->flickData.remove(webView);
+
+ return;
+ }
+}
+
+static QPoint scrollOffset(QWidget *widget)
+{
+ int x = 0, y = 0;
+
+ QAbstractScrollArea *scrollArea = dynamic_cast<QAbstractScrollArea*>(widget);
+ if (scrollArea) {
+ x = scrollArea->horizontalScrollBar()->value();
+ y = scrollArea->verticalScrollBar()->value();
+ }
+
+ QWebView *webView = dynamic_cast<QWebView*>(widget);
+ if (webView) {
+ QWebFrame *frame = webView->page()->mainFrame();
+ x = frame->evaluateJavaScript("window.scrollX").toInt();
+ y = frame->evaluateJavaScript("window.scrollY").toInt();
+ }
+
+ return QPoint(x, y);
+}
+
+static void setScrollOffset(QWidget *widget, const QPoint &p)
+{
+ QAbstractScrollArea *scrollArea = dynamic_cast<QAbstractScrollArea*>(widget);
+ if (scrollArea) {
+ scrollArea->horizontalScrollBar()->setValue(p.x());
+ scrollArea->verticalScrollBar()->setValue(p.y());
+ }
+
+ QWebView *webView = dynamic_cast<QWebView*>(widget);
+ QWebFrame *frame = webView ? webView->page()->mainFrame() : 0;
+ if (frame)
+ frame->evaluateJavaScript(QString("window.scrollTo(%1,%2);").arg(p.x()).arg(p.y()));
+}
+
+static QPoint deaccelerate(const QPoint &speed, int a = 1, int max = 64)
+{
+ int x = qBound(-max, speed.x(), max);
+ int y = qBound(-max, speed.y(), max);
+ x = (x == 0) ? x : (x > 0) ? qMax(0, x - a) : qMin(0, x + a);
+ y = (y == 0) ? y : (y > 0) ? qMax(0, y - a) : qMin(0, y + a);
+ return QPoint(x, y);
+}
+
+bool FlickCharm::eventFilter(QObject *object, QEvent *event)
+{
+ if (!object->isWidgetType())
+ return false;
+
+ QEvent::Type type = event->type();
+ if (type != QEvent::MouseButtonPress &&
+ type != QEvent::MouseButtonRelease &&
+ type != QEvent::MouseMove)
+ return false;
+
+ QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent*>(event);
+ if (!mouseEvent || mouseEvent->modifiers() != Qt::NoModifier)
+ return false;
+
+ QWidget *viewport = dynamic_cast<QWidget*>(object);
+ FlickData *data = d->flickData.value(viewport);
+ if (!viewport || !data || data->ignored.removeAll(event))
+ return false;
+
+ bool consumed = false;
+ switch (data->state) {
+
+ case FlickData::Steady:
+ if (mouseEvent->type() == QEvent::MouseButtonPress)
+ if (mouseEvent->buttons() == Qt::LeftButton) {
+ consumed = true;
+ data->state = FlickData::Pressed;
+ data->pressPos = mouseEvent->pos();
+ data->offset = scrollOffset(data->widget);
+ }
+ break;
+
+ case FlickData::Pressed:
+ if (mouseEvent->type() == QEvent::MouseButtonRelease) {
+ consumed = true;
+ data->state = FlickData::Steady;
+
+ QMouseEvent *event1 = new QMouseEvent(QEvent::MouseButtonPress,
+ data->pressPos, Qt::LeftButton,
+ Qt::LeftButton, Qt::NoModifier);
+ QMouseEvent *event2 = new QMouseEvent(*mouseEvent);
+
+ data->ignored << event1;
+ data->ignored << event2;
+ QApplication::postEvent(object, event1);
+ QApplication::postEvent(object, event2);
+ }
+ if (mouseEvent->type() == QEvent::MouseMove) {
+ consumed = true;
+ data->state = FlickData::ManualScroll;
+ data->dragPos = QCursor::pos();
+ if (!d->ticker.isActive())
+ d->ticker.start(20, this);
+ }
+ break;
+
+ case FlickData::ManualScroll:
+ if (mouseEvent->type() == QEvent::MouseMove) {
+ consumed = true;
+ QPoint delta = mouseEvent->pos() - data->pressPos;
+ setScrollOffset(data->widget, data->offset - delta);
+ }
+ if (mouseEvent->type() == QEvent::MouseButtonRelease) {
+ consumed = true;
+ data->state = FlickData::AutoScroll;
+ }
+ break;
+
+ case FlickData::AutoScroll:
+ if (mouseEvent->type() == QEvent::MouseButtonPress) {
+ consumed = true;
+ data->state = FlickData::Stop;
+ data->speed = QPoint(0, 0);
+ }
+ if (mouseEvent->type() == QEvent::MouseButtonRelease) {
+ consumed = true;
+ data->state = FlickData::Steady;
+ data->speed = QPoint(0, 0);
+ }
+ break;
+
+ case FlickData::Stop:
+ if (mouseEvent->type() == QEvent::MouseButtonRelease) {
+ consumed = true;
+ data->state = FlickData::Steady;
+ }
+ if (mouseEvent->type() == QEvent::MouseMove) {
+ consumed = true;
+ data->state = FlickData::ManualScroll;
+ data->dragPos = QCursor::pos();
+ if (!d->ticker.isActive())
+ d->ticker.start(20, this);
+ }
+ break;
+
+ default:
+ break;
+ }
+
+ return consumed;
+}
+
+void FlickCharm::timerEvent(QTimerEvent *event)
+{
+ int count = 0;
+ QHashIterator<QWidget*, FlickData*> item(d->flickData);
+ while (item.hasNext()) {
+ item.next();
+ FlickData *data = item.value();
+
+ if (data->state == FlickData::ManualScroll) {
+ count++;
+ data->speed = QCursor::pos() - data->dragPos;
+ data->dragPos = QCursor::pos();
+ }
+
+ if (data->state == FlickData::AutoScroll) {
+ count++;
+ data->speed = deaccelerate(data->speed);
+ QPoint p = scrollOffset(data->widget);
+ setScrollOffset(data->widget, p - data->speed);
+ if (data->speed == QPoint(0, 0))
+ data->state = FlickData::Steady;
+ }
+ }
+
+ if (!count)
+ d->ticker.stop();
+
+ QObject::timerEvent(event);
+}
diff --git a/flickcharm/flickcharm.h b/flickcharm/flickcharm.h
new file mode 100644
index 0000000..72ee207
--- /dev/null
+++ b/flickcharm/flickcharm.h
@@ -0,0 +1,49 @@
+/****************************************************************************
+**
+** Copyright (C) 2008-2008 Trolltech ASA. All rights reserved.
+**
+** This file is part of the Graphics Dojo project on Trolltech Labs.
+**
+** This file may be used under the terms of the GNU General Public
+** License version 2.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of
+** this file. Please review the following information to ensure GNU
+** General Public Licensing requirements will be met:
+** http://www.trolltech.com/products/qt/opensource.html
+**
+** If you are unsure which license is appropriate for your use, please
+** review the following information:
+** http://www.trolltech.com/products/qt/licensing.html or contact the
+** sales department at sales@trolltech.com.
+**
+** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+**
+****************************************************************************/
+
+#ifndef FLICKCHARM_H
+#define FLICKCHARM_H
+
+#include <QObject>
+
+class FlickCharmPrivate;
+class QWidget;
+
+class FlickCharm: public QObject
+{
+ Q_OBJECT
+public:
+ FlickCharm(QObject *parent = 0);
+ ~FlickCharm();
+ void activateOn(QWidget *widget);
+ void deactivateFrom(QWidget *widget);
+ bool eventFilter(QObject *object, QEvent *event);
+
+protected:
+ void timerEvent(QTimerEvent *event);
+
+private:
+ FlickCharmPrivate *d;
+};
+
+#endif // FLICKCHARM_H
diff --git a/flickcharm/flickcharm.pro b/flickcharm/flickcharm.pro
new file mode 100644
index 0000000..5b2cbba
--- /dev/null
+++ b/flickcharm/flickcharm.pro
@@ -0,0 +1,8 @@
+TEMPLATE = app
+TARGET = flickcharm
+SOURCES = flickcharm.cpp main.cpp
+HEADERS = flickcharm.h
+CONFIG += console
+CONFIG -= debug
+CONFIG += release
+QT += network webkit
diff --git a/flickcharm/main.cpp b/flickcharm/main.cpp
new file mode 100644
index 0000000..a49c6d6
--- /dev/null
+++ b/flickcharm/main.cpp
@@ -0,0 +1,134 @@
+/****************************************************************************
+**
+** Copyright (C) 2008-2008 Trolltech ASA. All rights reserved.
+**
+** This file is part of the Graphics Dojo project on Trolltech Labs.
+**
+** This file may be used under the terms of the GNU General Public
+** License version 2.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of
+** this file. Please review the following information to ensure GNU
+** General Public Licensing requirements will be met:
+** http://www.trolltech.com/products/qt/opensource.html
+**
+** If you are unsure which license is appropriate for your use, please
+** review the following information:
+** http://www.trolltech.com/products/qt/licensing.html or contact the
+** sales department at sales@trolltech.com.
+**
+** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+**
+****************************************************************************/
+
+#include <QtGui>
+#include <QtWebKit>
+
+#include "flickcharm.h"
+
+#define ITEM_WIDTH 300
+#define ITEM_HEIGHT 30
+
+class TextItem: public QGraphicsItem
+{
+public:
+ TextItem(const QString &str) {
+ QStringList list = str.split(' ');
+ str1 = list[0];
+ str2 = list[1];
+ font1 = QFont("Arial");
+ font2 = QFont("Arial");
+ font1.setBold(true);
+ font1.setPixelSize(ITEM_HEIGHT / 2);
+ font2.setPixelSize(ITEM_HEIGHT / 2);
+ offset = QFontMetrics(font1).width(str1) + 15;
+ }
+
+ QRectF boundingRect() const {
+ return QRectF(0, 0, ITEM_WIDTH, ITEM_HEIGHT);
+ }
+
+ void paint(QPainter *painter, const QStyleOptionGraphicsItem* option, QWidget*) {
+ if (option->state & QStyle::State_Selected) {
+ painter->fillRect(boundingRect(), QColor(0, 128, 240));
+ painter->setPen(Qt::white);
+ } else {
+ painter->setPen(Qt::lightGray);
+ painter->drawRect(boundingRect());
+ painter->setPen(Qt::black);
+ }
+ painter->setFont(font1);
+ painter->drawText(QRect(10, 0, offset, ITEM_HEIGHT), Qt::AlignVCenter, str1);
+ painter->setFont(font2);
+ painter->drawText(QRect(offset, 0, ITEM_WIDTH, ITEM_HEIGHT), Qt::AlignVCenter, str2);
+ }
+
+private:
+ QFont font1;
+ QFont font2;
+ QString str1;
+ QString str2;
+ int offset;
+};
+
+// Returns a list of two-word color names
+static QStringList colorPairs(int max)
+{
+ // capitalize the first letter
+ QStringList colors = QColor::colorNames();
+ int num = colors.count();
+ for (int c = 0; c < num; ++c)
+ colors[c] = colors[c][0].toUpper() + colors[c].mid(1);
+
+ // combine two colors, e.g. "lime skyblue"
+ QStringList combinedColors;
+ for (int i = 0; i < num; ++i)
+ for (int j = 0; j < num; ++j)
+ combinedColors << QString("%1 %2").arg(colors[i]).arg(colors[j]);
+
+ // randomize it
+ colors.clear();
+ while (combinedColors.count()) {
+ int i = qrand() % combinedColors.count();
+ colors << combinedColors[i];
+ combinedColors.removeAt(i);
+ if (colors.count() == max)
+ break;
+ }
+
+ return colors;
+}
+
+int main(int argc, char *argv[])
+{
+ QApplication app(argc, argv);
+
+ QStringList colors = colorPairs(5000);
+ QGraphicsScene scene;
+ scene.setItemIndexMethod(QGraphicsScene::NoIndex);
+ for (int i = 0; i < colors.count(); ++i) {
+ TextItem *item = new TextItem(colors[i]);
+ scene.addItem(item);
+ item->setPos(0, i*ITEM_HEIGHT);
+ item->setFlag(QGraphicsItem::ItemIsSelectable, true);
+ }
+ scene.setItemIndexMethod(QGraphicsScene::BspTreeIndex);
+
+ QGraphicsView canvas;
+ canvas.setScene(&scene);
+ canvas.setRenderHints(QPainter::TextAntialiasing);
+ canvas.setFrameShape(QFrame::NoFrame);
+ canvas.setWindowTitle("Flickable Canvas");
+ canvas.show();
+
+ QWebView web;
+ web.setUrl(QUrl("http://news.google.com"));
+ web.setWindowTitle("Flickable Web View");
+ web.show();
+
+ FlickCharm FlickCharm;
+ FlickCharm.activateOn(&canvas);
+ FlickCharm.activateOn(&web);
+
+ return app.exec();
+}