From e5fcad302d86d316390c6b0f62759a067313e8a9 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Mon, 23 Mar 2009 10:18:55 +0100 Subject: Long live Qt 4.5! --- examples/draganddrop/README | 40 ++++ .../delayedencoding/delayedencoding.pro | 14 ++ .../delayedencoding/delayedencoding.qrc | 6 + .../draganddrop/delayedencoding/images/drag.png | Bin 0 -> 977 bytes .../draganddrop/delayedencoding/images/example.svg | 59 ++++++ examples/draganddrop/delayedencoding/main.cpp | 52 +++++ examples/draganddrop/delayedencoding/mimedata.cpp | 66 +++++++ examples/draganddrop/delayedencoding/mimedata.h | 64 +++++++ .../draganddrop/delayedencoding/sourcewidget.cpp | 115 +++++++++++ .../draganddrop/delayedencoding/sourcewidget.h | 71 +++++++ examples/draganddrop/draganddrop.pro | 15 ++ .../draganddrop/draggableicons/draggableicons.pro | 10 + .../draganddrop/draggableicons/draggableicons.qrc | 7 + examples/draganddrop/draggableicons/dragwidget.cpp | 169 ++++++++++++++++ examples/draganddrop/draggableicons/dragwidget.h | 66 +++++++ .../draganddrop/draggableicons/images/boat.png | Bin 0 -> 2772 bytes examples/draganddrop/draggableicons/images/car.png | Bin 0 -> 2963 bytes .../draganddrop/draggableicons/images/house.png | Bin 0 -> 3292 bytes examples/draganddrop/draggableicons/main.cpp | 62 ++++++ .../draganddrop/draggabletext/draggabletext.pro | 12 ++ .../draganddrop/draggabletext/draggabletext.qrc | 5 + examples/draganddrop/draggabletext/draglabel.cpp | 52 +++++ examples/draganddrop/draggabletext/draglabel.h | 59 ++++++ examples/draganddrop/draggabletext/dragwidget.cpp | 164 ++++++++++++++++ examples/draganddrop/draggabletext/dragwidget.h | 63 ++++++ examples/draganddrop/draggabletext/main.cpp | 53 ++++++ examples/draganddrop/draggabletext/words.txt | 41 ++++ examples/draganddrop/dropsite/droparea.cpp | 127 ++++++++++++ examples/draganddrop/dropsite/droparea.h | 78 ++++++++ examples/draganddrop/dropsite/dropsite.pro | 12 ++ examples/draganddrop/dropsite/dropsitewindow.cpp | 146 ++++++++++++++ examples/draganddrop/dropsite/dropsitewindow.h | 78 ++++++++ examples/draganddrop/dropsite/main.cpp | 54 ++++++ examples/draganddrop/fridgemagnets/draglabel.cpp | 90 +++++++++ examples/draganddrop/fridgemagnets/draglabel.h | 65 +++++++ examples/draganddrop/fridgemagnets/dragwidget.cpp | 212 +++++++++++++++++++++ examples/draganddrop/fridgemagnets/dragwidget.h | 66 +++++++ .../draganddrop/fridgemagnets/fridgemagnets.pro | 12 ++ .../draganddrop/fridgemagnets/fridgemagnets.qrc | 5 + examples/draganddrop/fridgemagnets/main.cpp | 53 ++++++ examples/draganddrop/fridgemagnets/words.txt | 48 +++++ examples/draganddrop/puzzle/example.jpg | Bin 0 -> 42654 bytes examples/draganddrop/puzzle/main.cpp | 55 ++++++ examples/draganddrop/puzzle/mainwindow.cpp | 151 +++++++++++++++ examples/draganddrop/puzzle/mainwindow.h | 77 ++++++++ examples/draganddrop/puzzle/pieceslist.cpp | 122 ++++++++++++ examples/draganddrop/puzzle/pieceslist.h | 62 ++++++ examples/draganddrop/puzzle/puzzle.pro | 20 ++ examples/draganddrop/puzzle/puzzle.qrc | 5 + examples/draganddrop/puzzle/puzzlewidget.cpp | 205 ++++++++++++++++++++ examples/draganddrop/puzzle/puzzlewidget.h | 86 +++++++++ 51 files changed, 3094 insertions(+) create mode 100644 examples/draganddrop/README create mode 100644 examples/draganddrop/delayedencoding/delayedencoding.pro create mode 100644 examples/draganddrop/delayedencoding/delayedencoding.qrc create mode 100644 examples/draganddrop/delayedencoding/images/drag.png create mode 100644 examples/draganddrop/delayedencoding/images/example.svg create mode 100644 examples/draganddrop/delayedencoding/main.cpp create mode 100644 examples/draganddrop/delayedencoding/mimedata.cpp create mode 100644 examples/draganddrop/delayedencoding/mimedata.h create mode 100644 examples/draganddrop/delayedencoding/sourcewidget.cpp create mode 100644 examples/draganddrop/delayedencoding/sourcewidget.h create mode 100644 examples/draganddrop/draganddrop.pro create mode 100644 examples/draganddrop/draggableicons/draggableicons.pro create mode 100644 examples/draganddrop/draggableicons/draggableicons.qrc create mode 100644 examples/draganddrop/draggableicons/dragwidget.cpp create mode 100644 examples/draganddrop/draggableicons/dragwidget.h create mode 100644 examples/draganddrop/draggableicons/images/boat.png create mode 100644 examples/draganddrop/draggableicons/images/car.png create mode 100644 examples/draganddrop/draggableicons/images/house.png create mode 100644 examples/draganddrop/draggableicons/main.cpp create mode 100644 examples/draganddrop/draggabletext/draggabletext.pro create mode 100644 examples/draganddrop/draggabletext/draggabletext.qrc create mode 100644 examples/draganddrop/draggabletext/draglabel.cpp create mode 100644 examples/draganddrop/draggabletext/draglabel.h create mode 100644 examples/draganddrop/draggabletext/dragwidget.cpp create mode 100644 examples/draganddrop/draggabletext/dragwidget.h create mode 100644 examples/draganddrop/draggabletext/main.cpp create mode 100644 examples/draganddrop/draggabletext/words.txt create mode 100644 examples/draganddrop/dropsite/droparea.cpp create mode 100644 examples/draganddrop/dropsite/droparea.h create mode 100644 examples/draganddrop/dropsite/dropsite.pro create mode 100644 examples/draganddrop/dropsite/dropsitewindow.cpp create mode 100644 examples/draganddrop/dropsite/dropsitewindow.h create mode 100644 examples/draganddrop/dropsite/main.cpp create mode 100644 examples/draganddrop/fridgemagnets/draglabel.cpp create mode 100644 examples/draganddrop/fridgemagnets/draglabel.h create mode 100644 examples/draganddrop/fridgemagnets/dragwidget.cpp create mode 100644 examples/draganddrop/fridgemagnets/dragwidget.h create mode 100644 examples/draganddrop/fridgemagnets/fridgemagnets.pro create mode 100644 examples/draganddrop/fridgemagnets/fridgemagnets.qrc create mode 100644 examples/draganddrop/fridgemagnets/main.cpp create mode 100644 examples/draganddrop/fridgemagnets/words.txt create mode 100644 examples/draganddrop/puzzle/example.jpg create mode 100644 examples/draganddrop/puzzle/main.cpp create mode 100644 examples/draganddrop/puzzle/mainwindow.cpp create mode 100644 examples/draganddrop/puzzle/mainwindow.h create mode 100644 examples/draganddrop/puzzle/pieceslist.cpp create mode 100644 examples/draganddrop/puzzle/pieceslist.h create mode 100644 examples/draganddrop/puzzle/puzzle.pro create mode 100644 examples/draganddrop/puzzle/puzzle.qrc create mode 100644 examples/draganddrop/puzzle/puzzlewidget.cpp create mode 100644 examples/draganddrop/puzzle/puzzlewidget.h (limited to 'examples/draganddrop') diff --git a/examples/draganddrop/README b/examples/draganddrop/README new file mode 100644 index 0000000000..f4efd2e40b --- /dev/null +++ b/examples/draganddrop/README @@ -0,0 +1,40 @@ +Qt supports native drag and drop on all platforms via an extensible +MIME-based system that enables applications to send data to each other in the +most appropriate formats. + +Drag and drop can also be implemented for internal use by applications. + + +The example launcher provided with Qt can be used to explore each of the +examples in this directory. + +Documentation for these examples can be found via the Tutorial and Examples +link in the main Qt documentation. + + +Finding the Qt Examples and Demos launcher +========================================== + +On Windows: + +The launcher can be accessed via the Windows Start menu. Select the menu +entry entitled "Qt Examples and Demos" entry in the submenu containing +the Qt tools. + +On Mac OS X: + +For the binary distribution, the qtdemo executable is installed in the +/Developer/Applications/Qt directory. For the source distribution, it is +installed alongside the other Qt tools on the path specified when Qt is +configured. + +On Unix/Linux: + +The qtdemo executable is installed alongside the other Qt tools on the path +specified when Qt is configured. + +On all platforms: + +The source code for the launcher can be found in the demos/qtdemo directory +in the Qt package. This example is built at the same time as the Qt libraries, +tools, examples, and demonstrations. diff --git a/examples/draganddrop/delayedencoding/delayedencoding.pro b/examples/draganddrop/delayedencoding/delayedencoding.pro new file mode 100644 index 0000000000..c7b95b6ed5 --- /dev/null +++ b/examples/draganddrop/delayedencoding/delayedencoding.pro @@ -0,0 +1,14 @@ +QT += svg + +HEADERS = mimedata.h \ + sourcewidget.h +RESOURCES = delayedencoding.qrc +SOURCES = main.cpp \ + mimedata.cpp \ + sourcewidget.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/draganddrop/delayedencoding +sources.files = $$SOURCES $$HEADERS *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/itemviews/delayedencoding +INSTALLS += target sources diff --git a/examples/draganddrop/delayedencoding/delayedencoding.qrc b/examples/draganddrop/delayedencoding/delayedencoding.qrc new file mode 100644 index 0000000000..374f21147d --- /dev/null +++ b/examples/draganddrop/delayedencoding/delayedencoding.qrc @@ -0,0 +1,6 @@ + + + images/drag.png + images/example.svg + + diff --git a/examples/draganddrop/delayedencoding/images/drag.png b/examples/draganddrop/delayedencoding/images/drag.png new file mode 100644 index 0000000000..dd795cfffc Binary files /dev/null and b/examples/draganddrop/delayedencoding/images/drag.png differ diff --git a/examples/draganddrop/delayedencoding/images/example.svg b/examples/draganddrop/delayedencoding/images/example.svg new file mode 100644 index 0000000000..4707cf891e --- /dev/null +++ b/examples/draganddrop/delayedencoding/images/example.svg @@ -0,0 +1,59 @@ + + + + Sphere + A gradient filled sphere. + + + + + + + + + + + + diff --git a/examples/draganddrop/delayedencoding/main.cpp b/examples/draganddrop/delayedencoding/main.cpp new file mode 100644 index 0000000000..801e474fae --- /dev/null +++ b/examples/draganddrop/delayedencoding/main.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 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 the GNU General Public License version 3.0 requirements will be +** met: 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. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include "sourcewidget.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + SourceWidget window; + window.show(); + return app.exec(); +} + diff --git a/examples/draganddrop/delayedencoding/mimedata.cpp b/examples/draganddrop/delayedencoding/mimedata.cpp new file mode 100644 index 0000000000..5c7c23c9d5 --- /dev/null +++ b/examples/draganddrop/delayedencoding/mimedata.cpp @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 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 the GNU General Public License version 3.0 requirements will be +** met: 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. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include "mimedata.h" + +MimeData::MimeData() + : QMimeData() +{ +} + +//![0] +QStringList MimeData::formats() const +{ + return QMimeData::formats() << "image/png"; +} +//![0] + +//![1] +QVariant MimeData::retrieveData(const QString &mimeType, QVariant::Type type) + const +{ + emit dataRequested(mimeType); + + return QMimeData::retrieveData(mimeType, type); +} +//![1] + diff --git a/examples/draganddrop/delayedencoding/mimedata.h b/examples/draganddrop/delayedencoding/mimedata.h new file mode 100644 index 0000000000..13e834dae7 --- /dev/null +++ b/examples/draganddrop/delayedencoding/mimedata.h @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 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 the GNU General Public License version 3.0 requirements will be +** met: 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. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MIMEDATA_H +#define MIMEDATA_H + +#include + +//![0] +class MimeData : public QMimeData +{ + Q_OBJECT + +public: + MimeData(); + QStringList formats() const; + +signals: + void dataRequested(const QString &mimeType) const; + +protected: + QVariant retrieveData(const QString &mimetype, QVariant::Type type) const; +}; +//![0] + +#endif diff --git a/examples/draganddrop/delayedencoding/sourcewidget.cpp b/examples/draganddrop/delayedencoding/sourcewidget.cpp new file mode 100644 index 0000000000..f5aed89cff --- /dev/null +++ b/examples/draganddrop/delayedencoding/sourcewidget.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 examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 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 the GNU General Public License version 3.0 requirements will be +** met: 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. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include "mimedata.h" +#include "sourcewidget.h" + +SourceWidget::SourceWidget(QWidget *parent) + : QWidget(parent) +{ + QFile imageFile(":/images/example.svg"); + imageFile.open(QIODevice::ReadOnly); + imageData = imageFile.readAll(); + imageFile.close(); + + QScrollArea *imageArea = new QScrollArea; + imageLabel = new QSvgWidget; + imageLabel->renderer()->load(imageData); + imageArea->setWidget(imageLabel); + //imageLabel->setMinimumSize(imageLabel->renderer()->viewBox().size()); + + QLabel *instructTopLabel = new QLabel(tr("This is an SVG drawing:")); + QLabel *instructBottomLabel = new QLabel( + tr("Drag the icon to copy the drawing as a PNG file:")); + QPushButton *dragIcon = new QPushButton(tr("Export")); + dragIcon->setIcon(QIcon(":/images/drag.png")); + + connect(dragIcon, SIGNAL(pressed()), this, SLOT(startDrag())); + + QGridLayout *layout = new QGridLayout; + layout->addWidget(instructTopLabel, 0, 0, 1, 2); + layout->addWidget(imageArea, 1, 0, 2, 2); + layout->addWidget(instructBottomLabel, 3, 0); + layout->addWidget(dragIcon, 3, 1); + setLayout(layout); + setWindowTitle(tr("Delayed Encoding")); +} + +//![1] +void SourceWidget::createData(const QString &mimeType) +{ + if (mimeType != "image/png") + return; + + QImage image(imageLabel->size(), QImage::Format_RGB32); + QPainter painter; + painter.begin(&image); + imageLabel->renderer()->render(&painter); + painter.end(); + + QByteArray data; + QBuffer buffer(&data); + buffer.open(QIODevice::WriteOnly); + image.save(&buffer, "PNG"); + buffer.close(); + + mimeData->setData("image/png", data); +} +//![1] + +//![0] +void SourceWidget::startDrag() +{ + mimeData = new MimeData; + + connect(mimeData, SIGNAL(dataRequested(const QString &)), + this, SLOT(createData(const QString &)), Qt::DirectConnection); + + QDrag *drag = new QDrag(this); + drag->setMimeData(mimeData); + drag->setPixmap(QPixmap(":/images/drag.png")); + + drag->exec(Qt::CopyAction); +} +//![0] + diff --git a/examples/draganddrop/delayedencoding/sourcewidget.h b/examples/draganddrop/delayedencoding/sourcewidget.h new file mode 100644 index 0000000000..8d6a6901cd --- /dev/null +++ b/examples/draganddrop/delayedencoding/sourcewidget.h @@ -0,0 +1,71 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 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 the GNU General Public License version 3.0 requirements will be +** met: 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. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef SOURCEWIDGET_H +#define SOURCEWIDGET_H + +#include +#include + +QT_BEGIN_NAMESPACE +class QSvgWidget; +QT_END_NAMESPACE +class MimeData; + +class SourceWidget : public QWidget +{ + Q_OBJECT + +public: + SourceWidget(QWidget *parent = 0); +//![0] +public slots: + void createData(const QString &mimetype); + void startDrag(); + +private: + QByteArray imageData; + QSvgWidget *imageLabel; + MimeData *mimeData; +//![0] +}; + +#endif diff --git a/examples/draganddrop/draganddrop.pro b/examples/draganddrop/draganddrop.pro new file mode 100644 index 0000000000..0cd881ac2d --- /dev/null +++ b/examples/draganddrop/draganddrop.pro @@ -0,0 +1,15 @@ +TEMPLATE = subdirs +SUBDIRS = draggableicons \ + draggabletext \ + dropsite \ + fridgemagnets \ + puzzle + +contains(QT_CONFIG, svg): SUBDIRS += delayedencoding + +wince*: SUBDIRS -= dropsite + +# install +sources.files = README *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/draganddrop +INSTALLS += sources diff --git a/examples/draganddrop/draggableicons/draggableicons.pro b/examples/draganddrop/draggableicons/draggableicons.pro new file mode 100644 index 0000000000..74cfda9e36 --- /dev/null +++ b/examples/draganddrop/draggableicons/draggableicons.pro @@ -0,0 +1,10 @@ +HEADERS = dragwidget.h +RESOURCES = draggableicons.qrc +SOURCES = dragwidget.cpp \ + main.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/draganddrop/draggableicons +sources.files = $$SOURCES $$HEADERS $$RESOURCES *.pro images +sources.path = $$[QT_INSTALL_EXAMPLES]/draganddrop/draggableicons +INSTALLS += target sources diff --git a/examples/draganddrop/draggableicons/draggableicons.qrc b/examples/draganddrop/draggableicons/draggableicons.qrc new file mode 100644 index 0000000000..63f84ac066 --- /dev/null +++ b/examples/draganddrop/draggableicons/draggableicons.qrc @@ -0,0 +1,7 @@ + + + images/boat.png + images/car.png + images/house.png + + diff --git a/examples/draganddrop/draggableicons/dragwidget.cpp b/examples/draganddrop/draggableicons/dragwidget.cpp new file mode 100644 index 0000000000..021f816737 --- /dev/null +++ b/examples/draganddrop/draggableicons/dragwidget.cpp @@ -0,0 +1,169 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 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 the GNU General Public License version 3.0 requirements will be +** met: 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. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "dragwidget.h" + +//! [0] +DragWidget::DragWidget(QWidget *parent) + : QFrame(parent) +{ + setMinimumSize(200, 200); + setFrameStyle(QFrame::Sunken | QFrame::StyledPanel); + setAcceptDrops(true); + + QLabel *boatIcon = new QLabel(this); + boatIcon->setPixmap(QPixmap(":/images/boat.png")); + boatIcon->move(20, 20); + boatIcon->show(); + boatIcon->setAttribute(Qt::WA_DeleteOnClose); + + QLabel *carIcon = new QLabel(this); + carIcon->setPixmap(QPixmap(":/images/car.png")); + carIcon->move(120, 20); + carIcon->show(); + carIcon->setAttribute(Qt::WA_DeleteOnClose); + + QLabel *houseIcon = new QLabel(this); + houseIcon->setPixmap(QPixmap(":/images/house.png")); + houseIcon->move(20, 120); + houseIcon->show(); + houseIcon->setAttribute(Qt::WA_DeleteOnClose); +} +//! [0] + +void DragWidget::dragEnterEvent(QDragEnterEvent *event) +{ + if (event->mimeData()->hasFormat("application/x-dnditemdata")) { + if (event->source() == this) { + event->setDropAction(Qt::MoveAction); + event->accept(); + } else { + event->acceptProposedAction(); + } + } else { + event->ignore(); + } +} + +void DragWidget::dragMoveEvent(QDragMoveEvent *event) +{ + if (event->mimeData()->hasFormat("application/x-dnditemdata")) { + if (event->source() == this) { + event->setDropAction(Qt::MoveAction); + event->accept(); + } else { + event->acceptProposedAction(); + } + } else { + event->ignore(); + } +} + +void DragWidget::dropEvent(QDropEvent *event) +{ + if (event->mimeData()->hasFormat("application/x-dnditemdata")) { + QByteArray itemData = event->mimeData()->data("application/x-dnditemdata"); + QDataStream dataStream(&itemData, QIODevice::ReadOnly); + + QPixmap pixmap; + QPoint offset; + dataStream >> pixmap >> offset; + + QLabel *newIcon = new QLabel(this); + newIcon->setPixmap(pixmap); + newIcon->move(event->pos() - offset); + newIcon->show(); + newIcon->setAttribute(Qt::WA_DeleteOnClose); + + if (event->source() == this) { + event->setDropAction(Qt::MoveAction); + event->accept(); + } else { + event->acceptProposedAction(); + } + } else { + event->ignore(); + } +} + +//! [1] +void DragWidget::mousePressEvent(QMouseEvent *event) +{ + QLabel *child = static_cast(childAt(event->pos())); + if (!child) + return; + + QPixmap pixmap = *child->pixmap(); + + QByteArray itemData; + QDataStream dataStream(&itemData, QIODevice::WriteOnly); + dataStream << pixmap << QPoint(event->pos() - child->pos()); +//! [1] + +//! [2] + QMimeData *mimeData = new QMimeData; + mimeData->setData("application/x-dnditemdata", itemData); +//! [2] + +//! [3] + QDrag *drag = new QDrag(this); + drag->setMimeData(mimeData); + drag->setPixmap(pixmap); + drag->setHotSpot(event->pos() - child->pos()); +//! [3] + + QPixmap tempPixmap = pixmap; + QPainter painter; + painter.begin(&tempPixmap); + painter.fillRect(pixmap.rect(), QColor(127, 127, 127, 127)); + painter.end(); + + child->setPixmap(tempPixmap); + + if (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) == Qt::MoveAction) + child->close(); + else { + child->show(); + child->setPixmap(pixmap); + } +} diff --git a/examples/draganddrop/draggableicons/dragwidget.h b/examples/draganddrop/draggableicons/dragwidget.h new file mode 100644 index 0000000000..201adf83a4 --- /dev/null +++ b/examples/draganddrop/draggableicons/dragwidget.h @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 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 the GNU General Public License version 3.0 requirements will be +** met: 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. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DRAGWIDGET_H +#define DRAGWIDGET_H + +#include + +QT_BEGIN_NAMESPACE +class QDragEnterEvent; +class QDropEvent; +QT_END_NAMESPACE + +//! [0] +class DragWidget : public QFrame +{ +public: + DragWidget(QWidget *parent=0); + +protected: + void dragEnterEvent(QDragEnterEvent *event); + void dragMoveEvent(QDragMoveEvent *event); + void dropEvent(QDropEvent *event); + void mousePressEvent(QMouseEvent *event); +}; +//! [0] + +#endif diff --git a/examples/draganddrop/draggableicons/images/boat.png b/examples/draganddrop/draggableicons/images/boat.png new file mode 100644 index 0000000000..46c894f06b Binary files /dev/null and b/examples/draganddrop/draggableicons/images/boat.png differ diff --git a/examples/draganddrop/draggableicons/images/car.png b/examples/draganddrop/draggableicons/images/car.png new file mode 100644 index 0000000000..3cb35e56cb Binary files /dev/null and b/examples/draganddrop/draggableicons/images/car.png differ diff --git a/examples/draganddrop/draggableicons/images/house.png b/examples/draganddrop/draggableicons/images/house.png new file mode 100644 index 0000000000..ee9d5b1159 Binary files /dev/null and b/examples/draganddrop/draggableicons/images/house.png differ diff --git a/examples/draganddrop/draggableicons/main.cpp b/examples/draganddrop/draggableicons/main.cpp new file mode 100644 index 0000000000..cd945370cc --- /dev/null +++ b/examples/draganddrop/draggableicons/main.cpp @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 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 the GNU General Public License version 3.0 requirements will be +** met: 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. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include "dragwidget.h" + +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(draggableicons); + + QApplication app(argc, argv); + + QWidget mainWidget; + QHBoxLayout *horizontalLayout = new QHBoxLayout; + horizontalLayout->addWidget(new DragWidget); + horizontalLayout->addWidget(new DragWidget); + + mainWidget.setLayout(horizontalLayout); + mainWidget.setWindowTitle(QObject::tr("Draggable Icons")); + mainWidget.show(); + + return app.exec(); +} diff --git a/examples/draganddrop/draggabletext/draggabletext.pro b/examples/draganddrop/draggabletext/draggabletext.pro new file mode 100644 index 0000000000..07c7c24b46 --- /dev/null +++ b/examples/draganddrop/draggabletext/draggabletext.pro @@ -0,0 +1,12 @@ +HEADERS = draglabel.h \ + dragwidget.h +RESOURCES = draggabletext.qrc +SOURCES = draglabel.cpp \ + dragwidget.cpp \ + main.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/draganddrop/draggabletext +sources.files = $$SOURCES $$HEADERS $$RESOURCES *.txt *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/draganddrop/draggabletext +INSTALLS += target sources diff --git a/examples/draganddrop/draggabletext/draggabletext.qrc b/examples/draganddrop/draggabletext/draggabletext.qrc new file mode 100644 index 0000000000..b72217d701 --- /dev/null +++ b/examples/draganddrop/draggabletext/draggabletext.qrc @@ -0,0 +1,5 @@ + + + words.txt + + diff --git a/examples/draganddrop/draggabletext/draglabel.cpp b/examples/draganddrop/draggabletext/draglabel.cpp new file mode 100644 index 0000000000..4dfa099bb6 --- /dev/null +++ b/examples/draganddrop/draggabletext/draglabel.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 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 the GNU General Public License version 3.0 requirements will be +** met: 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. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "draglabel.h" + +DragLabel::DragLabel(const QString &text, QWidget *parent) + : QLabel(text, parent) +{ + setAutoFillBackground(true); + setFrameShape(QFrame::Panel); + setFrameShadow(QFrame::Raised); +} diff --git a/examples/draganddrop/draggabletext/draglabel.h b/examples/draganddrop/draggabletext/draglabel.h new file mode 100644 index 0000000000..8ba2778e77 --- /dev/null +++ b/examples/draganddrop/draggabletext/draglabel.h @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 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 the GNU General Public License version 3.0 requirements will be +** met: 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. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DRAGLABEL_H +#define DRAGLABEL_H + +#include + +QT_BEGIN_NAMESPACE +class QDragEnterEvent; +class QDragMoveEvent; +class QFrame; +QT_END_NAMESPACE + +class DragLabel : public QLabel +{ +public: + DragLabel(const QString &text, QWidget *parent); +}; + +#endif diff --git a/examples/draganddrop/draggabletext/dragwidget.cpp b/examples/draganddrop/draggabletext/dragwidget.cpp new file mode 100644 index 0000000000..2af7b295de --- /dev/null +++ b/examples/draganddrop/draggabletext/dragwidget.cpp @@ -0,0 +1,164 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 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 the GNU General Public License version 3.0 requirements will be +** met: 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. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "draglabel.h" +#include "dragwidget.h" + +DragWidget::DragWidget(QWidget *parent) + : QWidget(parent) +{ + QFile dictionaryFile(":/dictionary/words.txt"); + dictionaryFile.open(QIODevice::ReadOnly); + QTextStream inputStream(&dictionaryFile); + + int x = 5; + int y = 5; + + while (!inputStream.atEnd()) { + QString word; + inputStream >> word; + if (!word.isEmpty()) { + DragLabel *wordLabel = new DragLabel(word, this); + wordLabel->move(x, y); + wordLabel->show(); + wordLabel->setAttribute(Qt::WA_DeleteOnClose); + x += wordLabel->width() + 2; + if (x >= 195) { + x = 5; + y += wordLabel->height() + 2; + } + } + } + + QPalette newPalette = palette(); + newPalette.setColor(QPalette::Window, Qt::white); + setPalette(newPalette); + + setAcceptDrops(true); + setMinimumSize(400, qMax(200, y)); + setWindowTitle(tr("Draggable Text")); +} + +void DragWidget::dragEnterEvent(QDragEnterEvent *event) +{ + if (event->mimeData()->hasText()) { + if (children().contains(event->source())) { + event->setDropAction(Qt::MoveAction); + event->accept(); + } else { + event->acceptProposedAction(); + } + } else { + event->ignore(); + } +} + +void DragWidget::dropEvent(QDropEvent *event) +{ + if (event->mimeData()->hasText()) { + const QMimeData *mime = event->mimeData(); + QStringList pieces = mime->text().split(QRegExp("\\s+"), + QString::SkipEmptyParts); + QPoint position = event->pos(); + QPoint hotSpot; + + QList hotSpotPos = mime->data("application/x-hotspot").split(' '); + if (hotSpotPos.size() == 2) { + hotSpot.setX(hotSpotPos.first().toInt()); + hotSpot.setY(hotSpotPos.last().toInt()); + } + + foreach (QString piece, pieces) { + DragLabel *newLabel = new DragLabel(piece, this); + newLabel->move(position - hotSpot); + newLabel->show(); + newLabel->setAttribute(Qt::WA_DeleteOnClose); + + position += QPoint(newLabel->width(), 0); + } + + if (event->source() == this) { + event->setDropAction(Qt::MoveAction); + event->accept(); + } else { + event->acceptProposedAction(); + } + } else { + event->ignore(); + } + foreach (QObject *child, children()) { + if (child->inherits("QWidget")) { + QWidget *widget = static_cast(child); + if (!widget->isVisible()) + widget->deleteLater(); + } + } +} + +void DragWidget::mousePressEvent(QMouseEvent *event) +{ + QLabel *child = static_cast(childAt(event->pos())); + if (!child) + return; + + QPoint hotSpot = event->pos() - child->pos(); + + QMimeData *mimeData = new QMimeData; + mimeData->setText(child->text()); + mimeData->setData("application/x-hotspot", + QByteArray::number(hotSpot.x()) + + " " + QByteArray::number(hotSpot.y())); + + QPixmap pixmap(child->size()); + child->render(&pixmap); + + QDrag *drag = new QDrag(this); + drag->setMimeData(mimeData); + drag->setPixmap(pixmap); + drag->setHotSpot(hotSpot); + + Qt::DropAction dropAction = drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction); + + if (dropAction == Qt::MoveAction) + child->close(); +} diff --git a/examples/draganddrop/draggabletext/dragwidget.h b/examples/draganddrop/draggabletext/dragwidget.h new file mode 100644 index 0000000000..b51f05b2a4 --- /dev/null +++ b/examples/draganddrop/draggabletext/dragwidget.h @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 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 the GNU General Public License version 3.0 requirements will be +** met: 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. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DRAGWIDGET_H +#define DRAGWIDGET_H + +#include + +QT_BEGIN_NAMESPACE +class QDragEnterEvent; +class QDropEvent; +QT_END_NAMESPACE + +class DragWidget : public QWidget +{ +public: + DragWidget(QWidget *parent = 0); + +protected: + void dragEnterEvent(QDragEnterEvent *event); + void dropEvent(QDropEvent *event); + void mousePressEvent(QMouseEvent *event); +}; + +#endif diff --git a/examples/draganddrop/draggabletext/main.cpp b/examples/draganddrop/draggabletext/main.cpp new file mode 100644 index 0000000000..e66c8b71d5 --- /dev/null +++ b/examples/draganddrop/draggabletext/main.cpp @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 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 the GNU General Public License version 3.0 requirements will be +** met: 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. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include "dragwidget.h" + +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(draggabletext); + + QApplication app(argc, argv); + DragWidget window; + window.show(); + return app.exec(); +} diff --git a/examples/draganddrop/draggabletext/words.txt b/examples/draganddrop/draggabletext/words.txt new file mode 100644 index 0000000000..19b8b0325b --- /dev/null +++ b/examples/draganddrop/draggabletext/words.txt @@ -0,0 +1,41 @@ +Qt +Quarterly +is +a +paper +based +newsletter +exclusively +available +to +Qt +customers +Every +quarter +we +mail +out +an +issue +that +we +hope +will +bring +added +insight +and +pleasure +to +your +Qt +programming +with +high +quality +technical +articles +written +by +Qt +experts diff --git a/examples/draganddrop/dropsite/droparea.cpp b/examples/draganddrop/dropsite/droparea.cpp new file mode 100644 index 0000000000..495e2bfe3e --- /dev/null +++ b/examples/draganddrop/dropsite/droparea.cpp @@ -0,0 +1,127 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 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 the GNU General Public License version 3.0 requirements will be +** met: 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. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "droparea.h" + +//! [DropArea constructor] +DropArea::DropArea(QWidget *parent) + : QLabel(parent) +{ + setMinimumSize(200, 200); + setFrameStyle(QFrame::Sunken | QFrame::StyledPanel); + setAlignment(Qt::AlignCenter); + setAcceptDrops(true); + setAutoFillBackground(true); + clear(); +} +//! [DropArea constructor] + +//! [dragEnterEvent() function] +void DropArea::dragEnterEvent(QDragEnterEvent *event) +{ + setText(tr("")); + setBackgroundRole(QPalette::Highlight); + + event->acceptProposedAction(); + emit changed(event->mimeData()); +} +//! [dragEnterEvent() function] + +//! [dragMoveEvent() function] +void DropArea::dragMoveEvent(QDragMoveEvent *event) +{ + event->acceptProposedAction(); +} +//! [dragMoveEvent() function] + +//! [dropEvent() function part1] +void DropArea::dropEvent(QDropEvent *event) +{ + const QMimeData *mimeData = event->mimeData(); +//! [dropEvent() function part1] + +//! [dropEvent() function part2] + if (mimeData->hasImage()) { + setPixmap(qvariant_cast(mimeData->imageData())); + } else if (mimeData->hasHtml()) { + setText(mimeData->html()); + setTextFormat(Qt::RichText); + } else if (mimeData->hasText()) { + setText(mimeData->text()); + setTextFormat(Qt::PlainText); + } else if (mimeData->hasUrls()) { + QList urlList = mimeData->urls(); + QString text; + for (int i = 0; i < urlList.size() && i < 32; ++i) { + QString url = urlList.at(i).path(); + text += url + QString("\n"); + } + setText(text); + } else { + setText(tr("Cannot display data")); + } +//! [dropEvent() function part2] + +//! [dropEvent() function part3] + setBackgroundRole(QPalette::Dark); + event->acceptProposedAction(); +} +//! [dropEvent() function part3] + +//! [dragLeaveEvent() function] +void DropArea::dragLeaveEvent(QDragLeaveEvent *event) +{ + clear(); + event->accept(); +} +//! [dragLeaveEvent() function] + +//! [clear() function] +void DropArea::clear() +{ + setText(tr("")); + setBackgroundRole(QPalette::Dark); + + emit changed(); +} +//! [clear() function] diff --git a/examples/draganddrop/dropsite/droparea.h b/examples/draganddrop/dropsite/droparea.h new file mode 100644 index 0000000000..8a40acccda --- /dev/null +++ b/examples/draganddrop/dropsite/droparea.h @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 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 the GNU General Public License version 3.0 requirements will be +** met: 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. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DROPAREA_H +#define DROPAREA_H + +#include + +QT_BEGIN_NAMESPACE +class QMimeData; +QT_END_NAMESPACE + +//! [DropArea header part1] +class DropArea : public QLabel +{ + Q_OBJECT + +public: + DropArea(QWidget *parent = 0); + +public slots: + void clear(); + +signals: + void changed(const QMimeData *mimeData = 0); +//! [DropArea header part1] + +//! [DropArea header part2] +protected: + void dragEnterEvent(QDragEnterEvent *event); + void dragMoveEvent(QDragMoveEvent *event); + void dragLeaveEvent(QDragLeaveEvent *event); + void dropEvent(QDropEvent *event); + +private: + QLabel *label; +}; +//! [DropArea header part2] + +#endif diff --git a/examples/draganddrop/dropsite/dropsite.pro b/examples/draganddrop/dropsite/dropsite.pro new file mode 100644 index 0000000000..29dd0fdb4d --- /dev/null +++ b/examples/draganddrop/dropsite/dropsite.pro @@ -0,0 +1,12 @@ +HEADERS = droparea.h \ + dropsitewindow.h +SOURCES = droparea.cpp \ + dropsitewindow.cpp \ + main.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/draganddrop/dropsite +sources.files = $$SOURCES $$HEADERS $$RESOURCES *.pro images +sources.path = $$[QT_INSTALL_EXAMPLES]/draganddrop/dropsite +INSTALLS += target sources + diff --git a/examples/draganddrop/dropsite/dropsitewindow.cpp b/examples/draganddrop/dropsite/dropsitewindow.cpp new file mode 100644 index 0000000000..6e7055b8b6 --- /dev/null +++ b/examples/draganddrop/dropsite/dropsitewindow.cpp @@ -0,0 +1,146 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 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 the GNU General Public License version 3.0 requirements will be +** met: 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. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "droparea.h" +#include "dropsitewindow.h" + +//! [constructor part1] +DropSiteWindow::DropSiteWindow() +{ + abstractLabel = new QLabel(tr("This example accepts drags from other " + "applications and displays the MIME types " + "provided by the drag object.")); + abstractLabel->setWordWrap(true); + abstractLabel->adjustSize(); +//! [constructor part1] + +//! [constructor part2] + dropArea = new DropArea; + connect(dropArea, SIGNAL(changed(const QMimeData *)), + this, SLOT(updateFormatsTable(const QMimeData *))); +//! [constructor part2] + +//! [constructor part3] + QStringList labels; + labels << tr("Format") << tr("Content"); + + formatsTable = new QTableWidget; + formatsTable->setColumnCount(2); + formatsTable->setEditTriggers(QAbstractItemView::NoEditTriggers); + formatsTable->setHorizontalHeaderLabels(labels); + formatsTable->horizontalHeader()->setStretchLastSection(true); +//! [constructor part3] + +//! [constructor part4] + clearButton = new QPushButton(tr("Clear")); + quitButton = new QPushButton(tr("Quit")); + + buttonBox = new QDialogButtonBox; + buttonBox->addButton(clearButton, QDialogButtonBox::ActionRole); + buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole); + + connect(quitButton, SIGNAL(pressed()), this, SLOT(close())); + connect(clearButton, SIGNAL(pressed()), dropArea, SLOT(clear())); +//! [constructor part4] + +//! [constructor part5] + QVBoxLayout *mainLayout = new QVBoxLayout; + mainLayout->addWidget(abstractLabel); + mainLayout->addWidget(dropArea); + mainLayout->addWidget(formatsTable); + mainLayout->addWidget(buttonBox); + setLayout(mainLayout); + + setWindowTitle(tr("Drop Site")); + setMinimumSize(350, 500); +} +//! [constructor part5] + +//! [updateFormatsTable() part1] +void DropSiteWindow::updateFormatsTable(const QMimeData *mimeData) +{ + formatsTable->setRowCount(0); + if (!mimeData) + return; +//! [updateFormatsTable() part1] + +//! [updateFormatsTable() part2] + foreach (QString format, mimeData->formats()) { + QTableWidgetItem *formatItem = new QTableWidgetItem(format); + formatItem->setFlags(Qt::ItemIsEnabled); + formatItem->setTextAlignment(Qt::AlignTop | Qt::AlignLeft); +//! [updateFormatsTable() part2] + +//! [updateFormatsTable() part3] + QString text; + if (format == "text/plain") { + text = mimeData->text().simplified(); + } else if (format == "text/html") { + text = mimeData->html().simplified(); + } else if (format == "text/uri-list") { + QList urlList = mimeData->urls(); + for (int i = 0; i < urlList.size() && i < 32; ++i) { + QString url = urlList.at(i).path(); + text.append(url + " "); + } + } else { + QByteArray data = mimeData->data(format); + for (int i = 0; i < data.size() && i < 32; ++i) { + QString hex = QString("%1").arg(uchar(data[i]), 2, 16, + QChar('0')) + .toUpper(); + text.append(hex + " "); + } + } +//! [updateFormatsTable() part3] + +//! [updateFormatsTable() part4] + int row = formatsTable->rowCount(); + formatsTable->insertRow(row); + formatsTable->setItem(row, 0, new QTableWidgetItem(format)); + formatsTable->setItem(row, 1, new QTableWidgetItem(text)); + } + + formatsTable->resizeColumnToContents(0); +} +//! [updateFormatsTable() part4] diff --git a/examples/draganddrop/dropsite/dropsitewindow.h b/examples/draganddrop/dropsite/dropsitewindow.h new file mode 100644 index 0000000000..bbdc687ed3 --- /dev/null +++ b/examples/draganddrop/dropsite/dropsitewindow.h @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 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 the GNU General Public License version 3.0 requirements will be +** met: 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. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DROPSITEWINDOW_H +#define DROPSITEWINDOW_H + +#include + +QT_BEGIN_NAMESPACE +class QDialogButtonBox; +class QLabel; +class QMimeData; +class QPushButton; +class QTableWidget; +QT_END_NAMESPACE +class DropArea; + +//! [DropSiteWindow header] +class DropSiteWindow : public QWidget +{ + Q_OBJECT + +public: + DropSiteWindow(); + +public slots: + void updateFormatsTable(const QMimeData *mimeData); + +private: + DropArea *dropArea; + QLabel *abstractLabel; + QTableWidget *formatsTable; + + QPushButton *clearButton; + QPushButton *quitButton; + QDialogButtonBox *buttonBox; +}; +//! [DropSiteWindow header] + +#endif diff --git a/examples/draganddrop/dropsite/main.cpp b/examples/draganddrop/dropsite/main.cpp new file mode 100644 index 0000000000..62589ad98c --- /dev/null +++ b/examples/draganddrop/dropsite/main.cpp @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 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 the GNU General Public License version 3.0 requirements will be +** met: 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. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "dropsitewindow.h" + +//! [main() function] +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + DropSiteWindow window; + window.show(); + return app.exec(); +} +//! [main() function] diff --git a/examples/draganddrop/fridgemagnets/draglabel.cpp b/examples/draganddrop/fridgemagnets/draglabel.cpp new file mode 100644 index 0000000000..1bec024e2d --- /dev/null +++ b/examples/draganddrop/fridgemagnets/draglabel.cpp @@ -0,0 +1,90 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 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 the GNU General Public License version 3.0 requirements will be +** met: 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. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "draglabel.h" + +//! [0] +DragLabel::DragLabel(const QString &text, QWidget *parent) + : QLabel(parent) +{ + QFontMetrics metric(font()); + QSize size = metric.size(Qt::TextSingleLine, text); + + QImage image(size.width() + 12, size.height() + 12, + QImage::Format_ARGB32_Premultiplied); + image.fill(qRgba(0, 0, 0, 0)); + + QFont font; + font.setStyleStrategy(QFont::ForceOutline); +//! [0] + +//! [1] + QLinearGradient gradient(0, 0, 0, image.height()-1); + gradient.setColorAt(0.0, Qt::white); + gradient.setColorAt(0.2, QColor(200, 200, 255)); + gradient.setColorAt(0.8, QColor(200, 200, 255)); + gradient.setColorAt(1.0, QColor(127, 127, 200)); + + QPainter painter; + painter.begin(&image); + painter.setRenderHint(QPainter::Antialiasing); + painter.setBrush(gradient); + painter.drawRoundedRect(QRectF(0.5, 0.5, image.width()-1, image.height()-1), + 25, 25, Qt::RelativeSize); + + painter.setFont(font); + painter.setBrush(Qt::black); + painter.drawText(QRect(QPoint(6, 6), size), Qt::AlignCenter, text); + painter.end(); +//! [1] + +//! [2] + setPixmap(QPixmap::fromImage(image)); + m_labelText = text; +} +//! [2] + +QString DragLabel::labelText() const +{ + return m_labelText; +} diff --git a/examples/draganddrop/fridgemagnets/draglabel.h b/examples/draganddrop/fridgemagnets/draglabel.h new file mode 100644 index 0000000000..5d2fd5281b --- /dev/null +++ b/examples/draganddrop/fridgemagnets/draglabel.h @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 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 the GNU General Public License version 3.0 requirements will be +** met: 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. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DRAGLABEL_H +#define DRAGLABEL_H + +#include + +QT_BEGIN_NAMESPACE +class QDragEnterEvent; +class QDragMoveEvent; +class QFrame; +QT_END_NAMESPACE + +//! [0] +class DragLabel : public QLabel +{ +public: + DragLabel(const QString &text, QWidget *parent); + QString labelText() const; + +private: + QString m_labelText; +}; +//! [0] + +#endif diff --git a/examples/draganddrop/fridgemagnets/dragwidget.cpp b/examples/draganddrop/fridgemagnets/dragwidget.cpp new file mode 100644 index 0000000000..43a9588c5a --- /dev/null +++ b/examples/draganddrop/fridgemagnets/dragwidget.cpp @@ -0,0 +1,212 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 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 the GNU General Public License version 3.0 requirements will be +** met: 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. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "draglabel.h" +#include "dragwidget.h" + +//! [0] +DragWidget::DragWidget(QWidget *parent) + : QWidget(parent) +{ + QFile dictionaryFile(":/dictionary/words.txt"); + dictionaryFile.open(QFile::ReadOnly); + QTextStream inputStream(&dictionaryFile); +//! [0] + +//! [1] + int x = 5; + int y = 5; + + while (!inputStream.atEnd()) { + QString word; + inputStream >> word; + if (!word.isEmpty()) { + DragLabel *wordLabel = new DragLabel(word, this); + wordLabel->move(x, y); + wordLabel->show(); + wordLabel->setAttribute(Qt::WA_DeleteOnClose); + x += wordLabel->width() + 2; + if (x >= 245) { + x = 5; + y += wordLabel->height() + 2; + } + } + } +//! [1] + +//! [2] + QPalette newPalette = palette(); + newPalette.setColor(QPalette::Window, Qt::white); + setPalette(newPalette); + + setMinimumSize(400, qMax(200, y)); + setWindowTitle(tr("Fridge Magnets")); +//! [2] //! [3] + setAcceptDrops(true); +} +//! [3] + +//! [4] +void DragWidget::dragEnterEvent(QDragEnterEvent *event) +{ +//! [4] //! [5] + if (event->mimeData()->hasFormat("application/x-fridgemagnet")) { + if (children().contains(event->source())) { + event->setDropAction(Qt::MoveAction); + event->accept(); + } else { + event->acceptProposedAction(); +//! [5] //! [6] + } +//! [6] //! [7] + } else if (event->mimeData()->hasText()) { + event->acceptProposedAction(); + } else { + event->ignore(); + } +} +//! [7] + +//! [8] +void DragWidget::dragMoveEvent(QDragMoveEvent *event) +{ + if (event->mimeData()->hasFormat("application/x-fridgemagnet")) { + if (children().contains(event->source())) { + event->setDropAction(Qt::MoveAction); + event->accept(); + } else { + event->acceptProposedAction(); + } + } else if (event->mimeData()->hasText()) { + event->acceptProposedAction(); + } else { + event->ignore(); + } +} +//! [8] + +//! [9] +void DragWidget::dropEvent(QDropEvent *event) +{ + if (event->mimeData()->hasFormat("application/x-fridgemagnet")) { + const QMimeData *mime = event->mimeData(); +//! [9] //! [10] + QByteArray itemData = mime->data("application/x-fridgemagnet"); + QDataStream dataStream(&itemData, QIODevice::ReadOnly); + + QString text; + QPoint offset; + dataStream >> text >> offset; +//! [10] +//! [11] + DragLabel *newLabel = new DragLabel(text, this); + newLabel->move(event->pos() - offset); + newLabel->show(); + newLabel->setAttribute(Qt::WA_DeleteOnClose); + + if (event->source() == this) { + event->setDropAction(Qt::MoveAction); + event->accept(); + } else { + event->acceptProposedAction(); + } +//! [11] //! [12] + } else if (event->mimeData()->hasText()) { + QStringList pieces = event->mimeData()->text().split(QRegExp("\\s+"), + QString::SkipEmptyParts); + QPoint position = event->pos(); + + foreach (QString piece, pieces) { + DragLabel *newLabel = new DragLabel(piece, this); + newLabel->move(position); + newLabel->show(); + newLabel->setAttribute(Qt::WA_DeleteOnClose); + + position += QPoint(newLabel->width(), 0); + } + + event->acceptProposedAction(); + } else { + event->ignore(); + } +} +//! [12] + +//! [13] +void DragWidget::mousePressEvent(QMouseEvent *event) +{ +//! [13] +//! [14] + DragLabel *child = static_cast(childAt(event->pos())); + if (!child) + return; + + QPoint hotSpot = event->pos() - child->pos(); + + QByteArray itemData; + QDataStream dataStream(&itemData, QIODevice::WriteOnly); + dataStream << child->labelText() << QPoint(hotSpot); +//! [14] + +//! [15] + QMimeData *mimeData = new QMimeData; + mimeData->setData("application/x-fridgemagnet", itemData); + mimeData->setText(child->labelText()); +//! [15] + +//! [16] + QDrag *drag = new QDrag(this); + drag->setMimeData(mimeData); + drag->setPixmap(*child->pixmap()); + drag->setHotSpot(hotSpot); + + child->hide(); +//! [16] + +//! [17] + if (drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::CopyAction) == Qt::MoveAction) + child->close(); + else + child->show(); +} +//! [17] diff --git a/examples/draganddrop/fridgemagnets/dragwidget.h b/examples/draganddrop/fridgemagnets/dragwidget.h new file mode 100644 index 0000000000..4722917615 --- /dev/null +++ b/examples/draganddrop/fridgemagnets/dragwidget.h @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 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 the GNU General Public License version 3.0 requirements will be +** met: 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. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DRAGWIDGET_H +#define DRAGWIDGET_H + +#include + +QT_BEGIN_NAMESPACE +class QDragEnterEvent; +class QDropEvent; +QT_END_NAMESPACE + +//! [0] +class DragWidget : public QWidget +{ +public: + DragWidget(QWidget *parent = 0); + +protected: + void dragEnterEvent(QDragEnterEvent *event); + void dragMoveEvent(QDragMoveEvent *event); + void dropEvent(QDropEvent *event); + void mousePressEvent(QMouseEvent *event); +}; +//! [0] + +#endif diff --git a/examples/draganddrop/fridgemagnets/fridgemagnets.pro b/examples/draganddrop/fridgemagnets/fridgemagnets.pro new file mode 100644 index 0000000000..f1baaefdac --- /dev/null +++ b/examples/draganddrop/fridgemagnets/fridgemagnets.pro @@ -0,0 +1,12 @@ +HEADERS = draglabel.h \ + dragwidget.h +RESOURCES = fridgemagnets.qrc +SOURCES = draglabel.cpp \ + dragwidget.cpp \ + main.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/draganddrop/fridgemagnets +sources.files = $$SOURCES $$HEADERS $$RESOURCES *.pro *.txt +sources.path = $$[QT_INSTALL_EXAMPLES]/draganddrop/fridgemagnets +INSTALLS += target sources diff --git a/examples/draganddrop/fridgemagnets/fridgemagnets.qrc b/examples/draganddrop/fridgemagnets/fridgemagnets.qrc new file mode 100644 index 0000000000..b72217d701 --- /dev/null +++ b/examples/draganddrop/fridgemagnets/fridgemagnets.qrc @@ -0,0 +1,5 @@ + + + words.txt + + diff --git a/examples/draganddrop/fridgemagnets/main.cpp b/examples/draganddrop/fridgemagnets/main.cpp new file mode 100644 index 0000000000..c24cdb2198 --- /dev/null +++ b/examples/draganddrop/fridgemagnets/main.cpp @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 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 the GNU General Public License version 3.0 requirements will be +** met: 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. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include "dragwidget.h" + +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(fridgemagnets); + + QApplication app(argc, argv); + DragWidget window; + window.show(); + return app.exec(); +} diff --git a/examples/draganddrop/fridgemagnets/words.txt b/examples/draganddrop/fridgemagnets/words.txt new file mode 100644 index 0000000000..a7e1632b09 --- /dev/null +++ b/examples/draganddrop/fridgemagnets/words.txt @@ -0,0 +1,48 @@ +Colorless +green +ideas +sleep +furiously +A +colorless +green +idea +is +a +new +untried +idea +that +is +without +vividness +dull +and +unexciting +To +sleep +furiously +may +seem +a +puzzling +turn +of +phrase +but +the +mind +in +sleep +often +indeed +moves +furiously +with +ideas +and +images +flickering +in +and +out diff --git a/examples/draganddrop/puzzle/example.jpg b/examples/draganddrop/puzzle/example.jpg new file mode 100644 index 0000000000..e09fb70757 Binary files /dev/null and b/examples/draganddrop/puzzle/example.jpg differ diff --git a/examples/draganddrop/puzzle/main.cpp b/examples/draganddrop/puzzle/main.cpp new file mode 100644 index 0000000000..e0e5cc1907 --- /dev/null +++ b/examples/draganddrop/puzzle/main.cpp @@ -0,0 +1,55 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 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 the GNU General Public License version 3.0 requirements will be +** met: 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. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(puzzle); + + QApplication app(argc, argv); + MainWindow window; + window.openImage(":/images/example.jpg"); + window.show(); + return app.exec(); +} diff --git a/examples/draganddrop/puzzle/mainwindow.cpp b/examples/draganddrop/puzzle/mainwindow.cpp new file mode 100644 index 0000000000..f998882e9e --- /dev/null +++ b/examples/draganddrop/puzzle/mainwindow.cpp @@ -0,0 +1,151 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 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 the GNU General Public License version 3.0 requirements will be +** met: 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. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include + +#include "mainwindow.h" +#include "pieceslist.h" +#include "puzzlewidget.h" + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) +{ + setupMenus(); + setupWidgets(); + + setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed)); + setWindowTitle(tr("Puzzle")); +} + +void MainWindow::openImage(const QString &path) +{ + QString fileName = path; + + if (fileName.isNull()) + fileName = QFileDialog::getOpenFileName(this, + tr("Open Image"), "", "Image Files (*.png *.jpg *.bmp)"); + + if (!fileName.isEmpty()) { + QPixmap newImage; + if (!newImage.load(fileName)) { + QMessageBox::warning(this, tr("Open Image"), + tr("The image file could not be loaded."), + QMessageBox::Cancel); + return; + } + puzzleImage = newImage; + setupPuzzle(); + } +} + +void MainWindow::setCompleted() +{ + QMessageBox::information(this, tr("Puzzle Completed"), + tr("Congratulations! You have completed the puzzle!\n" + "Click OK to start again."), + QMessageBox::Ok); + + setupPuzzle(); +} + +void MainWindow::setupPuzzle() +{ + int size = qMin(puzzleImage.width(), puzzleImage.height()); + puzzleImage = puzzleImage.copy((puzzleImage.width() - size)/2, + (puzzleImage.height() - size)/2, size, size).scaled(400, + 400, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); + + piecesList->clear(); + + for (int y = 0; y < 5; ++y) { + for (int x = 0; x < 5; ++x) { + QPixmap pieceImage = puzzleImage.copy(x*80, y*80, 80, 80); + piecesList->addPiece(pieceImage, QPoint(x, y)); + } + } + + qsrand(QCursor::pos().x() ^ QCursor::pos().y()); + + for (int i = 0; i < piecesList->count(); ++i) { + if (int(2.0*qrand()/(RAND_MAX+1.0)) == 1) { + QListWidgetItem *item = piecesList->takeItem(i); + piecesList->insertItem(0, item); + } + } + + puzzleWidget->clear(); +} + +void MainWindow::setupMenus() +{ + QMenu *fileMenu = menuBar()->addMenu(tr("&File")); + + QAction *openAction = fileMenu->addAction(tr("&Open...")); + openAction->setShortcut(QKeySequence(tr("Ctrl+O"))); + + QAction *exitAction = fileMenu->addAction(tr("E&xit")); + exitAction->setShortcut(QKeySequence(tr("Ctrl+Q"))); + + QMenu *gameMenu = menuBar()->addMenu(tr("&Game")); + + QAction *restartAction = gameMenu->addAction(tr("&Restart")); + + connect(openAction, SIGNAL(triggered()), this, SLOT(openImage())); + connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit())); + connect(restartAction, SIGNAL(triggered()), this, SLOT(setupPuzzle())); +} + +void MainWindow::setupWidgets() +{ + QFrame *frame = new QFrame; + QHBoxLayout *frameLayout = new QHBoxLayout(frame); + + piecesList = new PiecesList; + puzzleWidget = new PuzzleWidget; + + connect(puzzleWidget, SIGNAL(puzzleCompleted()), + this, SLOT(setCompleted()), Qt::QueuedConnection); + + frameLayout->addWidget(piecesList); + frameLayout->addWidget(puzzleWidget); + setCentralWidget(frame); +} diff --git a/examples/draganddrop/puzzle/mainwindow.h b/examples/draganddrop/puzzle/mainwindow.h new file mode 100644 index 0000000000..edfbb12e03 --- /dev/null +++ b/examples/draganddrop/puzzle/mainwindow.h @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 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 the GNU General Public License version 3.0 requirements will be +** met: 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. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include + +class PiecesList; +class PuzzleWidget; +QT_BEGIN_NAMESPACE +class QListWidgetItem; +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(QWidget *parent = 0); + +public slots: + void openImage(const QString &path = QString()); + void setupPuzzle(); + +private slots: + void setCompleted(); + +private: + void setupMenus(); + void setupWidgets(); + + QPixmap puzzleImage; + PiecesList *piecesList; + PuzzleWidget *puzzleWidget; +}; + +#endif diff --git a/examples/draganddrop/puzzle/pieceslist.cpp b/examples/draganddrop/puzzle/pieceslist.cpp new file mode 100644 index 0000000000..77d06fb6ed --- /dev/null +++ b/examples/draganddrop/puzzle/pieceslist.cpp @@ -0,0 +1,122 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 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 the GNU General Public License version 3.0 requirements will be +** met: 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. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "pieceslist.h" + +PiecesList::PiecesList(QWidget *parent) + : QListWidget(parent) +{ + setDragEnabled(true); + setViewMode(QListView::IconMode); + setIconSize(QSize(60, 60)); + setSpacing(10); + setAcceptDrops(true); + setDropIndicatorShown(true); +} + +void PiecesList::dragEnterEvent(QDragEnterEvent *event) +{ + if (event->mimeData()->hasFormat("image/x-puzzle-piece")) + event->accept(); + else + event->ignore(); +} + +void PiecesList::dragMoveEvent(QDragMoveEvent *event) +{ + if (event->mimeData()->hasFormat("image/x-puzzle-piece")) { + event->setDropAction(Qt::MoveAction); + event->accept(); + } else + event->ignore(); +} + +void PiecesList::dropEvent(QDropEvent *event) +{ + if (event->mimeData()->hasFormat("image/x-puzzle-piece")) { + QByteArray pieceData = event->mimeData()->data("image/x-puzzle-piece"); + QDataStream dataStream(&pieceData, QIODevice::ReadOnly); + QPixmap pixmap; + QPoint location; + dataStream >> pixmap >> location; + + addPiece(pixmap, location); + + event->setDropAction(Qt::MoveAction); + event->accept(); + } else + event->ignore(); +} + +void PiecesList::addPiece(QPixmap pixmap, QPoint location) +{ + QListWidgetItem *pieceItem = new QListWidgetItem(this); + pieceItem->setIcon(QIcon(pixmap)); + pieceItem->setData(Qt::UserRole, QVariant(pixmap)); + pieceItem->setData(Qt::UserRole+1, location); + pieceItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable + | Qt::ItemIsDragEnabled); +} + +void PiecesList::startDrag(Qt::DropActions /*supportedActions*/) +{ + QListWidgetItem *item = currentItem(); + + QByteArray itemData; + QDataStream dataStream(&itemData, QIODevice::WriteOnly); + QPixmap pixmap = qVariantValue(item->data(Qt::UserRole)); + QPoint location = item->data(Qt::UserRole+1).toPoint(); + + dataStream << pixmap << location; + + QMimeData *mimeData = new QMimeData; + mimeData->setData("image/x-puzzle-piece", itemData); + + QDrag *drag = new QDrag(this); + drag->setMimeData(mimeData); + drag->setHotSpot(QPoint(pixmap.width()/2, pixmap.height()/2)); + drag->setPixmap(pixmap); + + if (drag->exec(Qt::MoveAction) == Qt::MoveAction) + delete takeItem(row(item)); +} diff --git a/examples/draganddrop/puzzle/pieceslist.h b/examples/draganddrop/puzzle/pieceslist.h new file mode 100644 index 0000000000..26685e3311 --- /dev/null +++ b/examples/draganddrop/puzzle/pieceslist.h @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 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 the GNU General Public License version 3.0 requirements will be +** met: 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. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef PIECESLIST_H +#define PIECESLIST_H + +#include + +class PiecesList : public QListWidget +{ + Q_OBJECT + +public: + PiecesList(QWidget *parent = 0); + void addPiece(QPixmap pixmap, QPoint location); + +protected: + void dragEnterEvent(QDragEnterEvent *event); + void dragMoveEvent(QDragMoveEvent *event); + void dropEvent(QDropEvent *event); + void startDrag(Qt::DropActions supportedActions); +}; + +#endif diff --git a/examples/draganddrop/puzzle/puzzle.pro b/examples/draganddrop/puzzle/puzzle.pro new file mode 100644 index 0000000000..26d2350e3d --- /dev/null +++ b/examples/draganddrop/puzzle/puzzle.pro @@ -0,0 +1,20 @@ +HEADERS = mainwindow.h \ + pieceslist.h \ + puzzlewidget.h +RESOURCES = puzzle.qrc +SOURCES = main.cpp \ + mainwindow.cpp \ + pieceslist.cpp \ + puzzlewidget.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/draganddrop/puzzle +sources.files = $$SOURCES $$HEADERS $$RESOURCES *.pro *.jpg +sources.path = $$[QT_INSTALL_EXAMPLES]/draganddrop/puzzle +INSTALLS += target sources + +wince*: { + addFile.sources = example.jpg + addFile.path = . + DEPLOYMENT += addFile +} diff --git a/examples/draganddrop/puzzle/puzzle.qrc b/examples/draganddrop/puzzle/puzzle.qrc new file mode 100644 index 0000000000..4076cec026 --- /dev/null +++ b/examples/draganddrop/puzzle/puzzle.qrc @@ -0,0 +1,5 @@ + + + example.jpg + + diff --git a/examples/draganddrop/puzzle/puzzlewidget.cpp b/examples/draganddrop/puzzle/puzzlewidget.cpp new file mode 100644 index 0000000000..8548db521c --- /dev/null +++ b/examples/draganddrop/puzzle/puzzlewidget.cpp @@ -0,0 +1,205 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 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 the GNU General Public License version 3.0 requirements will be +** met: 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. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "puzzlewidget.h" + +PuzzleWidget::PuzzleWidget(QWidget *parent) + : QWidget(parent) +{ + setAcceptDrops(true); + setMinimumSize(400, 400); + setMaximumSize(400, 400); +} + +void PuzzleWidget::clear() +{ + pieceLocations.clear(); + piecePixmaps.clear(); + pieceRects.clear(); + highlightedRect = QRect(); + inPlace = 0; + update(); +} + +void PuzzleWidget::dragEnterEvent(QDragEnterEvent *event) +{ + if (event->mimeData()->hasFormat("image/x-puzzle-piece")) + event->accept(); + else + event->ignore(); +} + +void PuzzleWidget::dragLeaveEvent(QDragLeaveEvent *event) +{ + QRect updateRect = highlightedRect; + highlightedRect = QRect(); + update(updateRect); + event->accept(); +} + +void PuzzleWidget::dragMoveEvent(QDragMoveEvent *event) +{ + QRect updateRect = highlightedRect.unite(targetSquare(event->pos())); + + if (event->mimeData()->hasFormat("image/x-puzzle-piece") + && findPiece(targetSquare(event->pos())) == -1) { + + highlightedRect = targetSquare(event->pos()); + event->setDropAction(Qt::MoveAction); + event->accept(); + } else { + highlightedRect = QRect(); + event->ignore(); + } + + update(updateRect); +} + +void PuzzleWidget::dropEvent(QDropEvent *event) +{ + if (event->mimeData()->hasFormat("image/x-puzzle-piece") + && findPiece(targetSquare(event->pos())) == -1) { + + QByteArray pieceData = event->mimeData()->data("image/x-puzzle-piece"); + QDataStream dataStream(&pieceData, QIODevice::ReadOnly); + QRect square = targetSquare(event->pos()); + QPixmap pixmap; + QPoint location; + dataStream >> pixmap >> location; + + pieceLocations.append(location); + piecePixmaps.append(pixmap); + pieceRects.append(square); + + highlightedRect = QRect(); + update(square); + + event->setDropAction(Qt::MoveAction); + event->accept(); + + if (location == QPoint(square.x()/80, square.y()/80)) { + inPlace++; + if (inPlace == 25) + emit puzzleCompleted(); + } + } else { + highlightedRect = QRect(); + event->ignore(); + } +} + +int PuzzleWidget::findPiece(const QRect &pieceRect) const +{ + for (int i = 0; i < pieceRects.size(); ++i) { + if (pieceRect == pieceRects[i]) { + return i; + } + } + return -1; +} + +void PuzzleWidget::mousePressEvent(QMouseEvent *event) +{ + QRect square = targetSquare(event->pos()); + int found = findPiece(square); + + if (found == -1) + return; + + QPoint location = pieceLocations[found]; + QPixmap pixmap = piecePixmaps[found]; + pieceLocations.removeAt(found); + piecePixmaps.removeAt(found); + pieceRects.removeAt(found); + + if (location == QPoint(square.x()/80, square.y()/80)) + inPlace--; + + update(square); + + QByteArray itemData; + QDataStream dataStream(&itemData, QIODevice::WriteOnly); + + dataStream << pixmap << location; + + QMimeData *mimeData = new QMimeData; + mimeData->setData("image/x-puzzle-piece", itemData); + + QDrag *drag = new QDrag(this); + drag->setMimeData(mimeData); + drag->setHotSpot(event->pos() - square.topLeft()); + drag->setPixmap(pixmap); + + if (!(drag->exec(Qt::MoveAction) == Qt::MoveAction)) { + pieceLocations.insert(found, location); + piecePixmaps.insert(found, pixmap); + pieceRects.insert(found, square); + update(targetSquare(event->pos())); + + if (location == QPoint(square.x()/80, square.y()/80)) + inPlace++; + } +} + +void PuzzleWidget::paintEvent(QPaintEvent *event) +{ + QPainter painter; + painter.begin(this); + painter.fillRect(event->rect(), Qt::white); + + if (highlightedRect.isValid()) { + painter.setBrush(QColor("#ffcccc")); + painter.setPen(Qt::NoPen); + painter.drawRect(highlightedRect.adjusted(0, 0, -1, -1)); + } + + for (int i = 0; i < pieceRects.size(); ++i) { + painter.drawPixmap(pieceRects[i], piecePixmaps[i]); + } + painter.end(); +} + +const QRect PuzzleWidget::targetSquare(const QPoint &position) const +{ + return QRect(position.x()/80 * 80, position.y()/80 * 80, 80, 80); +} diff --git a/examples/draganddrop/puzzle/puzzlewidget.h b/examples/draganddrop/puzzle/puzzlewidget.h new file mode 100644 index 0000000000..312e25fc77 --- /dev/null +++ b/examples/draganddrop/puzzle/puzzlewidget.h @@ -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 examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 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 the GNU General Public License version 3.0 requirements will be +** met: 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. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef PUZZLEWIDGET_H +#define PUZZLEWIDGET_H + +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE +class QDragEnterEvent; +class QDropEvent; +class QMouseEvent; +QT_END_NAMESPACE + +class PuzzleWidget : public QWidget +{ + Q_OBJECT + +public: + PuzzleWidget(QWidget *parent = 0); + void clear(); + +signals: + void puzzleCompleted(); + +protected: + void dragEnterEvent(QDragEnterEvent *event); + void dragLeaveEvent(QDragLeaveEvent *event); + void dragMoveEvent(QDragMoveEvent *event); + void dropEvent(QDropEvent *event); + void mousePressEvent(QMouseEvent *event); + void paintEvent(QPaintEvent *event); + +private: + int findPiece(const QRect &pieceRect) const; + const QRect targetSquare(const QPoint &position) const; + + QList piecePixmaps; + QList pieceRects; + QList pieceLocations; + QRect highlightedRect; + int inPlace; +}; + +#endif -- cgit v1.2.3