quill/src/apps/todo.cpp

216 lines
6.8 KiB
C++
Raw Normal View History

2022-09-13 05:20:37 -07:00
#include "todo.h"
#include "ui_todo.h"
#include "functions.h"
todo::todo(QWidget *parent) :
QWidget(parent),
ui(new Ui::todo)
{
ui->setupUi(this);
this->setStyleSheet(readFile("/mnt/onboard/.adds/inkbox/eink.qss"));
this->setFont(QFont("u001"));
2022-09-14 05:30:17 -07:00
ui->listWidget->setFont(QFont("u001"));
ui->itemsListWidget->setFont(QFont("u001"));
ui->deleteBtn->setEnabled(false);
ui->setupBtn->setEnabled(false);
2022-09-13 05:20:37 -07:00
ui->closeBtn->setProperty("type", "borderless");
2022-09-14 05:30:17 -07:00
ui->newBtn->setProperty("type", "borderless");
2022-09-13 20:03:51 -07:00
ui->deleteBtn->setProperty("type", "borderless");
ui->setupBtn->setProperty("type", "borderless");
ui->deleteBtn->setStyleSheet("padding: 10px");
ui->setupBtn->setStyleSheet("padding: 10px");
2022-09-13 05:20:37 -07:00
ui->closeBtn->setIcon(QIcon(":/resources/close.png"));
2022-09-14 05:30:17 -07:00
ui->newBtn->setIcon(QIcon(":/resources/new.png"));
2022-09-13 20:03:51 -07:00
ui->deleteBtn->setIcon(QIcon(":/resources/x-circle.png"));
ui->setupBtn->setIcon(QIcon(":/resources/arrow-right.png"));
2022-09-14 05:30:17 -07:00
ui->listWidget->setStyleSheet("font-size: 10pt");
ui->listWidget->setWordWrap(true);
ui->itemsListWidget->setStyleSheet("font-size: 10pt");
ui->itemsListWidget->setWordWrap(true);
2022-09-13 05:20:37 -07:00
refreshList();
}
todo::~todo()
{
delete ui;
}
void todo::on_closeBtn_clicked()
{
2022-09-14 05:30:17 -07:00
if(currentView == currentView::home) {
this->close();
}
else {
saveCurrentList();
ui->stackedWidget->setCurrentIndex(0);
ui->closeBtn->setIcon(QIcon(":/resources/close.png"));
ui->deleteBtn->setEnabled(true);
ui->setupBtn->setEnabled(true);
currentView = currentView::home;
}
2022-09-13 05:20:37 -07:00
}
2022-09-14 05:30:17 -07:00
void todo::on_newBtn_clicked()
2022-09-13 05:20:37 -07:00
{
global::keyboard::embed = false;
virtualkeyboard * virtualKeyboardWidget = new virtualkeyboard(this);
virtualKeyboardWidget->setAttribute(Qt::WA_DeleteOnClose);
2022-09-14 05:30:17 -07:00
if(currentView == currentView::home) {
QObject::connect(virtualKeyboardWidget, &virtualkeyboard::enterBtnPressed, this, &todo::createNewList);
ui->statusLabel->setText("Enter the list's name");
}
else {
QObject::connect(virtualKeyboardWidget, &virtualkeyboard::enterBtnPressed, this, &todo::addItem);
ui->statusLabel->setText("Enter the item's name");
}
2022-09-13 05:20:37 -07:00
virtualKeyboardWidget->show();
}
void todo::createNewList(QString listName) {
log("Initializing new To-Do list with name '" + listName + "'", className);
QJsonDocument document;
2022-09-13 20:03:51 -07:00
QJsonArray mainArray;
QJsonArray listArray;
QJsonObject object;
2022-09-13 05:20:37 -07:00
if(QFile::exists(global::localLibrary::todoDatabasePath)) {
document = readTodoDatabase();
2022-09-13 20:03:51 -07:00
object = document.object();
mainArray = object["List"].toArray();
2022-09-13 05:20:37 -07:00
}
2022-09-13 20:03:51 -07:00
listArray.append(listName);
2022-09-13 05:20:37 -07:00
2022-09-13 20:03:51 -07:00
mainArray.push_back(listArray);
object["List"] = mainArray;
2022-09-13 05:20:37 -07:00
2022-09-13 20:03:51 -07:00
document.setObject(object);
2022-09-13 05:20:37 -07:00
writeTodoDatabase(document);
ui->statusLabel->setText("Select or create a new list");
refreshList();
}
2022-09-14 05:30:17 -07:00
void todo::addItem(QString itemName) {
log("Adding item with name '" + itemName + "' to current list", className);
// Accessing the current list's items array
QJsonDocument document = readTodoDatabase();
QJsonObject object = document.object();
QJsonArray mainArray = object["List"].toArray();
QJsonArray listArray = mainArray.at(listIndex).toArray();
QJsonArray itemArray;
// Item name
itemArray.insert(0, itemName);
// Check state (always set to false)
itemArray.insert(1, false);
// Adding item array to list array
listArray.push_back(itemArray);
mainArray.replace(listIndex, listArray);
object["List"] = mainArray;
document.setObject(object);
writeTodoDatabase(document);
setupList(listArray.at(0).toString());
}
2022-09-13 05:20:37 -07:00
void todo::refreshList() {
if(QFile::exists(global::localLibrary::todoDatabasePath)) {
ui->listWidget->clear();
2022-09-13 20:03:51 -07:00
QJsonArray array = readTodoDatabase().object()["List"].toArray();
for(int i = 0; i < array.count(); i++) {
QString name = array.at(i).toArray().at(0).toString();
2022-09-13 05:20:37 -07:00
if(!name.isEmpty()) {
ui->listWidget->addItem(name);
}
}
}
}
2022-09-13 20:03:51 -07:00
void todo::setupList(QString listName) {
log("Setting up list with name '" + listName + "'", className);
QJsonArray array = readTodoDatabase().object()["List"].toArray();
2022-09-14 05:30:17 -07:00
// Iterate through the To-Do lists array to find the list's array index
2022-09-13 20:03:51 -07:00
for(int i = 0; i < array.count(); i++) {
QString name = array.at(i).toArray().at(0).toString();
if(name == listName) {
2022-09-14 05:30:17 -07:00
listIndex = i;
2022-09-13 20:03:51 -07:00
break;
}
}
2022-09-14 05:30:17 -07:00
log("List index is " + QString::number(listIndex), className);
ui->stackedWidget->setCurrentIndex(1);
currentView = currentView::list;
// Iterate through the selected list's array to find item arrays
int count = array.at(listIndex).toArray().count();
log("List's items count is " + QString::number(count - 1), className);
// Starting at index 1 because 0 represents the list's name
ui->itemsListWidget->clear();
for(int i = 1; i < count; i++) {
QJsonArray itemArray = array.at(listIndex).toArray().at(i).toArray();
QListWidgetItem * item = new QListWidgetItem();
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);
// Set the item's name
item->setText(array.at(listIndex).toArray().at(i).toArray().at(0).toString());
// Is the item checked?
if(itemArray.at(1).toBool() == true) {
item->setCheckState(Qt::Checked);
}
else {
item->setCheckState(Qt::Unchecked);
}
ui->itemsListWidget->addItem(item);
}
ui->closeBtn->setIcon(QIcon(":/resources/check.png"));
ui->statusLabel->setText("Select or create a new item");
2022-09-13 20:03:51 -07:00
}
void todo::on_setupBtn_clicked()
{
ui->deleteBtn->setEnabled(false);
ui->setupBtn->setEnabled(false);
setupList(ui->listWidget->currentItem()->text());
}
void todo::on_listWidget_itemClicked(QListWidgetItem *item)
{
ui->deleteBtn->setEnabled(true);
ui->setupBtn->setEnabled(true);
}
2022-09-14 05:30:17 -07:00
void todo::saveCurrentList() {
QJsonDocument document = readTodoDatabase();
QJsonObject object = document.object();
QJsonArray mainArray = object["List"].toArray();
QJsonArray listArray = mainArray.at(listIndex).toArray();
for(int i = 1; i < ui->itemsListWidget->count() + 1; i++) {
QJsonArray itemArray = listArray.at(i).toArray();
if(ui->itemsListWidget->item(i - 1)->checkState() == Qt::Checked) {
itemArray.replace(1, true);
}
else {
itemArray.replace(1, false);
}
listArray.replace(i, itemArray);
}
// Adding item array to list array
mainArray.replace(listIndex, listArray);
object["List"] = mainArray;
document.setObject(object);
writeTodoDatabase(document);
}
void todo::on_deleteBtn_clicked()
{
}