From e401b71788528edb6613afe69a43aec18f96160a Mon Sep 17 00:00:00 2001 From: Anika Raemer Date: Fri, 19 Oct 2018 14:19:19 +0200 Subject: [PATCH] added basic models and controllers --- LenaPi/LenaPi.pro | 10 ++- LenaPi/LenaPi.pro.user | 2 +- LenaPi/controllers/NavigationController.cpp | 27 ++++++++ LenaPi/controllers/NavigationController.h | 29 +++++++++ LenaPi/main.cpp | 5 ++ LenaPi/models/NavigationItemModel.cpp | 70 +++++++++++++++++++++ LenaPi/models/NavigationItemModel.h | 40 ++++++++++++ LenaPi/models/NavigationListModel.cpp | 17 +++++ LenaPi/models/NavigationListModel.h | 24 +++++++ 9 files changed, 222 insertions(+), 2 deletions(-) create mode 100644 LenaPi/controllers/NavigationController.cpp create mode 100644 LenaPi/controllers/NavigationController.h create mode 100644 LenaPi/models/NavigationItemModel.cpp create mode 100644 LenaPi/models/NavigationItemModel.h create mode 100644 LenaPi/models/NavigationListModel.cpp create mode 100644 LenaPi/models/NavigationListModel.h diff --git a/LenaPi/LenaPi.pro b/LenaPi/LenaPi.pro index 30a9c5d..500cd12 100644 --- a/LenaPi/LenaPi.pro +++ b/LenaPi/LenaPi.pro @@ -3,7 +3,10 @@ TEMPLATE = app QT += qml quick CONFIG += c++11 -SOURCES += main.cpp +SOURCES += main.cpp \ + models/NavigationListModel.cpp \ + models/NavigationItemModel.cpp \ + controllers/NavigationController.cpp RESOURCES += qml.qrc @@ -28,3 +31,8 @@ DEFINES += QT_DEPRECATED_WARNINGS qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target + +HEADERS += \ + models/NavigationListModel.h \ + models/NavigationItemModel.h \ + controllers/NavigationController.h diff --git a/LenaPi/LenaPi.pro.user b/LenaPi/LenaPi.pro.user index c477d3c..4953b64 100644 --- a/LenaPi/LenaPi.pro.user +++ b/LenaPi/LenaPi.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/LenaPi/controllers/NavigationController.cpp b/LenaPi/controllers/NavigationController.cpp new file mode 100644 index 0000000..c0ca406 --- /dev/null +++ b/LenaPi/controllers/NavigationController.cpp @@ -0,0 +1,27 @@ +#include "NavigationController.h" + +#include "../models/NavigationItemModel.h" +#include "../models/NavigationListModel.h" + +NavigationController::NavigationController(QObject *parent) : QObject(parent), + mRootItem(new NavigationItemModel(this)), + mNavList(new NavigationListModel(this)) +{ + +} + +void NavigationController::init(const QString &rootPath) +{ + +} + +void NavigationController::setContext(QQmlContext *context) +{ + mContext = context; + setContextProperties(); +} + +void NavigationController::setContextProperties() +{ + mContext->setContextProperty("navigationList", mNavList); +} diff --git a/LenaPi/controllers/NavigationController.h b/LenaPi/controllers/NavigationController.h new file mode 100644 index 0000000..7804171 --- /dev/null +++ b/LenaPi/controllers/NavigationController.h @@ -0,0 +1,29 @@ +#ifndef NAVIGATIONCONTROLLER_H +#define NAVIGATIONCONTROLLER_H + +#include +#include + +class NavigationItemModel; +class NavigationListModel; + +class NavigationController : public QObject +{ + Q_OBJECT +public: + NavigationController(QObject *parent = Q_NULLPTR); + + void init(const QString & rootPath); + + void setContext(QQmlContext* context); + +private: + void setContextProperties(); + + NavigationItemModel* mRootItem; + NavigationListModel* mNavList; + + QQmlContext* mContext; +}; + +#endif // NAVIGATIONCONTROLLER_H diff --git a/LenaPi/main.cpp b/LenaPi/main.cpp index d76049d..617f441 100644 --- a/LenaPi/main.cpp +++ b/LenaPi/main.cpp @@ -1,6 +1,8 @@ #include #include +#include "controllers/NavigationController.h" + int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); @@ -8,5 +10,8 @@ int main(int argc, char *argv[]) QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); + NavigationController navController; + navController.setContext(engine.rootContext()); + return app.exec(); } diff --git a/LenaPi/models/NavigationItemModel.cpp b/LenaPi/models/NavigationItemModel.cpp new file mode 100644 index 0000000..b8a3704 --- /dev/null +++ b/LenaPi/models/NavigationItemModel.cpp @@ -0,0 +1,70 @@ +#include "NavigationItemModel.h" + +#include +#include + +NavigationItemModel::NavigationItemModel(QObject *parent) : QObject(parent) +{ + +} + +QString NavigationItemModel::getImageSource() const +{ + return mImageSource; +} + +QString NavigationItemModel::getPath() const +{ + return mPath; +} + +void NavigationItemModel::onClicked() +{ + emit clicked(); +} + +bool NavigationItemModel::setPath(const QString &path) +{ + + //@todo check if image.jpg exist; use default image from qrc otherwise! + + auto dir = QDir(path); + if(!dir.exists()) return false; + + mPath = path; + + auto source = dir.filePath("file.jpg"); + if(QFile(source).exists()) + mImageSource = source; + else + mImageSource = "file:///home/ar/source/LenaPi/pics/benjamin.jpeg"; + + emit imageSourceChanged(); + + return true; +} + +NavigationItemModel *NavigationItemModel::getParentItem() +{ + return mParentItem; +} + +QList NavigationItemModel::getChildren() +{ + return mChildren; +} + +void NavigationItemModel::appendChild(NavigationItemModel *item) +{ + item->setParent(this); + mChildren.append(item); +} + +void NavigationItemModel::clearChildren() +{ + for(const auto& child : mChildren){ + child->deleteLater(); + } + mChildren.clear(); +} + diff --git a/LenaPi/models/NavigationItemModel.h b/LenaPi/models/NavigationItemModel.h new file mode 100644 index 0000000..97749be --- /dev/null +++ b/LenaPi/models/NavigationItemModel.h @@ -0,0 +1,40 @@ +#ifndef NAVIGATIONITEMMODEL_H +#define NAVIGATIONITEMMODEL_H + +#include + +class NavigationItemModel : public QObject +{ + Q_OBJECT + + Q_PROPERTY(QString pImageSource READ getImageSource NOTIFY imageSourceChanged) + +signals: + void imageSourceChanged(); + void clicked(); + +public: + NavigationItemModel(QObject *parent = Q_NULLPTR); + + QString getImageSource() const; + + QString getPath() const; + bool setPath(const QString & path); + + NavigationItemModel* getParentItem(); + QList getChildren(); + + void appendChild(NavigationItemModel* item); + void clearChildren(); + + Q_INVOKABLE void onClicked(); + +private: + QString mPath = QString(""); + QString mImageSource; + + QList mChildren = QList(); + NavigationItemModel* mParentItem = nullptr; +}; + +#endif // NAVIGATIONITEMMODEL_H diff --git a/LenaPi/models/NavigationListModel.cpp b/LenaPi/models/NavigationListModel.cpp new file mode 100644 index 0000000..f25484a --- /dev/null +++ b/LenaPi/models/NavigationListModel.cpp @@ -0,0 +1,17 @@ +#include "NavigationListModel.h" + +NavigationListModel::NavigationListModel(QObject* parent) : QObject(parent) +{ + +} + +QList NavigationListModel::getModelItems() +{ + return mItems; +} + +void NavigationListModel::setModelItems(QList list) +{ + mItems = list; + emit modelItemsChanged(); +} diff --git a/LenaPi/models/NavigationListModel.h b/LenaPi/models/NavigationListModel.h new file mode 100644 index 0000000..1458060 --- /dev/null +++ b/LenaPi/models/NavigationListModel.h @@ -0,0 +1,24 @@ +#ifndef NAVIGATIONLISTMODEL_H +#define NAVIGATIONLISTMODEL_H + +#include + +class NavigationListModel : public QObject +{ + Q_OBJECT + Q_PROPERTY(QList pModelItems READ getModelItems NOTIFY modelItemsChanged) + +signals: + void modelItemsChanged(); + +public: + NavigationListModel(QObject* parent = Q_NULLPTR); + + QList getModelItems(); + void setModelItems(QList list); + +private: + QList mItems; +}; + +#endif // NAVIGATIONLISTMODEL_H