| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the examples of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** 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 are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at qt-sales@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 | }
|
|---|