diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..08cc22f --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,83 @@ +{ + "files.associations": { + "cctype": "cpp", + "cmath": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "bitset": "cpp", + "charconv": "cpp", + "chrono": "cpp", + "cinttypes": "cpp", + "clocale": "cpp", + "codecvt": "cpp", + "compare": "cpp", + "concepts": "cpp", + "condition_variable": "cpp", + "cstdint": "cpp", + "deque": "cpp", + "list": "cpp", + "map": "cpp", + "set": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "unordered_set": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "ratio": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "format": "cpp", + "future": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "iosfwd": "cpp", + "istream": "cpp", + "limits": "cpp", + "mutex": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "semaphore": "cpp", + "span": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "stdfloat": "cpp", + "stop_token": "cpp", + "streambuf": "cpp", + "text_encoding": "cpp", + "thread": "cpp", + "typeinfo": "cpp", + "variant": "cpp", + "__bit_reference": "cpp", + "__config": "cpp", + "__hash_table": "cpp", + "__locale": "cpp", + "__node_handle": "cpp", + "__split_buffer": "cpp", + "__threading_support": "cpp", + "__verbose_abort": "cpp", + "ios": "cpp", + "locale": "cpp" + } +} diff --git a/src/Controller/FileController/FileController.cpp b/src/Controller/FileController/FileController.cpp index 2fb7783..53a1e64 100644 --- a/src/Controller/FileController/FileController.cpp +++ b/src/Controller/FileController/FileController.cpp @@ -4,12 +4,19 @@ #include FileController::FileController( ) { - m_fmWorker = std::make_shared(QString("/home/crylia/Dokumente"), +#ifdef _WIN32 + m_fmWorker = std::make_shared(std::filesystem::path(std::getenv("USERPROFILE")), std::chrono::milliseconds(1000)); +#else + m_fmWorker = std::make_shared(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::update, this, &FileController::update); + connect(m_fmWorker.get( ), &FileMonitor::newPathEntered, this, &FileController::emitNewPath); m_fsThread.start( ); @@ -21,6 +28,11 @@ FileController::~FileController( ) { m_fsThread.wait( ); } -void FileController::update(const QString path, const FileEvent event) { - std::cout << path.toStdString( ) << std::endl; +void FileController::update(const std::filesystem::path path, const FileEvent event) { + std::cout << path.string( ) << std::endl; +} + +void FileController::emitNewPath(const std::unordered_map paths) { + std::cout << "test" << std::endl; + emit newPathEntered(paths); } diff --git a/src/Controller/FileController/FileController.hpp b/src/Controller/FileController/FileController.hpp index c073bd9..7314774 100644 --- a/src/Controller/FileController/FileController.hpp +++ b/src/Controller/FileController/FileController.hpp @@ -18,7 +18,8 @@ private: private slots: - void update(const QString path, const FileEvent); + void update(const std::filesystem::path path, const FileEvent); + void emitNewPath(const std::unordered_map paths); signals: // Start signal to start the function in the thread @@ -26,5 +27,7 @@ signals: // Stop signal to stop the function in the thread void pause( ); // Signal to update the path inside the thread - void updatePath(const QString&); + void updatePath(const std::filesystem::path&); + + void newPathEntered(const std::unordered_map paths); }; diff --git a/src/Core/FileMonitor/FileMonitor.cpp b/src/Core/FileMonitor/FileMonitor.cpp index 4450d5e..0d35c99 100644 --- a/src/Core/FileMonitor/FileMonitor.cpp +++ b/src/Core/FileMonitor/FileMonitor.cpp @@ -1,16 +1,19 @@ #include "FileMonitor.hpp" #include #include +#include -FileMonitor::FileMonitor(QString path, +FileMonitor::FileMonitor(std::filesystem::path path, std::chrono::duration delay) : m_path(path), m_delay(delay) { for (auto& file : - std::filesystem::directory_iterator(m_path.toStdString( ))) { - m_paths[QString::fromStdString(file.path( ).string( ))] = - std::filesystem::last_write_time(file); + std::filesystem::directory_iterator(m_path)) { + m_paths[file.path( )] = std::filesystem::last_write_time(file); + std::cout << file.path( ).string( ) << std::endl; } + + emit newPathEntered(m_paths); } void FileMonitor::start( ) { @@ -19,7 +22,7 @@ void FileMonitor::start( ) { auto pbegin = m_paths.begin( ); while (pbegin != m_paths.end( )) { - if (!std::filesystem::exists(pbegin->first.toStdString( ))) { + if (!std::filesystem::exists(pbegin->first)) { emit update(pbegin->first, FileEvent::erased); pbegin = m_paths.erase(pbegin); } else @@ -27,21 +30,19 @@ void FileMonitor::start( ) { } for (auto& file : - std::filesystem::directory_iterator(m_path.toStdString( ))) { + std::filesystem::directory_iterator(m_path)) { 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( ))] = + if (!contains(file.path( ))) { + m_paths[file.path( )] = curr_file_last_write; - emit update(QString::fromStdString(file.path( ).string( )), - FileEvent::created); + emit update(file.path( ), FileEvent::created); } else { - if (m_paths[QString::fromStdString(file.path( ).string( ))] != + if (m_paths[file.path( )] != curr_file_last_write) { - m_paths[QString::fromStdString(file.path( ).string( ))] = + m_paths[file.path( )] = curr_file_last_write; - emit update(QString::fromStdString(file.path( ).string( )), - FileEvent::modified); + emit update(file.path( ), FileEvent::modified); } } } @@ -50,6 +51,6 @@ void FileMonitor::start( ) { void FileMonitor::stop( ) { m_running = false; } -bool FileMonitor::contains(const QString& key) { +bool FileMonitor::contains(const std::filesystem::path& key) { return m_paths.find(key) != m_paths.end( ); } diff --git a/src/Core/FileMonitor/FileMonitor.hpp b/src/Core/FileMonitor/FileMonitor.hpp index 88dbe66..0ff8f8b 100644 --- a/src/Core/FileMonitor/FileMonitor.hpp +++ b/src/Core/FileMonitor/FileMonitor.hpp @@ -1,7 +1,6 @@ #pragma once #include -#include #include #include #include @@ -13,22 +12,23 @@ class FileMonitor : public QObject { public: std::chrono::duration m_delay; - FileMonitor(QString path, std::chrono::duration delay); + FileMonitor(std::filesystem::path path, std::chrono::duration delay); ~FileMonitor( ) = default; - void SetPath(QString path) { m_path = path; } + void SetPath(std::filesystem::path path) { m_path = path; } public slots: void start( ); void stop( ); private: - std::unordered_map m_paths; + std::unordered_map m_paths; bool m_running; - QString m_path; + std::filesystem::path m_path; - bool contains(const QString& key); + bool contains(const std::filesystem::path& key); signals: - void update(const QString path, const FileEvent); + void update(const std::filesystem::path path, const FileEvent); + void newPathEntered(const std::unordered_map paths); }; diff --git a/src/View/GridItemView/GridItemView.cpp b/src/View/GridItemView/GridItemView.cpp new file mode 100644 index 0000000..114d89b --- /dev/null +++ b/src/View/GridItemView/GridItemView.cpp @@ -0,0 +1,20 @@ +#include "GridItemView.hpp" +#include + +GridItemView::GridItemView(QWidget* parent) + : QFrame(parent), + fileController(std::make_shared( )) { + + QGridLayout* mainLayout = new QGridLayout( ); + + connect(fileController.get( ), &FileController::newPathEntered, this, [this, mainLayout](const std::unordered_map paths) { + std::cout << "path.first" << std::endl; + + for (auto path : paths) { + mainLayout->addWidget(new GridItem(path.first, this)); + } + }); + +} + +GridItemView::~GridItemView( ) { } diff --git a/src/View/GridItemView/GridItemView.hpp b/src/View/GridItemView/GridItemView.hpp new file mode 100644 index 0000000..bba8139 --- /dev/null +++ b/src/View/GridItemView/GridItemView.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include + +#include "../Widgets/GridItem/GridItem.hpp" +#include "../../Controller/FileController/FileController.hpp" + +class GridItemView : public QFrame { + Q_OBJECT +private: + int m_rows; + int m_cols; + int m_spacing; + + QList m_gridItemList; + + std::shared_ptr fileController; + +public: + GridItemView(QWidget* parent = nullptr); + ~GridItemView( ); + +private slots: + //void onSizeChanged( ); +}; diff --git a/src/View/MainWidget/MainWidget.cpp b/src/View/MainWidget/MainWidget.cpp index 4053a78..80c833a 100644 --- a/src/View/MainWidget/MainWidget.cpp +++ b/src/View/MainWidget/MainWidget.cpp @@ -2,14 +2,16 @@ MainWidget::MainWidget(QWidget* parent) { - auto path_mainContentLayout = std::make_shared(this); - auto fileTree_fileGridLayout = std::make_shared(this); + auto path_mainContentLayout = new QHBoxLayout(this); + auto fileTree_fileGridLayout = new QVBoxLayout(this); - path_mainContentLayout->addLayout(fileTree_fileGridLayout.get( )); + auto gridLayout = new GridItemView(this); - setLayout(path_mainContentLayout.get( )); + fileTree_fileGridLayout->addWidget(gridLayout); - auto fmc = new FileController( ); + path_mainContentLayout->addLayout(fileTree_fileGridLayout); + + setLayout(path_mainContentLayout); } MainWidget::~MainWidget( ) { } diff --git a/src/View/MainWidget/MainWidget.hpp b/src/View/MainWidget/MainWidget.hpp index 2a61e8d..829405c 100644 --- a/src/View/MainWidget/MainWidget.hpp +++ b/src/View/MainWidget/MainWidget.hpp @@ -3,8 +3,10 @@ #include #include #include +#include -#include "../../Controller/FileController/FileController.hpp" +#include "../Widgets/GridItem/GridItem.hpp" +#include "../GridItemView/GridItemView.hpp" class MainWidget : public QWidget { Q_OBJECT diff --git a/src/View/MainWindow/MainWindow.cpp b/src/View/MainWindow/MainWindow.cpp index 33d3c01..72c137f 100644 --- a/src/View/MainWindow/MainWindow.cpp +++ b/src/View/MainWindow/MainWindow.cpp @@ -1,12 +1,12 @@ #include "MainWindow.hpp" MainWindow::MainWindow(QWidget* parent) - : QMainWindow(parent), m_mainWidget(std::make_shared( )) { + : QMainWindow(parent), m_mainWidget(new MainWidget( )) { setWindowTitle("QutieFM"); setObjectName("MainWindow"); - this->setCentralWidget(m_mainWidget.get( )); + this->setCentralWidget(m_mainWidget); } MainWindow::~MainWindow( ) { } diff --git a/src/View/MainWindow/MainWindow.hpp b/src/View/MainWindow/MainWindow.hpp index 593dbb6..fda47c7 100644 --- a/src/View/MainWindow/MainWindow.hpp +++ b/src/View/MainWindow/MainWindow.hpp @@ -12,5 +12,5 @@ public: private: - std::shared_ptr m_mainWidget; + MainWidget* m_mainWidget; }; diff --git a/src/View/Widgets/GridItem/GridItem.cpp b/src/View/Widgets/GridItem/GridItem.cpp new file mode 100644 index 0000000..08ce9dd --- /dev/null +++ b/src/View/Widgets/GridItem/GridItem.cpp @@ -0,0 +1,67 @@ +#include "GridItem.hpp" + +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); + setMinimumSize(widgetSize); + setMaximumSize(widgetSize); + + auto vLayout = new QVBoxLayout(this); + vLayout->setSpacing(10); + + m_iconLabel = new QLabel(this); + m_iconLabel->setPixmap(m_icon.pixmap(widgetSize.width( ), widgetSize.height( ))); + m_iconLabel->setAlignment(Qt::AlignCenter); + + m_nameLabel = new QLabel(m_name, this); + m_nameLabel->setAlignment(Qt::AlignCenter); + m_nameLabel->setWordWrap(true); + + vLayout->addWidget(m_iconLabel); + vLayout->addWidget(m_nameLabel); +} + +GridItem::~GridItem( ) { } + +QString GridItem::formatText(const QString& text) { + QFontMetrics fm(font( )); + QString elidedText = fm.elidedText(text, Qt::ElideRight, widgetSize.width( )); + QString result; + QStringList lines = elidedText.split('\n'); + for (int i = 0; i < std::min((int)lines.size( ), 3); ++i) + result.append(lines.at(i)).append('\n'); + + if (lines.size( ) > 3) + result.append("..."); + + return result.trimmed( ); +} + +QIcon GridItem::getIconForFileType(const std::filesystem::path path) const { + if (!std::filesystem::is_directory(path)) { + QIcon icon; + QList mime_types = mime_database.mimeTypesForFileName(QString::fromStdString(path.string( ))); + + for (int i = 0; i < mime_types.count( ) && icon.isNull( ); ++i) + icon = QIcon::fromTheme(mime_types[i].iconName( )); + + return icon.isNull( ) ? QApplication::style( )->standardIcon(QStyle::SP_FileIcon) : icon; + } else { + return QIcon::fromTheme("folder"); + } +} + +void GridItem::setIconSize(const QSize& size) { + widgetSize = size; +} + +void GridItem::paintEvent(QPaintEvent* event) { + QStyleOption opt; + opt.initFrom(this); + QPainter p(this); + style( )->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); + QWidget::paintEvent(event); +} diff --git a/src/View/Widgets/GridItem/GridItem.hpp b/src/View/Widgets/GridItem/GridItem.hpp new file mode 100644 index 0000000..58549e1 --- /dev/null +++ b/src/View/Widgets/GridItem/GridItem.hpp @@ -0,0 +1,39 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include + +class GridItem : public QWidget { + Q_OBJECT +private: + QString m_name; + QIcon m_icon; + int m_size; + + std::filesystem::path m_path; + + QSize widgetSize; + QMimeDatabase mime_database; + + QString formatText(const QString& text); + + QIcon getIconForFileType(const std::filesystem::path path) const; + +public: + GridItem(const std::filesystem::path path, QWidget* parent = nullptr); + ~GridItem( ); + + QLabel* m_nameLabel; + QLabel* m_iconLabel; + + void setIconSize(const QSize& size); + +protected: + void paintEvent(QPaintEvent* event)override; +};