init commit, add filemonitor and controller, add basic mainwindow
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -30,3 +30,6 @@
|
|||||||
*.exe
|
*.exe
|
||||||
*.out
|
*.out
|
||||||
*.app
|
*.app
|
||||||
|
|
||||||
|
build/
|
||||||
|
.cache/
|
||||||
|
|||||||
33
CMakeLists.txt
Normal file
33
CMakeLists.txt
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.5)
|
||||||
|
|
||||||
|
project(QutieFM VERSION 0.1 LANGUAGES CXX)
|
||||||
|
|
||||||
|
set(CMAKE_AUTOUIC ON)
|
||||||
|
set(CMAKE_AUTOMOC ON)
|
||||||
|
set(CMAKE_AUTORCC ON)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
|
find_package(QT NAMES Qt6 REQUIRED COMPONENTS Widgets Qml Svg Core Sql)
|
||||||
|
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets Qml Svg Core Sql)
|
||||||
|
|
||||||
|
file(GLOB_RECURSE PROJECT_SOURCES src/*.cpp)
|
||||||
|
file(GLOB_RECURSE PROJECT_HEADERS src/*.hpp)
|
||||||
|
file(GLOB_RECURSE PROJECT_RESOURCES assets/resources.qrc)
|
||||||
|
|
||||||
|
add_executable(QutieFM
|
||||||
|
${PROJECT_SOURCES}
|
||||||
|
${PROJECT_HEADERS}
|
||||||
|
${PROJECT_RESOURCES}
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(QutieFM PRIVATE
|
||||||
|
Qt${QT_VERSION_MAJOR}::Widgets
|
||||||
|
Qt${QT_VERSION_MAJOR}::Qml
|
||||||
|
Qt${QT_VERSION_MAJOR}::Svg
|
||||||
|
Qt${QT_VERSION_MAJOR}::Core
|
||||||
|
Qt${QT_VERSION_MAJOR}::Sql
|
||||||
|
fmt
|
||||||
|
stdc++fs
|
||||||
|
)
|
||||||
16
src/Controller/FileController/FileController.cpp
Normal file
16
src/Controller/FileController/FileController.cpp
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#include "FileController.hpp"
|
||||||
|
#include <chrono>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
FileController::FileController() {
|
||||||
|
m_fmWorker = std::make_unique<FileMonitor>(QString("/home/crylia/"),
|
||||||
|
std::chrono::milliseconds(1000));
|
||||||
|
m_fmWorker->moveToThread(&m_fsThread);
|
||||||
|
|
||||||
|
m_fmWorker->start(QString("/home/crylia/"));
|
||||||
|
}
|
||||||
|
|
||||||
|
FileController::~FileController() {
|
||||||
|
m_fsThread.quit();
|
||||||
|
m_fsThread.wait();
|
||||||
|
}
|
||||||
18
src/Controller/FileController/FileController.hpp
Normal file
18
src/Controller/FileController/FileController.hpp
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <QThread>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#include "../../Core/FileMonitor/FileMonitor.hpp"
|
||||||
|
|
||||||
|
class FileController : public QObject {
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
FileController();
|
||||||
|
~FileController();
|
||||||
|
|
||||||
|
private:
|
||||||
|
QThread m_fsThread;
|
||||||
|
|
||||||
|
std::unique_ptr<FileMonitor> m_fmWorker;
|
||||||
|
};
|
||||||
55
src/Core/FileMonitor/FileMonitor.cpp
Normal file
55
src/Core/FileMonitor/FileMonitor.cpp
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
#include "FileMonitor.hpp"
|
||||||
|
#include <filesystem>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
FileMonitor::FileMonitor(QString path,
|
||||||
|
std::chrono::duration<int, std::milli> delay)
|
||||||
|
: m_path(path), m_delay(delay) {
|
||||||
|
|
||||||
|
for (auto &file :
|
||||||
|
std::filesystem::recursive_directory_iterator(m_path.toStdString())) {
|
||||||
|
m_paths[QString::fromStdString(file.path().string())] =
|
||||||
|
std::filesystem::last_write_time(file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void FileMonitor::start(const QString path) {
|
||||||
|
while (m_running) {
|
||||||
|
std::this_thread::sleep_for(m_delay);
|
||||||
|
|
||||||
|
auto pbegin = m_paths.begin();
|
||||||
|
while (pbegin != m_paths.end()) {
|
||||||
|
if (!std::filesystem::exists(pbegin->first.toStdString())) {
|
||||||
|
emit update(pbegin->first, FileEvent::erased);
|
||||||
|
pbegin = m_paths.erase(pbegin);
|
||||||
|
} else
|
||||||
|
pbegin++;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto &file :
|
||||||
|
std::filesystem::recursive_directory_iterator(m_path.toStdString())) {
|
||||||
|
auto curr_file_last_write = std::filesystem::last_write_time(file);
|
||||||
|
|
||||||
|
if (!contains(QString::fromStdString(file.path().string()))) {
|
||||||
|
m_paths[QString::fromStdString(file.path().string())] =
|
||||||
|
curr_file_last_write;
|
||||||
|
emit update(QString::fromStdString(file.path().string()),
|
||||||
|
FileEvent::created);
|
||||||
|
} else {
|
||||||
|
if (m_paths[QString::fromStdString(file.path().string())] !=
|
||||||
|
curr_file_last_write) {
|
||||||
|
m_paths[QString::fromStdString(file.path().string())] =
|
||||||
|
curr_file_last_write;
|
||||||
|
emit update(QString::fromStdString(file.path().string()),
|
||||||
|
FileEvent::modified);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void FileMonitor::stop() { m_running = false; }
|
||||||
|
|
||||||
|
bool FileMonitor::contains(const QString &key) {
|
||||||
|
return m_paths.find(key) != m_paths.end();
|
||||||
|
}
|
||||||
34
src/Core/FileMonitor/FileMonitor.hpp
Normal file
34
src/Core/FileMonitor/FileMonitor.hpp
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
#include <QString>
|
||||||
|
#include <chrono>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
enum class FileEvent { created, modified, erased };
|
||||||
|
|
||||||
|
class FileMonitor : public QObject {
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
std::chrono::duration<int, std::milli> m_delay;
|
||||||
|
|
||||||
|
FileMonitor(QString path, std::chrono::duration<int, std::milli> delay);
|
||||||
|
~FileMonitor() = default;
|
||||||
|
|
||||||
|
void SetPath(QString path) { m_path = path; }
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
void start(const QString path);
|
||||||
|
void stop();
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::unordered_map<QString, std::filesystem::file_time_type> m_paths;
|
||||||
|
bool m_running;
|
||||||
|
QString m_path;
|
||||||
|
|
||||||
|
bool contains(const QString &key);
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void update(const QString path, const FileEvent);
|
||||||
|
};
|
||||||
0
src/View/MainWidget/MainWidget.cpp
Normal file
0
src/View/MainWidget/MainWidget.cpp
Normal file
15
src/View/MainWidget/MainWidget.hpp
Normal file
15
src/View/MainWidget/MainWidget.hpp
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <QLayout>
|
||||||
|
#include <QWidget>
|
||||||
|
#include <qboxlayout.h>
|
||||||
|
#include <qtmetamacros.h>
|
||||||
|
|
||||||
|
class MainWidget : public QWidget {
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
MainWidget() = default;
|
||||||
|
~MainWidget() = default;
|
||||||
|
|
||||||
|
private:
|
||||||
|
};
|
||||||
5
src/View/MainWindow/MainWindow.cpp
Normal file
5
src/View/MainWindow/MainWindow.cpp
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
#include "MainWindow.hpp"
|
||||||
|
|
||||||
|
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {}
|
||||||
|
|
||||||
|
MainWindow::~MainWindow() {}
|
||||||
12
src/View/MainWindow/MainWindow.hpp
Normal file
12
src/View/MainWindow/MainWindow.hpp
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <QMainWindow>
|
||||||
|
|
||||||
|
class MainWindow : public QMainWindow {
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
MainWindow(QWidget *parent = nullptr);
|
||||||
|
~MainWindow();
|
||||||
|
|
||||||
|
private:
|
||||||
|
};
|
||||||
14
src/main.cpp
Normal file
14
src/main.cpp
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#include "View/MainWindow/MainWindow.hpp"
|
||||||
|
#include <QApplication>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
QApplication app(argc, argv);
|
||||||
|
|
||||||
|
auto w = std::make_unique<MainWindow>();
|
||||||
|
|
||||||
|
w->setMinimumHeight(200);
|
||||||
|
w->show();
|
||||||
|
|
||||||
|
return app.exec();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user