lena_pi/LenaPi/controllers/NavigationController.cpp
2018-10-19 22:01:14 +02:00

67 lines
1.8 KiB
C++

#include "NavigationController.h"
#include <QDir>
#include <QDebug>
#include "../models/NavigationItemModel.h"
#include "../models/NavigationListModel.h"
///@todo impelement reaction to item's clicked signal; implement back navigation
NavigationController::NavigationController(QObject *parent) : QObject(parent),
mRootItem(new NavigationItemModel(this)),
mNavList(new NavigationListModel(this))
{
}
void NavigationController::init(const QString &rootPath)
{
auto rootDir = QDir(rootPath);
if(!rootDir.exists()) return;
mRootPath = rootPath;
add(mRootPath, mRootItem, true);
mNavList->setModelItems(mRootItem->getChildren());
}
void NavigationController::setContext(QQmlContext *context)
{
mContext = context;
setContextProperties();
}
void NavigationController::setContextProperties()
{
mContext->setContextProperty("navigationList", mNavList);
}
void NavigationController::add(const QString &path, NavigationItemModel *parentItem, bool isCircleDelegate)
{
auto dir = QDir(path);
if(!dir.exists()) return;
auto subDirsNames = dir.entryList(QDir::AllDirs);
for(const auto& name : subDirsNames){
if(name == "." || name == "..") continue;
auto item = new NavigationItemModel(parentItem);
if(!item->setPath(path + "/" + name)) {
item->deleteLater();
continue;
}
item->setCircleDelegate(isCircleDelegate);
connect(item, &NavigationItemModel::clicked, this, &NavigationController::onNavigationRequest);
parentItem->appendChild(item);
add(item->getPath(), item, false);
}
}
void NavigationController::onNavigationRequest()
{
auto item = qobject_cast<NavigationItemModel*>(QObject::sender());
if(!item) return;
if(item->hasChildren())
mNavList->setModelItems(item->getChildren());
}