2020#include " cardproxy.h"
2121#include " board.h"
2222
23+ #include < QPropertyAnimation>
24+
2325#include < iostream>
2426#include < sstream>
2527
@@ -30,6 +32,7 @@ Card::Card(cardcolor color, cardvalue value, Board* board) : AbstractCardHolder(
3032 mValue = value;
3133 mBoard = board;
3234 mIsOnAceSpot = false ;
35+ mSelected = false ;
3336
3437 mWidget = new CardWidget ();
3538 mWidget ->setText (getLabel ());
@@ -56,15 +59,15 @@ QPixmap Card::getWidgetPixmap()
5659 return mWidget ->grab ();
5760}
5861
59- void Card::setParent (AbstractCardHolder* parent)
62+ void Card::setParent (AbstractCardHolder* parent, bool animate )
6063{
6164 if (mParent ) {
6265 mParent ->setChild (0 );
6366 }
6467 mParent = parent;
6568 if (mParent ) {
6669 mParent ->setChild (this );
67- updatePosition ();
70+ updatePosition (animate );
6871 mBoard ->unselectCard ();
6972 }
7073}
@@ -203,6 +206,24 @@ QPoint Card::getPosition()
203206 return mPosition ;
204207}
205208
209+ void Card::animatePosition (QPoint pos)
210+ {
211+ mPosition = pos;
212+ setZIndex (100 );
213+
214+ QPropertyAnimation *animation = new QPropertyAnimation (mWidget , " pos" );
215+ animation->setDuration (100 );
216+ animation->setStartValue (mWidget ->pos ());
217+ animation->setEndValue (mPosition );
218+ animation->start (QAbstractAnimation::DeleteWhenStopped);
219+
220+ QObject::connect (animation, SIGNAL (finished ()), this , SLOT (resetZIndex ()));
221+
222+ if (mChild ) {
223+ mChild ->updatePosition (true );
224+ }
225+ }
226+
206227void Card::setPosition (QPoint pos)
207228{
208229 mPosition = pos;
@@ -212,10 +233,14 @@ void Card::setPosition(QPoint pos)
212233 }
213234}
214235
215- void Card::updatePosition ()
236+ void Card::updatePosition (bool animate )
216237{
217- setPosition (mParent ->getChildPosition ());
218- setZIndex (mParent ->getZIndex () + 1 );
238+ if (animate) {
239+ animatePosition (mParent ->getChildPosition ());
240+ } else {
241+ setPosition (mParent ->getChildPosition ());
242+ setZIndex (mParent ->getZIndex () + 1 );
243+ }
219244}
220245
221246int Card::getTopZIndex ()
@@ -239,6 +264,11 @@ void Card::setZIndex(int index, bool cascade)
239264 }
240265}
241266
267+ void Card::resetZIndex ()
268+ {
269+ setZIndex (mParent ->getZIndex () + 1 );
270+ }
271+
242272void Card::show ()
243273{
244274 mWidget ->show ();
@@ -256,13 +286,19 @@ void Card::select()
256286
257287void Card::setSelected (bool selected)
258288{
259- if (selected) {
289+ mSelected = selected;
290+ if (mSelected ) {
260291 mWidget ->setStyleSheet (" CardWidget {background-color:white;border: 2px solid yellow;border-radius:5px;}" );
261292 } else {
262293 mWidget ->setStyleSheet (" CardWidget {background-color:white;border: 2px solid black;border-radius:5px;}" );
263294 }
264295}
265296
297+ bool Card::isSelected ()
298+ {
299+ return mSelected ;
300+ }
301+
266302void Card::automaticMove ()
267303{
268304 mBoard ->automaticMove (this );
0 commit comments