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#include "dealer.h"
22#include "renderer.h"
23
24
25PatPile::PatPile( DealerScene * scene, int index, const QString & objectName )
26 : KCardPile( scene ),
27 m_index( index ),
28 m_role( NoRole )
29{
30 if ( objectName.isEmpty() )
31 setObjectName( QString(QLatin1String("pile%1" )).arg( m_index ) );
32 else
33 setObjectName( objectName );
34
35 // Set the default spread for all piles in KPat.
36 setSpread( 0, 0.33 );
37
38 if ( scene )
39 scene->addPatPile( this );
40}
41
42
43PatPile::~PatPile()
44{
45 DealerScene * dealerScene = dynamic_cast<DealerScene*>( scene() );
46 if ( dealerScene )
47 dealerScene->removePatPile( this );
48}
49
50
51int PatPile::index() const
52{
53 return m_index;
54}
55
56
57void PatPile::setPileRole( PileRole role )
58{
59 m_role = role;
60}
61
62
63PatPile::PileRole PatPile::pileRole() const
64{
65 return m_role;
66}
67
68
69bool PatPile::isFoundation() const
70{
71 return FoundationType1 <= m_role && m_role <= FoundationType4;
72}
73
74
75QList< QPointF > PatPile::cardPositions() const
76{
77 QList<QPointF> positions;
78 QPointF currentPosition( 0, 0 );
79 foreach( KCard * c, cards() )
80 {
81 positions << currentPosition;
82 qreal adjustment = c->isFaceUp() ? 1 : 0.6;
83 currentPosition += adjustment * spread();
84 }
85 return positions;
86}
87
88
89void PatPile::paintGraphic( QPainter * painter, qreal highlightedness )
90{
91 const QSize size = boundingRect().size().toSize();
92 Renderer * r = Renderer::self();
93
94 if ( highlightedness < 1 )
95 painter->drawPixmap( 0, 0, r->spritePixmap( "pile", size ) );
96
97 if ( highlightedness > 0 )
98 {
99 if ( highlightedness < 1 )
100 {
101 // Using QPainter::setOpacity is currently very inefficient, so to
102 // paint a semitransparent pixmap, we have to do some fiddling.
103 QPixmap transPix( size );
104 transPix.fill( Qt::transparent );
105 QPainter p( &transPix );
106 p.drawPixmap( 0, 0, r->spritePixmap( "pile_selected", size ) );
107 p.setCompositionMode( QPainter::CompositionMode_DestinationIn );
108 p.fillRect( transPix.rect(), QColor( 0, 0, 0, highlightedness * 255 ) );
109 painter->drawPixmap( 0, 0, transPix );
110 }
111 else
112 {
113 painter->drawPixmap( 0, 0, r->spritePixmap( "pile_selected", size ) );
114 }
115 }
116}
117
118