Switch from Qt6 to Slint

This commit is contained in:
Vyn 2024-08-16 21:35:12 +02:00
parent f8be14bcf8
commit 63bf267a22
107 changed files with 27532 additions and 2896 deletions

1
lib/Catch2 Submodule

@ -0,0 +1 @@
Subproject commit f2320724a74f95d83f8c259e1a96cf2cd9a1d6aa

65
lib/cpp-utils/debug.h Normal file
View file

@ -0,0 +1,65 @@
/*
* Mirai. Copyright (C) 2024 Vyn
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#ifndef CPP_UTILS_DEBUG_H
#define CPP_UTILS_DEBUG_H
#include <algorithm>
#include <chrono>
#include <iostream>
#include <ostream>
#include <string>
namespace cpputils::debug
{
class Timer
{
public:
Timer() : startTime(std::chrono::steady_clock::now())
{
}
void start()
{
startTime = std::chrono::steady_clock::now();
isRunning = true;
}
void stop()
{
const auto now = std::chrono::steady_clock::now();
duration += std::chrono::duration_cast<std::chrono::milliseconds>(now - startTime).count();
isRunning = false;
}
void reset()
{
duration = 0;
isRunning = false;
}
void printTimeElapsed(const std::string &message) const
{
long durationToShow = duration;
if (isRunning) {
const auto now = std::chrono::steady_clock::now();
durationToShow +=
std::chrono::duration_cast<std::chrono::milliseconds>(now - startTime).count();
}
std::cout << "[Debug - Timer] " << message << ": " << durationToShow << " ms" << std::endl;
}
private:
std::chrono::time_point<std::chrono::steady_clock, std::chrono::nanoseconds> startTime;
// Timer are always running when created. You can use .reset() after creation.
bool isRunning = true;
long duration = 0;
};
} // namespace cpputils::debug
#endif

27
lib/cpp-utils/string.h Normal file
View file

@ -0,0 +1,27 @@
/*
* Mirai. Copyright (C) 2024 Vyn
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#ifndef CPP_UTILS_STRING_H
#define CPP_UTILS_STRING_H
#include <algorithm>
#include <string>
namespace cpputils::string {
inline std::string trim(std::string str) {
str.erase(str.begin(), std::ranges::find_if(str, [](unsigned char ch) {
return !std::isspace(ch);
}));
str.erase(std::ranges::find_if(str.rbegin(), str.rend(), [](unsigned char ch) {
return !std::isspace(ch);
}).base(), str.end());
return str;
}
}
#endif

48
lib/cpp-utils/vector.h Normal file
View file

@ -0,0 +1,48 @@
/*
* Mirai. Copyright (C) 2024 Vyn
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#ifndef CPP_UTILS_VECTOR_H
#define CPP_UTILS_VECTOR_H
#include <algorithm>
#include <vector>
namespace cpputils::vector
{
template <typename C, typename T>
concept AnyIterable = std::same_as<typename C::value_type, T> && requires(C c) {
{
c.begin()
} -> std::forward_iterator;
{
c.end()
} -> std::forward_iterator;
{
const_cast<const C &>(c).begin()
} -> std::forward_iterator;
{
const_cast<const C &>(c).end()
} -> std::forward_iterator;
};
template <typename T> bool contains(const std::vector<T> &vec, const T &value)
{
return std::ranges::find(vec, value) != vec.end();
}
template <typename T> bool containsAll(const std::vector<T> &vec, const AnyIterable<T> auto vec2)
{
for (auto &elem : vec) {
if (!contains(vec2, elem)) {
return false;
}
}
return true;
}
} // namespace cpputils::vector
#endif

View file

@ -0,0 +1,40 @@
/*
* Mirai. Copyright (C) 2024 Vyn
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#ifndef MIRAI_BASE_FILE_RESOURCE_H
#define MIRAI_BASE_FILE_RESOURCE_H
#include "BaseResource.h"
#include <string>
namespace mirai
{
struct BaseFileResourceConstructor {
std::string name;
std::string path;
};
class BaseFileResource : public BaseResource
{
public:
BaseFileResource(BaseFileResourceConstructor data)
: BaseResource(data.name), path(data.path) {};
BaseFileResource(BaseFileResource &) = delete;
BaseFileResource(BaseFileResource &&) = delete;
~BaseFileResource() override = default;
const std::string &getPath() const
{
return path;
}
private:
std::string path;
};
} // namespace mirai
#endif

View file

@ -0,0 +1,127 @@
/*
* Mirai. Copyright (C) 2024 Vyn
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#include "BaseResource.h"
#include "TaskItem.h"
#include "mirai-core/Day.h"
#include <algorithm>
#include <functional>
#include <iostream>
#include <memory>
#include <ostream>
namespace mirai
{
void BaseResource::setDirty(bool shouldBeDirty)
{
isDirty_ = shouldBeDirty;
}
bool BaseResource::isDirty() const
{
return isDirty_;
}
Day *BaseResource::day(const Date &date)
{
auto dayFound = std::find_if(days_.begin(), days_.end(), [&](std::unique_ptr<Day> &day) {
return day->getDate().day == date.day && day->getDate().month == date.month &&
day->getDate().year == date.year;
});
if (dayFound == days_.end()) {
auto newDay = std::make_unique<Day>(this, DayData{.date = date});
Day *dayPtr = newDay.get();
days_.push_back(std::move(newDay));
return dayPtr;
}
return dayFound->get();
}
std::vector<std::unique_ptr<Day>> *BaseResource::days()
{
return &days_;
}
void BaseResource::addDay(const DayData &dayData)
{
days_.push_back(std::make_unique<Day>(this, dayData));
setDirty(true);
}
TaskItem *BaseResource::getTaskById(int taskId)
{
for (auto &day : days_) {
for (auto &task : *day->tasks()) {
if (task->id() == taskId) {
return task.get();
}
}
for (auto &event : *day->events()) {
for (auto &task : *event->tasks()) {
if (task->id() == taskId) {
return task.get();
}
}
}
}
return nullptr;
}
void BaseResource::deleteTask(const TaskItem &taskToDelete)
{
for (auto &day : days_) {
for (auto &task : *day->tasks()) {
if (task->id() == taskToDelete.id()) {
day->deleteTask(taskToDelete);
return;
}
}
for (auto &event : *day->events()) {
for (auto &task : *event->tasks()) {
if (task->id() == taskToDelete.id()) {
event->deleteTask(taskToDelete);
return;
}
}
}
}
}
Event *BaseResource::getEventById(int eventId)
{
for (auto &day : days_) {
for (auto &event : *day->events()) {
if (event->id() == eventId) {
return event.get();
}
}
}
return nullptr;
}
/*void BaseResource::addTask(TaskItemData taskItem)*/
/*{*/
/*tasks.push_back(std::make_unique<TaskItem>(this, taskItem));*/
/*setDirty(true);*/
/*}*/
/*void BaseResource::removeTask(const TaskItem *taskToRemove)*/
/*{*/
/*auto findFunction = [&](const std::unique_ptr<TaskItem> &taskInFilter) {*/
/*return taskInFilter.get() == taskToRemove;*/
/*};*/
/*auto taskToDelete = std::remove_if(tasks_.begin(), tasks_.end(), findFunction);*/
/*if (taskToDelete == tasks_.end()) {*/
/*return;*/
/*}*/
/*tasks_.erase(taskToDelete);*/
/*setDirty(true);*/
/*}*/
int BaseResource::nextId_ = 0;
} // namespace mirai

