diff options
| author | Aldrik Ramaekers <aldrikboy@gmail.com> | 2025-08-09 08:33:08 +0200 |
|---|---|---|
| committer | Aldrik Ramaekers <aldrikboy@gmail.com> | 2025-08-09 08:33:08 +0200 |
| commit | 5d34aff5888d3f0c624251f15bedb96c347978d6 (patch) | |
| tree | 69a49651271d645ca7eaa114cb1830bf759c0930 /src/ui | |
| parent | b94a7ae06b20d550c727d5192cea8baf3e8fb641 (diff) | |
refactors
Diffstat (limited to 'src/ui')
| -rw-r--r-- | src/ui/helpers.cpp | 28 | ||||
| -rw-r--r-- | src/ui/ui_contacts.cpp | 228 | ||||
| -rw-r--r-- | src/ui/ui_main.cpp | 110 | ||||
| -rw-r--r-- | src/ui/ui_projects.cpp | 173 |
4 files changed, 539 insertions, 0 deletions
diff --git a/src/ui/helpers.cpp b/src/ui/helpers.cpp new file mode 100644 index 0000000..74876c0 --- /dev/null +++ b/src/ui/helpers.cpp @@ -0,0 +1,28 @@ +#include "ui.hpp" +#include "imgui.h" +#include "locales.hpp" + +void view_draw_required_tag() +{ + ImDrawList* draw_list = ImGui::GetWindowDrawList(); + + const char* text = localize("form.required"); + ImVec2 text_pos = ImGui::GetCursorScreenPos(); + ImVec2 text_size = ImGui::CalcTextSize(text); + text_pos.y += text_size.y/4.0f; + + ImVec4 bg_color = ImVec4(0.9f, 0.235f, 0.235f, 0.4f); // Red background + ImVec4 text_color = ImVec4(1.0f, 1.0f, 1.0f, 1.0f); // White text + float rounding = 2.0f; + float padding = 2.0f; + + // Background rectangle + ImVec2 bg_min = ImVec2(text_pos.x - padding, text_pos.y - padding); + ImVec2 bg_max = ImVec2(text_pos.x + text_size.x + padding, text_pos.y + text_size.y + padding); + draw_list->AddRectFilled(bg_min, bg_max, ImColor(bg_color), rounding); + + // Foreground text + ImGui::PushStyleColor(ImGuiCol_Text, text_color); + ImGui::TextUnformatted(text); + ImGui::PopStyleColor(); +}
\ No newline at end of file diff --git a/src/ui/ui_contacts.cpp b/src/ui/ui_contacts.cpp new file mode 100644 index 0000000..3115a77 --- /dev/null +++ b/src/ui/ui_contacts.cpp @@ -0,0 +1,228 @@ +#include <stdio.h> + +#include "strops.hpp" +#include "ui.hpp" +#include "imgui.h" +#include "administration.hpp" +#include "locales.hpp" + +static view_state current_view_state = LIST; +static contact selected_for_removal; + +static contact active_contact; + +static void draw_contact_form() +{ + static const char* selected_country = NULL; + + if (ImGui::Button(localize("form.back"))) { + current_view_state = view_state::LIST; + memset(&active_contact, 0, sizeof(contact)); + selected_country = 0; + return; + } + ImGui::Spacing(); + + bool viewing_only = (current_view_state == view_state::VIEW); + + ImGui::BeginDisabled(); + + float widthAvailable = ImGui::GetContentRegionAvail().x; + + ImGui::SetNextItemWidth(widthAvailable*0.2f); + ImGui::InputText(localize("contact.form.identifier"), active_contact.id, IM_ARRAYSIZE(active_contact.id)); + if (!viewing_only) ImGui::EndDisabled(); + + ImGui::SetNextItemWidth(widthAvailable*0.5f); + ImGui::InputTextWithHint(localize("contact.form.fullname"), localize("contact.form.fullname"), active_contact.name, IM_ARRAYSIZE(active_contact.name)); + ImGui::SameLine();view_draw_required_tag(); + + ImGui::SetNextItemWidth(widthAvailable*0.5f); + ImGui::InputTextWithHint(localize("contact.form.address1"), localize("contact.form.address1"), active_contact.address1, IM_ARRAYSIZE(active_contact.address1)); + ImGui::SameLine();view_draw_required_tag(); + + ImGui::SetNextItemWidth(widthAvailable*0.5f); + ImGui::InputTextWithHint(localize("contact.form.address2"), localize("contact.form.address2"), active_contact.address2, IM_ARRAYSIZE(active_contact.address2)); + ImGui::SameLine();view_draw_required_tag(); + + ImGui::SetNextItemWidth(widthAvailable*0.5f); + + const char* countries[] = { localize("country.AT"),localize("country.BE"),localize("country.BG"),localize("country.HR"),localize("country.CY"),localize("country.CZ"),localize("country.DK"),localize("country.EE"),localize("country.FI"),localize("country.FR"),localize("country.DE"),localize("country.GR"),localize("country.HU"),localize("country.IE"),localize("country.IT"),localize("country.LV"),localize("country.LT"),localize("country.LU"),localize("country.MT"),localize("country.NL"),localize("country.PL"),localize("country.PT"),localize("country.RO"),localize("country.SK"),localize("country.SI"),localize("country.ES"),localize("country.SE") }; + s32 country_count = sizeof(countries) / sizeof(countries[0]); + if (selected_country == 0) { + for (int i = 0; i < country_count; i++) + { + if (strcmp(countries[i], active_contact.country) == 0) + { + selected_country = countries[i]; + break; + } + } + } + + if (ImGui::BeginCombo(localize("contact.form.country"), selected_country)) + { + for (int n = 0; n < IM_ARRAYSIZE(countries); n++) + { + bool is_selected = (selected_country == countries[n]); + if (ImGui::Selectable(countries[n], is_selected)) + selected_country = countries[n]; + } + ImGui::EndCombo(); + } + if (selected_country) { + strops_copy(active_contact.country, selected_country, IM_ARRAYSIZE(active_contact.country)); + } + ImGui::SameLine();view_draw_required_tag(); + + ImGui::SetNextItemWidth(widthAvailable*0.5f); + ImGui::InputTextWithHint(localize("contact.form.taxnumber"), localize("contact.form.taxnumber"), active_contact.taxid, IM_ARRAYSIZE(active_contact.taxid)); + + ImGui::SetNextItemWidth(widthAvailable*0.5f); + ImGui::InputTextWithHint(localize("contact.form.businessnumber"), localize("contact.form.businessnumber"), active_contact.businessid, IM_ARRAYSIZE(active_contact.businessid)); + + ImGui::SetNextItemWidth(widthAvailable*0.5f); + ImGui::InputTextWithHint(localize("contact.form.email"), localize("contact.form.email"), active_contact.email, IM_ARRAYSIZE(active_contact.email)); + + ImGui::SetNextItemWidth(widthAvailable*0.5f); + ImGui::InputTextWithHint(localize("contact.form.phonenumber"), localize("contact.form.phonenumber"), active_contact.phone_number, IM_ARRAYSIZE(active_contact.phone_number)); + + ImGui::SetNextItemWidth(widthAvailable*0.5f); + ImGui::InputTextWithHint(localize("contact.form.bankaccount"), localize("contact.form.bankaccount"), active_contact.bank_account, IM_ARRAYSIZE(active_contact.bank_account)); + + if (viewing_only) ImGui::EndDisabled(); + + if (!viewing_only) { + bool can_save = strlen(active_contact.name) > 0 && strlen(active_contact.address1) > 0 && + strlen(active_contact.address2) > 0 && strlen(active_contact.country) > 0; + + if (!can_save) ImGui::BeginDisabled(); + // Save button + ImGui::Spacing(); + if (ImGui::Button(localize("form.save"))) { + if (current_view_state == view_state::CREATE) + administration_create_contact(active_contact); + + else if (current_view_state == view_state::EDIT) + administration_update_contact(active_contact); + + memset(&active_contact, 0, sizeof(contact)); + current_view_state = view_state::LIST; + selected_country = 0; + } + if (!can_save) ImGui::EndDisabled(); + } + else { + // TODO list invoices connected to contact. + } +} + +static void draw_contact_list() +{ + const u32 items_per_page = 50; + static s32 current_page = 0; + s32 max_page = (administration_get_contact_count() + items_per_page - 1) / items_per_page; + if (max_page == 0) max_page = 1; + + if (ImGui::Button(localize("form.create"))) + { + current_view_state = view_state::CREATE; + memset(&active_contact, 0, sizeof(contact)); + snprintf(active_contact.id, IM_ARRAYSIZE(active_contact.id), "C/%d", administration_create_id()); + } + + 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("<< 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("Next >>") && current_page < max_page-1) current_page++; + if (!enable_next) ImGui::EndDisabled(); + + ImGui::Spacing(); + + if (ImGui::BeginTable("TableContacts", 4, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg)) { + + ImGui::TableSetupColumn(localize("contact.table.identifier"), ImGuiTableColumnFlags_WidthFixed, 80); + ImGui::TableSetupColumn(localize("contact.table.name"), ImGuiTableColumnFlags_WidthStretch); + ImGui::TableSetupColumn(localize("contact.table.address"), ImGuiTableColumnFlags_WidthStretch); + ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed, 160); + ImGui::TableHeadersRow(); + + contact contact_list[items_per_page]; + u32 contact_count = administration_get_contacts(current_page, items_per_page, contact_list); + + for (u32 i = 0; i < contact_count; i++) { + contact c = contact_list[i]; + + ImGui::TableNextRow(); + ImGui::TableSetColumnIndex(0); ImGui::Text(c.id); + ImGui::TableSetColumnIndex(1); ImGui::Text(c.name); + ImGui::TableSetColumnIndex(2); ImGui::Text("%s %s", c.address1, c.address2); + + ImGui::TableSetColumnIndex(3); + + char btn_name[20]; + snprintf(btn_name, sizeof(btn_name), "%s##%d", localize("form.view"), i); + if (ImGui::Button(btn_name)) { + active_contact = c; + current_view_state = view_state::VIEW; + } + + ImGui::SameLine(); + + snprintf(btn_name, sizeof(btn_name), "%s##%d", localize("form.change"), i); + if (ImGui::Button(btn_name)) { + active_contact = c; + current_view_state = view_state::EDIT; + } + + ImGui::SameLine(); + + // TODO check to make sure no invoices are connected to this contact. + snprintf(btn_name, sizeof(btn_name), "%s##%d", localize("form.delete"), i); + if (ImGui::Button(btn_name)) { + selected_for_removal = c; + ImGui::OpenPopup("ConfirmDeletePopup"); + } + } + + if (ImGui::BeginPopupModal("ConfirmDeletePopup", nullptr, ImGuiWindowFlags_AlwaysAutoResize|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoTitleBar)) { + ImGui::Text(localize("form.confirmDelete")); + ImGui::Separator(); + + if (ImGui::Button(localize("form.yes"), ImVec2(120, 0))) { + administration_remove_contact(selected_for_removal); + ImGui::CloseCurrentPopup(); + } + ImGui::SameLine(); + if (ImGui::Button(localize("form.no"), ImVec2(120, 0))) { + ImGui::CloseCurrentPopup(); + } + ImGui::EndPopup(); + } + + ImGui::EndTable(); + } +} + +void ui_draw_contacts() +{ + switch(current_view_state) + { + case view_state::LIST: draw_contact_list(); break; + case view_state::CREATE: draw_contact_form(); break; + case view_state::EDIT: draw_contact_form(); break; + case view_state::VIEW: draw_contact_form(); break; + } +}
\ No newline at end of file diff --git a/src/ui/ui_main.cpp b/src/ui/ui_main.cpp new file mode 100644 index 0000000..f6661b8 --- /dev/null +++ b/src/ui/ui_main.cpp @@ -0,0 +1,110 @@ +#include "ui.hpp" +#include "imgui.h" +#include "administration.hpp" +#include "locales.hpp" + +typedef enum +{ + INVOICES = 0, + EXPENSES = 1, + CONTACTS = 2, + REPORT_RESULTS = 3, + REPORT_TAX = 4, + PROJECTS = 5, + + END +} dashboard_view_state; + +static dashboard_view_state dashboard_state = dashboard_view_state::INVOICES; +void (*drawcalls[dashboard_view_state::END])(void) = { + 0, + 0, + ui_draw_contacts, + 0, + 0, + ui_draw_projects, +}; + +void ui_draw_main() +{ + // @localize + if (ImGui::BeginMainMenuBar()) + { + if (ImGui::BeginMenu("File")) + { + if (ImGui::MenuItem("Open", "Ctrl+O")) { /* Handle Open */ } + if (ImGui::MenuItem("Save", "Ctrl+S")) { /* Handle Save */ } + + ImGui::EndMenu(); + } + ImGui::EndMainMenuBar(); + } + + ImGuiIO& io = ImGui::GetIO(); + float menuBarHeight = ImGui::GetFrameHeight(); + float statusBarHeight = 26.0f; + float sidePanelWidth = 120.0f; + ImGui::SetNextWindowPos(ImVec2(0, menuBarHeight)); + ImGui::SetNextWindowSize(ImVec2(sidePanelWidth, io.DisplaySize.y - menuBarHeight - statusBarHeight)); + + // Side panel + ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0)); + ImGui::Begin("SidePanel", nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse); + { + // Navigation buttons with custom styling + ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.0f, 0.0f, 0.0f, 0.0f)); // Transparent background + ImGui::PushStyleVar(ImGuiStyleVar_ButtonTextAlign, ImVec2(0.0f, 0.5f)); + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0)); // Reduce spacing between buttons + ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(10.0f, 0.0f)); + + float buttonWidth = sidePanelWidth; + + if (ImGui::Button(localize("nav.invoices"), ImVec2(buttonWidth, 24))) dashboard_state = dashboard_view_state::INVOICES; + if (ImGui::Button(localize("nav.expenses"), ImVec2(buttonWidth, 24))) dashboard_state = dashboard_view_state::EXPENSES; + if (ImGui::Button(localize("nav.contacts"), ImVec2(buttonWidth, 24))) dashboard_state = dashboard_view_state::CONTACTS; + + static bool reports_opened = false; + if (ImGui::Button(localize("nav.reports"), ImVec2(buttonWidth, 24))) reports_opened = !reports_opened; + if (reports_opened) + { + ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(20.0f, 0.0f)); + if (ImGui::Button(localize("nav.reports.results"), ImVec2(buttonWidth, 24))) dashboard_state = dashboard_view_state::REPORT_RESULTS; + if (ImGui::Button(localize("nav.reports.tax"), ImVec2(buttonWidth, 24))) dashboard_state = dashboard_view_state::REPORT_TAX; + ImGui::PopStyleVar(); + } + + if (ImGui::Button(localize("nav.Projects"), ImVec2(buttonWidth, 24))) dashboard_state = dashboard_view_state::PROJECTS; + + ImGui::PopStyleColor(1); + ImGui::PopStyleVar(3); + } + ImGui::End(); + ImGui::PopStyleVar(); + + ImGui::SetNextWindowPos(ImVec2(sidePanelWidth, menuBarHeight)); + ImGui::SetNextWindowSize(ImVec2(io.DisplaySize.x - sidePanelWidth, io.DisplaySize.y - menuBarHeight - statusBarHeight)); + + // Main content + ImGui::Begin("AccountingMainWindow", nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse); + if (drawcalls[dashboard_state]) drawcalls[dashboard_state](); + ImGui::End(); + + // Status bar. + ImGui::SetNextWindowPos(ImVec2(0, io.DisplaySize.y - statusBarHeight)); + ImGui::SetNextWindowSize(ImVec2(io.DisplaySize.x, statusBarHeight)); + + ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(10, 3)); + ImGui::Begin("StatusBar", nullptr, + ImGuiWindowFlags_NoTitleBar | + ImGuiWindowFlags_NoResize | + ImGuiWindowFlags_NoMove | + ImGuiWindowFlags_NoScrollbar | + ImGuiWindowFlags_NoSavedSettings | + ImGuiWindowFlags_NoBringToFrontOnFocus | + ImGuiWindowFlags_NoCollapse); + + ImGui::Text("Working on: %s", administration_get_file_path()); // @localize + + ImGui::End(); + ImGui::PopStyleVar(); +}
\ No newline at end of file diff --git a/src/ui/ui_projects.cpp b/src/ui/ui_projects.cpp new file mode 100644 index 0000000..9490254 --- /dev/null +++ b/src/ui/ui_projects.cpp @@ -0,0 +1,173 @@ +#include <stdio.h> + +#include "ui.hpp" +#include "imgui.h" +#include "administration.hpp" +#include "locales.hpp" + +static view_state current_view_state = LIST; +static project selected_for_cancellation; + +static project active_project; + +static void draw_project_form() +{ + static const char* selected_country = NULL; + + if (ImGui::Button(localize("form.back"))) { + current_view_state = view_state::LIST; + memset(&active_project, 0, sizeof(project)); + selected_country = 0; + return; + } + ImGui::Spacing(); + + bool viewing_only = (current_view_state == view_state::VIEW); + + ImGui::BeginDisabled(); + + float widthAvailable = ImGui::GetContentRegionAvail().x; + + ImGui::SetNextItemWidth(widthAvailable*0.2f); + ImGui::InputText(localize("contact.form.identifier"), active_project.id, IM_ARRAYSIZE(active_project.id)); + if (!viewing_only) ImGui::EndDisabled(); + + ImGui::SetNextItemWidth(widthAvailable*0.5f); + ImGui::InputTextWithHint(localize("project.form.description"), localize("project.form.description"), active_project.description, IM_ARRAYSIZE(active_project.description)); + ImGui::SameLine();view_draw_required_tag(); + + if (viewing_only) ImGui::EndDisabled(); + + if (!viewing_only) { + bool can_save = strlen(active_project.description) > 0; + + if (!can_save) ImGui::BeginDisabled(); + // Save button + ImGui::Spacing(); + if (ImGui::Button(localize("form.save"))) { + if (current_view_state == view_state::CREATE) + administration_create_project(active_project); + + else if (current_view_state == view_state::EDIT) + administration_update_project(active_project); + + memset(&active_project, 0, sizeof(project)); + current_view_state = view_state::LIST; + selected_country = 0; + } + if (!can_save) ImGui::EndDisabled(); + } + else { + // TODO list invoices connected to project. + } +} + +static void draw_project_list() +{ + const u32 items_per_page = 50; + static s32 current_page = 0; + s32 max_page = (administration_get_project_count() + items_per_page - 1) / items_per_page; + if (max_page == 0) max_page = 1; + + if (ImGui::Button(localize("form.create"))) + { + current_view_state = view_state::CREATE; + memset(&active_project, 0, sizeof(project)); + snprintf(active_project.id, IM_ARRAYSIZE(active_project.id), "P/%d", administration_create_id()); + } + + 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("<< 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("Next >>") && current_page < max_page-1) current_page++; + if (!enable_next) ImGui::EndDisabled(); + + ImGui::Spacing(); + + if (ImGui::BeginTable("TableProjects", 4, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg)) { + + ImGui::TableSetupColumn(localize("project.table.identifier"), ImGuiTableColumnFlags_WidthFixed, 80); + ImGui::TableSetupColumn(localize("project.table.status"), ImGuiTableColumnFlags_WidthFixed, 140); + ImGui::TableSetupColumn(localize("project.table.description"), ImGuiTableColumnFlags_WidthStretch); + ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed, 160); + ImGui::TableHeadersRow(); + + project project_list[items_per_page]; + u32 project_count = administration_get_projects(current_page, items_per_page, project_list); + + for (u32 i = 0; i < project_count; i++) { + project c = project_list[i]; + + ImGui::TableNextRow(); + ImGui::TableSetColumnIndex(0); ImGui::Text(c.id); + ImGui::TableSetColumnIndex(1); ImGui::Text(localize(administration_project_get_status_string(c))); + ImGui::TableSetColumnIndex(2); ImGui::Text(c.description); + + ImGui::TableSetColumnIndex(3); + + char btn_name[20]; + snprintf(btn_name, sizeof(btn_name), "%s##%d", localize("form.view"), i); + if (ImGui::Button(btn_name)) { + active_project = c; + current_view_state = view_state::VIEW; + } + + if (c.state == project_state::RUNNING) + { + ImGui::SameLine(); + snprintf(btn_name, sizeof(btn_name), "%s##%d", localize("form.change"), i); + if (ImGui::Button(btn_name)) { + active_project = c; + current_view_state = view_state::EDIT; + } + + ImGui::SameLine(); + snprintf(btn_name, sizeof(btn_name), "%s##%d", localize("form.cancel"), i); + if (ImGui::Button(btn_name)) { + selected_for_cancellation = c; + ImGui::OpenPopup("ConfirmCancelProject"); + } + } + } + + if (ImGui::BeginPopupModal("ConfirmCancelProject", nullptr, ImGuiWindowFlags_AlwaysAutoResize|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoTitleBar)) { + ImGui::Text(localize("form.confirmCancelProject")); + ImGui::Separator(); + + if (ImGui::Button(localize("form.yes"), ImVec2(120, 0))) { + administration_cancel_project(selected_for_cancellation); + ImGui::CloseCurrentPopup(); + } + ImGui::SameLine(); + if (ImGui::Button(localize("form.no"), ImVec2(120, 0))) { + ImGui::CloseCurrentPopup(); + } + ImGui::EndPopup(); + } + + ImGui::EndTable(); + } +} + +void ui_draw_projects() +{ + switch(current_view_state) + { + case view_state::LIST: draw_project_list(); break; + case view_state::CREATE: draw_project_form(); break; + case view_state::EDIT: draw_project_form(); break; + case view_state::VIEW: draw_project_form(); break; + } +}
\ No newline at end of file |
