put the filemonitor into a seperate thread and connected an update signal
This commit is contained in:
@@ -1,16 +1,26 @@
|
||||
#include "FileController.hpp"
|
||||
#include <chrono>
|
||||
#include <memory>
|
||||
#include <iostream>
|
||||
|
||||
FileController::FileController( ) {
|
||||
m_fmWorker = std::make_unique<FileMonitor>(QString("/home/crylia/"),
|
||||
m_fmWorker = std::make_shared<FileMonitor>(QString("/home/crylia/Dokumente"),
|
||||
std::chrono::milliseconds(1000));
|
||||
m_fmWorker->moveToThread(&m_fsThread);
|
||||
|
||||
m_fmWorker->start(QString("/home/crylia/"));
|
||||
connect(this, &FileController::operate, m_fmWorker.get( ), &FileMonitor::start);
|
||||
connect(m_fmWorker.get( ), &FileMonitor::update, this, &FileController::update);
|
||||
|
||||
m_fsThread.start( );
|
||||
|
||||
emit operate( );
|
||||
}
|
||||
|
||||
FileController::~FileController( ) {
|
||||
m_fsThread.quit( );
|
||||
m_fsThread.wait( );
|
||||
}
|
||||
|
||||
void FileController::update(const QString path, const FileEvent event) {
|
||||
std::cout << path.toStdString( ) << std::endl;
|
||||
}
|
||||
|
||||
@@ -14,5 +14,17 @@ public:
|
||||
private:
|
||||
QThread m_fsThread;
|
||||
|
||||
std::unique_ptr<FileMonitor> m_fmWorker;
|
||||
std::shared_ptr<FileMonitor> m_fmWorker;
|
||||
|
||||
private slots:
|
||||
|
||||
void update(const QString path, const FileEvent);
|
||||
|
||||
signals:
|
||||
// Start signal to start the function in the thread
|
||||
void operate( );
|
||||
// Stop signal to stop the function in the thread
|
||||
void pause( );
|
||||
// Signal to update the path inside the thread
|
||||
void updatePath(const QString&);
|
||||
};
|
||||
|
||||
@@ -7,13 +7,13 @@ FileMonitor::FileMonitor(QString path,
|
||||
: m_path(path), m_delay(delay) {
|
||||
|
||||
for (auto& file :
|
||||
std::filesystem::recursive_directory_iterator(m_path.toStdString())) {
|
||||
std::filesystem::directory_iterator(m_path.toStdString( ))) {
|
||||
m_paths[QString::fromStdString(file.path( ).string( ))] =
|
||||
std::filesystem::last_write_time(file);
|
||||
}
|
||||
}
|
||||
|
||||
void FileMonitor::start(const QString path) {
|
||||
void FileMonitor::start( ) {
|
||||
while (m_running) {
|
||||
std::this_thread::sleep_for(m_delay);
|
||||
|
||||
@@ -27,7 +27,7 @@ void FileMonitor::start(const QString path) {
|
||||
}
|
||||
|
||||
for (auto& file :
|
||||
std::filesystem::recursive_directory_iterator(m_path.toStdString())) {
|
||||
std::filesystem::directory_iterator(m_path.toStdString( ))) {
|
||||
auto curr_file_last_write = std::filesystem::last_write_time(file);
|
||||
|
||||
if (!contains(QString::fromStdString(file.path( ).string( )))) {
|
||||
|
||||
@@ -19,7 +19,7 @@ public:
|
||||
void SetPath(QString path) { m_path = path; }
|
||||
|
||||
public slots:
|
||||
void start(const QString path);
|
||||
void start( );
|
||||
void stop( );
|
||||
|
||||
private:
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
#include "MainWidget.hpp"
|
||||
|
||||
MainWidget::MainWidget(QWidget* parent) {
|
||||
|
||||
auto path_mainContentLayout = std::make_shared<QHBoxLayout>(this);
|
||||
auto fileTree_fileGridLayout = std::make_shared<QVBoxLayout>(this);
|
||||
|
||||
path_mainContentLayout->addLayout(fileTree_fileGridLayout.get( ));
|
||||
|
||||
setLayout(path_mainContentLayout.get( ));
|
||||
|
||||
auto fmc = new FileController( );
|
||||
}
|
||||
|
||||
MainWidget::~MainWidget( ) { }
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <QLayout>
|
||||
#include <QLabel>
|
||||
#include <QWidget>
|
||||
#include <qboxlayout.h>
|
||||
#include <qtmetamacros.h>
|
||||
|
||||
#include "../../Controller/FileController/FileController.hpp"
|
||||
|
||||
class MainWidget : public QWidget {
|
||||
Q_OBJECT
|
||||
public:
|
||||
MainWidget() = default;
|
||||
~MainWidget() = default;
|
||||
MainWidget(QWidget* parent = nullptr);
|
||||
~MainWidget( );
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
#include "MainWindow.hpp"
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {}
|
||||
MainWindow::MainWindow(QWidget* parent)
|
||||
: QMainWindow(parent), m_mainWidget(std::make_shared<MainWidget>( )) {
|
||||
setWindowTitle("QutieFM");
|
||||
|
||||
setObjectName("MainWindow");
|
||||
|
||||
this->setCentralWidget(m_mainWidget.get( ));
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow( ) { }
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
#include <QMainWindow>
|
||||
|
||||
#include "../MainWidget/MainWidget.hpp"
|
||||
|
||||
class MainWindow : public QMainWindow {
|
||||
Q_OBJECT
|
||||
public:
|
||||
@@ -9,4 +11,6 @@ public:
|
||||
~MainWindow( );
|
||||
|
||||
private:
|
||||
|
||||
std::shared_ptr<MainWidget> m_mainWidget;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user