solitare/playingcard.h
ItsDrike 883d766dc3
Register QObject classes as QML elements via macros
Using manual registration with qmlRegisterType (or
qmlRegisterUncreatableType) isn't ideal, because Qt Creator can't pick
up on it. When using the macros, autocompletion & unknown type warnings
in the code disappear.
2024-12-03 15:05:30 +01:00

69 lines
1.5 KiB
C++

#ifndef PLAYINGCARD_H
#define PLAYINGCARD_H
#include <QObject>
#include <qqmlintegration.h>
class PlayingCard : public QObject
{
Q_OBJECT
QML_ELEMENT
QML_UNCREATABLE("Use C++ logic to instantiate")
Q_ENUMS(Value)
Q_ENUMS(Suit)
Q_PROPERTY(Suit suit READ suit WRITE setSuit NOTIFY suitChanged REQUIRED)
Q_PROPERTY(Value value READ value WRITE setValue NOTIFY valueChanged REQUIRED)
Q_PROPERTY(QString valueString READ valueString NOTIFY valueChanged)
Q_PROPERTY(QString suitString READ suitString NOTIFY suitChanged)
public:
enum Value {
Ace = 1,
Two = 2,
Three = 3,
Four = 4,
Five = 5,
Six = 6,
Seven = 7,
Eight = 8,
Nine = 9,
Ten = 10,
Jack = 11,
Queen = 12,
King = 13,
};
enum Suit {
Clubs,
Diamonds,
Hearts,
Spades,
};
explicit PlayingCard(const Suit &suit = Suit::Clubs, const Value &value = Value::Ace, QObject *parent = nullptr);
Suit suit() const;
QString suitString() const;
void setSuit(const Suit &suit);
Value value() const;
QString valueString() const;
void setValue(const Value &value);
static QList<PlayingCard*> createDeck();
static bool areOppositeColors(const PlayingCard &card1, const PlayingCard &card2);
signals:
void suitChanged();
void valueChanged();
private:
Suit m_suit;
Value m_value;
};
#endif // PLAYINGCARD_H