diff options
Diffstat (limited to 'examples/widgets/widgets/tetrix')
-rw-r--r-- | examples/widgets/widgets/tetrix/CMakeLists.txt | 42 | ||||
-rw-r--r-- | examples/widgets/widgets/tetrix/main.cpp | 61 | ||||
-rw-r--r-- | examples/widgets/widgets/tetrix/tetrix.pro | 13 | ||||
-rw-r--r-- | examples/widgets/widgets/tetrix/tetrixboard.cpp | 418 | ||||
-rw-r--r-- | examples/widgets/widgets/tetrix/tetrixboard.h | 126 | ||||
-rw-r--r-- | examples/widgets/widgets/tetrix/tetrixpiece.cpp | 153 | ||||
-rw-r--r-- | examples/widgets/widgets/tetrix/tetrixpiece.h | 85 | ||||
-rw-r--r-- | examples/widgets/widgets/tetrix/tetrixwindow.cpp | 138 | ||||
-rw-r--r-- | examples/widgets/widgets/tetrix/tetrixwindow.h | 85 |
9 files changed, 0 insertions, 1121 deletions
diff --git a/examples/widgets/widgets/tetrix/CMakeLists.txt b/examples/widgets/widgets/tetrix/CMakeLists.txt deleted file mode 100644 index 85ec447334..0000000000 --- a/examples/widgets/widgets/tetrix/CMakeLists.txt +++ /dev/null @@ -1,42 +0,0 @@ -# Generated from tetrix.pro. - -cmake_minimum_required(VERSION 3.16) -project(tetrix LANGUAGES CXX) - -set(CMAKE_INCLUDE_CURRENT_DIR ON) - -set(CMAKE_AUTOMOC ON) -set(CMAKE_AUTORCC ON) -set(CMAKE_AUTOUIC ON) - -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") -endif() - -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/widgets/tetrix") - -find_package(Qt6 COMPONENTS Core) -find_package(Qt6 COMPONENTS Gui) -find_package(Qt6 COMPONENTS Widgets) - -qt_add_executable(tetrix - main.cpp - tetrixboard.cpp tetrixboard.h - tetrixpiece.cpp tetrixpiece.h - tetrixwindow.cpp tetrixwindow.h -) -set_target_properties(tetrix PROPERTIES - WIN32_EXECUTABLE TRUE - MACOSX_BUNDLE TRUE -) -target_link_libraries(tetrix PUBLIC - Qt::Core - Qt::Gui - Qt::Widgets -) - -install(TARGETS tetrix - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" -) diff --git a/examples/widgets/widgets/tetrix/main.cpp b/examples/widgets/widgets/tetrix/main.cpp deleted file mode 100644 index 92f245ca14..0000000000 --- a/examples/widgets/widgets/tetrix/main.cpp +++ /dev/null @@ -1,61 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "tetrixwindow.h" - -#include <QApplication> - -int main(int argc, char *argv[]) -{ - QApplication app(argc, argv); - TetrixWindow window; - window.show(); - return app.exec(); -} diff --git a/examples/widgets/widgets/tetrix/tetrix.pro b/examples/widgets/widgets/tetrix/tetrix.pro deleted file mode 100644 index bb9f855fb0..0000000000 --- a/examples/widgets/widgets/tetrix/tetrix.pro +++ /dev/null @@ -1,13 +0,0 @@ -QT += widgets - -HEADERS = tetrixboard.h \ - tetrixpiece.h \ - tetrixwindow.h -SOURCES = main.cpp \ - tetrixboard.cpp \ - tetrixpiece.cpp \ - tetrixwindow.cpp - -# install -target.path = $$[QT_INSTALL_EXAMPLES]/widgets/widgets/tetrix -INSTALLS += target diff --git a/examples/widgets/widgets/tetrix/tetrixboard.cpp b/examples/widgets/widgets/tetrix/tetrixboard.cpp deleted file mode 100644 index c1192f264d..0000000000 --- a/examples/widgets/widgets/tetrix/tetrixboard.cpp +++ /dev/null @@ -1,418 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "tetrixboard.h" - -#include <QKeyEvent> -#include <QLabel> -#include <QPainter> - -//! [0] -TetrixBoard::TetrixBoard(QWidget *parent) - : QFrame(parent), isStarted(false), isPaused(false) -{ - setFrameStyle(QFrame::Panel | QFrame::Sunken); - setFocusPolicy(Qt::StrongFocus); - clearBoard(); - - nextPiece.setRandomShape(); -} -//! [0] - -//! [1] -void TetrixBoard::setNextPieceLabel(QLabel *label) -{ - nextPieceLabel = label; -} -//! [1] - -//! [2] -QSize TetrixBoard::sizeHint() const -{ - return QSize(BoardWidth * 15 + frameWidth() * 2, - BoardHeight * 15 + frameWidth() * 2); -} - -QSize TetrixBoard::minimumSizeHint() const -//! [2] //! [3] -{ - return QSize(BoardWidth * 5 + frameWidth() * 2, - BoardHeight * 5 + frameWidth() * 2); -} -//! [3] - -//! [4] -void TetrixBoard::start() -{ - if (isPaused) - return; - - isStarted = true; - isWaitingAfterLine = false; - numLinesRemoved = 0; - numPiecesDropped = 0; - score = 0; - level = 1; - clearBoard(); - - emit linesRemovedChanged(numLinesRemoved); - emit scoreChanged(score); - emit levelChanged(level); - - newPiece(); - timer.start(timeoutTime(), this); -} -//! [4] - -//! [5] -void TetrixBoard::pause() -{ - if (!isStarted) - return; - - isPaused = !isPaused; - if (isPaused) { - timer.stop(); - } else { - timer.start(timeoutTime(), this); - } - update(); -//! [5] //! [6] -} -//! [6] - -//! [7] -void TetrixBoard::paintEvent(QPaintEvent *event) -{ - QFrame::paintEvent(event); - - QPainter painter(this); - QRect rect = contentsRect(); -//! [7] - - if (isPaused) { - painter.drawText(rect, Qt::AlignCenter, tr("Pause")); - return; - } - -//! [8] - int boardTop = rect.bottom() - BoardHeight*squareHeight(); - - for (int i = 0; i < BoardHeight; ++i) { - for (int j = 0; j < BoardWidth; ++j) { - TetrixShape shape = shapeAt(j, BoardHeight - i - 1); - if (shape != NoShape) - drawSquare(painter, rect.left() + j * squareWidth(), - boardTop + i * squareHeight(), shape); - } -//! [8] //! [9] - } -//! [9] - -//! [10] - if (curPiece.shape() != NoShape) { - for (int i = 0; i < 4; ++i) { - int x = curX + curPiece.x(i); - int y = curY - curPiece.y(i); - drawSquare(painter, rect.left() + x * squareWidth(), - boardTop + (BoardHeight - y - 1) * squareHeight(), - curPiece.shape()); - } -//! [10] //! [11] - } -//! [11] //! [12] -} -//! [12] - -//! [13] -void TetrixBoard::keyPressEvent(QKeyEvent *event) -{ - if (!isStarted || isPaused || curPiece.shape() == NoShape) { - QFrame::keyPressEvent(event); - return; - } -//! [13] - -//! [14] - switch (event->key()) { - case Qt::Key_Left: - tryMove(curPiece, curX - 1, curY); - break; - case Qt::Key_Right: - tryMove(curPiece, curX + 1, curY); - break; - case Qt::Key_Down: - tryMove(curPiece.rotatedRight(), curX, curY); - break; - case Qt::Key_Up: - tryMove(curPiece.rotatedLeft(), curX, curY); - break; - case Qt::Key_Space: - dropDown(); - break; - case Qt::Key_D: - oneLineDown(); - break; - default: - QFrame::keyPressEvent(event); - } -//! [14] -} - -//! [15] -void TetrixBoard::timerEvent(QTimerEvent *event) -{ - if (event->timerId() == timer.timerId()) { - if (isWaitingAfterLine) { - isWaitingAfterLine = false; - newPiece(); - timer.start(timeoutTime(), this); - } else { - oneLineDown(); - } - } else { - QFrame::timerEvent(event); -//! [15] //! [16] - } -//! [16] //! [17] -} -//! [17] - -//! [18] -void TetrixBoard::clearBoard() -{ - for (int i = 0; i < BoardHeight * BoardWidth; ++i) - board[i] = NoShape; -} -//! [18] - -//! [19] -void TetrixBoard::dropDown() -{ - int dropHeight = 0; - int newY = curY; - while (newY > 0) { - if (!tryMove(curPiece, curX, newY - 1)) - break; - --newY; - ++dropHeight; - } - pieceDropped(dropHeight); -//! [19] //! [20] -} -//! [20] - -//! [21] -void TetrixBoard::oneLineDown() -{ - if (!tryMove(curPiece, curX, curY - 1)) - pieceDropped(0); -} -//! [21] - -//! [22] -void TetrixBoard::pieceDropped(int dropHeight) -{ - for (int i = 0; i < 4; ++i) { - int x = curX + curPiece.x(i); - int y = curY - curPiece.y(i); - shapeAt(x, y) = curPiece.shape(); - } - - ++numPiecesDropped; - if (numPiecesDropped % 25 == 0) { - ++level; - timer.start(timeoutTime(), this); - emit levelChanged(level); - } - - score += dropHeight + 7; - emit scoreChanged(score); - removeFullLines(); - - if (!isWaitingAfterLine) - newPiece(); -//! [22] //! [23] -} -//! [23] - -//! [24] -void TetrixBoard::removeFullLines() -{ - int numFullLines = 0; - - for (int i = BoardHeight - 1; i >= 0; --i) { - bool lineIsFull = true; - - for (int j = 0; j < BoardWidth; ++j) { - if (shapeAt(j, i) == NoShape) { - lineIsFull = false; - break; - } - } - - if (lineIsFull) { -//! [24] //! [25] - ++numFullLines; - for (int k = i; k < BoardHeight - 1; ++k) { - for (int j = 0; j < BoardWidth; ++j) - shapeAt(j, k) = shapeAt(j, k + 1); - } -//! [25] //! [26] - for (int j = 0; j < BoardWidth; ++j) - shapeAt(j, BoardHeight - 1) = NoShape; - } -//! [26] //! [27] - } -//! [27] - -//! [28] - if (numFullLines > 0) { - numLinesRemoved += numFullLines; - score += 10 * numFullLines; - emit linesRemovedChanged(numLinesRemoved); - emit scoreChanged(score); - - timer.start(500, this); - isWaitingAfterLine = true; - curPiece.setShape(NoShape); - update(); - } -//! [28] //! [29] -} -//! [29] - -//! [30] -void TetrixBoard::newPiece() -{ - curPiece = nextPiece; - nextPiece.setRandomShape(); - showNextPiece(); - curX = BoardWidth / 2 + 1; - curY = BoardHeight - 1 + curPiece.minY(); - - if (!tryMove(curPiece, curX, curY)) { - curPiece.setShape(NoShape); - timer.stop(); - isStarted = false; - } -//! [30] //! [31] -} -//! [31] - -//! [32] -void TetrixBoard::showNextPiece() -{ - if (!nextPieceLabel) - return; - - int dx = nextPiece.maxX() - nextPiece.minX() + 1; - int dy = nextPiece.maxY() - nextPiece.minY() + 1; - - QPixmap pixmap(dx * squareWidth(), dy * squareHeight()); - QPainter painter(&pixmap); - painter.fillRect(pixmap.rect(), nextPieceLabel->palette().window()); - - for (int i = 0; i < 4; ++i) { - int x = nextPiece.x(i) - nextPiece.minX(); - int y = nextPiece.y(i) - nextPiece.minY(); - drawSquare(painter, x * squareWidth(), y * squareHeight(), - nextPiece.shape()); - } - nextPieceLabel->setPixmap(pixmap); -//! [32] //! [33] -} -//! [33] - -//! [34] -bool TetrixBoard::tryMove(const TetrixPiece &newPiece, int newX, int newY) -{ - for (int i = 0; i < 4; ++i) { - int x = newX + newPiece.x(i); - int y = newY - newPiece.y(i); - if (x < 0 || x >= BoardWidth || y < 0 || y >= BoardHeight) - return false; - if (shapeAt(x, y) != NoShape) - return false; - } -//! [34] - -//! [35] - curPiece = newPiece; - curX = newX; - curY = newY; - update(); - return true; -} -//! [35] - -//! [36] -void TetrixBoard::drawSquare(QPainter &painter, int x, int y, TetrixShape shape) -{ - static constexpr QRgb colorTable[8] = { - 0x000000, 0xCC6666, 0x66CC66, 0x6666CC, - 0xCCCC66, 0xCC66CC, 0x66CCCC, 0xDAAA00 - }; - - QColor color = colorTable[int(shape)]; - painter.fillRect(x + 1, y + 1, squareWidth() - 2, squareHeight() - 2, - color); - - painter.setPen(color.lighter()); - painter.drawLine(x, y + squareHeight() - 1, x, y); - painter.drawLine(x, y, x + squareWidth() - 1, y); - - painter.setPen(color.darker()); - painter.drawLine(x + 1, y + squareHeight() - 1, - x + squareWidth() - 1, y + squareHeight() - 1); - painter.drawLine(x + squareWidth() - 1, y + squareHeight() - 1, - x + squareWidth() - 1, y + 1); -} -//! [36] diff --git a/examples/widgets/widgets/tetrix/tetrixboard.h b/examples/widgets/widgets/tetrix/tetrixboard.h deleted file mode 100644 index 86bcfa4d6e..0000000000 --- a/examples/widgets/widgets/tetrix/tetrixboard.h +++ /dev/null @@ -1,126 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef TETRIXBOARD_H -#define TETRIXBOARD_H - -#include <QBasicTimer> -#include <QFrame> -#include <QPointer> - -#include "tetrixpiece.h" - -QT_BEGIN_NAMESPACE -class QLabel; -QT_END_NAMESPACE - -//! [0] -class TetrixBoard : public QFrame -{ - Q_OBJECT - -public: - TetrixBoard(QWidget *parent = nullptr); - - void setNextPieceLabel(QLabel *label); - QSize sizeHint() const override; - QSize minimumSizeHint() const override; - -public slots: - void start(); - void pause(); - -signals: - void scoreChanged(int score); - void levelChanged(int level); - void linesRemovedChanged(int numLines); - -protected: - void paintEvent(QPaintEvent *event) override; - void keyPressEvent(QKeyEvent *event) override; - void timerEvent(QTimerEvent *event) override; -//! [0] - -//! [1] -private: - enum { BoardWidth = 10, BoardHeight = 22 }; - - TetrixShape &shapeAt(int x, int y) { return board[(y * BoardWidth) + x]; } - int timeoutTime() { return 1000 / (1 + level); } - int squareWidth() { return contentsRect().width() / BoardWidth; } - int squareHeight() { return contentsRect().height() / BoardHeight; } - void clearBoard(); - void dropDown(); - void oneLineDown(); - void pieceDropped(int dropHeight); - void removeFullLines(); - void newPiece(); - void showNextPiece(); - bool tryMove(const TetrixPiece &newPiece, int newX, int newY); - void drawSquare(QPainter &painter, int x, int y, TetrixShape shape); - - QBasicTimer timer; - QPointer<QLabel> nextPieceLabel; - bool isStarted; - bool isPaused; - bool isWaitingAfterLine; - TetrixPiece curPiece; - TetrixPiece nextPiece; - int curX; - int curY; - int numLinesRemoved; - int numPiecesDropped; - int score; - int level; - TetrixShape board[BoardWidth * BoardHeight]; -}; -//! [1] - -#endif diff --git a/examples/widgets/widgets/tetrix/tetrixpiece.cpp b/examples/widgets/widgets/tetrix/tetrixpiece.cpp deleted file mode 100644 index 9f6f6e863a..0000000000 --- a/examples/widgets/widgets/tetrix/tetrixpiece.cpp +++ /dev/null @@ -1,153 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "tetrixpiece.h" - -#include <QtCore> - -//! [0] -void TetrixPiece::setRandomShape() -{ - setShape(TetrixShape(QRandomGenerator::global()->bounded(7) + 1)); -} -//! [0] - -//! [1] -void TetrixPiece::setShape(TetrixShape shape) -{ - static constexpr int coordsTable[8][4][2] = { - { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } }, - { { 0, -1 }, { 0, 0 }, { -1, 0 }, { -1, 1 } }, - { { 0, -1 }, { 0, 0 }, { 1, 0 }, { 1, 1 } }, - { { 0, -1 }, { 0, 0 }, { 0, 1 }, { 0, 2 } }, - { { -1, 0 }, { 0, 0 }, { 1, 0 }, { 0, 1 } }, - { { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } }, - { { -1, -1 }, { 0, -1 }, { 0, 0 }, { 0, 1 } }, - { { 1, -1 }, { 0, -1 }, { 0, 0 }, { 0, 1 } } - }; - - for (int i = 0; i < 4 ; i++) { - for (int j = 0; j < 2; ++j) - coords[i][j] = coordsTable[shape][i][j]; - } - pieceShape = shape; -//! [1] //! [2] -} -//! [2] - -//! [3] -int TetrixPiece::minX() const -{ - int min = coords[0][0]; - for (int i = 1; i < 4; ++i) - min = qMin(min, coords[i][0]); - return min; -} - -int TetrixPiece::maxX() const -//! [3] //! [4] -{ - int max = coords[0][0]; - for (int i = 1; i < 4; ++i) - max = qMax(max, coords[i][0]); - return max; -} -//! [4] - -//! [5] -int TetrixPiece::minY() const -{ - int min = coords[0][1]; - for (int i = 1; i < 4; ++i) - min = qMin(min, coords[i][1]); - return min; -} - -int TetrixPiece::maxY() const -//! [5] //! [6] -{ - int max = coords[0][1]; - for (int i = 1; i < 4; ++i) - max = qMax(max, coords[i][1]); - return max; -} -//! [6] - -//! [7] -TetrixPiece TetrixPiece::rotatedLeft() const -{ - if (pieceShape == SquareShape) - return *this; - - TetrixPiece result; - result.pieceShape = pieceShape; - for (int i = 0; i < 4; ++i) { - result.setX(i, y(i)); - result.setY(i, -x(i)); - } -//! [7] - return result; -} - -//! [9] -TetrixPiece TetrixPiece::rotatedRight() const -{ - if (pieceShape == SquareShape) - return *this; - - TetrixPiece result; - result.pieceShape = pieceShape; - for (int i = 0; i < 4; ++i) { - result.setX(i, -y(i)); - result.setY(i, x(i)); - } -//! [9] - return result; -} diff --git a/examples/widgets/widgets/tetrix/tetrixpiece.h b/examples/widgets/widgets/tetrix/tetrixpiece.h deleted file mode 100644 index 0a01c23423..0000000000 --- a/examples/widgets/widgets/tetrix/tetrixpiece.h +++ /dev/null @@ -1,85 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef TETRIXPIECE_H -#define TETRIXPIECE_H - -enum TetrixShape { NoShape, ZShape, SShape, LineShape, TShape, SquareShape, - LShape, MirroredLShape }; - -//! [0] -class TetrixPiece -{ -public: - TetrixPiece() { setShape(NoShape); } - - void setRandomShape(); - void setShape(TetrixShape shape); - - TetrixShape shape() const { return pieceShape; } - int x(int index) const { return coords[index][0]; } - int y(int index) const { return coords[index][1]; } - int minX() const; - int maxX() const; - int minY() const; - int maxY() const; - TetrixPiece rotatedLeft() const; - TetrixPiece rotatedRight() const; - -private: - void setX(int index, int x) { coords[index][0] = x; } - void setY(int index, int y) { coords[index][1] = y; } - - TetrixShape pieceShape; - int coords[4][2]; -}; -//! [0] - -#endif diff --git a/examples/widgets/widgets/tetrix/tetrixwindow.cpp b/examples/widgets/widgets/tetrix/tetrixwindow.cpp deleted file mode 100644 index 5aa5c3f615..0000000000 --- a/examples/widgets/widgets/tetrix/tetrixwindow.cpp +++ /dev/null @@ -1,138 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "tetrixboard.h" -#include "tetrixwindow.h" - -#include <QCoreApplication> -#include <QGridLayout> -#include <QLabel> -#include <QLCDNumber> -#include <QPushButton> - -//! [0] -TetrixWindow::TetrixWindow(QWidget *parent) - : QWidget(parent), board(new TetrixBoard) -{ -//! [0] - nextPieceLabel = new QLabel; - nextPieceLabel->setFrameStyle(QFrame::Box | QFrame::Raised); - nextPieceLabel->setAlignment(Qt::AlignCenter); - board->setNextPieceLabel(nextPieceLabel); -//! [1] - scoreLcd = new QLCDNumber(5); - scoreLcd->setSegmentStyle(QLCDNumber::Filled); -//! [1] - levelLcd = new QLCDNumber(2); - levelLcd->setSegmentStyle(QLCDNumber::Filled); - linesLcd = new QLCDNumber(5); - linesLcd->setSegmentStyle(QLCDNumber::Filled); - -//! [2] - startButton = new QPushButton(tr("&Start")); - startButton->setFocusPolicy(Qt::NoFocus); - quitButton = new QPushButton(tr("&Quit")); - quitButton->setFocusPolicy(Qt::NoFocus); - pauseButton = new QPushButton(tr("&Pause")); -//! [2] //! [3] - pauseButton->setFocusPolicy(Qt::NoFocus); -//! [3] //! [4] - - connect(startButton, &QPushButton::clicked, board, &TetrixBoard::start); -//! [4] //! [5] - connect(quitButton , &QPushButton::clicked, qApp, &QCoreApplication::quit); - connect(pauseButton, &QPushButton::clicked, board, &TetrixBoard::pause); -#if __cplusplus >= 201402L - connect(board, &TetrixBoard::scoreChanged, - scoreLcd, qOverload<int>(&QLCDNumber::display)); - connect(board, &TetrixBoard::levelChanged, - levelLcd, qOverload<int>(&QLCDNumber::display)); - connect(board, &TetrixBoard::linesRemovedChanged, - linesLcd, qOverload<int>(&QLCDNumber::display)); -#else - connect(board, &TetrixBoard::scoreChanged, - scoreLcd, QOverload<int>::of(&QLCDNumber::display)); - connect(board, &TetrixBoard::levelChanged, - levelLcd, QOverload<int>::of(&QLCDNumber::display)); - connect(board, &TetrixBoard::linesRemovedChanged, - linesLcd, QOverload<int>::of(&QLCDNumber::display)); -#endif -//! [5] - -//! [6] - QGridLayout *layout = new QGridLayout; - layout->addWidget(createLabel(tr("NEXT")), 0, 0); - layout->addWidget(nextPieceLabel, 1, 0); - layout->addWidget(createLabel(tr("LEVEL")), 2, 0); - layout->addWidget(levelLcd, 3, 0); - layout->addWidget(startButton, 4, 0); - layout->addWidget(board, 0, 1, 6, 1); - layout->addWidget(createLabel(tr("SCORE")), 0, 2); - layout->addWidget(scoreLcd, 1, 2); - layout->addWidget(createLabel(tr("LINES REMOVED")), 2, 2); - layout->addWidget(linesLcd, 3, 2); - layout->addWidget(quitButton, 4, 2); - layout->addWidget(pauseButton, 5, 2); - setLayout(layout); - - setWindowTitle(tr("Tetrix")); - resize(550, 370); -} -//! [6] - -//! [7] -QLabel *TetrixWindow::createLabel(const QString &text) -{ - QLabel *label = new QLabel(text); - label->setAlignment(Qt::AlignHCenter | Qt::AlignBottom); - return label; -} -//! [7] - diff --git a/examples/widgets/widgets/tetrix/tetrixwindow.h b/examples/widgets/widgets/tetrix/tetrixwindow.h deleted file mode 100644 index 3743d6cd2a..0000000000 --- a/examples/widgets/widgets/tetrix/tetrixwindow.h +++ /dev/null @@ -1,85 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef TETRIXWINDOW_H -#define TETRIXWINDOW_H - -#include <QWidget> - -QT_BEGIN_NAMESPACE -class QLCDNumber; -class QLabel; -class QPushButton; -QT_END_NAMESPACE -class TetrixBoard; - -//! [0] -class TetrixWindow : public QWidget -{ - Q_OBJECT - -public: - TetrixWindow(QWidget *parent = nullptr); - -private: - QLabel *createLabel(const QString &text); - - TetrixBoard *board; - QLabel *nextPieceLabel; - QLCDNumber *scoreLcd; - QLCDNumber *levelLcd; - QLCDNumber *linesLcd; - QPushButton *startButton; - QPushButton *quitButton; - QPushButton *pauseButton; -}; -//! [0] - -#endif |