#include <puzzlewidget.h>
Inheritance diagram for PuzzleWidget:


Definition at line 36 of file puzzlewidget.h.
| 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 |
) |
| 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.
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.
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.
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.
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.
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:
...
extern void qt_x11_set_global_double_buffer(bool);
qt_x11_set_global_double_buffer(false);
...
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:

Definition at line 184 of file puzzlewidget.cpp.
References QPoint::x(), and QPoint::y().
Referenced by dragMoveEvent(), dropEvent(), and mousePressEvent().
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.
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.
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.
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.
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.
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:
...
extern void qt_x11_set_global_double_buffer(bool);
qt_x11_set_global_double_buffer(false);
...
Reimplemented from QWidget.
| int PuzzleWidget::findPiece | ( | const QRect & | pieceRect | ) | const [private] |
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.
1.5.1