PuzzleWidget Class Reference

#include <puzzlewidget.h>

Inheritance diagram for PuzzleWidget:

Inheritance graph
[legend]
Collaboration diagram for PuzzleWidget:

Collaboration graph
[legend]
List of all members.

Detailed Description

Definition at line 36 of file puzzlewidget.h.

Signals

void puzzleCompleted ()
void puzzleCompleted ()

Public Member Functions

 PuzzleWidget (QWidget *parent=0)
void clear ()
 PuzzleWidget (QWidget *parent=0)
void clear ()

Protected Member Functions

void dragEnterEvent (QDragEnterEvent *event)
void dragLeaveEvent (QDragLeaveEvent *event)
void dragMoveEvent (QDragMoveEvent *event)
void dropEvent (QDropEvent *event)
void mousePressEvent (QMouseEvent *event)
void paintEvent (QPaintEvent *event)
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 Member Functions

int findPiece (const QRect &pieceRect) const
const QRect targetSquare (const QPoint &position) const
int findPiece (const QRect &pieceRect) const
const QRect targetSquare (const QPoint &position) const

Private Attributes

QList< QPixmappiecePixmaps
QList< QRectpieceRects
QList< QPointpieceLocations
QRect highlightedRect
int inPlace
QList< QPixmappiecePixmaps
QList< QRectpieceRects
QList< QPointpieceLocations


Constructor & Destructor Documentation

PuzzleWidget::PuzzleWidget ( QWidget parent = 0  ) 

Definition at line 28 of file puzzlewidget.cpp.

References QWidget::setAcceptDrops(), QWidget::setMaximumSize(), and QWidget::setMinimumSize().

00029     : QWidget(parent)
00030 {
00031     setAcceptDrops(true);
00032     setMinimumSize(400, 400);
00033     setMaximumSize(400, 400);
00034 }

Here is the call graph for this function:

PuzzleWidget::PuzzleWidget ( QWidget parent = 0  ) 


Member Function Documentation

void PuzzleWidget::clear (  ) 

Definition at line 36 of file puzzlewidget.cpp.

References QList< T >::clear(), highlightedRect, inPlace, pieceLocations, piecePixmaps, pieceRects, and QWidget::update().

Referenced by MainWindow::setupPuzzle().

00037 {
00038     pieceLocations.clear();
00039     piecePixmaps.clear();
00040     pieceRects.clear();
00041     highlightedRect = QRect();
00042     inPlace = 0;
00043     update();
00044 }

Here is the call graph for this function:

void PuzzleWidget::puzzleCompleted (  )  [signal]

Referenced by dropEvent().

void PuzzleWidget::dragEnterEvent ( QDragEnterEvent event  )  [protected, virtual]

This event handler is called when a drag is in progress and the mouse enters this widget. The event is passed in the event parameter.

See the Drag-and-drop documentation for an overview of how to provide drag-and-drop in your application.

See also:
QTextDrag, QImageDrag, QDragEnterEvent

Reimplemented from QWidget.

Definition at line 46 of file puzzlewidget.cpp.

References QWidget::event().

00047 {
00048     if (event->mimeData()->hasFormat("image/x-puzzle-piece"))
00049         event->accept();
00050     else
00051         event->ignore();
00052 }

Here is the call graph for this function:

void PuzzleWidget::dragLeaveEvent ( QDragLeaveEvent event  )  [protected, virtual]

This event handler is called when a drag is in progress and the mouse leaves this widget. The event is passed in the event parameter.

See the Drag-and-drop documentation for an overview of how to provide drag-and-drop in your application.

See also:
QTextDrag, QImageDrag, QDragLeaveEvent

Reimplemented from QWidget.

Definition at line 54 of file puzzlewidget.cpp.

References QWidget::event(), highlightedRect, and QWidget::update().

00055 {
00056     QRect updateRect = highlightedRect;
00057     highlightedRect = QRect();
00058     update(updateRect);
00059     event->accept();
00060 }

Here is the call graph for this function:

void PuzzleWidget::dragMoveEvent ( QDragMoveEvent event  )  [protected, virtual]

This event handler is called if a drag is in progress, and when any of the following conditions occurs: the cursor enters this widget, the cursor moves within this widget, or a modifier key is pressed on the keyboard while this widget has the focus. The event is passed in the event parameter.

See the Drag-and-drop documentation for an overview of how to provide drag-and-drop in your application.

See also:
QTextDrag, QImageDrag, QDragMoveEvent

Reimplemented from QWidget.

Definition at line 62 of file puzzlewidget.cpp.

References QWidget::event(), findPiece(), highlightedRect, Qt::MoveAction, targetSquare(), QRect::unite(), and QWidget::update().

