Manage playing cards from C++ only

This commit is contained in:
ItsDrike 2024-12-01 19:43:49 +01:00
parent bfa876f87f
commit e3242b71e9
Signed by: ItsDrike
GPG key ID: FA2745890B7048C0
6 changed files with 106 additions and 13 deletions

View file

@ -21,7 +21,8 @@ qt_add_qml_module(appSolitare
QML_FILES
Main.qml
QML_FILES ScoreBar.qml
QML_FILES PlayingCard.qml
QML_FILES CardModel.qml
SOURCES playingcard.h playingcard.cpp
)
# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.

View file

@ -1,24 +1,24 @@
import Solitare
import QtQuick
Item {
id: playingCard
id: cardModel
width: 100
height: width * 1.4 // Maintian the aspect ratio of a playing card
required property PlayingCard card;
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.
required property string color
required property string value
property string backStyle: "red"
property bool isFaceDown: false
Image {
id: cardImage
anchors.fill: parent
source: playingCard.isFaceDown
? "qrc:/img/playing_cards/backs/" + playingCard.backStyle + ".svg"
: "qrc:/img/playing_cards/fronts/" + playingCard.color + "_" + playingCard.value + ".svg"
source: cardModel.card.isFaceDown
? "qrc:/img/playing_cards/backs/" + cardModel.backStyle + ".svg"
: "qrc:/img/playing_cards/fronts/" + cardModel.card.color + "_" + cardModel.card.value + ".svg"
fillMode: Image.PreserveAspectFit
}
}

View file

@ -1,3 +1,4 @@
import Solitare
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
@ -17,9 +18,8 @@ ApplicationWindow {
moves: 64
}
PlayingCard {
color: "clubs"
value: "ace"
isFaceDown: true
CardModel {
anchors.centerIn: parent
card: myCard
}
}

View file

@ -1,5 +1,7 @@
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "playingcard.h"
int main(int argc, char *argv[])
{
@ -12,6 +14,12 @@ int main(int argc, char *argv[])
&app,
[]() { QCoreApplication::exit(-1); },
Qt::QueuedConnection);
qmlRegisterUncreatableType<PlayingCard>("Solitare", 1, 0, "PlayingCard", "PlayingCard cannot be directly created in QML. Use C++ logic to instantiate.");
PlayingCard myCard("clubs", "ace");
engine.rootContext()->setContextProperty("myCard", &myCard);
engine.loadFromModule("Solitare", "Main");
return app.exec();

47
playingcard.cpp Normal file
View file

@ -0,0 +1,47 @@
#include "playingcard.h"
PlayingCard::PlayingCard(const QString &color, const QString &value, bool isFaceDown, QObject *parent)
: QObject{parent}, m_color{color}, m_value{value}, m_isFaceDown{isFaceDown}
{ }
QString PlayingCard::color() const
{
return m_color;
}
void PlayingCard::setColor(const QString &color)
{
if (m_color == color)
return;
m_color = color;
emit onColorChanged();
}
QString PlayingCard::value() const
{
return m_value;
}
void PlayingCard::setValue(const QString &value)
{
if (m_value == value)
return;
m_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();
}

37
playingcard.h Normal file
View file

@ -0,0 +1,37 @@
#ifndef PLAYINGCARD_H
#define PLAYINGCARD_H
#include <QObject>
class PlayingCard : public QObject
{
Q_OBJECT
Q_PROPERTY(QString color READ color WRITE setColor NOTIFY onColorChanged REQUIRED)
Q_PROPERTY(QString value READ value WRITE setValue NOTIFY onValueChanged REQUIRED)
Q_PROPERTY(bool isFaceDown READ isFaceDown WRITE setIsFaceDown NOTIFY onIsFaceDownChanged)
public:
explicit PlayingCard(const QString &color = "", const QString &value = "", bool isFaceDown = false, QObject *parent = nullptr);
QString color() const;
void setColor(const QString &color);
QString value() const;
void setValue(const QString &value);
bool isFaceDown() const;
void setIsFaceDown(bool faceDown);
signals:
void onColorChanged();
void onValueChanged();
void onIsFaceDownChanged();
private:
QString m_color;
QString m_value;
bool m_isFaceDown;
QString m_imgUrl;
};
#endif // PLAYINGCARD_H