| 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 | ** Commercial Usage | 
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 14 | ** a written agreement between you and Nokia. | 
|---|
| 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 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include "tetrixboard.h" | 
|---|
| 43 |  | 
|---|
| 44 | #include <QtGui> | 
|---|
| 45 |  | 
|---|
| 46 | Q_DECLARE_METATYPE(QPainter*) | 
|---|
| 47 |  | 
|---|
| 48 | TetrixBoard::TetrixBoard(QWidget *parent) | 
|---|
| 49 | : QFrame(parent) | 
|---|
| 50 | { | 
|---|
| 51 | timer = new QTimer(this); | 
|---|
| 52 | qMetaTypeId<QPainter*>(); | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | void TetrixBoard::setNextPieceLabel(QWidget *label) | 
|---|
| 56 | { | 
|---|
| 57 | nextPieceLabel = qobject_cast<QLabel*>(label); | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 | QObject *TetrixBoard::getTimer() | 
|---|
| 61 | { | 
|---|
| 62 | return timer; | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | QSize TetrixBoard::minimumSizeHint() const | 
|---|
| 66 | { | 
|---|
| 67 | return QSize(BoardWidth * 5 + frameWidth() * 2, | 
|---|
| 68 | BoardHeight * 5 + frameWidth() * 2); | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 | void TetrixBoard::paintEvent(QPaintEvent *event) | 
|---|
| 72 | { | 
|---|
| 73 | QFrame::paintEvent(event); | 
|---|
| 74 | QPainter painter(this); | 
|---|
| 75 | painter.drawImage(0, 0, image); | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | void TetrixBoard::keyPressEvent(QKeyEvent *event) | 
|---|
| 79 | { | 
|---|
| 80 | emit keyPressed(event->key()); | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | void TetrixBoard::showNextPiece(int width, int height) | 
|---|
| 84 | { | 
|---|
| 85 | if (!nextPieceLabel) | 
|---|
| 86 | return; | 
|---|
| 87 |  | 
|---|
| 88 | QPixmap pixmap(width * squareWidth(), height * squareHeight()); | 
|---|
| 89 | QPainter painter(&pixmap); | 
|---|
| 90 | painter.fillRect(pixmap.rect(), nextPieceLabel->palette().background()); | 
|---|
| 91 |  | 
|---|
| 92 | emit paintNextPieceRequested(&painter); | 
|---|
| 93 |  | 
|---|
| 94 | nextPieceLabel->setPixmap(pixmap); | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | void TetrixBoard::drawPauseScreen(QPainter *painter) | 
|---|
| 98 | { | 
|---|
| 99 | painter->drawText(contentsRect(), Qt::AlignCenter, tr("Pause")); | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | void TetrixBoard::drawSquare(QPainter *painter, int x, int y, int shape) | 
|---|
| 103 | { | 
|---|
| 104 | static const QRgb colorTable[8] = { | 
|---|
| 105 | 0x000000, 0xCC6666, 0x66CC66, 0x6666CC, | 
|---|
| 106 | 0xCCCC66, 0xCC66CC, 0x66CCCC, 0xDAAA00 | 
|---|
| 107 | }; | 
|---|
| 108 |  | 
|---|
| 109 | x = x*squareWidth(); | 
|---|
| 110 | y = y*squareHeight(); | 
|---|
| 111 |  | 
|---|
| 112 | QColor color = colorTable[shape]; | 
|---|
| 113 | painter->fillRect(x + 1, y + 1, squareWidth() - 2, squareHeight() - 2, | 
|---|
| 114 | color); | 
|---|
| 115 |  | 
|---|
| 116 | painter->setPen(color.light()); | 
|---|
| 117 | painter->drawLine(x, y + squareHeight() - 1, x, y); | 
|---|
| 118 | painter->drawLine(x, y, x + squareWidth() - 1, y); | 
|---|
| 119 |  | 
|---|
| 120 | painter->setPen(color.dark()); | 
|---|
| 121 | painter->drawLine(x + 1, y + squareHeight() - 1, | 
|---|
| 122 | x + squareWidth() - 1, y + squareHeight() - 1); | 
|---|
| 123 | painter->drawLine(x + squareWidth() - 1, y + squareHeight() - 1, | 
|---|
| 124 | x + squareWidth() - 1, y + 1); | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | void TetrixBoard::update() | 
|---|
| 128 | { | 
|---|
| 129 | QRect rect = contentsRect(); | 
|---|
| 130 | if (image.size() != rect.size()) | 
|---|
| 131 | image = QImage(rect.size(), QImage::Format_ARGB32_Premultiplied); | 
|---|
| 132 | image.fill(qRgba(0,0,0,0)); | 
|---|
| 133 | QPainter painter; | 
|---|
| 134 | painter.begin(&image); | 
|---|
| 135 | int boardTop = rect.bottom() - BoardHeight*squareHeight(); | 
|---|
| 136 | painter.translate(rect.left(), boardTop); | 
|---|
| 137 | emit paintRequested(&painter); | 
|---|
| 138 | QFrame::update(); | 
|---|
| 139 | } | 
|---|