load "guilib.ring"
import System.GUI
new QApp {
StyleFusion()
open_window(:TodoController)
exec()
}
class TodoController from WindowsControllerParent
oView = new ToDoView
tasks = []
selectedIndex = -1
func addTask
name = oView.txtName.text()
desc = oView.txtDesc.toPlainText()
priority = oView.comboPriority.currentText()
if name = ""
new QMessageBox(oView.win) {
setWindowTitle("Error")
setText("Please enter a task name!")
show()
}
return
ok
task = [name, desc, priority]
add(tasks, task)
updateTaskList()
clearInputs()
new QMessageBox(oView.win) {
setWindowTitle("Success")
setText("Task added successfully!")
show()
}
func editTask
if selectedIndex = -1
return
ok
name = oView.txtName.text()
desc = oView.txtDesc.toPlainText()
priority = oView.comboPriority.currentText()
if name = ""
new QMessageBox(oView.win) {
setWindowTitle("Error")
setText("Please enter a task name!")
show()
}
return
ok
tasks[selectedIndex] = [name, desc, priority]
updateTaskList()
clearInputs()
selectedIndex = -1
oView.btnEdit.setEnabled(false)
oView.btnDelete.setEnabled(false)
new QMessageBox(oView.win) {
setWindowTitle("Success")
setText("Task updated successfully!")
show()
}
func deleteTask
if selectedIndex = -1
return
ok
oMsg = new QMessageBox(oView.win) {
setWindowTitle("Confirm Delete")
setText("Are you sure you want to delete this task?")
setStandardButtons(QMessageBox_Yes | QMessageBox_No)
}
nResult = oMsg.exec()
if nResult = QMessageBox_Yes
del(tasks, selectedIndex)
updateTaskList()
clearInputs()
selectedIndex = -1
oView.btnEdit.setEnabled(false)
oView.btnDelete.setEnabled(false)
new QMessageBox(oView.win) {
setWindowTitle("Success")
setText("Task deleted successfully!")
show()
}
ok
func onTaskSelected
selectedIndex = oView.lstTasks.currentRow() + 1
if selectedIndex > 0 and selectedIndex <= len(tasks)
task = tasks[selectedIndex]
oView.txtName.setText(task[1])
oView.txtDesc.setPlainText(task[2])
priorityIndex = 0
if task[3] = "Low"
priorityIndex = 0
but task[3] = "Medium"
priorityIndex = 1
but task[3] = "High"
priorityIndex = 2
ok
oView.comboPriority.setCurrentIndex(priorityIndex)
oView.btnEdit.setEnabled(true)
oView.btnDelete.setEnabled(true)
ok
func updateTaskList
oView.lstTasks.clear()
for task in tasks
priorityColor = ""
if task[3] = "High"
priorityColor = "š“"
but task[3] = "Medium"
priorityColor = "š”"
else
priorityColor = "š¢"
ok
displayText = priorityColor + " " + task[1] + " - " + task[3]
oView.lstTasks.addItem(displayText)
next
func clearInputs
oView.txtName.setText("")
oView.txtDesc.setPlainText("")
oView.comboPriority.setCurrentIndex(1)
class ToDoView from WindowsViewParent
win txtName txtDesc comboPriority btnAdd btnEdit btnDelete lstTasks
win = new QWidget() {
setWindowTitle("ToDo Application")
resize(700, 500)
}
# Main layout
mainLayout = new QVBoxLayout()
# Title
lblTitle = new QLabel(win) {
setText("ToDo Application")
setAlignment(Qt_AlignHCenter)
setStyleSheet("font-size: 20px; font-weight: bold; padding: 10px;")
}
mainLayout.addWidget(lblTitle)
# Input Section
inputGroup = new QGroupBox(win) {
setTitle("Task Details")
}
inputLayout = new QVBoxLayout()
# Name input
nameLayout = new QHBoxLayout()
nameLayout.addWidget(new QLabel(win) { setText("Name:") setMinimumWidth(80) })
txtName = new QLineEdit(win)
nameLayout.addWidget(txtName)
inputLayout.addLayout(nameLayout)
# Description input
descLayout = new QHBoxLayout()
descLayout.addWidget(new QLabel(win) { setText("Description:") setMinimumWidth(80) })
txtDesc = new QTextEdit(win) {
setMaximumHeight(80)
}
descLayout.addWidget(txtDesc)
inputLayout.addLayout(descLayout)
# Priority input
priorityLayout = new QHBoxLayout()
priorityLayout.addWidget(new QLabel(win) { setText("Priority:") setMinimumWidth(80) })
comboPriority = new QComboBox(win) {
addItem("Low", 0)
addItem("Medium", 0)
addItem("High", 0)
setCurrentIndex(1)
}
priorityLayout.addWidget(comboPriority)
priorityLayout.addStretch(1)
inputLayout.addLayout(priorityLayout)
inputGroup.setLayout(inputLayout)
mainLayout.addWidget(inputGroup)
# Buttons Section
btnLayout = new QHBoxLayout()
btnAdd = new QPushButton(win) {
setText("Add Task")
setStyleSheet("background-color: #4CAF50; color: white; padding: 8px; font-weight: bold;")
setClickEvent(Method(:addTask))
}
btnLayout.addWidget(btnAdd)
btnEdit = new QPushButton(win) {
setText("Edit Task")
setStyleSheet("background-color: #2196F3; color: white; padding: 8px; font-weight: bold;")
setEnabled(false)
setClickEvent(Method(:editTask))
}
btnLayout.addWidget(btnEdit)
btnDelete = new QPushButton(win) {
setText("Delete Task")
setStyleSheet("background-color: #f44336; color: white; padding: 8px; font-weight: bold;")
setEnabled(false)
setClickEvent(Method(:deleteTask))
}
btnLayout.addWidget(btnDelete)
mainLayout.addLayout(btnLayout)
# Tasks List Section
listGroup = new QGroupBox(win) {
setTitle("Tasks List")
}
listLayout = new QVBoxLayout()
lstTasks = new QListWidget(win) {
setItemClickedEvent(Method(:onTaskSelected))
}
listLayout.addWidget(lstTasks)
listGroup.setLayout(listLayout)
mainLayout.addWidget(listGroup)
win.setLayout(mainLayout)
win.show()