View file

@ -0,0 +1,73 @@
/*
* Mirai. Copyright (C) 2024 Vyn
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#ifndef MIRAI_BASE_RESOURCE_H
#define MIRAI_BASE_RESOURCE_H
#include "TaskItem.h"
#include "mirai-core/DateTime.h"
#include "mirai-core/Day.h"
#include "mirai-core/Event.h"
#include <memory>
#include <optional>
#include <string>
#include <vector>
namespace mirai
{
class BaseResource
{
public:
struct AddTaskData {
std::string text;
Tags tags;
};
BaseResource(std::string name) : name_(name) {};
BaseResource(BaseResource &) = delete;
BaseResource(BaseResource &&) = delete;
virtual ~BaseResource() = default;
virtual void save() = 0;
virtual void load() = 0;
void setName(std::string name)
{
this->name_ = name;
}
const std::string &getName() const
{
return name_;
}
void addDay(const DayData &dayData);
Day *day(const Date &date);
std::vector<std::unique_ptr<Day>> *days();
TaskItem *getTaskById(int taskId);
Event *getEventById(int eventId);
void deleteTask(const TaskItem &task);
void setDirty(bool shouldBeDirty);
bool isDirty() const;
int id() const
{
return id_;
}
private:
static int nextId_;
int id_ = nextId_++;
std::string name_;
std::vector<std::unique_ptr<Day>> days_;
bool isDirty_ = false;
};
} // namespace mirai
#endif

35
lib/mirai-core/Config.cpp Normal file
View file

@ -0,0 +1,35 @@
/*
* Mirai. Copyright (C) 2024 Vyn
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#include "Config.h"
#include "nlohmann/json.hpp"
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
namespace mirai
{
Config::Config(const std::string &path) : path_(path)
{
std::ifstream file(path_);
configJson_ = nlohmann::json::parse(file);
}
std::vector<std::string> Config::sources() const
{
std::vector<std::string> sources;
assert(configJson_.is_object());
assert(configJson_["files"].is_array());
auto jsonSources = configJson_["files"];
for (const auto &filePath : jsonSources) {
assert(filePath.is_string());
sources.push_back(filePath.get<std::string>());
}
return sources;
}
}; // namespace mirai

27
lib/mirai-core/Config.h Normal file
View file

@ -0,0 +1,27 @@
/*
* Mirai. Copyright (C) 2024 Vyn
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#pragma once
#include "nlohmann/json.hpp"
#include <string>
#include <vector>
namespace mirai
{
class Config
{
public:
Config(const std::string &path);
std::vector<std::string> sources() const;
private:
std::string path_;
nlohmann::json configJson_;
};
} // namespace mirai

View file

@ -0,0 +1,37 @@
/*
* Mirai. Copyright (C) 2024 Vyn
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#include "mirai-core/DateTime.h"
#include <charconv>
#include <optional>
#include <ranges>
#include <string>
#include <vector>
namespace mirai
{
std::optional<mirai::Date> stringToDate(const std::string &dateStr)
{
using std::operator""sv;
auto dateSplitView =
dateStr | std::views::split("-"sv) | std::views::transform([](auto v) -> int {
int i = 0;
std::from_chars(v.data(), v.data() + v.size(), i);
return i;
});
std::vector<int> dateSplit{dateSplitView.begin(), dateSplitView.end()};
if (dateSplit.size() != 3) {
return std::nullopt;
}
auto year = dateSplit[0];
auto month = dateSplit[1];
auto day = dateSplit[2];
return mirai::Date(year, static_cast<unsigned>(month), static_cast<unsigned>(day));
}
} // namespace mirai

95
lib/mirai-core/DateTime.h Normal file
View file

@ -0,0 +1,95 @@
/*
* Mirai. Copyright (C) 2024 Vyn
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#pragma once
#include <bits/chrono.h>
#include <chrono>
#include <ctime>
#include <optional>
#include <string>
namespace mirai
{
struct Date {
explicit Date(int year, unsigned month, unsigned day) : year(year), month(month), day(day)
{
}
explicit Date(std::chrono::time_point<std::chrono::system_clock> tp)
{
auto chronoDate = std::chrono::year_month_day(std::chrono::floor<std::chrono::days>(tp));
year = static_cast<int>(chronoDate.year());
month = static_cast<unsigned>(chronoDate.month());
day = static_cast<unsigned>(chronoDate.day());
}
explicit Date(std::chrono::year_month_day chronoDate)
{
year = static_cast<int>(chronoDate.year());
month = static_cast<unsigned>(chronoDate.month());
day = static_cast<unsigned>(chronoDate.day());
}
int year;
unsigned month;
unsigned day;
bool operator==(const Date &other) const
{
return other.year == year && other.month == month && other.day == day;
}
bool operator<(const Date &other) const
{
if (year < other.year) {
return true;
}
if (year == other.year && month < other.month) {
return true;
}
if (year == other.year && month == other.month && day < other.day) {
return true;
}
return false;
}
bool operator>(const Date &other) const
{
return !(*this < other) && !(*this == other);
}
};
struct Time {
int hour;
int minute;
bool operator==(const Time &other) const
{
return other.hour == hour && other.minute == minute;
}
bool operator<(const Time &other) const
{
if (hour < other.hour) {
return true;
}
if (hour == other.hour && minute < other.minute) {
return true;
}
return false;
}
bool operator>(const Time &other) const
{
return !(*this < other) && !(*this == other);
}
};
std::optional<mirai::Date> stringToDate(const std::string &dateStr);
} // namespace mirai

89
lib/mirai-core/Day.cpp Normal file
View file

@ -0,0 +1,89 @@
/*
* Mirai. Copyright (C) 2024 Vyn
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#include "Day.h"
#include "mirai-core/BaseResource.h"
#include <algorithm>
#include <iostream>
#include <memory>
#include <ostream>
#include <vector>
namespace mirai
{
Day::Day(BaseResource *source, const DayData &data) : source_(source), data_(data)
{
}
Event *Day::createEvent(const EventData &eventData)
{
auto event = std::make_unique<Event>(source_, this, eventData);
mirai::Event *eventPtr = event.get();
events_.push_back(std::move(event));
onChange();
return eventPtr;
}
std::vector<std::unique_ptr<Event>> *Day::events()
{
return &events_;
}
const Date &Day::getDate() const
{
return data_.date;
}
TaskItem *Day::createTask(const TaskItemData &taskData)
{
auto task = std::make_unique<TaskItem>(source_, this, taskData);
mirai::TaskItem *taskPtr = task.get();
tasks_.push_back(std::move(task));
onChange();
return taskPtr;
}
void Day::deleteTask(const TaskItem &taskToDelete)
{
int id = taskToDelete.id();
tasks_.erase(
std::remove_if(
tasks_.begin(), tasks_.end(),
[&](const std::unique_ptr<TaskItem> &task) {
return task->id() == id;
}
),
tasks_.end()
);
onChange();
}
void Day::deleteEvent(const Event &eventToDelete)
{
int id = eventToDelete.id();
events_.erase(
std::remove_if(
events_.begin(), events_.end(),
[&](const std::unique_ptr<Event> &event) {
return event->id() == id;
}
),
events_.end()
);
onChange();
}
void Day::onChange()
{
source()->setDirty(true);
}
std::vector<std::unique_ptr<TaskItem>> *Day::tasks()
{
return &tasks_;
}
} // namespace mirai

66
lib/mirai-core/Day.h Normal file
View file

@ -0,0 +1,66 @@
/*
* Mirai. Copyright (C) 2024 Vyn
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#pragma once
#include "mirai-core/DateTime.h"
#include "mirai-core/Event.h"
#include "mirai-core/TaskItem.h"
#include "using.h"
#include <memory>
#include <string>
#include <vector>
namespace mirai
{
class BaseResource;
struct DayData {
Date date;
std::vector<EventData> events;
std::vector<TaskItemData> tasks;
};
class Day
{
public:
Day(BaseResource *source, const DayData &data);
void setDate(const Date &date);
Event *createEvent(const EventData &eventData);
TaskItem *createTask(const TaskItemData &taskData);
void deleteTask(const TaskItem &taskToDelete);
void deleteEvent(const Event &eventToDelete);
std::vector<std::unique_ptr<Event>> *events();
std::vector<std::unique_ptr<TaskItem>> *tasks();
const Date &getDate() const;
Event *getEventById(int eventId)
{
for (auto &event : events_) {
if (event->id() == eventId) {
return event.get();
}
}
return nullptr;
}
BaseResource *source()
{
return source_;
}
private:
void onChange();
BaseResource *source_;
std::vector<std::unique_ptr<Event>> events_;
std::vector<std::unique_ptr<TaskItem>> tasks_;
DayData data_;
};
} // namespace mirai

99
lib/mirai-core/Event.cpp Normal file
View file

@ -0,0 +1,99 @@
/*
* Mirai. Copyright (C) 2024 Vyn
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#include "Event.h"
#include "BaseResource.h"
#include "mirai-core/TaskItem.h"
#include "utils.h"
namespace mirai
{
Event::Event(BaseResource *source, Day *parent, const EventData &data)
: source_(source), day_(parent), data(data)
{
}
void Event::setText(const std::string &text)
{
this->data.description = text;
onChange();
}
const std::string &Event::getText() const
{
return data.description;
}
const Date &Event::getDate() const
{
return data.date;
}
const Time &Event::getStartTime() const
{
return data.startTime;
}
const Time &Event::getEndTime() const
{
return data.endTime;
}
const Tags &Event::getTags() const
{
return data.tags;
}
bool Event::hasTag(const std::string &tag) const
{
return vectorUtils::contains(data.tags, tag);
}
TaskItem *Event::createTask(const TaskItemData &taskData)
{
auto task = std::make_unique<TaskItem>(source_, day_, taskData);
mirai::TaskItem *taskPtr = task.get();
tasks_.push_back(std::move(task));
onChange();
return taskPtr;
}
void Event::deleteTask(const TaskItem &taskToDelete)
{
tasks_.erase(std::remove_if(
tasks_.begin(), tasks_.end(),
[&](const std::unique_ptr<TaskItem> &task) {
return task->id() == taskToDelete.id();
}
));
}
std::vector<std::unique_ptr<TaskItem>> *Event::tasks()
{
return &tasks_;
}
void Event::setStartTime(const Time &time)
{
data.startTime = time;
onChange();
}
void Event::setEndTime(const Time &time)
{
data.endTime = time;
onChange();
}
void Event::onChange()
{
source_->setDirty(true);
}
int Event::nextId = 0;
} // namespace mirai

80
lib/mirai-core/Event.h Normal file
View file

@ -0,0 +1,80 @@
/*
* Mirai. Copyright (C) 2024 Vyn
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#pragma once
#include "mirai-core/DateTime.h"
#include "mirai-core/TaskItem.h"
#include "using.h"
#include <memory>
#include <string>
#include <vector>
namespace mirai
{
class BaseResource;
struct EventData {
std::string description;
Date date;
Time startTime;
Time endTime;
Tags tags;
std::vector<TaskItemData> tasks;
};
class Day;
class Event
{
private:
public:
Event(BaseResource *source, Day *parent, const EventData &data);
Event &operator=(const EventData &newData)
{
data = newData;
onChange();
return *this;
};
void setText(const std::string &text);
void setStartTime(const Time &time);
void setEndTime(const Time &time);
TaskItem *createTask(const TaskItemData &taskData);
void deleteTask(const TaskItem &taskToDelete);
std::vector<std::unique_ptr<TaskItem>> *tasks();
const std::string &getText() const;
const Date &getDate() const;
const Time &getStartTime() const;
const Time &getEndTime() const;
const Tags &getTags() const;
bool hasTag(const std::string &tag) const;
int id() const
{
return id_;
}
Day *day()
{
return day_;
}
private:
void onChange();
static int nextId;
int id_ = nextId++;
Day *day_;
EventData data;
std::vector<std::unique_ptr<TaskItem>> tasks_;
BaseResource *source_;
};
} // namespace mirai

View file

@ -0,0 +1,7 @@
/*
* Mirai. Copyright (C) 2024 Vyn
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#include "EventEmitter.h"

View file

@ -0,0 +1,28 @@
/*
* Mirai. Copyright (C) 2024 Vyn
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#include <functional>
#include <vector>
template <typename T> class EventEmitter
{
public:
void registerCallback(std::function<T> func)
{
callbacks.push_back(func);
}
void emit(T data)
{
for (auto &callback : callbacks) {
callback(data);
}
}
private:
std::vector<std::function<T>> callbacks;
};

92
lib/mirai-core/Mirai.cpp Normal file
View file

@ -0,0 +1,92 @@
/*
* Mirai. Copyright (C) 2024 Vyn
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#include "Mirai.h"
#include "TaskItem.h"
#include "cpp-utils/debug.h"
#include "mirai-core/Config.h"
#include "utils.h"
#include <algorithm>
#include <iostream>
#include <memory>
#include <optional>
#include <ostream>
#include <vector>
namespace mirai
{
void Mirai::loadResource(std::unique_ptr<BaseResource> &&resource)
{
resource->load();
resources_.push_back(std::move(resource));
reloadTags();
};
void Mirai::unloadAllResources()
{
resources_.clear();
}
void Mirai::save()
{
for (auto &resource : resources_) {
if (resource->isDirty()) {
resource->save();
resource->setDirty(false);
}
}
reloadTags();
}
void Mirai::removeTask(const TaskItem *taskItem)
{
for (auto &resource : resources_) {
// resource->removeTask(taskItem); // TODO REWORK
}
}
std::vector<std::unique_ptr<BaseResource>> &Mirai::getResources()
{
return resources_;
}
std::optional<std::reference_wrapper<BaseResource>> Mirai::getResourceByName(const std::string &name
)
{
auto resourceIterator =
std::ranges::find_if(resources_, [&](const std::unique_ptr<BaseResource> &resource) {
return resource->getName() == name;
});
if (resourceIterator == resources_.end()) {
return std::nullopt;
}
return *(resourceIterator->get());
}
const std::vector<std::string> &Mirai::getTags()
{
return tags_;
}
void Mirai::reloadTags()
{
/*cpputils::debug::Timer reloadingTagsDuration;*/ // TODO REWORK
/*tags.clear();*/
/*for (auto &resource : resources) {*/
/*for (auto &task : resource->getTasks()) {*/
/*for (auto &tag : task->getTags()) {*/
/*if (!vectorUtils::contains(tags, tag)) {*/
/*tags.push_back(tag);*/
/*}*/
/*}*/
/*}*/
/*}*/
/*reloadingTagsDuration.printTimeElapsed("ReloadingTags");*/
}
} // namespace mirai

