To-Do app: Add 'Edit item' option

This commit is contained in:
Nicolas Mailloux 2022-09-16 20:56:19 -04:00
parent 08db998667
commit 67686c6be6
4 changed files with 113 additions and 13 deletions

View file

@ -16,6 +16,15 @@ todo::todo(QWidget *parent) :
ui->deleteBtn->setEnabled(false); ui->deleteBtn->setEnabled(false);
ui->setupBtn->setEnabled(false); ui->setupBtn->setEnabled(false);
buttonPadding;
if(global::deviceID == "n705\n" or global::deviceID == "n905\n" or global::deviceID == "kt\n") {
buttonPadding = 5;
}
else {
buttonPadding = 10;
}
buttonPaddingString = QString::number(buttonPadding);
ui->closeBtn->setProperty("type", "borderless"); ui->closeBtn->setProperty("type", "borderless");
ui->newBtn->setProperty("type", "borderless"); ui->newBtn->setProperty("type", "borderless");
ui->deleteBtn->setProperty("type", "borderless"); ui->deleteBtn->setProperty("type", "borderless");
@ -26,14 +35,16 @@ todo::todo(QWidget *parent) :
ui->selectItemsModeBtn->setProperty("type", "borderless"); ui->selectItemsModeBtn->setProperty("type", "borderless");
ui->deselectAllItemsBtn->setProperty("type", "borderless"); ui->deselectAllItemsBtn->setProperty("type", "borderless");
ui->saveCurrentListViewBtn->setProperty("type", "borderless"); ui->saveCurrentListViewBtn->setProperty("type", "borderless");
ui->deleteBtn->setStyleSheet("padding: 10px"); ui->editItemBtn->setProperty("type", "borderless");
ui->setupBtn->setStyleSheet("padding: 10px"); ui->deleteBtn->setStyleSheet("padding: " + buttonPaddingString + "px;");
ui->newItemBtn->setStyleSheet("padding: 10px"); ui->setupBtn->setStyleSheet("padding: " + buttonPaddingString + "px;");
ui->deleteItemBtn->setStyleSheet("padding: 10px"); ui->newItemBtn->setStyleSheet("padding: " + buttonPaddingString + "px;");
ui->selectAllItemsBtn->setStyleSheet("padding: 10px"); ui->deleteItemBtn->setStyleSheet("padding: " + buttonPaddingString + "px;");
ui->selectItemsModeBtn->setStyleSheet("padding: 10px"); ui->selectAllItemsBtn->setStyleSheet("padding: " + buttonPaddingString + "px;");
ui->deselectAllItemsBtn->setStyleSheet("padding: 10px"); ui->selectItemsModeBtn->setStyleSheet("padding: " + buttonPaddingString + "px;");
ui->saveCurrentListViewBtn->setStyleSheet("padding: 10px"); ui->deselectAllItemsBtn->setStyleSheet("padding: " + buttonPaddingString + "px;");
ui->saveCurrentListViewBtn->setStyleSheet("padding: " + buttonPaddingString + "px;");
ui->editItemBtn->setStyleSheet("padding: " + buttonPaddingString + "px;");
ui->closeBtn->setIcon(QIcon(":/resources/close.png")); ui->closeBtn->setIcon(QIcon(":/resources/close.png"));
ui->newBtn->setIcon(QIcon(":/resources/new.png")); ui->newBtn->setIcon(QIcon(":/resources/new.png"));
ui->newItemBtn->setIcon(QIcon(":/resources/plus.png")); ui->newItemBtn->setIcon(QIcon(":/resources/plus.png"));
@ -43,6 +54,7 @@ todo::todo(QWidget *parent) :
ui->selectAllItemsBtn->setIcon(QIcon(":/resources/checkbox-checked-small.png")); ui->selectAllItemsBtn->setIcon(QIcon(":/resources/checkbox-checked-small.png"));
ui->selectItemsModeBtn->setIcon(QIcon(":/resources/checkbox-unchecked-small.png")); ui->selectItemsModeBtn->setIcon(QIcon(":/resources/checkbox-unchecked-small.png"));
ui->deselectAllItemsBtn->setIcon(QIcon(":/resources/checkbox-x-small.png")); ui->deselectAllItemsBtn->setIcon(QIcon(":/resources/checkbox-x-small.png"));
ui->editItemBtn->setIcon(QIcon(":/resources/edit.png"));
ui->saveCurrentListViewBtn->setIcon(QIcon(":/resources/save.png")); ui->saveCurrentListViewBtn->setIcon(QIcon(":/resources/save.png"));
ui->listWidget->setStyleSheet("font-size: 10pt"); ui->listWidget->setStyleSheet("font-size: 10pt");
ui->listWidget->setWordWrap(true); ui->listWidget->setWordWrap(true);
@ -277,7 +289,7 @@ void todo::switchItemsSelectionMode(bool selectionMode) {
log("Entering selection mode", className); log("Entering selection mode", className);
saveCurrentList(); saveCurrentList();
ui->newItemBtn->setEnabled(false); ui->newItemBtn->setEnabled(false);
ui->selectItemsModeBtn->setStyleSheet("padding: 10px; background-color: lightGray;"); ui->selectItemsModeBtn->setStyleSheet("padding: " + buttonPaddingString + "px; background-color: lightGray;");
ui->editToolbarWidget->show(); ui->editToolbarWidget->show();
ui->statusLabel->setText("Selection mode"); ui->statusLabel->setText("Selection mode");
selectItemsMode = true; selectItemsMode = true;
@ -285,13 +297,13 @@ void todo::switchItemsSelectionMode(bool selectionMode) {
else { else {
log("Exiting selection mode", className); log("Exiting selection mode", className);
setupList(readTodoDatabase().object()["List"].toArray().at(listIndex).toArray().at(0).toString()); setupList(readTodoDatabase().object()["List"].toArray().at(listIndex).toArray().at(0).toString());
ui->selectItemsModeBtn->setStyleSheet("padding: 10px; background-color: white;"); ui->selectItemsModeBtn->setStyleSheet("padding: " + buttonPaddingString + "px; background-color: white;");
ui->editToolbarWidget->hide(); ui->editToolbarWidget->hide();
ui->newItemBtn->setEnabled(true); ui->newItemBtn->setEnabled(true);
ui->statusLabel->setText("Select or create a new item"); ui->statusLabel->setText("Select or create a new item");
selectItemsMode = false; selectItemsMode = false;
} }
QTimer::singleShot(500, this, SLOT(repaint())); QTimer::singleShot(500, this, SLOT(resize()));
} }
void todo::on_deleteItemBtn_clicked() void todo::on_deleteItemBtn_clicked()
@ -365,3 +377,63 @@ void todo::on_saveCurrentListViewBtn_clicked()
saveCurrentList(); saveCurrentList();
switchItemsSelectionMode(false); switchItemsSelectionMode(false);
} }
void todo::on_editItemBtn_clicked()
{
QList<int> selectedItems;
for(int i = 1; i < ui->itemsListWidget->count() + 1; i++) {
if(ui->itemsListWidget->item(i - 1)->checkState() == Qt::Checked) {
selectedItems.append(i);
}
}
int count = selectedItems.count();
if(count == 1) {
currentEditItemIndex = selectedItems.at(0);
global::keyboard::embed = false;
global::keyboard::keyboardText = ui->itemsListWidget->item(currentEditItemIndex - 1)->text();
virtualkeyboard * virtualKeyboardWidget = new virtualkeyboard(this);
virtualKeyboardWidget->setAttribute(Qt::WA_DeleteOnClose);
QObject::connect(virtualKeyboardWidget, &virtualkeyboard::enterBtnPressed, this, &todo::editItemWrapper);
QObject::connect(virtualKeyboardWidget, &virtualkeyboard::closeBtnPressed, this, &todo::setDefaultListPageStatusText);
ui->statusLabel->setText("Enter the edited item's name");
virtualKeyboardWidget->show();
}
else if(count < 1) {
global::toast::delay = 3000;
emit showToast("Please select an item");
}
else {
global::toast::delay = 3000;
emit showToast("Please select only one item");
}
}
void todo::editItem(int index, QString replacement) {
QJsonDocument document = readTodoDatabase();
QJsonObject object = document.object();
QJsonArray mainArray = object["List"].toArray();
QJsonArray listArray = mainArray.at(listIndex).toArray();
QJsonArray itemArray = listArray.at(index).toArray();
// Replacing item string with provided replacement string
itemArray.replace(0, replacement);
listArray.replace(index, itemArray);
// Adding item array to list array
mainArray.replace(listIndex, listArray);
object["List"] = mainArray;
document.setObject(object);
writeTodoDatabase(document);
setupList(listArray.at(0).toString());
}
void todo::editItemWrapper(QString replacement) {
editItem(currentEditItemIndex, replacement);
currentEditItemIndex = NULL;
}
void todo::resize() {
this->setGeometry(QRect(QPoint(0,0), qApp->primaryScreen()->geometry().size()));
this->repaint();
}

