File indexing completed on 2024-05-12 04:04:51

0001 /*
0002  *  Copyright (C) 2010 Parker Coates <coates@kde.org>
0003  *
0004  *  This program is free software; you can redistribute it and/or
0005  *  modify it under the terms of the GNU General Public License as
0006  *  published by the Free Software Foundation; either version 2 of
0007  *  the License, or (at your option) any later version.
0008  *
0009  *  This program is distributed in the hope that it will be useful,
0010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  *  GNU General Public License for more details.
0013  *
0014  *  You should have received a copy of the GNU General Public License
0015  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
0016  *
0017  */
0018 
0019 #include "patpile.h"
0020 
0021 // own
0022 #include "dealer.h"
0023 #include "renderer.h"
0024 
0025 PatPile::PatPile(DealerScene *scene, int index, const QString &objectName)
0026     : KCardPile(scene)
0027     , m_index(index)
0028     , m_role(NoRole)
0029 {
0030     if (objectName.isEmpty())
0031         setObjectName(QStringLiteral("pile%1").arg(m_index));
0032     else
0033         setObjectName(objectName);
0034 
0035     // Set the default spread for all piles in KPat.
0036     setSpread(0, 0.33);
0037 
0038     if (scene)
0039         scene->addPatPile(this);
0040 }
0041 
0042 PatPile::~PatPile()
0043 {
0044     DealerScene *dealerScene = dynamic_cast<DealerScene *>(scene());
0045     if (dealerScene)
0046         dealerScene->removePatPile(this);
0047 }
0048 
0049 int PatPile::index() const
0050 {
0051     return m_index;
0052 }
0053 
0054 void PatPile::setPileRole(PileRole role)
0055 {
0056     m_role = role;
0057 }
0058 
0059 PatPile::PileRole PatPile::pileRole() const
0060 {
0061     return m_role;
0062 }
0063 
0064 bool PatPile::isFoundation() const
0065 {
0066     return FoundationType1 <= m_role && m_role <= FoundationType4;
0067 }
0068 
0069 QList<QPointF> PatPile::cardPositions() const
0070 {
0071     QList<QPointF> positions;
0072     QPointF currentPosition(0, 0);
0073     const auto cards = this->cards();
0074     for (KCard *c : cards) {
0075         positions << currentPosition;
0076         qreal adjustment = c->isFaceUp() ? 1 : 0.6;
0077         currentPosition += adjustment * spread();
0078     }
0079     return positions;
0080 }
0081 
0082 void PatPile::paintGraphic(QPainter *painter, qreal highlightedness)
0083 {
0084     const QSize size = boundingRect().size().toSize();
0085     Renderer *r = Renderer::self();
0086 
0087     if (highlightedness < 1)
0088         painter->drawPixmap(0, 0, r->spritePixmap(QStringLiteral("pile"), size));
0089 
0090     if (highlightedness > 0) {
0091         if (highlightedness < 1) {
0092             // Using QPainter::setOpacity is currently very inefficient, so to
0093             // paint a semitransparent pixmap, we have to do some fiddling.
0094             QPixmap transPix(size);
0095             transPix.fill(Qt::transparent);
0096             QPainter p(&transPix);
0097             p.drawPixmap(0, 0, r->spritePixmap(QStringLiteral("pile_selected"), size));
0098             p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
0099             p.fillRect(transPix.rect(), QColor(0, 0, 0, highlightedness * 255));
0100             painter->drawPixmap(0, 0, transPix);
0101         } else {
0102             painter->drawPixmap(0, 0, r->spritePixmap(QStringLiteral("pile_selected"), size));
0103         }
0104     }
0105 }