55
lib/mirai-core/Mirai.h Normal file
View file

@ -0,0 +1,55 @@
/*
* Mirai. Copyright (C) 2024 Vyn
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#ifndef MIRAI_MIRAI_H
#define MIRAI_MIRAI_H
#include "BaseFileResource.h"
#include "TaskItem.h"
#include "TodoMd.h"
#include "mirai-core/BaseResource.h"
#include <algorithm>
#include <functional>
#include <map>
#include <memory>
#include <optional>
#include <string>
namespace mirai
{
class Mirai
{
public:
void loadResource(std::unique_ptr<BaseResource> &&resource);
void unloadAllResources();
void save();
void removeTask(const TaskItem *taskItem);
std::optional<std::reference_wrapper<BaseResource>> getResourceByName(const std::string &name);
std::vector<std::unique_ptr<BaseResource>> &getResources();
const std::vector<std::string> &getTags();
void reloadTags();
// new stuff
// Returns a non owning pointer to the requested resource or nullptr if not found.
BaseResource *getResourceById(int id)
{
if (id >= resources_.size()) {
return nullptr;
}
return resources_.at(id).get();
}
private:
std::vector<std::unique_ptr<BaseResource>> resources_;
std::vector<std::string> tags_;
};
} // namespace mirai
#endif

View file

@ -0,0 +1,74 @@
/*
* Mirai. Copyright (C) 2024 Vyn
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#ifndef MIRAI_STD_FILE_RESOURCE_H
#define MIRAI_STD_FILE_RESOURCE_H
#include "BaseFileResource.h"
#include "TodoMd.h"
#include <fstream>
#include <iostream>
#include <ostream>
#include <string>
namespace mirai
{
class StdFileResource : public BaseFileResource
{
public:
StdFileResource(BaseFileResourceConstructor data) : BaseFileResource(data) {};
StdFileResource(StdFileResource &) = delete;
StdFileResource(StdFileResource &&) = delete;
StdFileResource operator=(StdFileResource &) = delete;
StdFileResource operator=(StdFileResource &&) = delete;
~StdFileResource() override = default;
void save() override
{
std::ofstream file(getPath());
if (!file.is_open()) {
throw std::runtime_error("can't create " + getPath());
}
const std::string content = TodoMdFormat::stringify(getName(), *days());
file << content;
file.close();
};
void load() override
{
std::ifstream file(getPath());
if (!file.is_open()) {
return;
}
std::string content = "";
std::string line;
while (std::getline(file, line)) {
content += line + "\n";
}
file.close();
auto result = TodoMdFormat::parse(content);
for (const auto &dayData : result.days) {
Day *newDay = day(dayData.date);
for (const auto &eventData : dayData.events) {
Event *newEvent = newDay->createEvent(eventData);
for (const auto &taskData : eventData.tasks) {
TaskItem *newTask = newEvent->createTask(taskData);
}
}
for (const auto &taskData : dayData.tasks) {
TaskItem *newTask = newDay->createTask(taskData);
}
}
setName(result.name);
};
};
} // namespace mirai
#endif

View file

@ -0,0 +1,65 @@
/*
* Mirai. Copyright (C) 2024 Vyn
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#include "TaskItem.h"
#include "BaseResource.h"
#include "utils.h"
#include <iostream>
namespace mirai
{
int TaskItem::nextId = 0;
TaskItem::TaskItem(BaseResource *source, Day *day, TaskItemData data)
: source_(source), day_(day), data(data)
{
}
void TaskItem::markAsDone()
{
data.state = DONE;
onChange();
}
void TaskItem::markAsUndone()
{
data.state = TODO;
onChange();
}
void TaskItem::setText(const std::string &text)
{
this->data.text = text;
onChange();
}
const std::string &TaskItem::getText() const
{
return data.text;
}
const TaskItemState &TaskItem::getState() const
{
return data.state;
}
const Tags &TaskItem::getTags() const
{
return data.tags;
}
bool TaskItem::hasTag(const std::string &tag) const
{
return vectorUtils::contains(data.tags, tag);
}
void TaskItem::onChange()
{
source()->setDirty(true);
}
} // namespace mirai

81
lib/mirai-core/TaskItem.h Normal file
View file

@ -0,0 +1,81 @@
/*
* Mirai. Copyright (C) 2024 Vyn
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#ifndef MIRAI_TASKITEM_H
#define MIRAI_TASKITEM_H
#include "using.h"
#include <functional>
#include <string>
namespace mirai
{
class BaseResource;
class Day;
enum TaskItemState { TODO, DONE };
struct TaskItemData {
std::string text;
TaskItemState state;
Tags tags;
};
class TaskItem
{
public:
TaskItem(BaseResource *source, Day *day, const TaskItemData data);
TaskItem &operator=(const TaskItemData &newData)
{
data = newData;
onChange();
return *this;
};
void markAsDone();
void markAsUndone();
void setDate(const std::string &date);
void setText(const std::string &text);
const std::string &getLine() const;
const std::string &getText() const;
const TaskItemState &getState() const;
const Tags &getTags() const;
bool hasTag(const std::string &tag) const;
int id() const
{
return id_;
}
BaseResource *source()
{
return source_;
}
Day *day()
{
return day_;
}
private:
void onChange();
static int nextId;
int id_ = nextId++;
TaskItemData data;
BaseResource *source_;
Day *day_;
};
} // namespace mirai
#endif

View file

@ -0,0 +1,176 @@
/*
* Mirai. Copyright (C) 2024 Vyn
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#include "TasksView.h"
#include "Mirai.h"
#include "mirai-core/TaskItem.h"
#include "utils.h"
#include <algorithm>
#include <chrono>
#include <cstddef>
#include <ctime>
#include <format>
#include <iostream>
#include <memory>
#include <ostream>
#include <stdexcept>
#include <string>
#include <vector>
namespace mirai
{
TasksView::TasksView(Mirai *miraiInstance) : mirai(miraiInstance)
{
if (!miraiInstance) {
throw std::runtime_error("NULL pointer passed in TasksView");
}
update();
}
FilteredDay &TasksView::operator[](int index)
{
return filteredDays.at(index);
}
size_t TasksView::count() const
{
return filteredDays.size();
}
void TasksView::update()
{
auto todayDate = std::format("{:%Y-%m-%d}", std::chrono::system_clock::now());
filteredDays.clear();
for (auto &file : mirai->getResources()) {
if (resourcesFilter.size() > 0 &&
!vectorUtils::contains(resourcesFilter, file->getName())) {
continue;
}
for (auto &day : *file->days()) {
FilteredDay filteredDay{.day = day.get()};
for (auto &task : *day->tasks()) {
auto taskDate = std::format(
"{}-0{}-{}", day->getDate().year, // TODO REWORK REMOVE 0{}
day->getDate().month,
day->getDate().day // TODO REWORK CLEAN THIS MESS
);
if (shouldHideCompletedTasks() && task->getState() == DONE &&
taskDate < todayDate) {
continue;
}
if (tagsFilter.size() > 0 &&
!vectorUtils::containsAll(tagsFilter, task->getTags())) {
continue;
}
filteredDay.filteredTasks.push_back(task.get());
}
for (auto &event : *day->events()) {
FilteredEvent filteredEvent{.event = event.get()};
auto eventDate = std::format(
// TODO REWORK
"{}-0{}-{}", day->getDate().year, day->getDate().month, day->getDate().day
);
for (auto &task : *event->tasks()) {
if (shouldHideCompletedTasks() && task->getState() == DONE &&
eventDate < todayDate) {
continue;
}
if (tagsFilter.size() > 0 &&
!vectorUtils::containsAll(tagsFilter, task->getTags())) {
continue;
}
filteredEvent.filteredTasks.push_back(task.get());
}
if (!filteredEvent.filteredTasks.empty() || !shouldHideCompletedTasks() ||
eventDate >= todayDate) {
filteredDay.filteredEvents.push_back(filteredEvent);
}
}
if (!filteredDay.filteredEvents.empty()) {
std::ranges::sort(
filteredDay.filteredEvents,
[](const FilteredEvent &t1, const FilteredEvent &t2) {
return t1.event->getStartTime().hour < t2.event->getStartTime().hour;
}
);
}
if (!filteredDay.filteredEvents.empty() || !filteredDay.filteredTasks.empty()) {
auto existingDay = std::ranges::find_if(filteredDays, [&](const FilteredDay &date) {
return date.day->getDate() == filteredDay.day->getDate();
});
if (existingDay == filteredDays.end()) {
filteredDays.push_back(filteredDay);
} else {
existingDay->filteredTasks.insert(
existingDay->filteredTasks.end(), filteredDay.filteredTasks.begin(),
filteredDay.filteredTasks.end()
);
existingDay->filteredEvents.insert(
existingDay->filteredEvents.end(), filteredDay.filteredEvents.begin(),
filteredDay.filteredEvents.end()
);
}
}
}
std::ranges::sort(filteredDays, [](const FilteredDay &t1, const FilteredDay &t2) {
return t1.day->getDate() < t2.day->getDate();
});
}
}
void TasksView::addTagFilter(const std::string &tag)
{
tagsFilter.push_back(tag);
update();
}
void TasksView::removeTagFilter(const std::string &tag)
{
tagsFilter
.erase(std::remove_if(tagsFilter.begin(), tagsFilter.end(), [&](const auto &tagInFilter) {
return tag == tagInFilter;
}));
update();
}
void TasksView::addResourceFilter(const std::string &fileName)
{
resourcesFilter.push_back(fileName);
update();
}
void TasksView::removeResourceFilter(const std::string &fileName)
{
resourcesFilter.erase(std::remove_if(
resourcesFilter.begin(), resourcesFilter.end(),
[&](const auto &fileInFilter) {
return fileName == fileInFilter;
}
));
update();
}
void TasksView::removeFilters()
{
tagsFilter.clear();
update();
}
const std::vector<std::string> &TasksView::getActiveTagsFilter()
{
return tagsFilter;
}
const std::vector<std::string> &TasksView::getActiveFilesFilter()
{
return resourcesFilter;
}
} // namespace mirai

View file

@ -0,0 +1,87 @@
/*
* Mirai. Copyright (C) 2024 Vyn
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#ifndef MIRAI_TASKSVIEW_H
#define MIRAI_TASKSVIEW_H
#include "Mirai.h"
#include "mirai-core/TaskItem.h"
#include "using.h"
#include <cstddef>
#include <string>
#include <vector>
namespace mirai
{
struct FilteredEvent {
Event *event;
std::vector<TaskItem *> filteredTasks;
};
struct FilteredDay {
Day *day;
std::vector<FilteredEvent> filteredEvents;
std::vector<TaskItem *> filteredTasks;
};
class TasksView
{
public:
TasksView(Mirai *mirai);
FilteredDay &operator[](int index);
size_t count() const;
void update();
void addTagFilter(const std::string &tag);
void removeTagFilter(const std::string &tag);
void addResourceFilter(const std::string &fileName);
void removeResourceFilter(const std::string &fileName);
void removeFilters();
const Tags &getActiveTagsFilter();
const std::vector<std::string> &getActiveFilesFilter();
void hideCompletedTasks(bool hide)
{
shouldHideCompletedTasks_ = hide;
}
bool shouldHideCompletedTasks() const
{
return shouldHideCompletedTasks_;
}
/*auto begin()*/ // TODO REWORK
/*{*/
/*return tasksToShow.begin();*/
/*}*/
/*auto begin() const*/
/*{*/
/*return tasksToShow.cbegin();*/
/*}*/
/*auto end() const*/
/*{*/
/*return tasksToShow.cend();*/
/*}*/
/*auto end()*/
/*{*/
/*return tasksToShow.end();*/
/*}*/
private:
std::vector<FilteredDay> filteredDays;
Mirai *mirai;
Tags tagsFilter;
std::vector<std::string> resourcesFilter;
bool shouldHideCompletedTasks_ = true;
};
} // namespace mirai
#endif

