From 806dda08d685bc5f9ed71dfe8b61f21848d48066 Mon Sep 17 00:00:00 2001 From: Gabriel de Dietrich Date: Fri, 17 Aug 2012 13:23:19 +0200 Subject: Moving .qdoc files under examples/widgets/doc Updated those .qdoc files to refer to the new relative examples emplacement. Images and snippets to be moved later. Also grouped all widgets related examples under widgets. Change-Id: Ib29696e2d8948524537f53e8dda88f9ee26a597f Reviewed-by: J-P Nurmi --- examples/widgets/draganddrop/puzzle/example.jpg | Bin 0 -> 42654 bytes examples/widgets/draganddrop/puzzle/main.cpp | 54 ++++++ examples/widgets/draganddrop/puzzle/mainwindow.cpp | 152 +++++++++++++++ examples/widgets/draganddrop/puzzle/mainwindow.h | 76 ++++++++ examples/widgets/draganddrop/puzzle/pieceslist.cpp | 121 ++++++++++++ examples/widgets/draganddrop/puzzle/pieceslist.h | 63 ++++++ examples/widgets/draganddrop/puzzle/puzzle.desktop | 11 ++ examples/widgets/draganddrop/puzzle/puzzle.pro | 23 +++ examples/widgets/draganddrop/puzzle/puzzle.qrc | 5 + .../widgets/draganddrop/puzzle/puzzlewidget.cpp | 214 +++++++++++++++++++++ examples/widgets/draganddrop/puzzle/puzzlewidget.h | 89 +++++++++ 11 files changed, 808 insertions(+) create mode 100644 examples/widgets/draganddrop/puzzle/example.jpg create mode 100644 examples/widgets/draganddrop/puzzle/main.cpp create mode 100644 examples/widgets/draganddrop/puzzle/mainwindow.cpp create mode 100644 examples/widgets/draganddrop/puzzle/mainwindow.h create mode 100644 examples/widgets/draganddrop/puzzle/pieceslist.cpp create mode 100644 examples/widgets/draganddrop/puzzle/pieceslist.h create mode 100644 examples/widgets/draganddrop/puzzle/puzzle.desktop create mode 100644 examples/widgets/draganddrop/puzzle/puzzle.pro create mode 100644 examples/widgets/draganddrop/puzzle/puzzle.qrc create mode 100644 examples/widgets/draganddrop/puzzle/puzzlewidget.cpp create mode 100644 examples/widgets/draganddrop/puzzle/puzzlewidget.h (limited to 'examples/widgets/draganddrop/puzzle') diff --git a/examples/widgets/draganddrop/puzzle/example.jpg b/examples/widgets/draganddrop/puzzle/example.jpg new file mode 100644 index 0000000000..e09fb70757 Binary files /dev/null and b/examples/widgets/draganddrop/puzzle/example.jpg differ diff --git a/examples/widgets/draganddrop/puzzle/main.cpp b/examples/widgets/draganddrop/puzzle/main.cpp new file mode 100644 index 0000000000..31b7142819 --- /dev/null +++ b/examples/widgets/draganddrop/puzzle/main.cpp @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(puzzle); + + QApplication app(argc, argv); + MainWindow window; + window.openImage(":/images/example.jpg"); + window.show(); + return app.exec(); +} diff --git a/examples/widgets/draganddrop/puzzle/mainwindow.cpp b/examples/widgets/draganddrop/puzzle/mainwindow.cpp new file mode 100644 index 0000000000..0540aaef9f --- /dev/null +++ b/examples/widgets/draganddrop/puzzle/mainwindow.cpp @@ -0,0 +1,152 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include + +#include "mainwindow.h" +#include "pieceslist.h" +#include "puzzlewidget.h" + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) +{ + setupMenus(); + setupWidgets(); + + setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed)); + setWindowTitle(tr("Puzzle")); +} + +void MainWindow::openImage(const QString &path) +{ + QString fileName = path; + + if (fileName.isNull()) + fileName = QFileDialog::getOpenFileName(this, + tr("Open Image"), "", "Image Files (*.png *.jpg *.bmp)"); + + if (!fileName.isEmpty()) { + QPixmap newImage; + if (!newImage.load(fileName)) { + QMessageBox::warning(this, tr("Open Image"), + tr("The image file could not be loaded."), + QMessageBox::Cancel); + return; + } + puzzleImage = newImage; + setupPuzzle(); + } +} + +void MainWindow::setCompleted() +{ + QMessageBox::information(this, tr("Puzzle Completed"), + tr("Congratulations! You have completed the puzzle!\n" + "Click OK to start again."), + QMessageBox::Ok); + + setupPuzzle(); +} + +void MainWindow::setupPuzzle() +{ + int size = qMin(puzzleImage.width(), puzzleImage.height()); + puzzleImage = puzzleImage.copy((puzzleImage.width() - size)/2, + (puzzleImage.height() - size)/2, size, size).scaled(puzzleWidget->width(), + puzzleWidget->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); + + piecesList->clear(); + + for (int y = 0; y < 5; ++y) { + for (int x = 0; x < 5; ++x) { + int pieceSize = puzzleWidget->pieceSize(); + QPixmap pieceImage = puzzleImage.copy(x * pieceSize, y * pieceSize, pieceSize, pieceSize); + 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->setShortcuts(QKeySequence::Open); + + QAction *exitAction = fileMenu->addAction(tr("E&xit")); + exitAction->setShortcuts(QKeySequence::Quit); + + 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); + puzzleWidget = new PuzzleWidget(400); + + piecesList = new PiecesList(puzzleWidget->pieceSize(), this); + + + connect(puzzleWidget, SIGNAL(puzzleCompleted()), + this, SLOT(setCompleted()), Qt::QueuedConnection); + + frameLayout->addWidget(piecesList); + frameLayout->addWidget(puzzleWidget); + setCentralWidget(frame); +} diff --git a/examples/widgets/draganddrop/puzzle/mainwindow.h b/examples/widgets/draganddrop/puzzle/mainwindow.h new file mode 100644 index 0000000000..c7ce2c8ef3 --- /dev/null +++ b/examples/widgets/draganddrop/puzzle/mainwindow.h @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include + +class PiecesList; +class PuzzleWidget; +QT_BEGIN_NAMESPACE +class QListWidgetItem; +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(QWidget *parent = 0); + +public slots: + void openImage(const QString &path = QString()); + void setupPuzzle(); + +private slots: + void setCompleted(); + +private: + void setupMenus(); + void setupWidgets(); + + QPixmap puzzleImage; + PiecesList *piecesList; + PuzzleWidget *puzzleWidget; +}; + +#endif diff --git a/examples/widgets/draganddrop/puzzle/pieceslist.cpp b/examples/widgets/draganddrop/puzzle/pieceslist.cpp new file mode 100644 index 0000000000..8e0892aae6 --- /dev/null +++ b/examples/widgets/draganddrop/puzzle/pieceslist.cpp @@ -0,0 +1,121 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "pieceslist.h" + +PiecesList::PiecesList(int pieceSize, QWidget *parent) + : QListWidget(parent), m_PieceSize(pieceSize) +{ + setDragEnabled(true); + setViewMode(QListView::IconMode); + setIconSize(QSize(m_PieceSize, m_PieceSize)); + 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 = qvariant_cast(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/widgets/draganddrop/puzzle/pieceslist.h b/examples/widgets/draganddrop/puzzle/pieceslist.h new file mode 100644 index 0000000000..3c6b042b4e --- /dev/null +++ b/examples/widgets/draganddrop/puzzle/pieceslist.h @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef PIECESLIST_H +#define PIECESLIST_H + +#include + +class PiecesList : public QListWidget +{ + Q_OBJECT + +public: + PiecesList(int pieceSize, 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); + + int m_PieceSize; +}; + +#endif diff --git a/examples/widgets/draganddrop/puzzle/puzzle.desktop b/examples/widgets/draganddrop/puzzle/puzzle.desktop new file mode 100644 index 0000000000..f6765e1e01 --- /dev/null +++ b/examples/widgets/draganddrop/puzzle/puzzle.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=Drag and Drop Puzzle +Exec=/opt/usr/bin/puzzle +Icon=puzzle +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/draganddrop/puzzle/puzzle.pro b/examples/widgets/draganddrop/puzzle/puzzle.pro new file mode 100644 index 0000000000..7c69a41a39 --- /dev/null +++ b/examples/widgets/draganddrop/puzzle/puzzle.pro @@ -0,0 +1,23 @@ +HEADERS = mainwindow.h \ + pieceslist.h \ + puzzlewidget.h +RESOURCES = puzzle.qrc +SOURCES = main.cpp \ + mainwindow.cpp \ + pieceslist.cpp \ + puzzlewidget.cpp + +QMAKE_PROJECT_NAME = dndpuzzle + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/draganddrop/puzzle +sources.files = $$SOURCES $$HEADERS $$RESOURCES *.pro *.jpg +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/draganddrop/puzzle +INSTALLS += target sources + +wince*: { + addFile.files = example.jpg + addFile.path = . + DEPLOYMENT += addFile +} +QT += widgets diff --git a/examples/widgets/draganddrop/puzzle/puzzle.qrc b/examples/widgets/draganddrop/puzzle/puzzle.qrc new file mode 100644 index 0000000000..4076cec026 --- /dev/null +++ b/examples/widgets/draganddrop/puzzle/puzzle.qrc @@ -0,0 +1,5 @@ + + + example.jpg + + diff --git a/examples/widgets/draganddrop/puzzle/puzzlewidget.cpp b/examples/widgets/draganddrop/puzzle/puzzlewidget.cpp new file mode 100644 index 0000000000..2e39f84e9e --- /dev/null +++ b/examples/widgets/draganddrop/puzzle/puzzlewidget.cpp @@ -0,0 +1,214 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "puzzlewidget.h" + +PuzzleWidget::PuzzleWidget(int imageSize, QWidget *parent) + : QWidget(parent), m_ImageSize(imageSize) +{ + setAcceptDrops(true); + setMinimumSize(m_ImageSize, m_ImageSize); + setMaximumSize(m_ImageSize, m_ImageSize); +} + +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.united(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()/pieceSize(), square.y()/pieceSize())) { + 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()/pieceSize(), square.y()/pieceSize())) + 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()/pieceSize(), square.y()/pieceSize())) + 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()/pieceSize() * pieceSize(), position.y()/pieceSize() * pieceSize(), pieceSize(), pieceSize()); +} + +int PuzzleWidget::pieceSize() const +{ + return m_ImageSize / 5; +} + +int PuzzleWidget::imageSize() const +{ + return m_ImageSize; +} diff --git a/examples/widgets/draganddrop/puzzle/puzzlewidget.h b/examples/widgets/draganddrop/puzzle/puzzlewidget.h new file mode 100644 index 0000000000..1c0fc77a5f --- /dev/null +++ b/examples/widgets/draganddrop/puzzle/puzzlewidget.h @@ -0,0 +1,89 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef PUZZLEWIDGET_H +#define PUZZLEWIDGET_H + +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE +class QDragEnterEvent; +class QDropEvent; +class QMouseEvent; +QT_END_NAMESPACE + +class PuzzleWidget : public QWidget +{ + Q_OBJECT + +public: + PuzzleWidget(int imageSize, QWidget *parent = 0); + void clear(); + + int pieceSize() const; + int imageSize() const; + +signals: + void puzzleCompleted(); + +protected: + void dragEnterEvent(QDragEnterEvent *event); + void dragLeaveEvent(QDragLeaveEvent *event); + void dragMoveEvent(QDragMoveEvent *event); + void dropEvent(QDropEvent *event); + void mousePressEvent(QMouseEvent *event); + void paintEvent(QPaintEvent *event); + +private: + int findPiece(const QRect &pieceRect) const; + const QRect targetSquare(const QPoint &position) const; + + QList piecePixmaps; + QList pieceRects; + QList pieceLocations; + QRect highlightedRect; + int inPlace; + int m_ImageSize; +}; + +#endif -- cgit v1.2.3