View file

@ -3,6 +3,7 @@
#include <QWidget> #include <QWidget>
#include <QListWidgetItem> #include <QListWidgetItem>
#include <QScreen>
#include "virtualkeyboard.h" #include "virtualkeyboard.h"
@ -25,12 +26,17 @@ public:
bool selectItemsMode = false; bool selectItemsMode = false;
currentView currentView = currentView::home; currentView currentView = currentView::home;
int listIndex; int listIndex;
int currentEditItemIndex;
int buttonPadding;
QString buttonPaddingString;
private slots: private slots:
void on_closeBtn_clicked(); void on_closeBtn_clicked();
void on_newBtn_clicked(); void on_newBtn_clicked();
void createNewList(QString listName); void createNewList(QString listName);
void addItem(QString itemName); void addItem(QString itemName);
void editItem(int itemIndex, QString replacement);
void editItemWrapper(QString replacement);
void refreshList(); void refreshList();
void saveCurrentList(); void saveCurrentList();
void setupList(QString listName); void setupList(QString listName);
@ -38,6 +44,7 @@ private slots:
void switchItemsSelectionMode(bool selectionMode); void switchItemsSelectionMode(bool selectionMode);
void setDefaultHomePageStatusText(); void setDefaultHomePageStatusText();
void setDefaultListPageStatusText(); void setDefaultListPageStatusText();
void resize();
void on_setupBtn_clicked(); void on_setupBtn_clicked();
void on_listWidget_itemClicked(QListWidgetItem *item); void on_listWidget_itemClicked(QListWidgetItem *item);
void on_deleteBtn_clicked(); void on_deleteBtn_clicked();
@ -47,6 +54,7 @@ private slots:
void on_selectAllItemsBtn_clicked(); void on_selectAllItemsBtn_clicked();
void on_deselectAllItemsBtn_clicked(); void on_deselectAllItemsBtn_clicked();
void on_saveCurrentListViewBtn_clicked(); void on_saveCurrentListViewBtn_clicked();
void on_editItemBtn_clicked();
signals: signals:
void showToast(QString messageToDisplay); void showToast(QString messageToDisplay);

View file

@ -6,7 +6,7 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>483</width> <width>451</width>
<height>300</height> <height>300</height>
</rect> </rect>
</property> </property>
@ -286,7 +286,7 @@
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QPushButton" name="deleteItemBtn"> <widget class="QPushButton" name="editItemBtn">
<property name="text"> <property name="text">
<string/> <string/>
</property> </property>
@ -302,6 +302,23 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QPushButton" name="deleteItemBtn">
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="Line" name="line_11">
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</item>
<item> <item>
<widget class="QPushButton" name="selectAllItemsBtn"> <widget class="QPushButton" name="selectAllItemsBtn">
<property name="text"> <property name="text">

View file

@ -44,6 +44,9 @@ virtualkeyboard::virtualkeyboard(QWidget *parent) :
ui->enterBtn->hide(); ui->enterBtn->hide();
ui->enterBtn->deleteLater(); ui->enterBtn->deleteLater();
} }
if(!global::keyboard::keyboardText.isEmpty()) {
ui->lineEdit->setText(global::keyboard::keyboardText);
}
ui->n1->setProperty("type", "borderless"); ui->n1->setProperty("type", "borderless");
ui->n2->setProperty("type", "borderless"); ui->n2->setProperty("type", "borderless");