multiple things

This commit is contained in:
Crylia
2024-06-26 11:06:09 +02:00
parent df5e9bdfa9
commit 5c9a49e634
68 changed files with 77165 additions and 90 deletions

View File

@@ -5,45 +5,45 @@
#include <memory>
#include <unordered_map>
FileController::FileController() {
FileController::FileController( ) {
#ifdef _WIN32
m_fmWorker = std::make_shared<FileMonitor>(
std::filesystem::path(std::getenv("USERPROFILE")),
std::chrono::milliseconds(1000));
std::filesystem::path(std::getenv("USERPROFILE")),
std::chrono::milliseconds(1000));
#else
m_fmWorker = std::make_shared<FileMonitor>(
std::filesystem::path("/home/crylia/Dokumente"),
std::chrono::milliseconds(1000));
std::filesystem::path("/home/crylia/Dokumente"),
std::chrono::milliseconds(1000));
#endif
m_fmWorker->moveToThread(&m_fsThread);
connect(this, &FileController::operate, m_fmWorker.get(),
&FileMonitor::start);
connect(m_fmWorker.get(), &FileMonitor::fileCreated, this,
&FileController::update);
connect(m_fmWorker.get(), &FileMonitor::pathChanged, this,
&FileController::newPath);
connect(this, &FileController::operate, m_fmWorker.get( ),
&FileMonitor::start);
connect(m_fmWorker.get( ), &FileMonitor::fileHappened, this,
&FileController::update);
connect(m_fmWorker.get( ), &FileMonitor::pathChanged, this,
&FileController::newPath);
connect(this, &FileController::updatePath, m_fmWorker.get( ), &FileMonitor::SetPath);
m_fsThread.start();
m_fsThread.start( );
emit operate();
emit operate( );
}
FileController::~FileController() {
m_fsThread.quit();
m_fsThread.wait();
FileController::~FileController( ) {
m_fsThread.quit( );
m_fsThread.wait( );
}
void FileController::newPath(
std::unordered_map<std::filesystem::path, std::filesystem::file_time_type>
path) {
std::cout << "test" << std::endl;
std::unordered_map<std::filesystem::path, std::filesystem::file_time_type>
path) {
std::cout << "bruh" << std::endl;
emit pathChanged(path);
}
void FileController::update(const std::filesystem::path path,
const FileEvent event) {
std::cout << "bruh" << std::endl;
std::cout << path.string() << std::endl;
const FileEvent event) {
emit contentChanged(path, event);
}

View File

@@ -8,8 +8,8 @@
class FileController : public QObject {
Q_OBJECT
public:
FileController();
~FileController();
FileController( );
~FileController( );
private:
QThread m_fsThread;
@@ -20,18 +20,20 @@ private slots:
void update(const std::filesystem::path path, const FileEvent);
void newPath(const std::unordered_map<std::filesystem::path,
std::filesystem::file_time_type>
paths);
std::filesystem::file_time_type>
paths);
signals:
// Start signal to start the function in the thread
void operate();
void operate( );
// Stop signal to stop the function in the thread
void pause();
void pause( );
// Signal to update the path inside the thread
void updatePath(const std::filesystem::path &);
void updatePath(const std::filesystem::path&);
void pathChanged(const std::unordered_map<std::filesystem::path,
std::filesystem::file_time_type>
paths);
std::filesystem::file_time_type>
paths);
void contentChanged(std::filesystem::path, FileEvent event);
};

View File

