File indexing completed on 2024-05-19 04:05:42
0001 /* 0002 This file is part of Knights, a chess board for KDE SC 4. 0003 SPDX-FileCopyrightText: 2009, 2010, 2011 Miha Čančula <miha@noughmad.eu> 0004 0005 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0006 */ 0007 0008 #include "piece.h" 0009 0010 #include <KLocalizedString> 0011 0012 namespace Knights { 0013 0014 QString Piece::spriteKey ( PieceType type, Color color ) { 0015 QString id; 0016 switch ( color ) { 0017 case White: 0018 id.append ( QLatin1String ( "White" ) ); 0019 break; 0020 case Black: 0021 id.append ( QLatin1String ( "Black" ) ); 0022 break; 0023 default: 0024 break; 0025 } 0026 switch ( type ) { 0027 case Pawn: 0028 id.append ( QLatin1String ( "Pawn" ) ); 0029 break; 0030 case Rook: 0031 id.append ( QLatin1String ( "Rook" ) ); 0032 break; 0033 case Knight: 0034 id.append ( QLatin1String ( "Knight" ) ); 0035 break; 0036 case Bishop: 0037 id.append ( QLatin1String ( "Bishop" ) ); 0038 break; 0039 case Queen: 0040 id.append ( QLatin1String ( "Queen" ) ); 0041 break; 0042 case King: 0043 id.append ( QLatin1String ( "King" ) ); 0044 break; 0045 default: 0046 break; 0047 } 0048 return id; 0049 } 0050 0051 QChar Piece::charFromType ( PieceType t ) { 0052 switch ( t ) { 0053 case Pawn: 0054 return QLatin1Char ( 'P' ); 0055 case Queen: 0056 return QLatin1Char ( 'Q' ); 0057 case King: 0058 return QLatin1Char ( 'K' ); 0059 case Bishop: 0060 return QLatin1Char ( 'B' ); 0061 case Knight: 0062 return QLatin1Char ( 'N' ); 0063 case Rook: 0064 return QLatin1Char ( 'R' ); 0065 default: 0066 break; 0067 } 0068 return QLatin1Char ( 'E' ); 0069 } 0070 0071 PieceType Piece::typeFromChar ( QChar typeChar ) { 0072 PieceType pType = Queen; 0073 if ( typeChar == QLatin1Char ( 'N' ) || typeChar == QLatin1Char ( 'n' ) ) 0074 pType = Knight; 0075 else if ( typeChar == QLatin1Char ( 'R' ) || typeChar == QLatin1Char ( 'r' ) ) 0076 pType = Rook; 0077 else if ( typeChar == QLatin1Char ( 'B' ) || typeChar == QLatin1Char ( 'b' ) ) 0078 pType = Bishop; 0079 else if ( typeChar == QLatin1Char ( 'P' ) || typeChar == QLatin1Char ( 'p' ) ) 0080 pType = Pawn; 0081 else if ( typeChar == QLatin1Char ( 'K' ) || typeChar == QLatin1Char ( 'k' ) ) 0082 pType = King; 0083 return pType; 0084 } 0085 0086 Piece::Piece ( KGameGraphicsViewRenderer* renderer, PieceType type, Color color, QGraphicsScene* scene, Pos boardPos, QGraphicsItem* parent ) : 0087 Item ( renderer, spriteKey ( type, color ), scene, boardPos, parent ) { 0088 m_color = color; 0089 m_type = type; 0090 } 0091 0092 Piece::~Piece() = default; 0093 0094 Color oppositeColor ( Color color ) { 0095 switch ( color ) { 0096 case Black: 0097 return White; 0098 case White: 0099 return Black; 0100 default: 0101 return color; 0102 } 0103 } 0104 0105 QString colorName ( Color color ) { 0106 switch ( color ) { 0107 case White: 0108 return i18n ( "White" ); 0109 case Black: 0110 return i18n ( "Black" ); 0111 default: 0112 return QString(); 0113 } 0114 } 0115 0116 QString pieceTypeName ( PieceType type ) { 0117 switch ( type ) { 0118 case Pawn: 0119 return i18n ( "Pawn" ); 0120 case Rook: 0121 return i18n ( "Rook" ); 0122 case Knight: 0123 return i18n ( "Knight" ); 0124 case Bishop: 0125 return i18n ( "Bishop" ); 0126 case Queen: 0127 return i18n ( "Queen" ); 0128 case King: 0129 return i18n ( "King" ); 0130 default: 0131 return QString(); 0132 } 0133 } 0134 0135 Color Piece::color() { 0136 return m_color; 0137 } 0138 0139 PieceType Piece::pieceType() { 0140 return m_type; 0141 } 0142 0143 void Piece::setPieceType ( PieceType type ) { 0144 m_type = type; 0145 updateSpriteKey(); 0146 } 0147 0148 void Piece::updateSpriteKey() { 0149 setSpriteKey ( spriteKey ( m_type, m_color ) ); 0150 update(); 0151 } 0152 0153 } 0154 0155 #include "moc_piece.cpp"