134
lib/mirai-core/TodoMd.cpp Normal file
View file

@ -0,0 +1,134 @@
/*
* Mirai. Copyright (C) 2024 Vyn
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#include "TodoMd.h"
#include "TaskItem.h"
#include "cpp-utils/debug.h"
#include "utils.h"
#include <charconv>
#include <fstream>
#include <iostream>
#include <memory>
#include <ranges>
#include <stdexcept>
#include <string>
#include <string_view>
namespace mirai
{
Tags TodoMdFormat::extractTagsFromMetadata(std::string metadata)
{
Tags tags;
std::regex regex("(#([a-zA-Z0-1]+))");
std::smatch matches;
while (std::regex_search(metadata, matches, regex)) {
if (!vectorUtils::contains(tags, matches[2].str())) {
tags.push_back(matches[2]);
}
metadata = matches.suffix();
}
return tags;
}
std::string TodoMdFormat::fieldWithSpace(const std::string &field)
{
if (field.length() == 0) {
return "";
}
return (field + " ");
}
TaskItemData TodoMdFormat::stringToTask(const std::string &str, const std::string &date)
{
std::smatch matches;
std::regex regex("- \\[(\\s|X)\\] (([0-9]{2}:[0-9]{2})-([0-9]{2}:[0-9]{2}) > )?(.*?)( -- (.*))?"
);
std::regex_match(str, matches, regex);
/*std::cout << "line " << str << std::endl;*/
/*std::cout << "M 0 " << matches[0] << std::endl;*/
/*std::cout << "M 1 " << matches[1] << std::endl;*/
/*std::cout << "M 2 " << matches[2] << std::endl;*/
/*std::cout << "M 3 " << matches[3] << std::endl;*/
/*std::cout << "M 4 " << matches[4] << std::endl;*/
/*std::cout << "M 5 " << matches[5] << std::endl;*/
/*std::cout << "M 6 " << matches[6] << std::endl;*/
/*std::cout << "M 7 " << matches[7] << std::endl;*/
std::string text = stringUtils::trim(matches[5]);
TaskItemData taskItem{
.text = text,
.state = str.substr(0, 5) == "- [X]" ? DONE : TODO,
.tags = extractTagsFromMetadata(matches[7])
};
return taskItem;
}
Time stringToTime(const std::string &str)
{
if (str.length() != 5) {
throw std::runtime_error("Str time length must be 5 (got '" + str + "')");
}
if (str[2] != 'h') {
throw std::runtime_error("Malformated time range");
}
Time time{};
std::from_chars(str.data(), str.data() + 2, time.hour);
std::from_chars(str.data() + 3, str.data() + 5, time.minute);
return time;
}
EventData TodoMdFormat::stringToEvent(const std::string &str, const std::string &dateString)
{
std::smatch matches;
std::regex regex("> (([0-9]{2}h[0-9]{2})-([0-9]{2}h[0-9]{2}) )(.*?)( -- (.*))?");
std::regex_match(str, matches, regex);
/*std::cout << "line " << str << std::endl;*/
/*std::cout << "M 0 " << matches[0] << std::endl;*/
/*std::cout << "M 1 " << matches[1] << std::endl;*/
/*std::cout << "M 2 " << matches[2] << std::endl;*/
/*std::cout << "M 3 " << matches[3] << std::endl;*/
/*std::cout << "M 4 " << matches[4] << std::endl;*/
/*std::cout << "M 5 " << matches[5] << std::endl;*/
/*std::cout << "M 6 " << matches[6] << std::endl;*/
std::string text = stringUtils::trim(matches[4]);
auto date = stringToDate(dateString);
if (!date.has_value()) {
throw std::runtime_error("Malformated date");
}
EventData eventData{
.description = text,
.date = date.value(),
.startTime = stringToTime(matches[2]),
.endTime = stringToTime(matches[3]),
.tags = extractTagsFromMetadata(matches[6])
};
return eventData;
}
std::string TodoMdFormat::taskToString(const TaskItem &task)
{
std::string str = task.getText();
if (task.getTags().size() > 0) {
str += " --";
for (const std::string &tag : task.getTags()) {
str += " #" + tag;
}
}
str = (task.getState() == DONE ? "- [X] " : "- [ ] ") + str;
return str;
}
} // namespace mirai