00063 {
00064     QRect updateRect = highlightedRect.unite(targetSquare(event->pos()));
00065 
00066     if (event->mimeData()->hasFormat("image/x-puzzle-piece")
00067         && findPiece(targetSquare(event->pos())) == -1) {
00068 
00069         highlightedRect = targetSquare(event->pos());
00070         event->setDropAction(Qt::MoveAction);
00071         event->accept();
00072     } else {
00073         highlightedRect = QRect();
00074         event->ignore();
00075     }
00076 
00077     update(updateRect);
00078 }

Here is the call graph for this function:

void PuzzleWidget::dropEvent ( QDropEvent event  )  [protected, virtual]

This event handler is called when the drag is dropped on this widget which are passed in the event parameter.

See the Drag-and-drop documentation for an overview of how to provide drag-and-drop in your application.

See also:
QTextDrag, QImageDrag, QDropEvent

Reimplemented from QWidget.

Definition at line 80 of file puzzlewidget.cpp.

References QList< T >::append(), emit, QWidget::event(), findPiece(), highlightedRect, inPlace, location, Qt::MoveAction, pieceLocations, piecePixmaps, pieceRects, puzzleCompleted(), QIODevice::ReadOnly, targetSquare(), QWidget::update(), QRect::x(), and QRect::y().

00081 {
00082     if (event->mimeData()->hasFormat("image/x-puzzle-piece")
00083         && findPiece(targetSquare(event->pos())) == -1) {
00084 
00085         QByteArray pieceData = event->mimeData()->data("image/x-puzzle-piece");
00086         QDataStream dataStream(&pieceData, QIODevice::ReadOnly);
00087         QRect square = targetSquare(event->pos());
00088         QPixmap pixmap;
00089         QPoint location;
00090         dataStream >> pixmap >> location;
00091 
00092         pieceLocations.append(location);
00093         piecePixmaps.append(pixmap);
00094         pieceRects.append(square);
00095 
00096         highlightedRect = QRect();
00097         update(square);
00098 
00099         event->setDropAction(Qt::MoveAction);
00100         event->accept();
00101 
00102         if (location == QPoint(square.x()/80, square.y()/80)) {
00103             inPlace++;
00104             if (inPlace == 25)
00105                 emit puzzleCompleted();
00106         }
00107     } else {
00108         highlightedRect = QRect();
00109         event->ignore();
00110     }
00111 }

Here is the call graph for this function:

void PuzzleWidget::mousePressEvent ( QMouseEvent event  )  [protected, virtual]

This event handler, for event event, can be reimplemented in a subclass to receive mouse press events for the widget.

If you create new widgets in the mousePressEvent() the mouseReleaseEvent() may not end up where you expect, depending on the underlying window system (or X11 window manager), the widgets' location and maybe more.

The default implementation implements the closing of popup widgets when you click outside the window. For other widget types it does nothing.

See also:
mouseReleaseEvent(), mouseDoubleClickEvent(), mouseMoveEvent(), event(), QMouseEvent, {Scribble Example}

Reimplemented from QWidget.

Definition at line 123 of file puzzlewidget.cpp.

References QWidget::event(), findPiece(), inPlace, QList< T >::insert(), location, Qt::MoveAction, pieceLocations, piecePixmaps, pieceRects, QList< T >::removeAt(), QMimeData::setData(), QDrag::setHotSpot(), QDrag::setMimeData(), QDrag::setPixmap(), QDrag::start(), targetSquare(), QRect::topLeft(), QWidget::update(), QIODevice::WriteOnly, QRect::x(), and QRect::y().

00124 {
00125     QRect square = targetSquare(event->pos());
00126     int found = findPiece(square);
00127 
00128     if (found == -1)
00129         return;
00130 
00131     QPoint location = pieceLocations[found];
00132     QPixmap pixmap = piecePixmaps[found];
00133     pieceLocations.removeAt(found);
00134     piecePixmaps.removeAt(found);
00135     pieceRects.removeAt(found);
00136 
00137     if (location == QPoint(square.x()/80, square.y()/80))
00138         inPlace--;
00139 
00140     update(square);
00141 
00142     QByteArray itemData;
00143     QDataStream dataStream(&itemData, QIODevice::WriteOnly);
00144 
00145     dataStream << pixmap << location;
00146 
00147     QMimeData *mimeData = new QMimeData;
00148     mimeData->setData("image/x-puzzle-piece", itemData);
00149 
00150     QDrag *drag = new QDrag(this);
00151     drag->setMimeData(mimeData);
00152     drag->setHotSpot(event->pos() - square.topLeft());
00153     drag->setPixmap(pixmap);
00154 
00155     if (!(drag->start(Qt::MoveAction) == Qt::MoveAction)) {
00156         pieceLocations.insert(found, location);
00157         piecePixmaps.insert(found, pixmap);
00158         pieceRects.insert(found, square);
00159         update(targetSquare(event->pos()));
00160 
00161         if (location == QPoint(square.x()/80, square.y()/80))
00162             inPlace++;
00163     }
00164 }

