commit abec938590ebe4b376abe6bb6c738178d9e0e0a3 Author: florian Date: Tue Jun 3 20:01:13 2025 +0200 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..232fbb6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,27 @@ +# Created by https://www.toptal.com/developers/gitignore/api/cmake +# Edit at https://www.toptal.com/developers/gitignore?templates=cmake + +### CMake ### +CMakeLists.txt.user +CMakeCache.txt +CMakeFiles +CMakeScripts +Testing +Makefile +cmake_install.cmake +install_manifest.txt +compile_commands.json +CTestTestfile.cmake +_deps + +### CMake Patch ### +CMakeUserPresets.json + +# External projects +*-prefix/ + +# End of https://www.toptal.com/developers/gitignore/api/cmake + +build + +data \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..c18c52c --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 3.16) +project(stock_manager) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_AUTOMOC ON) + +find_package(Qt5 REQUIRED COMPONENTS Widgets) +find_package(nlohmann_json REQUIRED) + +include_directories(include src) + +add_executable(stock_manager + main.cpp + src/stockmanager.cpp +) + +target_link_libraries(stock_manager + Qt5::Widgets + nlohmann_json::nlohmann_json +) diff --git a/include/models.hpp b/include/models.hpp new file mode 100644 index 0000000..40b460c --- /dev/null +++ b/include/models.hpp @@ -0,0 +1,18 @@ +#ifndef MODELS_HPP +#define MODELS_HPP + +#include +#include + +struct Item { + QString name; + int quantity; + int limit; +}; + +struct Category { + QString name; + std::vector items; +}; + +#endif // MODELS_HPP diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..6fd0fb8 --- /dev/null +++ b/main.cpp @@ -0,0 +1,14 @@ +#include +#include "stockmanager.h" + +int main(int argc, char *argv[]) { + QApplication app(argc, argv); + StockManager w; + + w.setGeometry(0, 0, 1024, 600); // position en haut Ă  gauche, taille 1024x600 + w.setWindowFlags(Qt::FramelessWindowHint); // pas de barre de fenĂȘtre + w.setFixedSize(1024, 600); // taille non redimensionnable + w.show(); + + return app.exec(); +} diff --git a/src/stockmanager.cpp b/src/stockmanager.cpp new file mode 100644 index 0000000..559876f --- /dev/null +++ b/src/stockmanager.cpp @@ -0,0 +1,131 @@ +#include "stockmanager.h" +#include +#include +#include +#include +#include +#include +#include +#include + +using json = nlohmann::json; + +StockManager::StockManager() { + setWindowTitle("Gestionnaire de Stock"); + + QWidget* central = new QWidget; + QVBoxLayout* mainLayout = new QVBoxLayout; + QHBoxLayout* topLayout = new QHBoxLayout; + + categoryList = new QListWidget; + itemList = new QListWidget; + + topLayout->addWidget(categoryList); + topLayout->addWidget(itemList); + + QHBoxLayout* addCatLayout = new QHBoxLayout; + catNameInput = new QLineEdit; + QPushButton* addCatButton = new QPushButton("Ajouter Categorie"); + addCatLayout->addWidget(catNameInput); + addCatLayout->addWidget(addCatButton); + + QHBoxLayout* addItemLayout = new QHBoxLayout; + itemNameInput = new QLineEdit; + itemQtyInput = new QSpinBox; + itemLimitInput = new QSpinBox; + itemQtyInput->setMaximum(10000); + itemLimitInput->setMaximum(10000); + QPushButton* addItemButton = new QPushButton("Ajouter Objet"); + addItemLayout->addWidget(itemNameInput); + addItemLayout->addWidget(itemQtyInput); + addItemLayout->addWidget(itemLimitInput); + addItemLayout->addWidget(addItemButton); + + QPushButton* saveButton = new QPushButton("Sauvegarder"); + QPushButton* loadButton = new QPushButton("Charger"); + + mainLayout->addLayout(topLayout); + mainLayout->addLayout(addCatLayout); + mainLayout->addLayout(addItemLayout); + mainLayout->addWidget(saveButton); + mainLayout->addWidget(loadButton); + central->setLayout(mainLayout); + setCentralWidget(central); + + connect(addCatButton, &QPushButton::clicked, this, &StockManager::addCategory); + connect(addItemButton, &QPushButton::clicked, this, &StockManager::addItem); + connect(saveButton, &QPushButton::clicked, this, &StockManager::saveToFile); + connect(loadButton, &QPushButton::clicked, this, &StockManager::loadFromFile); + connect(categoryList, &QListWidget::currentTextChanged, this, &StockManager::updateItemList); +} + +void StockManager::addCategory() { + QString name = catNameInput->text(); + if (name.isEmpty() || categories.count(name)) return; + categories[name] = Category{name}; + categoryList->addItem(name); + catNameInput->clear(); +} + +void StockManager::addItem() { + QString catName = categoryList->currentItem() ? categoryList->currentItem()->text() : ""; + if (catName.isEmpty()) return; + QString name = itemNameInput->text(); + int qty = itemQtyInput->value(); + int lim = itemLimitInput->value(); + if (name.isEmpty()) return; + + auto& items = categories[catName].items; + for (const auto& i : items) + if (i.name == name) return; + + items.push_back(Item{name, qty, lim}); + updateItemList(catName); + itemNameInput->clear(); +} + +void StockManager::updateItemList(const QString& catName) { + itemList->clear(); + for (const auto& item : categories[catName].items) { + itemList->addItem(item.name + " | Stock: " + QString::number(item.quantity) + " / Limite: " + QString::number(item.limit)); + } +} + +void StockManager::saveToFile() { + QString filename = QFileDialog::getSaveFileName(this, "Sauvegarder sous", "stock.json"); + if (filename.isEmpty()) return; + json j; + for (const auto& [catName, cat] : categories) { + for (const auto& item : cat.items) { + j[catName.toStdString()].push_back({{"name", item.name.toStdString()}, {"quantity", item.quantity}, {"limit", item.limit}}); + } + } + std::ofstream file(filename.toStdString()); + file << j.dump(4); +} + +void StockManager::loadFromFile() { + QString filename = QFileDialog::getOpenFileName(this, "Charger depuis", "stock.json"); + if (filename.isEmpty()) return; + std::ifstream file(filename.toStdString()); + if (!file) return; + + json j; + file >> j; + categories.clear(); + categoryList->clear(); + itemList->clear(); + + for (auto& [catName, items] : j.items()) { + Category cat{QString::fromStdString(catName)}; + for (auto& it : items) { + cat.items.push_back(Item{ + QString::fromStdString(it["name"]), + it["quantity"], + it["limit"] + }); + } + categories[QString::fromStdString(catName)] = cat; + categoryList->addItem(QString::fromStdString(catName)); + } +} diff --git a/src/stockmanager.h b/src/stockmanager.h new file mode 100644 index 0000000..34878d2 --- /dev/null +++ b/src/stockmanager.h @@ -0,0 +1,35 @@ +#ifndef STOCKMANAGER_H +#define STOCKMANAGER_H + +#include +#include +#include +#include +#include +#include "models.hpp" + +class StockManager : public QMainWindow { + Q_OBJECT + +private: + std::unordered_map categories; + + QListWidget* categoryList; + QListWidget* itemList; + QLineEdit* catNameInput; + QLineEdit* itemNameInput; + QSpinBox* itemQtyInput; + QSpinBox* itemLimitInput; + +public: + StockManager(); + +private slots: + void addCategory(); + void addItem(); + void updateItemList(const QString& catName); + void saveToFile(); + void loadFromFile(); +}; + +#endif // STOCKMANAGER_H