examples/script/qstetrix/tetrixboard.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 "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 }