Here is the call graph for this function:

void PuzzleWidget::paintEvent ( QPaintEvent event  )  [protected, virtual]

This event handler can be reimplemented in a subclass to receive paint events which are passed in the event parameter.

A paint event is a request to repaint all or part of the widget. It can happen as a result of repaint() or update(), or because the widget was obscured and has now been uncovered, or for many other reasons.

Many widgets can simply repaint their entire surface when asked to, but some slow widgets need to optimize by painting only the requested region: QPaintEvent::region(). This speed optimization does not change the result, as painting is clipped to that region during event processing. QListView and QTableView do this, for example.

Qt also tries to speed up painting by merging multiple paint events into one. When update() is called several times or the window system sends several paint events, Qt merges these events into one event with a larger region (see QRegion::united()). repaint() does not permit this optimization, so we suggest using update() whenever possible.

When the paint event occurs, the update region has normally been erased, so that you're painting on the widget's background.

The background can be set using setBackgroundRole() and setPalette().

From Qt 4.0, QWidget automatically double-buffers its painting, so there's no need to write double-buffering code in paintEvent() to avoid flicker.

Note: Under X11 it is possible to toggle the global double buffering by calling qt_x11_set_global_double_buffer(). Example usage:

See also:
event(), repaint(), update(), QPainter, QPixmap, QPaintEvent, {Analog Clock Example}

Reimplemented from QWidget.

Definition at line 166 of file puzzlewidget.cpp.

References QRect::adjusted(), QPainter::begin(), QPainter::drawPixmap(), QPainter::drawRect(), QPainter::end(), QWidget::event(), QPainter::fillRect(), highlightedRect, i, QRect::isValid(), Qt::NoPen, piecePixmaps, pieceRects, QPainter::setBrush(), QPainter::setPen(), QList< T >::size(), and Qt::white.

00167 {
00168     QPainter painter;
00169     painter.begin(this);
00170     painter.fillRect(event->rect(), Qt::white);
00171 
00172     if (highlightedRect.isValid()) {
00173         painter.setBrush(QColor("#ffcccc"));
00174         painter.setPen(Qt::NoPen);
00175         painter.drawRect(highlightedRect.adjusted(0, 0, -1, -1));
00176     }
00177 
00178     for (int i = 0; i < pieceRects.size(); ++i) {
00179         painter.drawPixmap(pieceRects[i], piecePixmaps[i]);
00180     }
00181     painter.end();
00182 }

Here is the call graph for this function:

int PuzzleWidget::findPiece ( const QRect pieceRect  )  const [private]

Definition at line 113 of file puzzlewidget.cpp.

References i, pieceRects, and QList< T >::size().

Referenced by dragMoveEvent(), dropEvent(), and mousePressEvent().

00114 {
00115     for (int i = 0; i < pieceRects.size(); ++i) {
00116         if (pieceRect == pieceRects[i]) {
00117             return i;
00118         }
00119     }
00120     return -1;
00121 }

Here is the call graph for this function:

const QRect PuzzleWidget::targetSquare ( const QPoint position  )  const [private]

Definition at line 184 of file puzzlewidget.cpp.

References QPoint::x(), and QPoint::y().

Referenced by dragMoveEvent(), dropEvent(), and mousePressEvent().

00185 {
00186     return QRect(position.x()/80 * 80, position.y()/80 * 80, 80, 80);
00187 }

Here is the call graph for this function:

void PuzzleWidget::clear (  ) 

void PuzzleWidget::puzzleCompleted (  )  [signal]

void PuzzleWidget::dragEnterEvent ( QDragEnterEvent event  )  [protected, virtual]

This event handler is called when a drag is in progress and the mouse enters this widget. The event is passed in the event parameter.

See the Drag-and-drop documentation for an overview of how to provide drag-and-drop in your application.

See also:
QTextDrag, QImageDrag, QDragEnterEvent

Reimplemented from QWidget.

void PuzzleWidget::dragLeaveEvent ( QDragLeaveEvent event  )  [protected, virtual]

This event handler is called when a drag is in progress and the mouse leaves this widget. The event is passed in the event parameter.

See the Drag-and-drop documentation for an overview of how to provide drag-and-drop in your application.

