diff options
author | Ariya Hidayat <ariya.hidayat@trolltech.com> | 2009-03-19 15:15:33 +0100 |
---|---|---|
committer | Ariya Hidayat <ariya.hidayat@trolltech.com> | 2009-03-19 15:15:33 +0100 |
commit | f5ad29e91fe8056e4848b09faf9c5ef0f7be35bb (patch) | |
tree | f21246acea5d43cfa26988a6155aa525873db185 /dragmove | |
parent | c8d0c381b994d7417863832929cc4c3f710f2db5 (diff) |
Add new example of moving top-level window with mouse drag.
Diffstat (limited to 'dragmove')
-rw-r--r-- | dragmove/dragmove.pro | 6 | ||||
-rw-r--r-- | dragmove/dragmove.qrc | 5 | ||||
-rw-r--r-- | dragmove/dragmovecharm.cpp | 115 | ||||
-rw-r--r-- | dragmove/dragmovecharm.h | 46 | ||||
-rw-r--r-- | dragmove/example.cpp | 86 | ||||
-rw-r--r-- | dragmove/pudding.jpg | bin | 0 -> 33235 bytes |
6 files changed, 258 insertions, 0 deletions
diff --git a/dragmove/dragmove.pro b/dragmove/dragmove.pro new file mode 100644 index 0000000..b614024 --- /dev/null +++ b/dragmove/dragmove.pro @@ -0,0 +1,6 @@ +TEMPLATE = app +TARGET = dragmove +HEADERS = dragmovecharm.h +SOURCES = example.cpp dragmovecharm.cpp +RESOURCES = dragmove.qrc +QT += network webkit diff --git a/dragmove/dragmove.qrc b/dragmove/dragmove.qrc new file mode 100644 index 0000000..a5e5f76 --- /dev/null +++ b/dragmove/dragmove.qrc @@ -0,0 +1,5 @@ +<RCC> + <qresource prefix="/" > + <file>pudding.jpg</file> + </qresource> +</RCC> diff --git a/dragmove/dragmovecharm.cpp b/dragmove/dragmovecharm.cpp new file mode 100644 index 0000000..2dfff69 --- /dev/null +++ b/dragmove/dragmovecharm.cpp @@ -0,0 +1,115 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the Graphics Dojo project on Qt Labs. +** +** This file may be used under the terms of the GNU General Public +** License version 2.0 or 3.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.fsf.org/licensing/licenses/info/GPLv2.html and +** http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.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 "dragmovecharm.h" + +#include <QEvent> +#include <QHash> +#include <QMouseEvent> +#include <QWidget> + +struct DragMoveData { + bool isMoving; + QPoint startDrag; +}; + +class DragMoveCharmPrivate +{ +public: + QHash<QWidget*, DragMoveData*> dragMoveData; +}; + +DragMoveCharm::DragMoveCharm(QObject *parent): QObject(parent) +{ + d = new DragMoveCharmPrivate; +} + +DragMoveCharm::~DragMoveCharm() +{ + delete d; +} + +void DragMoveCharm::activateOn(QWidget *widget) +{ + if (d->dragMoveData.contains(widget)) + return; + + DragMoveData *data = new DragMoveData; + data->startDrag = QPoint(0, 0); + data->isMoving = false; + d->dragMoveData[widget] = data; + + widget->installEventFilter(this); +} + +void DragMoveCharm::deactivateFrom(QWidget *widget) +{ + delete d->dragMoveData[widget]; + d->dragMoveData.remove(widget); + widget->removeEventFilter(this); +} + +bool DragMoveCharm::eventFilter(QObject *object, QEvent *event) +{ + QWidget *widget = qobject_cast<QWidget*>(object); + if (!widget) + 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; + Qt::MouseButton button = mouseEvent->button(); + + DragMoveData *data = d->dragMoveData.value(widget); + if (!widget || !data) + return false; + + bool consumed = false; + + if (type == QEvent::MouseButtonPress && button == Qt::LeftButton) { + data->startDrag = mouseEvent->globalPos(); + data->isMoving = true; + event->accept(); + consumed = true; + } + + if (type == QEvent::MouseButtonRelease) { + data->startDrag = QPoint(0, 0); + data->isMoving = false; + } + + if (type == QEvent::MouseMove && data->isMoving) { + QPoint pos = mouseEvent->globalPos(); + widget->move(widget->pos() + pos - data->startDrag); + data->startDrag = pos; + consumed = true; + } + + return consumed; +} diff --git a/dragmove/dragmovecharm.h b/dragmove/dragmovecharm.h new file mode 100644 index 0000000..35c378d --- /dev/null +++ b/dragmove/dragmovecharm.h @@ -0,0 +1,46 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the Graphics Dojo project on Qt Labs. +** +** This file may be used under the terms of the GNU General Public +** License version 2.0 or 3.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.fsf.org/licensing/licenses/info/GPLv2.html and +** http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.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 DRAGMOVECHARM_H +#define DRAGMOVECHARM_H + +#include <QObject> + +class DragMoveCharmPrivate; +class QWidget; + +class DragMoveCharm: public QObject +{ + Q_OBJECT +public: + DragMoveCharm(QObject *parent = 0); + ~DragMoveCharm(); + void activateOn(QWidget *widget); + void deactivateFrom(QWidget *widget); + bool eventFilter(QObject *object, QEvent *event); + +private: + DragMoveCharmPrivate *d; +}; + +#endif // DRAGMOVECHARM_H diff --git a/dragmove/example.cpp b/dragmove/example.cpp new file mode 100644 index 0000000..9f45777 --- /dev/null +++ b/dragmove/example.cpp @@ -0,0 +1,86 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the Graphics Dojo project on Qt Labs. +** +** This file may be used under the terms of the GNU General Public +** License version 2.0 or 3.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.fsf.org/licensing/licenses/info/GPLv2.html and +** http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.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 "dragmovecharm.h" + +QWidget *imageShow() +{ + QLabel *label = new QLabel; + label->setText("<img src=':/pudding.jpg'>"); + label->adjustSize(); + label->setWindowTitle("Drag to move around"); + label->show(); + + return label; +} + +QWidget *miniBrowser() +{ + QDialog *widget = new QDialog; + QVBoxLayout *layout = new QVBoxLayout(widget); + layout->setContentsMargins(3, 15, 3, 3); + widget->setLayout(layout); + widget->setWindowFlags(Qt::FramelessWindowHint); + widget->setSizeGripEnabled(true); + widget->setWindowTitle("Drag to move around"); + widget->show(); + + QTabWidget *tab = new QTabWidget(widget); + QWebView *search = new QWebView(tab); + search->load(QUrl("http://www.google.com/m?hl=en")); + tab->addTab(search, "Search"); + QWebView *news = new QWebView(tab); + news->load(QUrl("http://www.google.com/m/news?source=mobileproducts")); + tab->addTab(news, "News"); + QWebView *bbc = new QWebView(tab); + bbc->load(QUrl("http://news.bbc.co.uk/text_only.stm")); + tab->addTab(bbc, "BBC"); + QWebView *fb = new QWebView(tab); + fb->load(QUrl("http://iphone.facebook.com")); + tab->addTab(fb, "Facebook"); + + layout->addWidget(tab); + widget->resize(350, 500); + search->setFocus(); + +#if QT_VERSION >= 0x040500 + tab->setDocumentMode(true); +#endif + + return widget; +} + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + DragMoveCharm charm; + charm.activateOn(imageShow()); + charm.activateOn(miniBrowser()); + + return app.exec(); +} + diff --git a/dragmove/pudding.jpg b/dragmove/pudding.jpg Binary files differnew file mode 100644 index 0000000..dc162d8 --- /dev/null +++ b/dragmove/pudding.jpg |