Skip to content

Commit

Permalink
Added GroupDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
nlogozzo committed Nov 2, 2022
1 parent fc5239d commit 92b6bff
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/controllers/groupdialogcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const Group& GroupDialogController::getGroup() const

const std::string& GroupDialogController::getName() const
{
return m_group.getDescription();
return m_group.getName();
}

const std::string& GroupDialogController::getDescription() const
Expand Down
2 changes: 2 additions & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ sources = files('main.cpp',
'ui/views/mainwindow.cpp',
'ui/views/accountview.hpp',
'ui/views/accountview.cpp',
'ui/views/groupdialog.hpp',
'ui/views/groupdialog.cpp',
'ui/views/transactiondialog.hpp',
'ui/views/transactiondialog.cpp',
'ui/views/preferencesdialog.hpp',
Expand Down
15 changes: 13 additions & 2 deletions src/ui/views/accountview.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "accountview.hpp"
#include "groupdialog.hpp"
#include "transactiondialog.hpp"
#include "../controls/messagedialog.hpp"

Expand Down Expand Up @@ -220,12 +221,22 @@ void AccountView::onImportFromCSV()

void AccountView::onNewGroup()
{

GroupDialogController controller{ m_controller.createGroupDialogController() };
GroupDialog dialog{ m_parentWindow, controller };
if(dialog.run())
{
m_controller.addGroup(controller.getGroup());
}
}

void AccountView::onEditGroup(unsigned int id)
{

GroupDialogController controller{ m_controller.createGroupDialogController(id) };
GroupDialog dialog{ m_parentWindow, controller };
if(dialog.run())
{
m_controller.updateGroup(controller.getGroup());
}
}

void AccountView::onDeleteGroup(unsigned int id)
Expand Down
82 changes: 82 additions & 0 deletions src/ui/views/groupdialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include "groupdialog.hpp"

using namespace NickvisionMoney::Controllers;
using namespace NickvisionMoney::UI::Views;

GroupDialog::GroupDialog(GtkWindow* parent, NickvisionMoney::Controllers::GroupDialogController& controller) : m_controller{ controller }, m_gobj{ adw_message_dialog_new(parent, "Group", nullptr) }
{
//Dialog Settings
gtk_window_set_hide_on_close(GTK_WINDOW(m_gobj), true);
adw_message_dialog_add_responses(ADW_MESSAGE_DIALOG(m_gobj), "cancel", "Cancel", "ok", "OK", nullptr);
adw_message_dialog_set_response_appearance(ADW_MESSAGE_DIALOG(m_gobj), "ok", ADW_RESPONSE_SUGGESTED);
adw_message_dialog_set_default_response(ADW_MESSAGE_DIALOG(m_gobj), "ok");
adw_message_dialog_set_close_response(ADW_MESSAGE_DIALOG(m_gobj), "cancel");
g_signal_connect(m_gobj, "response", G_CALLBACK((void (*)(AdwMessageDialog*, gchar*, gpointer))([](AdwMessageDialog*, gchar* response, gpointer data) { reinterpret_cast<GroupDialog*>(data)->setResponse({ response }); })), this);
//Preferences Group
m_preferencesGroup = adw_preferences_group_new();
//Name
m_rowName = adw_entry_row_new();
gtk_widget_set_size_request(m_rowName, 420, -1);
adw_preferences_row_set_title(ADW_PREFERENCES_ROW(m_rowName), "Name");
adw_entry_row_set_activates_default(ADW_ENTRY_ROW(m_rowName), true);
adw_preferences_group_add(ADW_PREFERENCES_GROUP(m_preferencesGroup), m_rowName);
//Description
m_rowDescription = adw_entry_row_new();
gtk_widget_set_size_request(m_rowDescription, 420, -1);
adw_preferences_row_set_title(ADW_PREFERENCES_ROW(m_rowDescription), "Description");
adw_entry_row_set_activates_default(ADW_ENTRY_ROW(m_rowDescription), true);
adw_preferences_group_add(ADW_PREFERENCES_GROUP(m_preferencesGroup), m_rowDescription);
//Layout
adw_message_dialog_set_extra_child(ADW_MESSAGE_DIALOG(m_gobj), m_preferencesGroup);
//Load Transaction
gtk_editable_set_text(GTK_EDITABLE(m_rowName), m_controller.getName().c_str());
gtk_editable_set_text(GTK_EDITABLE(m_rowDescription), m_controller.getDescription().c_str());
}

GtkWidget* GroupDialog::gobj()
{
return m_gobj;
}

bool GroupDialog::run()
{
gtk_widget_show(m_gobj);
gtk_widget_grab_focus(m_rowName);
while(gtk_widget_is_visible(m_gobj))
{
g_main_context_iteration(g_main_context_default(), false);
}
if(m_controller.getResponse() == "ok")
{
gtk_widget_hide(m_gobj);
GroupCheckStatus status{ m_controller.updateGroup(gtk_editable_get_text(GTK_EDITABLE(m_rowName)), gtk_editable_get_text(GTK_EDITABLE(m_rowDescription))) };
//Invalid Group
if(status != GroupCheckStatus::Valid)
{
//Reset UI
gtk_style_context_remove_class(gtk_widget_get_style_context(m_rowName), "error");
adw_preferences_row_set_title(ADW_PREFERENCES_ROW(m_rowName), "Name");
gtk_style_context_remove_class(gtk_widget_get_style_context(m_rowDescription), "error");
adw_preferences_row_set_title(ADW_PREFERENCES_ROW(m_rowDescription), "Description");
//Mark Error
if(status == GroupCheckStatus::EmptyName)
{
gtk_style_context_add_class(gtk_widget_get_style_context(m_rowName), "error");
adw_preferences_row_set_title(ADW_PREFERENCES_ROW(m_rowName), "Name (Empty)");
}
else if(status == GroupCheckStatus::EmptyDescription)
{
gtk_style_context_add_class(gtk_widget_get_style_context(m_rowDescription), "error");
adw_preferences_row_set_title(ADW_PREFERENCES_ROW(m_rowDescription), "Description (Empty)");
}
return run();
}
}
gtk_window_destroy(GTK_WINDOW(m_gobj));
return m_controller.getResponse() == "ok";
}

void GroupDialog::setResponse(const std::string& response)
{
m_controller.setResponse(response);
}
48 changes: 48 additions & 0 deletions src/ui/views/groupdialog.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once

#include <string>
#include <adwaita.h>
#include "../../controllers/groupdialogcontroller.hpp"

namespace NickvisionMoney::UI::Views
{
/**
* A dialog for managing a group
*/
class GroupDialog
{
public:
/**
* Constructs a GroupDialog
*
* @param parent The parent window for the dialog
* @param controller The GroupDialogController
*/
GroupDialog(GtkWindow* parent, NickvisionMoney::Controllers::GroupDialogController& controller);
/**
* Gets the GtkWidget* representing the GroupDialog
*
* @returns The GtkWidget* representing the GroupDialog
*/
GtkWidget* gobj();
/**
* Run the GroupDialog
*
* @returns True if dialog was accepted, else false
*/
bool run();

private:
NickvisionMoney::Controllers::GroupDialogController& m_controller;
GtkWidget* m_gobj;
GtkWidget* m_preferencesGroup{ nullptr };
GtkWidget* m_rowName{ nullptr };
GtkWidget* m_rowDescription{ nullptr };
/**
* Sets the response
*
* @param The new response
*/
void setResponse(const std::string& response);
};
}

0 comments on commit 92b6bff

Please sign in to comment.