135
lib/mirai-core/TodoMd.h Normal file
View file

@ -0,0 +1,135 @@
/*
* Mirai. Copyright (C) 2024 Vyn
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#ifndef MIRAI_TODOMD_H
#define MIRAI_TODOMD_H
#include "TaskItem.h"
#include "cpp-utils/debug.h"
#include "cpp-utils/string.h"
#include "mirai-core/DateTime.h"
#include "mirai-core/Day.h"
#include "mirai-core/Event.h"
#include <format>
#include <optional>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
namespace mirai
{
struct MiraiMarkdownFormatParseResult {
std::string name;
std::vector<DayData> days;
};
class TodoMdFormat
{
public:
static std::string
stringify(const std::string &name, const std::vector<std::unique_ptr<Day>> &days)
{
std::string result = "# " + name + "\n\n";
std::string currentDate = "";
for (const auto &day : days) {
auto &date = day->getDate();
result += "## " + std::format("{:02d}-{:02d}-{:02d}", date.year, date.month, date.day) +
"\n\n";
for (const auto &event : *(day->events())) {
auto &start = event->getStartTime();
auto &end = event->getEndTime();
result += "> " +
std::format(
"{:02d}h{:02d}-{:02d}h{:02d} {}", start.hour, start.minute, end.hour,
end.minute, event->getText()
) +
"\n";
for (const auto &task : *(event->tasks())) {
result += taskToString(*task) + '\n';
}
result += '\n';
}
for (const auto &task : *(day->tasks())) {
result += taskToString(*task) + '\n';
}
result += '\n';
}
return result;
};
static MiraiMarkdownFormatParseResult parse(const std::string &content)
{
cpputils::debug::Timer readMdFormatDuration;
std::vector<TaskItem> taskItems;
std::string line;
std::stringstream contentStream(content);
if (!std::getline(contentStream, line) || line.substr(0, 2) != "# ") {
std::cerr << "Couldn't find the task list name" << std::endl;
}
std::string name = line.substr(2);
std::string currentDateString = "";
std::vector<mirai::DayData> daysData;
mirai::DayData *currentDay = nullptr;
mirai::EventData *currentEvent = nullptr;
cpputils::debug::Timer stringToTaskDuration;
stringToTaskDuration.reset();
cpputils::debug::Timer gelinesDuration;
while (std::getline(contentStream, line)) {
if (std::string_view{line.data(), 3} == "## ") {
currentDateString = line.substr(3);
auto dateOpt = mirai::stringToDate(currentDateString);
if (!dateOpt.has_value()) {
throw std::runtime_error("Malformated date");
}
daysData.push_back({.date = dateOpt.value()});
currentDay = &daysData.back();
} else if (line.starts_with("> ")) {
auto event = stringToEvent(line, currentDateString);
if (currentDay) {
currentDay->events.push_back(event);
currentEvent = &currentDay->events.back();
}
} else if (line.starts_with("- [ ]") || line.starts_with("- [X]")) {
stringToTaskDuration.start();
TaskItemData taskItemData = stringToTask(line, currentDateString);
stringToTaskDuration.stop();
if (currentEvent) {
currentEvent->tasks.push_back(taskItemData);
} else if (currentDay) {
currentDay->tasks.push_back(taskItemData);
}
} else if (cpputils::string::trim(line) == "") {
currentEvent = nullptr;
}
}
gelinesDuration.printTimeElapsed("getlinesDuration");
stringToTaskDuration.printTimeElapsed("stringToTaskDuration");
readMdFormatDuration.printTimeElapsed("Reading MD File duration");
return {.name = name, .days = daysData};
}
static std::string taskToString(const TaskItem &task);
static TaskItemData stringToTask(const std::string &str, const std::string &date);
static EventData stringToEvent(const std::string &str, const std::string &date);
private:
static std::string fieldWithSpace(const std::string &field);
static TaskItem parseLine(const std::string &line);
static Tags extractTagsFromMetadata(std::string metadata);
};
} // namespace mirai
#endif

94
lib/mirai-core/TodoTxt.h Normal file
View file

@ -0,0 +1,94 @@
/*
* Mirai. Copyright (C) 2024 Vyn
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#ifndef MIRAI_TODOTXT_H
#define MIRAI_TODOTXT_H
/*#include "TaskItem.h"*/
/*#include "utils.h"*/
/*#include <fstream>*/
/*#include <iostream>*/
/*#include <iterator>*/
/*#include <memory>*/
/*#include <regex>*/
/*#include <stdexcept>*/
/*#include <string>*/
/*#include <vector>*/
/*namespace mirai {*/
/*class TodoTxtFormat {*/
/*public:*/
/*static TaskList readFile() {*/
/*std::ifstream file("../newqml/todo.txt");*/
/*if (!file.is_open()) {*/
/*throw std::runtime_error("todo.txt file not found");*/
/*}*/
/*std::vector<TaskItem> taskItems;*/
/*std::string line;*/
/*while (std::getline(file, line)) {*/
/*auto taskItem = parseLine(line);*/
/*taskItems.push_back(taskItem);*/
/*}*/
/*file.close();*/
/*TaskList tasks({*/
/*.tasks = taskItems*/
/*});*/
/*return tasks;*/
/*}*/
/*static void writeFile(TaskList& tasks) {*/
/*std::ofstream file("../newqml/todo.txt");*/
/*if (!file.is_open()) {*/
/*throw std::runtime_error("can't create todo.txt");*/
/*}*/
/*for (const auto& task : tasks.getTasks()) {*/
/*file << fieldWithSpace(task->state == DONE ? "x" : task->priority);*/
/*file << fieldWithSpace(task->state == DONE ? task->getDate() : "");*/
/*file << fieldWithSpace(task->getCreationDate());*/
/*file << task->getText() << '\n';*/
/*}*/
/*file.close();*/
/*}*/
/*private:*/
/*static std::string fieldWithSpace(const std::string& field) {*/
/*if (field.length() == 0)*/
/*return "";*/
/*return (field + " ");*/
/*}*/
/*static TaskItem parseLine(const std::string& line) {*/
/*std::smatch matches;*/
/*std::regex regex("(^x )?(\\([A-Z]\\) )?([0-9]{4}-[0-9]{2}-[0-9]{2} )?([0-9]{4}-[0-9]{2}-[0-9]{2}
* )?(.*)");*/
/*std::regex_match(line, matches, regex);*/
/*for (size_t i = 0; i < matches.size(); ++i) {*/
/*std::ssub_match sub_match = matches[i];*/
/*std::string piece = sub_match.str();*/
/*}*/
/*const TaskItemState taskState = trim_copy(matches[1].str()) == "x" ? DONE : TODO;*/
/*const std::string priority = trim_copy(matches[2].str());*/
/*const std::string firstDate = trim_copy(matches[3].str());*/
/*const std::string secondDate = trim_copy(matches[4].str());*/
/*const std::string text = trim_copy(matches[5].str());*/
/*const std::string date = taskState == DONE ? firstDate : "";*/
/*return {*/
/*.text = text,*/
/*.state = taskState,*/
/*.date = date,*/
/*};*/
/*}*/
/*};*/
/*}*/
#endif

