examples/draganddrop/puzzle/puzzlewidget.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the examples of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QtGui>
       
    43 
       
    44 #include "puzzlewidget.h"
       
    45 
       
    46 PuzzleWidget::PuzzleWidget(QWidget *parent)
       
    47     : QWidget(parent)
       
    48 {
       
    49     setAcceptDrops(true);
       
    50     setMinimumSize(400, 400);
       
    51     setMaximumSize(400, 400);
       
    52 }
       
    53 
       
    54 void PuzzleWidget::clear()
       
    55 {
       
    56     pieceLocations.clear();
       
    57     piecePixmaps.clear();
       
    58     pieceRects.clear();
       
    59     highlightedRect = QRect();
       
    60     inPlace = 0;
       
    61     update();
       
    62 }
       
    63 
       
    64 void PuzzleWidget::dragEnterEvent(QDragEnterEvent *event)
       
    65 {
       
    66     if (event->mimeData()->hasFormat("image/x-puzzle-piece"))
       
    67         event->accept();
       
    68     else
       
    69         event->ignore();
       
    70 }
       
    71 
       
    72 void PuzzleWidget::dragLeaveEvent(QDragLeaveEvent *event)
       
    73 {
       
    74     QRect updateRect = highlightedRect;
       
    75     highlightedRect = QRect();
       
    76     update(updateRect);
       
    77     event->accept();
       
    78 }
       
    79 
       
    80 void PuzzleWidget::dragMoveEvent(QDragMoveEvent *event)
       
    81 {
       
    82     QRect updateRect = highlightedRect.unite(targetSquare(event->pos()));
       
    83 
       
    84     if (event->mimeData()->hasFormat("image/x-puzzle-piece")
       
    85         && findPiece(targetSquare(event->pos())) == -1) {
       
    86 
       
    87         highlightedRect = targetSquare(event->pos());
       
    88         event->setDropAction(Qt::MoveAction);
       
    89         event->accept();
       
    90     } else {
       
    91         highlightedRect = QRect();
       
    92         event->ignore();
       
    93     }
       
    94 
       
    95     update(updateRect);
       
    96 }
       
    97 
       
    98 void PuzzleWidget::dropEvent(QDropEvent *event)
       
    99 {
       
   100     if (event->mimeData()->hasFormat("image/x-puzzle-piece")
       
   101         && findPiece(targetSquare(event->pos())) == -1) {
       
   102 
       
   103         QByteArray pieceData = event->mimeData()->data("image/x-puzzle-piece");
       
   104         QDataStream dataStream(&pieceData, QIODevice::ReadOnly);
       
   105         QRect square = targetSquare(event->pos());
       
   106         QPixmap pixmap;
       
   107         QPoint location;
       
   108         dataStream >> pixmap >> location;
       
   109 
       
   110         pieceLocations.append(location);
       
   111         piecePixmaps.append(pixmap);
       
   112         pieceRects.append(square);
       
   113 
       
   114         highlightedRect = QRect();
       
   115         update(square);
       
   116 
       
   117         event->setDropAction(Qt::MoveAction);
       
   118         event->accept();
       
   119 
       
   120         if (location == QPoint(square.x()/80, square.y()/80)) {
       
   121             inPlace++;
       
   122             if (inPlace == 25)
       
   123                 emit puzzleCompleted();
       
   124         }
       
   125     } else {
       
   126         highlightedRect = QRect();
       
   127         event->ignore();
       
   128     }
       
   129 }
       
   130 
       
   131 int PuzzleWidget::findPiece(const QRect &pieceRect) const
       
   132 {
       
   133     for (int i = 0; i < pieceRects.size(); ++i) {
       
   134         if (pieceRect == pieceRects[i]) {
       
   135             return i;
       
   136         }
       
   137     }
       
   138     return -1;
       
   139 }
       
   140 
       
   141 void PuzzleWidget::mousePressEvent(QMouseEvent *event)
       
   142 {
       
   143     QRect square = targetSquare(event->pos());
       
   144     int found = findPiece(square);
       
   145 
       
   146     if (found == -1)
       
   147         return;
       
   148 
       
   149     QPoint location = pieceLocations[found];
       
   150     QPixmap pixmap = piecePixmaps[found];
       
   151     pieceLocations.removeAt(found);
       
   152     piecePixmaps.removeAt(found);
       
   153     pieceRects.removeAt(found);
       
   154 
       
   155     if (location == QPoint(square.x()/80, square.y()/80))
       
   156         inPlace--;
       
   157 
       
   158     update(square);
       
   159 
       
   160     QByteArray itemData;
       
   161     QDataStream dataStream(&itemData, QIODevice::WriteOnly);
       
   162 
       
   163     dataStream << pixmap << location;
       
   164 
       
   165     QMimeData *mimeData = new QMimeData;
       
   166     mimeData->setData("image/x-puzzle-piece", itemData);
       
   167 
       
   168     QDrag *drag = new QDrag(this);
       
   169     drag->setMimeData(mimeData);
       
   170     drag->setHotSpot(event->pos() - square.topLeft());
       
   171     drag->setPixmap(pixmap);
       
   172 
       
   173     if (!(drag->exec(Qt::MoveAction) == Qt::MoveAction)) {
       
   174         pieceLocations.insert(found, location);
       
   175         piecePixmaps.insert(found, pixmap);
       
   176         pieceRects.insert(found, square);
       
   177         update(targetSquare(event->pos()));
       
   178 
       
   179         if (location == QPoint(square.x()/80, square.y()/80))
       
   180             inPlace++;
       
   181     }
       
   182 }
       
   183 
       
   184 void PuzzleWidget::paintEvent(QPaintEvent *event)
       
   185 {
       
   186     QPainter painter;
       
   187     painter.begin(this);
       
   188     painter.fillRect(event->rect(), Qt::white);
       
   189 
       
   190     if (highlightedRect.isValid()) {
       
   191         painter.setBrush(QColor("#ffcccc"));
       
   192         painter.setPen(Qt::NoPen);
       
   193         painter.drawRect(highlightedRect.adjusted(0, 0, -1, -1));
       
   194     }
       
   195 
       
   196     for (int i = 0; i < pieceRects.size(); ++i) {
       
   197         painter.drawPixmap(pieceRects[i], piecePixmaps[i]);
       
   198     }
       
   199     painter.end();
       
   200 }
       
   201 
       
   202 const QRect PuzzleWidget::targetSquare(const QPoint &position) const
       
   203 {
       
   204     return QRect(position.x()/80 * 80, position.y()/80 * 80, 80, 80);
       
   205 }