Skip to content

Commit

Permalink
GNOME - Better Dialog Management
Browse files Browse the repository at this point in the history
  • Loading branch information
nlogozzo committed May 21, 2024
1 parent 6933cd2 commit d8534da
Show file tree
Hide file tree
Showing 45 changed files with 1,014 additions and 859 deletions.
2 changes: 1 addition & 1 deletion docs/po/denaro.pot
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2024-05-21 00:20-0400\n"
"POT-Creation-Date: 2024-05-21 19:48-0400\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

namespace Nickvision::Money::Shared::Controllers
{
/**
* @brief A controller for an AccountSettingsDialog.
*/
class AccountSettingsDialogController
{
public:
Expand Down
6 changes: 3 additions & 3 deletions libdenaro/src/controllers/accountsettingsdialogcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ namespace Nickvision::Money::Shared::Controllers
metadata.setType(accountType);
m_account->setMetadata(metadata);
}

void AccountSettingsDialogController::setDefaultTransactionType(Models::TransactionType defaultTransactionType)
{
AccountMetadata metadata{ m_account->getMetadata() };
metadata.setDefaultTransactionType(defaultTransactionType);
m_account->setMetadata(metadata);
}

void AccountSettingsDialogController::setCustomCurrencyOff()
{
AccountMetadata metadata{ m_account->getMetadata() };
metadata.setUseCustomCurrency(false);
metadata.setCustomCurrency({});
m_account->setMetadata(metadata);
}

Models::CurrencyCheckStatus AccountSettingsDialogController::setCustomCurrency(const std::string& symbol, const std::string& code, char decimalSeparator, char groupSeparator, int decimalDigits, Models::AmountStyle amountStyle)
{
Currency currency{ symbol, code };
Expand Down
4 changes: 3 additions & 1 deletion org.nickvision.money.gnome/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ if(LINUX)
add_executable(org.nickvision.money.gnome
"src/controls/currencyconverterpage.cpp"
"src/helpers/builder.cpp"
"src/helpers/dialogbase.cpp"
"src/views/accountpage.cpp"
"src/views/accountsettingsdialog.cpp"
"src/views/newaccountdialog.cpp"
"src/views/mainwindow.cpp"
"src/views/preferencesdialog.cpp"
Expand Down Expand Up @@ -66,4 +68,4 @@ if(LINUX)
if(UPDATE_MIME_DATABASE_EXECUTABLE)
install(CODE "execute_process(COMMAND ${UPDATE_MIME_DATABASE_EXECUTABLE} ${CMAKE_INSTALL_FULL_DATADIR}/mime)")
endif()
endif()
endif()
15 changes: 15 additions & 0 deletions org.nickvision.money.gnome/blueprints/account_settings_dialog.blp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Gtk 4.0;
using Adw 1;

Adw.Dialog root {
content-width: 420;

child: Adw.ToolbarView {
[top]
Adw.HeaderBar {
title-widget: Adw.WindowTitle {
title: _("Account Settings");
};
}
};
}
4 changes: 2 additions & 2 deletions org.nickvision.money.gnome/blueprints/new_account_dialog.blp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ using Gtk 4.0;
using Adw 1;

Adw.Dialog root {
content-width: 460;
content-width: 420;

child: Adw.ToolbarView {
[top]
Expand Down Expand Up @@ -318,4 +318,4 @@ Adw.Dialog root {
}
}
};
}
}
43 changes: 43 additions & 0 deletions org.nickvision.money.gnome/include/helpers/dialogbase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef DIALOGBASE_H
#define DIALOGBASE_H

#include <string>
#include <adwaita.h>

namespace Nickvision::Money::GNOME::Helpers
{
/**
* @brief A base class for custom AdwDialogs with blueprints.
*/
class DialogBase
{
public:
/**
* @brief Constructs a DialogBase.
* @param parent GtkWindow*
* @param fileName The file name for the blueprint file of the dialog
* @param rootName The name of the AdwDialog component in the blueprint file
*/
DialogBase(GtkWindow* parent, const std::string& fileName, const std::string& rootName = "root");
/**
* @brief Destructs a DialogBase.
*/
~DialogBase();
/**
* @brief Gets the underlying AdwDialog pointer.
* @return AdwDialog*
*/
AdwDialog* get();
/**
* @brief Presents the AdwDialog.
*/
void present() const;

protected:
GtkBuilder* m_builder;
GtkWindow* m_parent;
AdwDialog* m_dialog;
};
}

#endif //DIALOGBASE_H
54 changes: 54 additions & 0 deletions org.nickvision.money.gnome/include/helpers/dialogptr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#ifndef DIALOGPTR_H
#define DIALOGPTR_H

#include <adwaita.h>
#include "dialogbase.h"

namespace Nickvision::Money::GNOME::Helpers
{
template<typename T>
concept DerivedDialogBase = std::is_base_of_v<DialogBase, T>;

/**
* @brief A pointer for a custom AdwDialog.
* @brief Keeps the pointer alive until the dialog has closed.
*/
template<DerivedDialogBase T>
class DialogPtr
{
public:
/**
* @brief Constructs a DialogPtr.
* @param args The arguments to pass to T's constructor.
*/
template<typename... Args>
DialogPtr(Args... args)
: m_ptr{ new T(args...) }
{
g_signal_connect(m_ptr->get(), "closed", GCallback(+[](AdwDialog*, gpointer data){ delete reinterpret_cast<T*>(data); }), m_ptr);
}
/**
* @brief Constructs a DialogPtr.
* @brief The DialogPtr assumes ownership of ptr.
* @param ptr T*
*/
DialogPtr(T* ptr)
: m_ptr{ ptr }
{
g_signal_connect(m_ptr->get(), "closed", GCallback(+[](AdwDialog*, gpointer data){ delete reinterpret_cast<T*>(data); }), m_ptr);
}
/**
* @brief Returns the underlying pointer.
* @return T*
*/
T* operator->()
{
return m_ptr;
}

private:
T* m_ptr;
};
}

#endif //DIALOGPTR_H
29 changes: 29 additions & 0 deletions org.nickvision.money.gnome/include/views/accountsettingsdialog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef ACCOUNTSETTINGSDIALOG_H
#define ACCOUNTSETTINGSDIALOG_H

#include <memory>
#include <adwaita.h>
#include "controllers/accountsettingsdialogcontroller.h"
#include "helpers/dialogbase.h"

namespace Nickvision::Money::GNOME::Views
{
/**
* @brief A dialog for managing account settings.
*/
class AccountSettingsDialog : public Helpers::DialogBase
{
public:
/**
* @brief Constructs an AccountSettingsDialog.
* @param controller AccountSettingsDialogController
* @param parent The GtkWindow object of the parent window
*/
AccountSettingsDialog(const std::shared_ptr<Shared::Controllers::AccountSettingsDialogController>& controller, GtkWindow* parent);

private:
std::shared_ptr<Shared::Controllers::AccountSettingsDialogController> m_controller;
};
}

#endif //ACCOUNTSETTINGSDIALOG_H
33 changes: 6 additions & 27 deletions org.nickvision.money.gnome/include/views/newaccountdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,29 @@
#include <libnick/events/event.h>
#include <libnick/events/parameventargs.h>
#include "controllers/newaccountdialogcontroller.h"
#include "helpers/dialogbase.h"

namespace Nickvision::Money::GNOME::Views
{
/**
* @brief A dialog for creating a new account.
*/
class NewAccountDialog
class NewAccountDialog : public Helpers::DialogBase
{
public:
/**
* @brief Creates a new NewAccountDialog.
* @param controller The NewAccountDialogController
* @brief Constructs a NewAccountDialog.
* @param controller NewAccountDialogController
* @param parent The GtkWindow object of the parent window
* @return NewAccountDialog* (The caller is NOT responsible for deleting the returned pointer)
*/
static NewAccountDialog* create(const std::shared_ptr<Shared::Controllers::NewAccountDialogController>& controller, GtkWindow* parent);
/**
* @brief Destructs a NewAccountDialog.
*/
~NewAccountDialog();
NewAccountDialog(const std::shared_ptr<Shared::Controllers::NewAccountDialogController>& controller, GtkWindow* parent);
/**
* @brief Gets the event when the dialog is finished (i.e. the user has created the account)
* @return The finished event
*/
Nickvision::Events::Event<Nickvision::Events::ParamEventArgs<std::shared_ptr<Shared::Controllers::NewAccountDialogController>>>& finished();
/**
* @brief Presents the NewAccountDialog.
*/
void present() const;

private:
/**
* @brief Constructs a NewAccountDialog.
* @param controller NewAccountDialogController
* @param parent The GtkWindow object of the parent window
*/
NewAccountDialog(const std::shared_ptr<Shared::Controllers::NewAccountDialogController>& controller, GtkWindow* parent);
/**
* @brief Handles when the dialog is closed.
*/
void onClosed();
/**
* @brief Navigates to the previous page.
*/
Expand Down Expand Up @@ -80,14 +62,11 @@ namespace Nickvision::Money::GNOME::Views
*/
void clearImportFile();
/**
* @brief Finishes the dialog.
* @brief Finishes the dialog.
*/
void finish();
std::shared_ptr<Shared::Controllers::NewAccountDialogController> m_controller;
Nickvision::Events::Event<Nickvision::Events::ParamEventArgs<std::shared_ptr<Shared::Controllers::NewAccountDialogController>>> m_finished;
GtkBuilder* m_builder;
GtkWindow* m_parent;
AdwDialog* m_dialog;
int m_currentPageNumber;
};
}
Expand Down
29 changes: 5 additions & 24 deletions org.nickvision.money.gnome/include/views/preferencesdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,24 @@
#include <memory>
#include <adwaita.h>
#include "controllers/preferencesviewcontroller.h"
#include "helpers/dialogbase.h"

namespace Nickvision::Money::GNOME::Views
{
/**
* @brief The preferences dialog for the application.
*/
class PreferencesDialog
class PreferencesDialog : public Helpers::DialogBase
{
public:
/**
* @brief Creates a new PreferencesDialog.
* @brief Constructs a PreferencesDialog.
* @param controller The PreferencesViewController
* @return PreferencesDialog* (The caller is NOT responsible for deleting the returned pointer)
*/
static PreferencesDialog* create(const std::shared_ptr<Shared::Controllers::PreferencesViewController>& controller);
/**
* @brief Destructs a PreferencesDialog.
*/
~PreferencesDialog();
/**
* @brief Presents the PreferencesDialog.
* @param parent The GtkWindow object of the parent window
*/
void present(GtkWindow* parent) const;
PreferencesDialog(const std::shared_ptr<Shared::Controllers::PreferencesViewController>& controller, GtkWindow* parent);

private:
/**
* @brief Constructs a PreferencesDialog.
* @param controller The PreferencesViewController
*/
PreferencesDialog(const std::shared_ptr<Shared::Controllers::PreferencesViewController>& controller);
/**
* @brief Handles when the dialog is closed.
*/
void onClosed();
/**
* @brief Applies the changes to the app's configuration object.
*/
Expand All @@ -48,9 +31,7 @@ namespace Nickvision::Money::GNOME::Views
*/
void onThemeChanged();
std::shared_ptr<Shared::Controllers::PreferencesViewController> m_controller;
GtkBuilder* m_builder;
AdwPreferencesDialog* m_dialog;
};
}

#endif //PREFERENCESDIALOG_H
#endif //PREFERENCESDIALOG_H
29 changes: 29 additions & 0 deletions org.nickvision.money.gnome/src/helpers/dialogbase.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "helpers/dialogbase.h"
#include "helpers/builder.h"

namespace Nickvision::Money::GNOME::Helpers
{
DialogBase::DialogBase(GtkWindow* parent, const std::string& fileName, const std::string& rootName)
: m_builder{ BuilderHelpers::fromBlueprint(fileName) },
m_parent{ parent },
m_dialog{ ADW_DIALOG(gtk_builder_get_object(m_builder, rootName.c_str())) }
{

}

DialogBase::~DialogBase()
{
adw_dialog_force_close(m_dialog);
g_object_unref(m_builder);
}

AdwDialog* DialogBase::get()
{
return m_dialog;
}

void DialogBase::present() const
{
adw_dialog_present(m_dialog, GTK_WIDGET(m_parent));
}
}
15 changes: 15 additions & 0 deletions org.nickvision.money.gnome/src/views/accountsettingsdialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "views/accountsettingsdialog.h"
#include <libnick/localization/gettext.h>

using namespace Nickvision::Money::GNOME::Helpers;
using namespace Nickvision::Money::Shared::Controllers;

namespace Nickvision::Money::GNOME::Views
{
AccountSettingsDialog::AccountSettingsDialog(const std::shared_ptr<AccountSettingsDialogController>& controller, GtkWindow* parent)
: DialogBase{ parent, "account_settings_dialog" },
m_controller{ controller }
{

}
}
Loading

0 comments on commit d8534da

Please sign in to comment.