19
lib/mirai-core/using.h Normal file
View file

@ -0,0 +1,19 @@
/*
* Mirai. Copyright (C) 2024 Vyn
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#ifndef MIRAI_USING_H
#define MIRAI_USING_H
#include <memory>
#include <string>
#include <vector>
namespace mirai
{
using Tags = std::vector<std::string>;
}
#endif

38
lib/mirai-core/utils.cpp Normal file
View file

@ -0,0 +1,38 @@
/*
* Mirai. Copyright (C) 2024 Vyn
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#include "utils.h"
#include <cctype>
namespace mirai
{
bool isDate(const std::string &dateStr)
{
// std regex are really slow
/*std::regex regex("[0-9]{4}-[0-9]{2}-[0-9]{2}");*/
/*return std::regex_match(dateStr, regex);*/
// Quick hand made check instead of regex until I rework date to use timestamp or std::chrono
if (dateStr.length() != 10) {
return false;
}
if (dateStr[4] != '-' || dateStr[7] != '-') {
return false;
}
for (int i = 0; i < dateStr.length(); ++i) {
if (i == 4 || i == 7) {
// These are the '-' characters
continue;
}
if (!isdigit(dateStr[i])) {
return false;
}
}
return true;
}
} // namespace mirai

25
lib/mirai-core/utils.h Normal file
View file

