From c3e3f4e53fadabdc90ac01940d492941511c1676 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Delobel?= Date: Thu, 27 Mar 2025 15:02:22 +0100 Subject: [PATCH] refactor flag --- 2024/idees_tp/flags/Country.qml | 5 ++-- 2024/idees_tp/flags/CountryDetail.qml | 35 +++++++++++++++++++++++++++ 2024/idees_tp/flags/country.cpp | 11 +++++++++ 2024/idees_tp/flags/country.h | 8 ++++++ 2024/idees_tp/flags/main.cpp | 1 + 2024/idees_tp/flags/main.qml | 25 ++++--------------- 2024/idees_tp/flags/qml.qrc | 1 + 7 files changed, 64 insertions(+), 22 deletions(-) create mode 100644 2024/idees_tp/flags/CountryDetail.qml diff --git a/2024/idees_tp/flags/Country.qml b/2024/idees_tp/flags/Country.qml index 365b734..d069c7c 100644 --- a/2024/idees_tp/flags/Country.qml +++ b/2024/idees_tp/flags/Country.qml @@ -1,4 +1,5 @@ import QtQuick 2.15 +import I.hate.dictators 1.0 Rectangle { width: lv.width @@ -6,6 +7,7 @@ Rectangle { required property string name required property string iso + required property CountryType self Text { @@ -25,8 +27,7 @@ Rectangle { MouseArea { anchors.fill: parent onClicked: { - choosenText.text = name - choosenFlag.source = flag.source + mainWindow.selectedCountry = self } } } diff --git a/2024/idees_tp/flags/CountryDetail.qml b/2024/idees_tp/flags/CountryDetail.qml new file mode 100644 index 0000000..af9a0ac --- /dev/null +++ b/2024/idees_tp/flags/CountryDetail.qml @@ -0,0 +1,35 @@ +import QtQuick 2.15 +import I.hate.dictators 1.0 + +Rectangle { + required property CountryType country + + color: "lightgrey" + + Column { + width: parent.width + Image { + id: choosenFlag + source: country?'qrc:/assets/flags/'+country.iso+'.svg':'' + fillMode: Image.PreserveAspectFit + width: parent.width + } + + Text { + id: nameText + text: country ? country.name : "" + font.pixelSize: 50 + anchors.horizontalCenter: parent.horizontalCenter + } + Text { + text: country ? country.capital : "" + font.pixelSize: nameText.font.pixelSize + anchors.horizontalCenter: parent.horizontalCenter + } + Text { + text: country ? country.region : "" + font.pixelSize: nameText.font.pixelSize + anchors.horizontalCenter: parent.horizontalCenter + } + } +} diff --git a/2024/idees_tp/flags/country.cpp b/2024/idees_tp/flags/country.cpp index 8c8de94..35cc303 100644 --- a/2024/idees_tp/flags/country.cpp +++ b/2024/idees_tp/flags/country.cpp @@ -1,5 +1,9 @@ #include "country.h" +Country::Country() +{ +} + Country::Country(QString name, QString capital, QString iso, QString region, QString alt, QObject *object) : QObject{object}, m_name{name}, @@ -73,3 +77,10 @@ void Country::setAlt(const QString &newAlt) m_alt = newAlt; emit altChanged(); } + +Country* Country::self() +{ + return this; +} + +void Country::setSelf(const Country *){} diff --git a/2024/idees_tp/flags/country.h b/2024/idees_tp/flags/country.h index f025668..4ffc486 100644 --- a/2024/idees_tp/flags/country.h +++ b/2024/idees_tp/flags/country.h @@ -13,9 +13,12 @@ class Country : public QObject Q_PROPERTY(QString iso READ iso WRITE setIso NOTIFY isoChanged FINAL) Q_PROPERTY(QString region READ region WRITE setRegion NOTIFY regionChanged FINAL) Q_PROPERTY(QString alt READ alt WRITE setAlt NOTIFY altChanged FINAL) + Q_PROPERTY(Country* self READ self WRITE setSelf NOTIFY selfChanged FINAL) + public: + Country(); Country(QString name, QString capital, QString iso, QString region, QString alt, QObject *object = nullptr); QString capital() const; @@ -32,6 +35,9 @@ public: QString alt() const; void setAlt(const QString &newAlt); + Country *self(); + void setSelf(const Country*);; + signals: void capitalChanged(); void nameChanged(); @@ -42,6 +48,8 @@ signals: void altChanged(); + void selfChanged(); + private: QString m_name; QString m_capital; diff --git a/2024/idees_tp/flags/main.cpp b/2024/idees_tp/flags/main.cpp index 58cb2dd..29f8bc9 100644 --- a/2024/idees_tp/flags/main.cpp +++ b/2024/idees_tp/flags/main.cpp @@ -29,6 +29,7 @@ int main(int argc, char *argv[]) for ( auto cr : countries) cc.append(static_cast(cr)); engine.rootContext()->setContextProperty("countries", QVariant::fromValue(cc)); + qmlRegisterType("I.hate.dictators",1,0,"CountryType" ); engine.load(url); diff --git a/2024/idees_tp/flags/main.qml b/2024/idees_tp/flags/main.qml index 380d7fc..a687360 100644 --- a/2024/idees_tp/flags/main.qml +++ b/2024/idees_tp/flags/main.qml @@ -1,11 +1,14 @@ import QtQuick 2.15 import QtQuick.Window 2.15 +import I.hate.dictators 1.0 Window { width: 640 height: 480 visible: true title: qsTr("Flags of the world") + id: mainWindow + property CountryType selectedCountry ListView { id: lv @@ -14,31 +17,13 @@ Window { width: parent.width / 3 model: countries delegate: Country { - } } - Rectangle { + CountryDetail { anchors.right: parent.right anchors.left: lv.right height: parent.height - color: "lightgrey" - - Column { - width: parent.width - Image { - id: choosenFlag - - fillMode: Image.PreserveAspectFit - width: parent.width - } - - Text { - id: choosenText - text: "" - font.pixelSize: 50 - anchors.horizontalCenter: parent.horizontalCenter - } - } + country: selectedCountry } } diff --git a/2024/idees_tp/flags/qml.qrc b/2024/idees_tp/flags/qml.qrc index 2b6dc06..5e049d3 100644 --- a/2024/idees_tp/flags/qml.qrc +++ b/2024/idees_tp/flags/qml.qrc @@ -204,5 +204,6 @@ assets/map.svg assets/map.svg.txt Country.qml + CountryDetail.qml