| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2017 The Qt Company Ltd. | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the examples of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:BSD$ | 
| 9 | ** Commercial License Usage | 
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 11 | ** accordance with the commercial license agreement provided with the | 
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** BSD License Usage | 
| 18 | ** Alternatively, you may use this file under the terms of the BSD license | 
| 19 | ** as follows: | 
| 20 | ** | 
| 21 | ** "Redistribution and use in source and binary forms, with or without | 
| 22 | ** modification, are permitted provided that the following conditions are | 
| 23 | ** met: | 
| 24 | **   * Redistributions of source code must retain the above copyright | 
| 25 | **     notice, this list of conditions and the following disclaimer. | 
| 26 | **   * Redistributions in binary form must reproduce the above copyright | 
| 27 | **     notice, this list of conditions and the following disclaimer in | 
| 28 | **     the documentation and/or other materials provided with the | 
| 29 | **     distribution. | 
| 30 | **   * Neither the name of The Qt Company Ltd nor the names of its | 
| 31 | **     contributors may be used to endorse or promote products derived | 
| 32 | **     from this software without specific prior written permission. | 
| 33 | ** | 
| 34 | ** | 
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." | 
| 46 | ** | 
| 47 | ** $QT_END_LICENSE$ | 
| 48 | ** | 
| 49 | ****************************************************************************/ | 
| 50 |  | 
| 51 | #include "tetrixboard.h" | 
| 52 |  | 
| 53 | #include <QtWidgets> | 
| 54 |  | 
| 55 | Q_DECLARE_METATYPE(QPainter*) | 
| 56 |  | 
| 57 | TetrixBoard::TetrixBoard(QWidget *parent) | 
| 58 |     : QFrame(parent) | 
| 59 | { | 
| 60 |     timer = new QTimer(this); | 
| 61 |     qMetaTypeId<QPainter*>(); | 
| 62 | } | 
| 63 |  | 
| 64 | void TetrixBoard::setNextPieceLabel(QWidget *label) | 
| 65 | { | 
| 66 |     nextPieceLbl = qobject_cast<QLabel*>(object: label); | 
| 67 | } | 
| 68 |  | 
| 69 | QLabel *TetrixBoard::nextPieceLabel() const | 
| 70 | {  | 
| 71 |     return nextPieceLbl; | 
| 72 | } | 
| 73 |  | 
| 74 | QObject *TetrixBoard::getTimer() | 
| 75 | { | 
| 76 |     return timer; | 
| 77 | } | 
| 78 |  | 
| 79 | QSize TetrixBoard::minimumSizeHint() const | 
| 80 | { | 
| 81 |     return QSize(BoardWidth * 5 + frameWidth() * 2, | 
| 82 |                  BoardHeight * 5 + frameWidth() * 2); | 
| 83 | } | 
| 84 |  | 
| 85 | void TetrixBoard::paintEvent(QPaintEvent *event) | 
| 86 | { | 
| 87 |     QFrame::paintEvent(event); | 
| 88 |     QPainter painter(this); | 
| 89 |     painter.drawImage(x: 0, y: 0, image); | 
| 90 | } | 
| 91 |  | 
| 92 | void TetrixBoard::keyPressEvent(QKeyEvent *event) | 
| 93 | { | 
| 94 |     emit keyPressed(key: event->key()); | 
| 95 | } | 
| 96 |  | 
| 97 | void TetrixBoard::showNextPiece(int width, int height) | 
| 98 | { | 
| 99 |     if (!nextPieceLabel()) | 
| 100 |         return; | 
| 101 |  | 
| 102 |     QPixmap pixmap(width * squareWidth(), height * squareHeight()); | 
| 103 |     QPainter painter(&pixmap); | 
| 104 |     painter.fillRect(pixmap.rect(), nextPieceLabel()->palette().window()); | 
| 105 |  | 
| 106 |     emit paintNextPieceRequested(painter: &painter); | 
| 107 |  | 
| 108 |     nextPieceLabel()->setPixmap(pixmap); | 
| 109 | } | 
| 110 |  | 
| 111 | void TetrixBoard::drawPauseScreen(QPainter *painter) | 
| 112 | { | 
| 113 |     painter->drawText(r: contentsRect(), flags: Qt::AlignCenter, text: tr(s: "Pause" )); | 
| 114 | } | 
| 115 |  | 
| 116 | void TetrixBoard::drawSquare(QPainter *painter, int x, int y, int shape) | 
| 117 | { | 
| 118 |     static const QRgb colorTable[8] = { | 
| 119 |         0x000000, 0xCC6666, 0x66CC66, 0x6666CC, | 
| 120 |         0xCCCC66, 0xCC66CC, 0x66CCCC, 0xDAAA00 | 
| 121 |     }; | 
| 122 |  | 
| 123 |     x = x*squareWidth(); | 
| 124 |     y = y*squareHeight(); | 
| 125 |  | 
| 126 |     QColor color = colorTable[shape]; | 
| 127 |     painter->fillRect(x: x + 1, y: y + 1, w: squareWidth() - 2, h: squareHeight() - 2, | 
| 128 |                       b: color); | 
| 129 |  | 
| 130 |     painter->setPen(color.lighter()); | 
| 131 |     painter->drawLine(x1: x, y1: y + squareHeight() - 1, x2: x, y2: y); | 
| 132 |     painter->drawLine(x1: x, y1: y, x2: x + squareWidth() - 1, y2: y); | 
| 133 |  | 
| 134 |     painter->setPen(color.darker()); | 
| 135 |     painter->drawLine(x1: x + 1, y1: y + squareHeight() - 1, | 
| 136 |                       x2: x + squareWidth() - 1, y2: y + squareHeight() - 1); | 
| 137 |     painter->drawLine(x1: x + squareWidth() - 1, y1: y + squareHeight() - 1, | 
| 138 |                       x2: x + squareWidth() - 1, y2: y + 1); | 
| 139 | } | 
| 140 |  | 
| 141 | void TetrixBoard::update() | 
| 142 | { | 
| 143 |     QRect rect = contentsRect(); | 
| 144 |     if (image.size() != rect.size()) | 
| 145 |         image = QImage(rect.size(), QImage::Format_ARGB32_Premultiplied); | 
| 146 |     image.fill(pixel: qRgba(r: 0,g: 0,b: 0,a: 0)); | 
| 147 |     QPainter painter; | 
| 148 |     painter.begin(&image); | 
| 149 |     int boardTop = rect.bottom() - BoardHeight*squareHeight(); | 
| 150 |     painter.translate(dx: rect.left(), dy: boardTop); | 
| 151 |     emit paintRequested(painter: &painter); | 
| 152 |     QFrame::update(); | 
| 153 | } | 
| 154 |  |