/* * Copyright (c) 2025 Aldrik Ramaekers * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include #include "ui.hpp" #include "administration.hpp" #include "administration_writer.hpp" #include "locales.hpp" #include "strops.hpp" static ui::view_state current_view_state = ui::view_state::LIST_ALL; static project selected_for_cancellation; static project active_project; static void _reset_to_default_view() { current_view_state = ui::view_state::LIST_ALL; active_project = administration::project_create_empty(); } void ui::setup_projects() { _reset_to_default_view(); } static void draw_project_create() { if (ImGui::Button(locale::get("form.back"), true, false)) { _reset_to_default_view(); } ImGui::SameLine(); bool can_save = administration::project_is_valid(active_project) == A_ERR_SUCCESS; if (!can_save) ImGui::BeginDisabled(); if (ImGui::Button(locale::get("form.save"), true)) { administration_writer::set_write_completed_event_callback(0); administration::project_add(active_project); current_view_state = ui::view_state::EDIT_EXISTING; } if (!can_save) ImGui::EndDisabled(); ImGui::Spacing(); ImGui::Separator(1.0f); ImGui::Spacing(); ImGui::ProjectForm(&active_project, false); } static void draw_project_update() { if (ImGui::Button(locale::get("form.back"), true, false)) { current_view_state = ui::view_state::VIEW_EXISTING; } ImGui::SameLine(); bool can_save = administration::project_is_valid(active_project) == A_ERR_SUCCESS; if (!can_save) ImGui::BeginDisabled(); if (ImGui::Button(locale::get("form.save"), true)) { administration_writer::set_write_completed_event_callback(0); administration::project_update(active_project); } if (!can_save) ImGui::EndDisabled(); if (active_project.state == project_state::PROJECT_RUNNING) { ImGui::SameLine(); if (ImGui::Button(locale::get("form.cancel"))) { selected_for_cancellation = active_project; ImGui::OpenPopup("ConfirmCancelProject"); } } if (ImGui::BeginPopupModal("ConfirmCancelProject", nullptr, ImGuiWindowFlags_AlwaysAutoResize|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoTitleBar)) { ImGui::Text(locale::get("form.confirmCancelProject")); ImGui::Separator(); if (ImGui::Button(locale::get("form.yes"), ImVec2(120, 0))) { administration::project_cancel(selected_for_cancellation); ImGui::CloseCurrentPopup(); _reset_to_default_view(); } ImGui::SameLine(); if (ImGui::Button(locale::get("form.no"), ImVec2(120, 0))) { ImGui::CloseCurrentPopup(); } ImGui::EndPopup(); } ImGui::Spacing(); ImGui::Separator(1.0f); ImGui::Spacing(); ImGui::ProjectForm(&active_project, false); } static void draw_project_view() { if (ImGui::Button(locale::get("form.back"), true, false)) { _reset_to_default_view(); } ImGui::SameLine(); if (ImGui::Button(locale::get("form.change"))) { current_view_state = ui::view_state::EDIT_EXISTING; } ImGui::Spacing(); ImGui::Separator(1.0f); ImGui::Spacing(); ImGui::ProjectForm(&active_project, true); } static void draw_project_list() { if (ImGui::InvalidCompanyInfoWarning()) return; const u32 items_per_page = 50; static s32 current_page = 0; s32 max_page = (administration::project_count() + items_per_page - 1) / items_per_page; if (max_page == 0) max_page = 1; if (ImGui::Button(locale::get("form.create"))) { current_view_state = ui::view_state::CREATE; active_project = administration::project_create_empty(); } if (current_page >= max_page-1) current_page = max_page-1; if (current_page < 0) current_page = 0; ImGui::SameLine(); bool enable_prev = current_page > 0; if (!enable_prev) ImGui::BeginDisabled(); if (ImGui::Button(locale::get("ui.prev")) && current_page > 0) current_page--; if (!enable_prev) ImGui::EndDisabled(); ImGui::SameLine(); ImGui::Text("(%d/%d)", current_page+1, max_page); ImGui::SameLine(); bool enable_next = current_page < max_page-1; if (!enable_next) ImGui::BeginDisabled(); if (ImGui::Button(locale::get("ui.next")) && current_page < max_page-1) current_page++; if (!enable_next) ImGui::EndDisabled(); ImGui::Spacing(); if (ImGui::BeginTable("TableProjects", 3, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg)) { ImGui::TableSetupColumn(locale::get("project.table.identifier"), ImGuiTableColumnFlags_WidthFixed, 80); ImGui::TableSetupColumn(locale::get("project.table.status"), ImGuiTableColumnFlags_WidthFixed, 140); ImGui::TableSetupColumn(locale::get("project.table.description"), ImGuiTableColumnFlags_WidthStretch); ImGui::PushFont(ui::fontBold); ImGui::TableHeadersRow(); ImGui::PopFont(); project project_list[items_per_page]; u32 project_count = administration::project_get_partial_list(current_page, items_per_page, project_list); ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0)); for (u32 i = 0; i < project_count; i++) { project c = project_list[i]; ImGui::TableNextRow(); ImGui::TableSetColumnIndex(0); ImGui::PushID(i); ImGuiSelectableFlags selectable_flags = ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowOverlap; bool selected = false; if (ImGui::Selectable("##invisible_selectable", selected, selectable_flags, ImVec2(0, ImGui::GetFrameHeight()+2.0f))) { active_project = c; current_view_state = ui::view_state::VIEW_EXISTING; } ImGui::SetItemAllowOverlap(); ImGui::SameLine(); ImGui::PopID(); ImGui::Text(c.id); if (administration::project_is_valid(c) != A_ERR_SUCCESS) { if (ImGui::WarningIcon(8.0f)) { ImGui::SetTooltip(locale::get("ui.tooltip.invalidInvoice")); } } ImGui::TableSetColumnIndex(1); ImGui::Text(locale::get(administration::project_get_status_string(c))); ImGui::TableSetColumnIndex(2); ImGui::Text(c.description); } ImGui::PopStyleVar(); ImGui::EndTable(); } } void ui::draw_projects() { switch(current_view_state) { case ui::view_state::LIST_ALL: draw_project_list(); break; case ui::view_state::CREATE: draw_project_create(); break; case ui::view_state::EDIT_EXISTING: draw_project_update(); break; case ui::view_state::VIEW_EXISTING: draw_project_view(); break; default: break; } }