@@ -4,57 +4,53 @@
#include <thread>
FileMonitor::FileMonitor(std::filesystem::path path,
std::chrono::duration<int, std::milli> delay)
: m_path(path), m_delay(delay) {
initPathsMap();
emit pathChanged(m_paths);
}
std::chrono::duration<int, std::milli> delay)
: m_path(path), m_delay(delay) { }
void FileMonitor::SetPath(std::filesystem::path newPath) {
m_path = newPath;
initPathsMap();
initPathsMap( );
emit pathChanged(m_paths);
}
void FileMonitor::initPathsMap() {
m_paths.clear();
for (auto &file : std::filesystem::directory_iterator(m_path)) {
m_paths[file.path()] = std::filesystem::last_write_time(file);
void FileMonitor::initPathsMap( ) {
m_paths.clear( );
for (auto& file : std::filesystem::directory_iterator(m_path)) {
m_paths[file.path( )] = std::filesystem::last_write_time(file);
}
}
void FileMonitor::start() {
void FileMonitor::start( ) {
while (m_running) {
std::this_thread::sleep_for(m_delay);
auto pbegin = m_paths.begin();
while (pbegin != m_paths.end()) {
auto pbegin = m_paths.begin( );
while (pbegin != m_paths.end( )) {
if (!std::filesystem::exists(pbegin->first)) {
emit fileDeleted(pbegin->first, FileEvent::erased);
emit fileHappened(pbegin->first, FileEvent::erased);
pbegin = m_paths.erase(pbegin);
} else
pbegin++;
}
for (auto &file : std::filesystem::directory_iterator(m_path)) {
for (auto& file : std::filesystem::directory_iterator(m_path)) {
auto curr_file_last_write = std::filesystem::last_write_time(file);
if (!contains(file.path())) {
m_paths[file.path()] = curr_file_last_write;
emit fileCreated(file.path(), FileEvent::created);
if (!contains(file.path( ))) {
m_paths[file.path( )] = curr_file_last_write;
emit fileHappened(file.path( ), FileEvent::created);
} else {
if (m_paths[file.path()] != curr_file_last_write) {
m_paths[file.path()] = curr_file_last_write;
emit fileModified(file.path(), FileEvent::modified);
if (m_paths[file.path( )] != curr_file_last_write) {
m_paths[file.path( )] = curr_file_last_write;
emit fileHappened(file.path( ), FileEvent::modified);
}
}
}
}
}
void FileMonitor::stop() { m_running = false; }
void FileMonitor::stop( ) { m_running = false; }
bool FileMonitor::contains(const std::filesystem::path &key) {
return m_paths.find(key) != m_paths.end();
bool FileMonitor::contains(const std::filesystem::path& key) {
return m_paths.find(key) != m_paths.end( );
}

View File

@@ -13,30 +13,28 @@ public:
std::chrono::duration<int, std::milli> m_delay;
FileMonitor(std::filesystem::path path,
std::chrono::duration<int, std::milli> delay);
~FileMonitor() = default;
std::chrono::duration<int, std::milli> delay);
~FileMonitor( ) = default;
void SetPath(std::filesystem::path path);
public slots:
void start();
void stop();
void start( );
void stop( );
private:
std::unordered_map<std::filesystem::path, std::filesystem::file_time_type>
m_paths;
m_paths;
bool m_running;
std::filesystem::path m_path;
bool contains(const std::filesystem::path &key);
bool contains(const std::filesystem::path& key);
void initPathsMap();
void initPathsMap( );
signals:
void fileDeleted(const std::filesystem::path path, const FileEvent);
void fileCreated(const std::filesystem::path path, const FileEvent);
void fileModified(const std::filesystem::path path, const FileEvent);
void fileHappened(const std::filesystem::path path, const FileEvent);
void pathChanged(const std::unordered_map<std::filesystem::path,
std::filesystem::file_time_type>
paths);
std::filesystem::file_time_type>
paths);
};

View File

@@ -1,22 +1,57 @@
#include "GridItemView.hpp"
#include <iostream>
GridItemView::GridItemView(QWidget *parent)
: QFrame(parent), fileController(std::make_shared<FileController>()) {
GridItemView::GridItemView(QWidget* parent)
: QFrame(parent), fileController(std::make_shared<FileController>( )) {
QGridLayout *mainLayout = new QGridLayout();
QGridLayout* mainLayout = new QGridLayout( );
connect(fileController.get(), &FileController::pathChanged, this,
[this,
mainLayout](const std::unordered_map<std::filesystem::path,
std::filesystem::file_time_type>
paths) {
std::cout << "path.first" << std::endl;
this->setLayout(mainLayout);
connect(fileController.get( ), &FileController::pathChanged, this,
[this, mainLayout](const std::unordered_map<std::filesystem::path, std::filesystem::file_time_type> paths) {
/* int r{ 0 }, c{ 0 };
for (auto path : paths) {
auto w = new GridItem(path.first);
gridMap[path.first] = w;
mainLayout->addWidget(w, r, c);
w->show( );
++r;
if ((r % 8) == 0) {
++c;
r = 0;
}
} */
});
fileController.get( )->updatePath("/home/crylia/Dokumente");
connect(fileController.get( ), &FileController::contentChanged, this, [this, mainLayout](std::filesystem::path path, FileEvent event) {
if (event == FileEvent::created) {
auto w = new GridItem(path);
gridMap[path] = w;
int pos = gridMap.size( ) - 1;
mainLayout->addWidget(w, pos / 8, pos % 8);
w->show( );
} else if (event == FileEvent::erased) {
mainLayout->removeWidget(gridMap[path]);
delete gridMap[path];
gridMap.erase(path);
// Re-layout the remaining widgets
int pos = 0;
for (const auto& [key, widget] : gridMap) {
mainLayout->addWidget(widget, pos / 8, pos % 8);
pos++;
}
} else if (event == FileEvent::modified) {
std::cout << "modified" << std::endl;
// Do nothing ig? In theory nothing has to update unless we show the time
}
});
for (auto path : paths) {
mainLayout->addWidget(new GridItem(path.first, this));
}
});
}
GridItemView::~GridItemView() {}
GridItemView::~GridItemView( ) { }

View File

@@ -16,6 +16,10 @@ private:
std::shared_ptr<FileController> fileController;
//Grid map to keep track of the widgets, and delete them easily as QT doesnt
//provide an easy way to find children by a property
std::unordered_map<std::filesystem::path, QWidget*> gridMap;
public:
GridItemView(QWidget* parent = nullptr);
~GridItemView( );

View File

@@ -2,13 +2,15 @@
MainWidget::MainWidget(QWidget* parent) {
auto path_mainContentLayout = new QHBoxLayout(this);
auto fileTree_fileGridLayout = new QVBoxLayout(this);
auto path_mainContentLayout = new QVBoxLayout(this);
auto fileTree_fileGridLayout = new QHBoxLayout(this);
auto gridLayout = new GridItemView(this);
auto path = new Path(this);
fileTree_fileGridLayout->addWidget(gridLayout);
path_mainContentLayout->addWidget(path);
path_mainContentLayout->addLayout(fileTree_fileGridLayout);
setLayout(path_mainContentLayout);

View File

@@ -7,6 +7,7 @@
#include "../Widgets/GridItem/GridItem.hpp"
#include "../GridItemView/GridItemView.hpp"
#include "../Path/Path.hpp"
class MainWidget : public QWidget {
Q_OBJECT

14
src/View/Path/Path.cpp Normal file
View File

@@ -0,0 +1,14 @@
#include "Path.hpp"
Path::Path(QWidget* parent)
:QWidget(parent),
#ifdef _WIN32
pathLabel_m(new QLabel(std::getenv("USERPROFILE"))) {
#else
pathLabel_m(new QLabel(std::getenv("HOME"))) {
#endif
}
Path::~Path( ) { }

23
src/View/Path/Path.hpp Normal file
View File

@@ -0,0 +1,23 @@
#pragma once
#include <QWidget>
#include <QLabel>
class Path : public QWidget {
Q_OBJECT
private:
QLabel* pathLabel_m;
QString path_m;
public:
Path(QWidget* parent = nullptr);
~Path( );
QString path( ) { return path_m; }
void setPath(QString path) {
if (path == this->path_m) { return; }
path_m = path;
pathLabel_m->setText(path_m);
}
};

View File

@@ -1,11 +1,12 @@
#include "GridItem.hpp"
#include <iostream>
GridItem::GridItem(const std::filesystem::path path, QWidget* parent)
: QWidget(parent),
m_path(path),
m_name(QString::fromStdString(path.filename( ).string( ))),
m_icon(getIconForFileType(path)) {
widgetSize = QSize(128, 128);
widgetSize = QSize(80, 128);
setMinimumSize(widgetSize);
setMaximumSize(widgetSize);
@@ -43,14 +44,26 @@ QString GridItem::formatText(const QString& text) {
QIcon GridItem::getIconForFileType(const std::filesystem::path path) const {
if (!std::filesystem::is_directory(path)) {
QIcon icon;
QList<QMimeType> mime_types = mime_database.mimeTypesForFileName(QString::fromStdString(path.string( )));
auto mdb = new QMimeDatabase( );
QMimeType mime_type = mdb->mimeTypeForFile(QString::fromStdString(path.string( )));
icon = QIcon::fromTheme(mime_type.iconName( ));
for (int i = 0; i < mime_types.count( ) && icon.isNull( ); ++i)
icon = QIcon::fromTheme(mime_types[i].iconName( ));
if (!icon.isNull( )) {
QPixmap pixmap = icon.pixmap(QSize(64, 64));
icon = QIcon(pixmap);
}
return icon.isNull( ) ? QApplication::style( )->standardIcon(QStyle::SP_FileIcon) : icon;
} else {
return QIcon::fromTheme("folder");
auto icon = QIcon::fromTheme("folder");
if (!icon.isNull( )) {
QPixmap pixmap = icon.pixmap(QSize(64, 64));
icon = QIcon(pixmap);
}
return icon;
}
}

View File

@@ -19,7 +19,6 @@ private:
std::filesystem::path m_path;
QSize widgetSize;
QMimeDatabase mime_database;
QString formatText(const QString& text);