From ee897f17eff8f6cd6608510a7b6578aec15bd267 Mon Sep 17 00:00:00 2001 From: Felix Hammer Date: Thu, 13 Aug 2015 22:36:11 +0200 Subject: [PATCH] Put c++ data to QML the first time: YEAAAAHHHH ;-) --- pokerth_qml-client.pro | 6 +++-- src/gui/qml/cpp/qmlconfig.cpp | 19 +++++++++++++++ src/gui/qml/cpp/qmlconfig.h | 24 +++++++++++++++++++ src/gui/qml/cpp/qmlconfigwrapper.cpp | 19 --------------- src/gui/qml/cpp/qmlconfigwrapper.h | 19 --------------- src/gui/qml/cpp/qmlwrapper.cpp | 6 +++-- src/gui/qml/cpp/qmlwrapper.h | 2 ++ .../CreateLocalGameView.qml | 18 ++++++++++---- 8 files changed, 66 insertions(+), 47 deletions(-) create mode 100644 src/gui/qml/cpp/qmlconfig.cpp create mode 100644 src/gui/qml/cpp/qmlconfig.h delete mode 100644 src/gui/qml/cpp/qmlconfigwrapper.cpp delete mode 100644 src/gui/qml/cpp/qmlconfigwrapper.h diff --git a/pokerth_qml-client.pro b/pokerth_qml-client.pro index 4fd50043..af03b081 100644 --- a/pokerth_qml-client.pro +++ b/pokerth_qml-client.pro @@ -51,7 +51,8 @@ HEADERS += \ src/core/common/qttools/qthelper/qthelper.h \ src/core/common/qttools/qttoolswrapper.h \ src/core/common/qttoolsinterface.h \ - src/gui/qml/cpp/qmlwrapper.h + src/gui/qml/cpp/qmlwrapper.h \ + src/gui/qml/cpp/qmlconfig.h SOURCES += \ @@ -60,7 +61,8 @@ SOURCES += \ src/core/common/qttools/qthelper/qthelper.cpp \ src/core/common/qttools/qttoolswrapper.cpp \ src/core/common/qttoolsinterface.cpp \ - src/gui/qml/cpp/qmlwrapper.cpp + src/gui/qml/cpp/qmlwrapper.cpp \ + src/gui/qml/cpp/qmlconfig.cpp LIBS += \ -lpokerth_lib \ diff --git a/src/gui/qml/cpp/qmlconfig.cpp b/src/gui/qml/cpp/qmlconfig.cpp new file mode 100644 index 00000000..98434be4 --- /dev/null +++ b/src/gui/qml/cpp/qmlconfig.cpp @@ -0,0 +1,19 @@ +#include "qmlconfig.h" +#include "configfile.h" + +QmlConfig::QmlConfig(boost::shared_ptr c) + :QObject(), myConfig(c) +{ + +} + +QmlConfig::~QmlConfig() {} +QmlConfig::QmlConfig(const QmlConfig&) :QObject() {} + +QString QmlConfig::readConfigString(QString varName) { + return QString::fromUtf8(myConfig->readConfigString(varName.toStdString()).c_str()); +} + +QString QmlConfig::readConfigInt(QString varName) { + return QString::number(myConfig->readConfigInt(varName.toStdString())); +} diff --git a/src/gui/qml/cpp/qmlconfig.h b/src/gui/qml/cpp/qmlconfig.h new file mode 100644 index 00000000..f561ffa9 --- /dev/null +++ b/src/gui/qml/cpp/qmlconfig.h @@ -0,0 +1,24 @@ +#ifndef QMLCONFIG_H +#define QMLCONFIG_H +#include +#include + +class ConfigFile; + +class QmlConfig: public QObject +{ + Q_OBJECT +public: + + Q_INVOKABLE QString readConfigString(QString); + Q_INVOKABLE QString readConfigInt(QString); + + QmlConfig(boost::shared_ptr); + ~QmlConfig(); + QmlConfig(const QmlConfig&); + +private: + boost::shared_ptr myConfig; +}; + +#endif // QMLCONFIG_H diff --git a/src/gui/qml/cpp/qmlconfigwrapper.cpp b/src/gui/qml/cpp/qmlconfigwrapper.cpp deleted file mode 100644 index 9587b682..00000000 --- a/src/gui/qml/cpp/qmlconfigwrapper.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include "qmlconfigwrapper.h" -#include "configfile.h" - -QmlConfigWrapper::QmlConfigWrapper(ConfigFile *c) - :QObject(), myConfig(c) -{ - -} - -QmlConfigWrapper::~QmlConfigWrapper() -{ - -} - -QmlConfigWrapper::QmlConfigWrapper(const QmlConfigWrapper&) - :QObject() -{ - -} diff --git a/src/gui/qml/cpp/qmlconfigwrapper.h b/src/gui/qml/cpp/qmlconfigwrapper.h deleted file mode 100644 index ad94ada8..00000000 --- a/src/gui/qml/cpp/qmlconfigwrapper.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef QMLCONFIGWRAPPER_H -#define QMLCONFIGWRAPPER_H -#include - -class ConfigFile; - -class QmlConfigWrapper: public QObject -{ - Q_OBJECT -public: - QmlConfigWrapper(ConfigFile *c); - ~QmlConfigWrapper(); - QmlConfigWrapper(const QmlConfigWrapper&); - -private: - ConfigFile *myConfig; -}; - -#endif // QMLCONFIGWRAPPER_H diff --git a/src/gui/qml/cpp/qmlwrapper.cpp b/src/gui/qml/cpp/qmlwrapper.cpp index e3e13011..6b71fd04 100644 --- a/src/gui/qml/cpp/qmlwrapper.cpp +++ b/src/gui/qml/cpp/qmlwrapper.cpp @@ -1,18 +1,20 @@ #include #include +#include #include #include +#include "qmlconfig.h" #include "qmlwrapper.h" QmlWrapper::QmlWrapper(boost::shared_ptr c) :QObject(), myConfig(c) { - qDebug("QmlWrapperKonstruktor"); myEngine.reset(new QQmlApplicationEngine); + myQmlConfig = new QmlConfig(myConfig); //need to be a default pointer because QML doesnt support shared_ptr() // Add c++ content here -// myEngine.rootContext()->setContextProperty("model1", &model1); + myEngine->rootContext()->setContextProperty("Config", myQmlConfig); myEngine->load(QUrl(QStringLiteral("qrc:/main.qml"))); } diff --git a/src/gui/qml/cpp/qmlwrapper.h b/src/gui/qml/cpp/qmlwrapper.h index c4fd85b3..22afe834 100644 --- a/src/gui/qml/cpp/qmlwrapper.h +++ b/src/gui/qml/cpp/qmlwrapper.h @@ -4,6 +4,7 @@ #include class ConfigFile; +class QmlConfig; class QQmlApplicationEngine; class QmlWrapper: public QObject @@ -19,6 +20,7 @@ public slots: private: boost::shared_ptr myEngine; boost::shared_ptr myConfig; + QmlConfig *myQmlConfig; }; #endif // QMLWRAPPER_H diff --git a/src/gui/qml/views/createlocalgameview/CreateLocalGameView.qml b/src/gui/qml/views/createlocalgameview/CreateLocalGameView.qml index def3eba4..8d27c63b 100644 --- a/src/gui/qml/views/createlocalgameview/CreateLocalGameView.qml +++ b/src/gui/qml/views/createlocalgameview/CreateLocalGameView.qml @@ -35,7 +35,7 @@ Rectangle { ListElement { value: "2" } ] myValueIsIndex: false - myValue: "10" + myValue: "" } ListElement { myId: "spinBox_StartCash" @@ -44,7 +44,7 @@ Rectangle { myMaxValue: 1000000 myMinValue: 1000 myPrefix: "$" - myValue: "5000" + myValue: "" } ListElement { myId: "comboBox_Blinds" @@ -54,7 +54,7 @@ Rectangle { ListElement { value: qsTr("Use saved blinds settings") }, ListElement { value: qsTr("Change blinds settings ...") } ] - myValue: "0" + myValue: "" myValueIsIndex: true } ListElement { @@ -74,7 +74,7 @@ Rectangle { ListElement { value: "2" }, ListElement { value: "1" } ] - myValue: "5" + myValue: "" myValueIsIndex: false } ListElement { @@ -94,9 +94,17 @@ Rectangle { ListElement { value: "2" }, ListElement { value: "1" } ] - myValue: "5" + myValue: "" myValueIsIndex: false } + + Component.onCompleted: { + createLocalGameViewModel.setProperty(0, "myValue", Config.readConfigInt("NumberOfPlayers")); + createLocalGameViewModel.setProperty(1, "myValue", Config.readConfigInt("StartCash")); + createLocalGameViewModel.setProperty(2, "myValue", "0"); + createLocalGameViewModel.setProperty(3, "myValue", Config.readConfigInt("GameSpeed")); + createLocalGameViewModel.setProperty(4, "myValue", Config.readConfigInt("GameSpeed")); + } } delegate: MyListViewDelegateFactory { myListViewsHeight: createLocalGameViewList.height