1 /*
2  *  Copyright (C) 2010 Parker Coates <coates@kde.org>
3  *
4  *  This program is free software; you can redistribute it and/or
5  *  modify it under the terms of the GNU General Public License as
6  *  published by the Free Software Foundation; either version 2 of
7  *  the License, or (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  */
18 
19 #include "patpile.h"
20 
21 // own
22 #include "dealer.h"
23 #include "renderer.h"
24 
25 
PatPile(DealerScene * scene,int index,const QString & objectName)26 PatPile::PatPile( DealerScene * scene, int index, const QString & objectName )
27   : KCardPile( scene ),
28     m_index( index ),
29     m_role( NoRole )
30 {
31     if ( objectName.isEmpty() )
32         setObjectName( QStringLiteral("pile%1" ).arg( m_index ) );
33     else
34         setObjectName( objectName );
35 
36     // Set the default spread for all piles in KPat.
37     setSpread( 0, 0.33 );
38 
39     if ( scene )
40         scene->addPatPile( this );
41 }
42 
43 
~PatPile()44 PatPile::~PatPile()
45 {
46     DealerScene * dealerScene = dynamic_cast<DealerScene*>( scene() );
47     if ( dealerScene )
48         dealerScene->removePatPile( this );
49 }
50 
51 
index() const52 int PatPile::index() const
53 {
54     return m_index;
55 }
56 
57 
setPileRole(PileRole role)58 void PatPile::setPileRole( PileRole role )
59 {
60     m_role = role;
61 }
62 
63 
pileRole() const64 PatPile::PileRole PatPile::pileRole() const
65 {
66     return m_role;
67 }
68 
69 
isFoundation() const70 bool PatPile::isFoundation() const
71 {
72     return FoundationType1 <= m_role && m_role <= FoundationType4;
73 }
74 
75 
cardPositions() const76 QList< QPointF > PatPile::cardPositions() const
77 {
78     QList<QPointF> positions;
79     QPointF currentPosition( 0, 0 );
80     const auto cards = this->cards();
81     for( KCard * c : cards) {
82         positions << currentPosition;
83         qreal adjustment = c->isFaceUp() ? 1 : 0.6;
84         currentPosition += adjustment * spread();
85     }
86     return positions;
87 }
88 
89 
paintGraphic(QPainter * painter,qreal highlightedness)90 void PatPile::paintGraphic( QPainter * painter, qreal highlightedness )
91 {
92     const QSize size = boundingRect().size().toSize();
93     Renderer * r = Renderer::self();
94 
95     if ( highlightedness < 1 )
96         painter->drawPixmap( 0, 0, r->spritePixmap( QStringLiteral("pile"), size ) );
97 
98     if ( highlightedness > 0 )
99     {
100         if ( highlightedness < 1 )
101         {
102             // Using QPainter::setOpacity is currently very inefficient, so to
103             // paint a semitransparent pixmap, we have to do some fiddling.
104             QPixmap transPix( size );
105             transPix.fill( Qt::transparent );
106             QPainter p( &transPix );
107             p.drawPixmap( 0, 0, r->spritePixmap( QStringLiteral("pile_selected"), size ) );
108             p.setCompositionMode( QPainter::CompositionMode_DestinationIn );
109             p.fillRect( transPix.rect(), QColor( 0, 0, 0, highlightedness * 255 ) );
110             painter->drawPixmap( 0, 0, transPix );
111         }
112         else
113         {
114             painter->drawPixmap( 0, 0, r->spritePixmap( QStringLiteral("pile_selected"), size ) );
115         }
116     }
117 }
118 
119