diff options
author | axis <qt-info@nokia.com> | 2009-04-24 11:34:15 (GMT) |
---|---|---|
committer | axis <qt-info@nokia.com> | 2009-04-24 11:34:15 (GMT) |
commit | 8f427b2b914d5b575a4a7c0ed65d2fb8f45acc76 (patch) | |
tree | a17e1a767a89542ab59907462206d7dcf2e504b2 /examples/draganddrop | |
download | Qt-8f427b2b914d5b575a4a7c0ed65d2fb8f45acc76.zip Qt-8f427b2b914d5b575a4a7c0ed65d2fb8f45acc76.tar.gz Qt-8f427b2b914d5b575a4a7c0ed65d2fb8f45acc76.tar.bz2 |
Long live Qt for S60!
Diffstat (limited to 'examples/draganddrop')
51 files changed, 3120 insertions, 0 deletions
diff --git a/examples/draganddrop/README b/examples/draganddrop/README new file mode 100644 index 0000000..f4efd2e --- /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 0000000..c7b95b6 --- /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 0000000..374f211 --- /dev/null +++ b/examples/draganddrop/delayedencoding/delayedencoding.qrc @@ -0,0 +1,6 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource prefix=""> + <file>images/drag.png</file> + <file>images/example.svg</file> +</qresource> +</RCC> diff --git a/examples/draganddrop/delayedencoding/images/drag.png b/examples/draganddrop/delayedencoding/images/drag.png Binary files differnew file mode 100644 index 0000000..dd795cf --- /dev/null +++ b/examples/draganddrop/delayedencoding/images/drag.png diff --git a/examples/draganddrop/delayedencoding/images/example.svg b/examples/draganddrop/delayedencoding/images/example.svg new file mode 100644 index 0000000..4707cf8 --- /dev/null +++ b/examples/draganddrop/delayedencoding/images/example.svg @@ -0,0 +1,59 @@ +<?xml version="1.0" standalone="no"?> +<!-- *********************************************************************** +** +** 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$ +** +************************************************************************ --> +<svg width="8cm" height="8cm" viewBox="0 0 400 400" + xmlns="http://www.w3.org/2000/svg" baseProfile="tiny" version="1.1"> + <title>Sphere</title> + <desc>A gradient filled sphere.</desc> + <g> + <defs> + <radialGradient id="blueSphere" gradientUnits="userSpaceOnUse" + cx="200" cy="200" r="100" fx="150" fy="150"> + <stop offset="0%" stop-color="white" /> + <stop offset="75%" stop-color="blue" /> + <stop offset="100%" stop-color="black" /> + </radialGradient> + </defs> + <rect fill="lightblue" x="0" y="0" width="400" height="400" /> + <circle fill="url(#blueSphere)" stroke="black" stroke-width="2" + cx="200" cy="200" r="100"/> + </g> +</svg> diff --git a/examples/draganddrop/delayedencoding/main.cpp b/examples/draganddrop/delayedencoding/main.cpp new file mode 100644 index 0000000..801e474 --- /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 <QApplication> +#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 0000000..5c7c23c --- /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 <QtGui> +#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 0000000..13e834d --- /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 <QMimeData> + +//![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 0000000..f5aed89 --- /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 <QtGui> +#include <QtSvg> +#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 0000000..8d6a690 --- /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 <QByteArray> +#include <QWidget> + +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 0000000..fa857db --- /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 +symbian: 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 0000000..5212abc --- /dev/null +++ b/examples/draganddrop/draggableicons/draggableicons.pro @@ -0,0 +1,12 @@ +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 + +include($$QT_SOURCE_TREE/examples/examplebase.pri) diff --git a/examples/draganddrop/draggableicons/draggableicons.qrc b/examples/draganddrop/draggableicons/draggableicons.qrc new file mode 100644 index 0000000..63f84ac --- /dev/null +++ b/examples/draganddrop/draggableicons/draggableicons.qrc @@ -0,0 +1,7 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource prefix=""> + <file>images/boat.png</file> + <file>images/car.png</file> + <file>images/house.png</file> +</qresource> +</RCC> diff --git a/examples/draganddrop/draggableicons/dragwidget.cpp b/examples/draganddrop/draggableicons/dragwidget.cpp new file mode 100644 index 0000000..021f816 --- /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 <QtGui> + +#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<QLabel*>(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 0000000..201adf8 --- /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 <QFrame> + +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 Binary files differnew file mode 100644 index 0000000..46c894f --- /dev/null +++ b/examples/draganddrop/draggableicons/images/boat.png diff --git a/examples/draganddrop/draggableicons/images/car.png b/examples/draganddrop/draggableicons/images/car.png Binary files differnew file mode 100644 index 0000000..3cb35e5 --- /dev/null +++ b/examples/draganddrop/draggableicons/images/car.png diff --git a/examples/draganddrop/draggableicons/images/house.png b/examples/draganddrop/draggableicons/images/house.png Binary files differnew file mode 100644 index 0000000..ee9d5b1 --- /dev/null +++ b/examples/draganddrop/draggableicons/images/house.png diff --git a/examples/draganddrop/draggableicons/main.cpp b/examples/draganddrop/draggableicons/main.cpp new file mode 100644 index 0000000..cd94537 --- /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 <QApplication> +#include <QHBoxLayout> +#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 0000000..109be5d --- /dev/null +++ b/examples/draganddrop/draggabletext/draggabletext.pro @@ -0,0 +1,14 @@ +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 + +include($$QT_SOURCE_TREE/examples/examplebase.pri) diff --git a/examples/draganddrop/draggabletext/draggabletext.qrc b/examples/draganddrop/draggabletext/draggabletext.qrc new file mode 100644 index 0000000..b72217d --- /dev/null +++ b/examples/draganddrop/draggabletext/draggabletext.qrc @@ -0,0 +1,5 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource prefix="/dictionary"> + <file>words.txt</file> +</qresource> +</RCC> diff --git a/examples/draganddrop/draggabletext/draglabel.cpp b/examples/draganddrop/draggabletext/draglabel.cpp new file mode 100644 index 0000000..4dfa099 --- /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 <QtGui> + +#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 0000000..8ba2778 --- /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 <QLabel> + +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 0000000..2af7b29 --- /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 <QtGui> + +#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<QByteArray> 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<QWidget *>(child); + if (!widget->isVisible()) + widget->deleteLater(); + } + } +} + +void DragWidget::mousePressEvent(QMouseEvent *event) +{ + QLabel *child = static_cast<QLabel*>(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 0000000..b51f05b --- /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 <QWidget> + +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 0000000..e66c8b7 --- /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 <QApplication> +#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 0000000..19b8b03 --- /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 0000000..495e2bf --- /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 <QtGui> + +#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("<drop content>")); + 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<QPixmap>(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<QUrl> 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("<drop content>")); + 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 0000000..8a40acc --- /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 <QLabel> + +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 0000000..268e247 --- /dev/null +++ b/examples/draganddrop/dropsite/dropsite.pro @@ -0,0 +1,14 @@ +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 + +include($$QT_SOURCE_TREE/examples/examplebase.pri) + diff --git a/examples/draganddrop/dropsite/dropsitewindow.cpp b/examples/draganddrop/dropsite/dropsitewindow.cpp new file mode 100644 index 0000000..6e7055b --- /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 <QtGui> + +#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<QUrl> 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 0000000..bbdc687 --- /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 <QWidget> + +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 0000000..62589ad --- /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 <QApplication> + +#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 0000000..1bec024 --- /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 <QtGui> + +#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 0000000..5d2fd52 --- /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 <QLabel> + +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 0000000..2f96a58 --- /dev/null +++ b/examples/draganddrop/fridgemagnets/dragwidget.cpp @@ -0,0 +1,215 @@ +/**************************************************************************** +** +** 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 <QtGui> + +#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] + #ifndef Q_WS_S60 + //Fridge magnets is used for demoing Qt on S60 and themed backgrounds look better than white + QPalette newPalette = palette(); + newPalette.setColor(QPalette::Window, Qt::white); + setPalette(newPalette); + #endif + + 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<DragLabel*>(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 0000000..4722917 --- /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 <QWidget> + +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 0000000..b452746 --- /dev/null +++ b/examples/draganddrop/fridgemagnets/fridgemagnets.pro @@ -0,0 +1,15 @@ +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 +TARGET.UID3 = 0xA000C610 + +include($$QT_SOURCE_TREE/examples/examplebase.pri) diff --git a/examples/draganddrop/fridgemagnets/fridgemagnets.qrc b/examples/draganddrop/fridgemagnets/fridgemagnets.qrc new file mode 100644 index 0000000..b72217d --- /dev/null +++ b/examples/draganddrop/fridgemagnets/fridgemagnets.qrc @@ -0,0 +1,5 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource prefix="/dictionary"> + <file>words.txt</file> +</qresource> +</RCC> diff --git a/examples/draganddrop/fridgemagnets/main.cpp b/examples/draganddrop/fridgemagnets/main.cpp new file mode 100644 index 0000000..23dfe79 --- /dev/null +++ b/examples/draganddrop/fridgemagnets/main.cpp @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** 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 <QApplication> +#include "dragwidget.h" + +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(fridgemagnets); + bool smallScreen = false; + for (int i=0; i<argc; i++) + if (QString(argv[i]) == "-small-screen") + smallScreen = true; + + QApplication app(argc, argv); + DragWidget window; + if (smallScreen) + window.showFullScreen(); + else + window.show(); + return app.exec(); +} diff --git a/examples/draganddrop/fridgemagnets/words.txt b/examples/draganddrop/fridgemagnets/words.txt new file mode 100644 index 0000000..a7e1632 --- /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 Binary files differnew file mode 100644 index 0000000..e09fb70 --- /dev/null +++ b/examples/draganddrop/puzzle/example.jpg diff --git a/examples/draganddrop/puzzle/main.cpp b/examples/draganddrop/puzzle/main.cpp new file mode 100644 index 0000000..e0e5cc1 --- /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 <QApplication> + +#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 0000000..f998882 --- /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 <QtGui> +#include <stdlib.h> + +#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 0000000..edfbb12 --- /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 <QPixmap> +#include <QMainWindow> + +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 0000000..77d06fb --- /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 <QtGui> + +#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<QPixmap>(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 0000000..26685e3 --- /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 <QListWidget> + +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 0000000..e51eb78 --- /dev/null +++ b/examples/draganddrop/puzzle/puzzle.pro @@ -0,0 +1,27 @@ +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 + +include($$QT_SOURCE_TREE/examples/examplebase.pri) + +symbian:{ + addFile.sources = example.jpg + addFile.path = . + DEPLOYMENT += addFile +} +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 0000000..4076cec --- /dev/null +++ b/examples/draganddrop/puzzle/puzzle.qrc @@ -0,0 +1,5 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource prefix="/images"> + <file>example.jpg</file> +</qresource> +</RCC> diff --git a/examples/draganddrop/puzzle/puzzlewidget.cpp b/examples/draganddrop/puzzle/puzzlewidget.cpp new file mode 100644 index 0000000..8548db5 --- /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 <QtGui> + +#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 0000000..312e25f --- /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 <QList> +#include <QPoint> +#include <QPixmap> +#include <QWidget> + +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<QPixmap> piecePixmaps; + QList<QRect> pieceRects; + QList<QPoint> pieceLocations; + QRect highlightedRect; + int inPlace; +}; + +#endif |