/*************************************************************************** * 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 "piecelibrary.h" #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]); m_pieces[i]->render(&p); } } void PieceLibrary::paint(QPainter &pt, int place) const { if (place == 0) return; if (place < 0) place = 6 - place; if (place <= 0 || place > 12) return; pt.drawImage(0, 0, *m_buffers[place-1]); } void PieceLibrary::paint(QPainter &pt, int place, const QRect &rect) const { if (place == 0) return; if (place < 0) place = 6 - place; if (place <= 0 || place > 12) return; pt.drawImage(rect, *m_buffers[place-1]); } }