Make isFaceDown visual property only (pure QML)

This commit is contained in:
ItsDrike 2024-12-01 22:36:01 +01:00
parent 868699979d
commit 6ad3fe5bba
Signed by: ItsDrike
GPG key ID: FA2745890B7048C0
4 changed files with 6 additions and 30 deletions

View file

@ -7,16 +7,13 @@ Item {
height: width * 1.4 // Maintian the aspect ratio of a playing card
required property PlayingCard card;
required property bool isFaceDown;
property string backStyle: "red"
// Annoyingly, there is no easy way to make this type-safe, QML does have enums
// but they only act as ints, and since we need the string names for the img paths
// anyways, typing these as simple strings is the easiest way to do this.
Image {
id: cardImage
anchors.fill: parent
source: cardModel.card.isFaceDown
source: cardModel.isFaceDown
? "qrc:/img/playing_cards/backs/" + cardModel.backStyle + ".svg"
: "qrc:/img/playing_cards/fronts/" + cardModel.card.suitString + "_" + cardModel.card.valueString + ".svg"
fillMode: Image.PreserveAspectFit

View file

@ -21,5 +21,6 @@ ApplicationWindow {
CardModel {
anchors.centerIn: parent
card: myCard
isFaceDown: false
}
}

View file

@ -1,7 +1,7 @@
#include "playingcard.h"
PlayingCard::PlayingCard(const PlayingCard::Suit &suit, const PlayingCard::Value &value, bool isFaceDown, QObject *parent)
: QObject{parent}, m_suit{suit}, m_value{value}, m_isFaceDown{isFaceDown}
PlayingCard::PlayingCard(const PlayingCard::Suit &suit, const PlayingCard::Value &value, QObject *parent)
: QObject{parent}, m_suit{suit}, m_value{value}
{ }
PlayingCard::Suit PlayingCard::suit() const
@ -63,20 +63,6 @@ void PlayingCard::setValue(const PlayingCard::Value &value)
emit onValueChanged();
}
bool PlayingCard::isFaceDown() const
{
return m_isFaceDown;
}
void PlayingCard::setIsFaceDown(bool faceDown)
{
if (m_isFaceDown == faceDown)
return;
m_isFaceDown = faceDown;
emit onIsFaceDownChanged();
}
QList<PlayingCard *> PlayingCard::createDeck()
{
QList<PlayingCard*> deck;
@ -86,7 +72,6 @@ QList<PlayingCard *> PlayingCard::createDeck()
PlayingCard *card = new PlayingCard();
card->setSuit(static_cast<PlayingCard::Suit>(suitIndex));
card->setValue(static_cast<PlayingCard::Value>(valueIndex));
card->setIsFaceDown(true); // All cards are face down initially
deck.append(card);
}
}

View file

@ -12,7 +12,6 @@ class PlayingCard : public QObject
Q_PROPERTY(Suit suit READ suit WRITE setSuit NOTIFY onSuitChanged REQUIRED)
Q_PROPERTY(Value value READ value WRITE setValue NOTIFY onValueChanged REQUIRED)
Q_PROPERTY(bool isFaceDown READ isFaceDown WRITE setIsFaceDown NOTIFY onIsFaceDownChanged)
Q_PROPERTY(QString valueString READ valueString NOTIFY onValueChanged)
Q_PROPERTY(QString suitString READ suitString NOTIFY onSuitChanged)
@ -41,7 +40,7 @@ public:
Spades,
};
explicit PlayingCard(const Suit &suit = Suit::Clubs, const Value &value = Value::Ace, bool isFaceDown = false, QObject *parent = nullptr);
explicit PlayingCard(const Suit &suit = Suit::Clubs, const Value &value = Value::Ace, QObject *parent = nullptr);
Suit suit() const;
QString suitString() const;
@ -51,21 +50,15 @@ public:
QString valueString() const;
void setValue(const Value &value);
bool isFaceDown() const;
void setIsFaceDown(bool faceDown);
static QList<PlayingCard*> createDeck();
signals:
void onSuitChanged();
void onValueChanged();
void onIsFaceDownChanged();
private:
Suit m_suit;
Value m_value;
bool m_isFaceDown;
QString m_imgUrl;
};
#endif // PLAYINGCARD_H