From 204d5ef3db06d6273c805ab185bebfa3c992b778 Mon Sep 17 00:00:00 2001 From: Yanis Dziki <yanis.dziki@ens-lyon.fr> Date: Fri, 11 Apr 2025 20:02:31 +0200 Subject: [PATCH] insch on avance --- motus/CMakeLists.txt | 1 + motus/GameWindow.qml | 34 ++++++++++++++++++++++++++++------ motus/jeu.cpp | 14 ++++++++++++++ motus/jeu.h | 4 ++++ motus/lettermodel.cpp | 36 ++++++++++++++++++++++++++++++++++++ motus/lettermodel.h | 25 +++++++++++++++++++++++++ motus/main.cpp | 7 +++++++ 7 files changed, 115 insertions(+), 6 deletions(-) create mode 100644 motus/lettermodel.cpp create mode 100644 motus/lettermodel.h diff --git a/motus/CMakeLists.txt b/motus/CMakeLists.txt index 47d03d0..7a913d9 100644 --- a/motus/CMakeLists.txt +++ b/motus/CMakeLists.txt @@ -38,6 +38,7 @@ qt_add_qml_module(appmotus SOURCES grillemanager.h grillemanager.cpp QML_FILES Case.qml QML_FILES Grille.qml + SOURCES lettermodel.h lettermodel.cpp ) # Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1. diff --git a/motus/GameWindow.qml b/motus/GameWindow.qml index eb6f17e..79a1368 100644 --- a/motus/GameWindow.qml +++ b/motus/GameWindow.qml @@ -1,10 +1,5 @@ - - - import QtQuick - - - +import QtQuick.Controls Item { width: 900 @@ -15,6 +10,33 @@ Item { color: "#ffffff" anchors.fill: parent + Grid { + id: letterGrid + columns: 8 + rows: 5 + spacing: 4 + anchors.centerIn: parent + width: 600 + height: 400 + + Repeater { + model: letterModel // <-- C++ ListModel exposé ici + delegate: Rectangle { + width: 60 + height: 60 + color: "#dddddd" + border.color: "#888" + radius: 5 + + Text { + anchors.centerIn: parent + text: model.letter + font.pixelSize: 24 + } + } + } + } + Clavier { id: clavier x: 225 diff --git a/motus/jeu.cpp b/motus/jeu.cpp index 1aa63c6..0ab0812 100644 --- a/motus/jeu.cpp +++ b/motus/jeu.cpp @@ -24,6 +24,16 @@ void Jeu::setContext(QQmlComponent *grilleComponent, QQuickItem* parentItem) { } void Jeu::onClavierClick(QString lettre) { + + if (!m_letterModel) + return; + + if (currentIndex < 40) { // on évite de dépasser la grille + m_letterModel->setLetter(currentIndex, lettre); + currentIndex++; + } + + brain->entreLettre(lettre.toStdString()[0]); } @@ -43,3 +53,7 @@ void Jeu::startGame() { // grilleManager.createGrid(6, 5); // à synchroniser avec les paramètres du brain } + +void Jeu::setLetterModel(LetterModel* model) { + m_letterModel = model; +} diff --git a/motus/jeu.h b/motus/jeu.h index a13350f..b5fb357 100644 --- a/motus/jeu.h +++ b/motus/jeu.h @@ -6,6 +6,7 @@ #include <QQmlComponent> #include "brain.h" #include "grillemanager.h" +#include "lettermodel.h" class Jeu : public QObject { Q_OBJECT @@ -22,6 +23,7 @@ public: GrilleManager* getGrilleManager() { return &grilleManager; } QQuickItem* getGameWindow() const { return gameWindow; } // Getter for gameWindow void setContext(QQmlComponent *grilleComponent, QQuickItem* parentItem); + void setLetterModel(LetterModel* model); // Setter pour le modèle signals: @@ -34,6 +36,8 @@ private: GrilleManager grilleManager; QQmlComponent* grilleComponent; QQuickItem* gameWindow; // Pointer to the QQuickItem of the GameWindow + LetterModel* m_letterModel = nullptr; // Modèle QML + int currentIndex = 0; // Pour suivre où on écrit }; #endif // JEU_H diff --git a/motus/lettermodel.cpp b/motus/lettermodel.cpp new file mode 100644 index 0000000..bc63370 --- /dev/null +++ b/motus/lettermodel.cpp @@ -0,0 +1,36 @@ +#include "lettermodel.h" + +LetterModel::LetterModel(QObject *parent) + : QAbstractListModel(parent) +{ + // Initialiser 40 lettres avec "_" + m_letters.fill("_", 40); +} + +int LetterModel::rowCount(const QModelIndex &parent) const { + Q_UNUSED(parent); + return m_letters.size(); +} + +QVariant LetterModel::data(const QModelIndex &index, int role) const { + if (!index.isValid() || index.row() < 0 || index.row() >= m_letters.size()) + return QVariant(); + + if (role == LetterRole) + return m_letters.at(index.row()); + + return QVariant(); +} + +QHash<int, QByteArray> LetterModel::roleNames() const { + return { + { LetterRole, "letter" } + }; +} + +void LetterModel::setLetter(int index, const QString &value) { + if (index >= 0 && index < m_letters.size()) { + m_letters[index] = value; + emit dataChanged(this->index(index), this->index(index), { LetterRole }); + } +} diff --git a/motus/lettermodel.h b/motus/lettermodel.h new file mode 100644 index 0000000..fe90feb --- /dev/null +++ b/motus/lettermodel.h @@ -0,0 +1,25 @@ +#pragma once + +#include <QAbstractListModel> +#include <QString> +#include <QVector> + +class LetterModel : public QAbstractListModel { + Q_OBJECT + +public: + enum Roles { + LetterRole = Qt::UserRole + 1 + }; + + explicit LetterModel(QObject *parent = nullptr); + + int rowCount(const QModelIndex &parent = QModelIndex()) const override; + QVariant data(const QModelIndex &index, int role) const override; + QHash<int, QByteArray> roleNames() const override; + + Q_INVOKABLE void setLetter(int index, const QString &value); + +private: + QVector<QString> m_letters; +}; diff --git a/motus/main.cpp b/motus/main.cpp index 7a39861..b8a1926 100644 --- a/motus/main.cpp +++ b/motus/main.cpp @@ -6,6 +6,7 @@ #include "jeu.h" #include "vraiecase.h" #include "grillemanager.h" +#include "lettermodel.h" int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); @@ -17,6 +18,9 @@ int main(int argc, char *argv[]) { // Charger l'interface utilisateur (QML) + LetterModel letterModel; + engine.rootContext()->setContextProperty("letterModel", &letterModel); + engine.loadFromModule("motus", "Main"); QObject *rootObject = engine.rootObjects().first(); // Gets the first root object QQuickItem *rootItem = qobject_cast<QQuickItem *>(rootObject); @@ -26,6 +30,7 @@ int main(int argc, char *argv[]) { QQuickItem *parentItem = rootItem; // Créer l'objet Jeu sans mot Jeu jeu; + jeu.setLetterModel(&letterModel); engine.rootContext()->setContextProperty("jeu", &jeu); jeu.setContext(&grilleComponent,parentItem); @@ -33,3 +38,5 @@ int main(int argc, char *argv[]) { return app.exec(); } + + -- GitLab