/*************************************************************************** * Copyright (C) 2009 by Stefan Bühler * * stbuehler@web.de * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "testgame.h" #include #include #include namespace toruschess { PieceLibrary::PieceLibrary(QObject *parent) : QObject(parent), m_pieces(0), m_buffers(0) { setSize(45, 45); } PieceLibrary::~PieceLibrary() { delete [] m_pieces; delete [] m_buffers; } void PieceLibrary::setSize(int width, int height) { m_width = qMax(10, width); m_height = qMax(10, height); if (m_buffers) { for (int i = 0; i < 12; i++) { delete m_buffers[i]; } } else { m_pieces = new QSvgRenderer* [12]; m_buffers = new QImage* [12]; for (int i = 0; i < 12; i++) { m_pieces[i] = new QSvgRenderer(QString(":/media/chess_%1.svg").arg(i+1), this); } } for (int i = 0; i < 12; i++) { m_buffers[i] = new QImage(QSize(m_width, m_height), QImage::Format_ARGB32_Premultiplied); m_buffers[i]->fill(0x0); QPainter p(m_buffers[i]); p.setViewport(1, 1, m_width-1, m_height-1); m_pieces[i]->render(&p); } } void PieceLibrary::paint(QPainter &pt, int piece) const { if (piece == 0) return; if (piece < 0) piece = 6 - piece; if (piece <= 0 || piece > 12) return; pt.drawImage(0, 0, *m_buffers[piece-1]); } TestGame::TestGame(QWidget *parent) : QMainWindow(parent), m_game(new Game()) { PieceLibrary *lib = new PieceLibrary(this); setCentralWidget(new TestField(this, lib, m_game)); } TestField::TestField(QWidget *parent, PieceLibrary *lib, Game *game) : QWidget(parent), m_lib(lib), m_game(game) { QGridLayout *layout = new QGridLayout(); for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x++) { layout->addWidget(new TestPlace(this, m_lib, m_game->field(), Pos(x, y)), y, x); } } layout->setSpacing(0); setLayout(layout); show(); } void TestField::resizeEvent(QResizeEvent *event) { m_lib->setSize(event->size().width()/8, event->size().height()/8); } TestPlace::TestPlace(QWidget *parent, const PieceLibrary *lib, const Field *field, const Pos &p) : QWidget(parent), m_field(field), m_lib(lib), m_pos(p) { setMinimumSize(m_lib->minPieceSize()); } QSize TestPlace::sizeHint() const { return m_lib->pieceSize(); } void TestPlace::paintEvent(QPaintEvent *) { QPainter pt(this); if ((m_pos.x() + m_pos.y()) % 2) { // #D18B47 pt.fillRect(this->rect(), QBrush(QColor(0xD1, 0x8B, 0x47))); } else { // #FFCE9E pt.fillRect(this->rect(), QBrush(QColor(0xFF, 0xCE, 0x9E))); } m_lib->paint(pt, m_field->place(m_pos)); } }