See also:
QTextDrag, QImageDrag, QDragLeaveEvent

Reimplemented from QWidget.

void PuzzleWidget::dragMoveEvent ( QDragMoveEvent event  )  [protected, virtual]

This event handler is called if a drag is in progress, and when any of the following conditions occurs: the cursor enters this widget, the cursor moves within this widget, or a modifier key is pressed on the keyboard while this widget has the focus. The event is passed in the event parameter.

See the Drag-and-drop documentation for an overview of how to provide drag-and-drop in your application.

See also:
QTextDrag, QImageDrag, QDragMoveEvent

Reimplemented from QWidget.

void PuzzleWidget::dropEvent ( QDropEvent event  )  [protected, virtual]

This event handler is called when the drag is dropped on this widget which are passed in the event parameter.

See the Drag-and-drop documentation for an overview of how to provide drag-and-drop in your application.

See also:
QTextDrag, QImageDrag, QDropEvent

Reimplemented from QWidget.

void PuzzleWidget::mousePressEvent ( QMouseEvent event  )  [protected, virtual]

This event handler, for event event, can be reimplemented in a subclass to receive mouse press events for the widget.

If you create new widgets in the mousePressEvent() the mouseReleaseEvent() may not end up where you expect, depending on the underlying window system (or X11 window manager), the widgets' location and maybe more.

The default implementation implements the closing of popup widgets when you click outside the window. For other widget types it does nothing.

See also:
mouseReleaseEvent(), mouseDoubleClickEvent(), mouseMoveEvent(), event(), QMouseEvent, {Scribble Example}

Reimplemented from QWidget.

void PuzzleWidget::paintEvent ( QPaintEvent event  )  [protected, virtual]

This event handler can be reimplemented in a subclass to receive paint events which are passed in the event parameter.

A paint event is a request to repaint all or part of the widget. It can happen as a result of repaint() or update(), or because the widget was obscured and has now been uncovered, or for many other reasons.

Many widgets can simply repaint their entire surface when asked to, but some slow widgets need to optimize by painting only the requested region: QPaintEvent::region(). This speed optimization does not change the result, as painting is clipped to that region during event processing. QListView and QTableView do this, for example.

Qt also tries to speed up painting by merging multiple paint events into one. When update() is called several times or the window system sends several paint events, Qt merges these events into one event with a larger region (see QRegion::united()). repaint() does not permit this optimization, so we suggest using update() whenever possible.

When the paint event occurs, the update region has normally been erased, so that you're painting on the widget's background.

The background can be set using setBackgroundRole() and setPalette().

From Qt 4.0, QWidget automatically double-buffers its painting, so there's no need to write double-buffering code in paintEvent() to avoid flicker.

Note: Under X11 it is possible to toggle the global double buffering by calling qt_x11_set_global_double_buffer(). Example usage:

See also:
event(), repaint(), update(), QPainter, QPixmap, QPaintEvent, {Analog Clock Example}

Reimplemented from QWidget.

int PuzzleWidget::findPiece ( const QRect pieceRect  )  const [private]

const QRect PuzzleWidget::targetSquare ( const QPoint position  )  const [private]


Member Data Documentation

QList<QPixmap> PuzzleWidget::piecePixmaps [private]

Definition at line 59 of file puzzlewidget.h.

Referenced by clear(), dropEvent(), mousePressEvent(), and paintEvent().

QList<QRect> PuzzleWidget::pieceRects [private]

Definition at line 60 of file puzzlewidget.h.

Referenced by clear(), dropEvent(), findPiece(), mousePressEvent(), and paintEvent().

QList<QPoint> PuzzleWidget::pieceLocations [private]

Definition at line 61 of file puzzlewidget.h.

Referenced by clear(), dropEvent(), and mousePressEvent().

QRect PuzzleWidget::highlightedRect [private]

Definition at line 62 of file puzzlewidget.h.

Referenced by clear(), dragLeaveEvent(), dragMoveEvent(), dropEvent(), and paintEvent().

int PuzzleWidget::inPlace [private]

Definition at line 63 of file puzzlewidget.h.

Referenced by clear(), dropEvent(), and mousePressEvent().

QList<QPixmap> PuzzleWidget::piecePixmaps [private]

Definition at line 59 of file puzzlewidget.h.

QList<QRect> PuzzleWidget::pieceRects [private]

Definition at line 60 of file puzzlewidget.h.

QList<QPoint> PuzzleWidget::pieceLocations [private]

Definition at line 61 of file puzzlewidget.h.


The documentation for this class was generated from the following files:
Generated on Thu Mar 15 15:39:04 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1