added basic models and controllers

This commit is contained in:
Anika Raemer 2018-10-19 14:19:19 +02:00
parent 269b48ed9d
commit e401b71788
9 changed files with 222 additions and 2 deletions

View file

@ -3,7 +3,10 @@ TEMPLATE = app
QT += qml quick QT += qml quick
CONFIG += c++11 CONFIG += c++11
SOURCES += main.cpp SOURCES += main.cpp \
models/NavigationListModel.cpp \
models/NavigationItemModel.cpp \
controllers/NavigationController.cpp
RESOURCES += qml.qrc RESOURCES += qml.qrc
@ -28,3 +31,8 @@ DEFINES += QT_DEPRECATED_WARNINGS
qnx: target.path = /tmp/$${TARGET}/bin qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target !isEmpty(target.path): INSTALLS += target
HEADERS += \
models/NavigationListModel.h \
models/NavigationItemModel.h \
controllers/NavigationController.h

View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject> <!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.2.0, 2018-10-17T20:42:11. --> <!-- Written by QtCreator 4.2.0, 2018-10-19T13:26:21. -->
<qtcreator> <qtcreator>
<data> <data>
<variable>EnvironmentId</variable> <variable>EnvironmentId</variable>

View file

@ -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);
}

View file

@ -0,0 +1,29 @@
#ifndef NAVIGATIONCONTROLLER_H
#define NAVIGATIONCONTROLLER_H
#include <QObject>
#include <QQmlContext>
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

View file

@ -1,6 +1,8 @@
#include <QGuiApplication> #include <QGuiApplication>
#include <QQmlApplicationEngine> #include <QQmlApplicationEngine>
#include "controllers/NavigationController.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QGuiApplication app(argc, argv); QGuiApplication app(argc, argv);
@ -8,5 +10,8 @@ int main(int argc, char *argv[])
QQmlApplicationEngine engine; QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
NavigationController navController;
navController.setContext(engine.rootContext());
return app.exec(); return app.exec();
} }

View file

@ -0,0 +1,70 @@
#include "NavigationItemModel.h"
#include <QDir>
#include <QFile>
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 *> 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();
}

View file

@ -0,0 +1,40 @@
#ifndef NAVIGATIONITEMMODEL_H
#define NAVIGATIONITEMMODEL_H
#include <QObject>
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<NavigationItemModel*> getChildren();
void appendChild(NavigationItemModel* item);
void clearChildren();
Q_INVOKABLE void onClicked();
private:
QString mPath = QString("");
QString mImageSource;
QList<NavigationItemModel*> mChildren = QList<NavigationItemModel*>();
NavigationItemModel* mParentItem = nullptr;
};
#endif // NAVIGATIONITEMMODEL_H

View file

@ -0,0 +1,17 @@
#include "NavigationListModel.h"
NavigationListModel::NavigationListModel(QObject* parent) : QObject(parent)
{
}
QList<QObject *> NavigationListModel::getModelItems()
{
return mItems;
}
void NavigationListModel::setModelItems(QList<QObject *> list)
{
mItems = list;
emit modelItemsChanged();
}

View file

@ -0,0 +1,24 @@
#ifndef NAVIGATIONLISTMODEL_H
#define NAVIGATIONLISTMODEL_H
#include <QObject>
class NavigationListModel : public QObject
{
Q_OBJECT
Q_PROPERTY(QList<QObject*> pModelItems READ getModelItems NOTIFY modelItemsChanged)
signals:
void modelItemsChanged();
public:
NavigationListModel(QObject* parent = Q_NULLPTR);
QList<QObject*> getModelItems();
void setModelItems(QList<QObject*> list);
private:
QList<QObject*> mItems;
};
#endif // NAVIGATIONLISTMODEL_H