@ -0,0 +1,25 @@
/*
* Mirai. Copyright (C) 2024 Vyn
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#ifndef MIRAI_UTILS_H
#define MIRAI_UTILS_H
#include <algorithm>
#include <cctype>
#include <cpp-utils/string.h>
#include <cpp-utils/vector.h>
#include <locale>
#include <regex>
namespace mirai
{
namespace stringUtils = cpputils::string;
namespace vectorUtils = cpputils::vector;
bool isDate(const std::string &dateStr);
} // namespace mirai
#endif

24767
lib/nlohmann/json.hpp Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,47 @@
import { Palette } from "Palette.slint";
import { VText } from "Text.slint";
export component VButton inherits Rectangle {
in property<string> text;
in property<brush> text-color: Palette.foreground;
in property<image> icon-source;
in property<brush> icon-colorize;
in property<length> icon-size: 1rem;
in property enabled <=> ta.enabled;
callback clicked;
private property<bool> active: false;
background: enabled ? Palette.control-background : Palette.control-background.darker(0.2);
border-radius: 4px;
ta := TouchArea {
mouse-cursor: pointer;
clicked => {
active = !active;
root.clicked();
}
}
HorizontalLayout {
alignment: center;
spacing: 8px;
padding: 8px;
padding-top: 4px;
padding-bottom: 4px;
if root.icon-source.width != 0 : Image {
padding: 8px;
source: icon-source;
colorize: icon-colorize;
width: icon-size;
}
if root.text != "" : VerticalLayout {
VText {
text: root.text;
color: root.text-color;
horizontal-alignment: center;
}
}
}
}

View file

@ -0,0 +1,42 @@
import { VLabeledComponent } from "LabeledComponent.slint";
import { VText } from "Text.slint";
import { Palette } from "Palette.slint";
export component VCheckBox {
in-out property<string> text;
in property <brush> color <=> textComponent.color;
in-out property<bool> checked: false;
callback toggled(bool); // toggled(state: bool)
HorizontalLayout {
spacing: 8px;
VerticalLayout {
alignment: center;
if checked : Rectangle {
background: Palette.accent;
height: 1rem;
width: self.height;
border-radius: 32px;
}
if !checked : Rectangle {
border-color: Palette.accent;
border-width: 2px;
height: 1rem;
width: self.height;
border-radius: 32px;
}
}
textComponent := VText {
text: root.text;
vertical-alignment: center;
}
}
ta := TouchArea {
clicked => {
checked = !checked;
root.toggled(checked)
}
}
}

View file

@ -0,0 +1,27 @@
import { Button, DatePickerPopup, Date, Palette } from "std-widgets.slint";
import { VLabeledComponent } from "LabeledComponent.slint";
import { VButton } from "Button.slint";
export component VDatePicker inherits VLabeledComponent {
in-out property<Date> date;
in-out property<string> dateDisplay: formatZeroPadding(date.day) + "/" + formatZeroPadding(date.month) + "/" + date.year;
pure function formatZeroPadding(number: int) -> string {
if (number < 10) {
return "0\{number}";
}
return number;
}
button := VButton {
text: dateDisplay;
enabled: root.enabled;
clicked => { taskDateInput.show() }
}
taskDateInput := DatePickerPopup {
accepted(date) => { root.date = date }
}
}

View file

@ -0,0 +1,22 @@
import { Palette } from "Palette.slint";
import { VText } from "Text.slint";
export component VLabeledComponent {
in property<string> label <=> labelComponent.text;
in property<bool> enabled: true;
VerticalLayout {
labelComponent := VText {
}
Rectangle {
background: enabled ? Palette.control-background : Palette.control-background.darker(0.2);
border-radius: 4px;
VerticalLayout {
padding: 4px;
@children
}
}
}
}

View file

@ -0,0 +1,43 @@
// OneDark
//black = "#181a1f",
//bg0 = "#282c34",
//bg1 = "#31353f",
//bg2 = "#393f4a",
//bg3 = "#3b3f4c",
//bg_d = "#21252b",
//bg_blue = "#73b8f1",
//bg_yellow = "#ebd09c",
//fg = "#abb2bf",
//purple = "#c678dd",
//green = "#98c379",
//orange = "#d19a66",
//blue = "#61afef",
//yellow = "#e5c07b",
//cyan = "#56b6c2",
//red = "#e86671",
//grey = "#5c6370",
//light_grey = "#848b98",
//dark_cyan = "#2b6f77",
//dark_red = "#993939",
//dark_yellow = "#93691d",
//dark_purple = "#8a3fa0",
//diff_add = "#31392b",
//diff_delete = "#382b2c",
//diff_change = "#1c3448",
//diff_text = "#2c5372",
// ----
export global Palette {
out property<brush> accent: Colors.cyan.darker(0.4);
out property<brush> foreground: #abb2bf;
out property<brush> foreground-hint: foreground.darker(0.2);
out property<brush> background: #282c34;
out property<brush> pane: #21252b;
out property<brush> control-foreground: #abb2bf;
out property<brush> control-background: #393f4a;
out property<brush> card-background: background.brighter(0.2);
out property<brush> green: Colors.greenyellow;
out property<brush> orange: Colors.orange;
out property<brush> red: Colors.red;
}

View file

@ -0,0 +1,27 @@
import { Palette } from "Palette.slint";
import { VButton } from "Button.slint";
export component VPopupIconMenu inherits Rectangle {
private property<length> popup-x: 200px;
private property<length> popup-y: 200px;
public function show(x: length, y: length) {
popup-x = x;
popup-y = y;
popup.show();
}
popup := PopupWindow {
x: popup-x;
y: popup-y;
Rectangle {
background: Palette.background.brighter(0.2);
border-radius: 8px;
clip: true;
HorizontalLayout {
alignment: start;
@children
}
}
}
}

31
lib/slint-vynui/Tag.slint Normal file
View file

@ -0,0 +1,31 @@
import { Palette } from "Palette.slint";
import { VText } from "Text.slint";
export component VTag inherits Rectangle {
in property text <=> text-component.text;
in property text-color <=> text-component.color;
in property size <=> text-component.font-size;
in property background-color <=> self.background;
callback clicked;
background: Palette.control-background;
border-radius: 8px;
ta := TouchArea {
mouse-cursor: pointer;
clicked => {
root.clicked();
}
}
VerticalLayout {
padding-left: 8px;
padding-right: 8px;
alignment: center;
text-component := VText {
horizontal-alignment: center;
}
}
}

View file

@ -0,0 +1,5 @@
import { Palette } from "Palette.slint";
export component VText inherits Text {
color: Palette.foreground;
}

View file

@ -0,0 +1,13 @@
import { VLabeledComponent } from "LabeledComponent.slint";
import { Palette } from "Palette.slint";
export component VTextInput inherits VLabeledComponent {
in-out property text <=> textInputComponent.text;
in-out property wrap <=> textInputComponent.wrap;
callback accepted();
textInputComponent := TextInput {
color: Palette.foreground;
accepted => { root.accepted() }
}
}

View file

@ -0,0 +1,25 @@
import { Button, TimePickerPopup, Date, Palette, Time } from "std-widgets.slint";
import { VLabeledComponent } from "LabeledComponent.slint";
import { VButton } from "Button.slint";
export component VTimePicker inherits VLabeledComponent {
in-out property<Time> time;
in-out property<string> timeDisplay: formatZeroPadding(time.hour) + "h" + formatZeroPadding(time.minute);
pure function formatZeroPadding(number: int) -> string {
if (number < 10) {
return "0\{number}";
}
return number;
}
button := VButton {
text: timeDisplay;
enabled: root.enabled;
clicked => { timePickerPopup.show() }
}
timePickerPopup := TimePickerPopup {
accepted(time) => { root.time = time }
}
}

View file

@ -0,0 +1,35 @@
import { Palette } from "Palette.slint";
import { VText } from "Text.slint";
export component ToggleButton inherits Rectangle {
in property text <=> text-component.text;
in property text-color <=> text-component.color;
in property text-alignment <=> text-component.horizontal-alignment;
callback clicked;
private property<bool> active: false;
background: root.active ? Palette.control-background
: ta.has-hover ? Palette.control-background.transparentize(0.5)
: Colors.transparent;
border-radius: 4px;
ta := TouchArea {
mouse-cursor: pointer;
clicked => {
active = !active;
root.clicked();
}
}
VerticalLayout {
padding: 8px;
padding-top: 4px;
padding-bottom: 4px;
text-component := VText {
horizontal-alignment: center;
}
}
}

View file

@ -0,0 +1,11 @@
export { Palette } from "Palette.slint";
export { VButton } from "Button.slint";
export { VText } from "Text.slint";
export { VCheckBox } from "CheckBox.slint";
export { VDatePicker } from "DatePicker.slint";
export { VTimePicker } from "TimePicker.slint";
export { VLabeledComponent } from "LabeledComponent.slint";
export { VPopupIconMenu } from "PopupIconMenu.slint";
export { VTag } from "Tag.slint";
export { VTextInput } from "TextInput.slint";
export { ToggleButton } from "ToggleButton.slint";