Add time config

This commit is contained in:
daleclack 2023-09-09 18:47:35 +08:00
parent 70a4984575
commit b813ed623b
3 changed files with 153 additions and 9 deletions

View File

@ -1,15 +1,147 @@
#include "MyReminder.h"
#include "MyPrefs.h"
#include "config.h"
#include <fstream>
struct _MyPrefs
{
GtkWindow parent_instance;
GtkWidget *main_box, *btn_box;
GtkWidget *label_color;
GtkWidget *calendar_frame, *calendar;
GtkWidget *color_button;
GtkWidget *btnapply;
GtkColorDialog *dialog;
GtkWidget *color_drop;
};
G_DEFINE_TYPE(MyPrefs, my_prefs, GTK_TYPE_WINDOW)
static void load_reminder_config(MyPrefs *prefs)
{
// Open file for json config
std::fstream infile;
infile.open("config.json", std::ios_base::in);
json json_data;
GDateTime *selected_day;
if (infile.is_open())
{
json_data = json::parse(infile);
}
else
{
// Select Default Date
GTimeZone *timezone = g_time_zone_new_local();
selected_day = g_date_time_new(timezone, default_year,
default_month, default_day, 8, 0, 0);
gtk_calendar_select_day(GTK_CALENDAR(prefs->calendar), selected_day);
return;
}
if (!json_data.empty())
{
// Get date from json data
int year = json_data["year"];
int month = json_data["month"];
int day = json_data["day"];
// Select Day
GTimeZone *timezone = g_time_zone_new_local();
selected_day = g_date_time_new(timezone, year,
month, day, 8, 0, 0);
gtk_calendar_select_day(GTK_CALENDAR(prefs->calendar), selected_day);
}
else
{
// Select Default Date
GTimeZone *timezone = g_time_zone_new_local();
selected_day = g_date_time_new(timezone, default_year,
default_month, default_day, 8, 0, 0);
gtk_calendar_select_day(GTK_CALENDAR(prefs->calendar), selected_day);
}
}
static void btnapply_clicked(GtkWidget *widget, MyPrefs *prefs)
{
// Get Selected year,month and day
GDateTime *selected_day;
selected_day = gtk_calendar_get_date(GTK_CALENDAR(prefs->calendar));
int year1, month1, day1;
year1 = g_date_time_get_year(selected_day);
month1 = g_date_time_get_month(selected_day);
day1 = g_date_time_get_day_of_month(selected_day);
// Create json data
json out_data = json::parse(R"(
{
"color_set":"blue",
"year":2023,
"month":12,
"day":23
}
)");
out_data["year"] = year1;
out_data["month"] = month1;
out_data["day"] = day1;
// Save data to the json file
std::fstream outfile;
outfile.open("config.json", std::ios_base::out);
// Save data to the file when available
if (outfile.is_open())
{
outfile<<out_data;
}
outfile.close();
}
static void my_prefs_init(MyPrefs *self)
{
// Initalize preferneces window
// gtk_window_set_icon_name(GTK_WINDOW(self), "org.gtk.daleclack");
gtk_window_set_default_size(GTK_WINDOW(self), 300, 200);
// Create layout and widgets
self->main_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
self->btn_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
self->calendar_frame = gtk_frame_new("Select a date For reminder");
gtk_widget_set_halign(self->main_box, GTK_ALIGN_CENTER);
gtk_widget_set_valign(self->main_box, GTK_ALIGN_CENTER);
// Create calendar and add it to the frame
self->calendar = gtk_calendar_new();
gtk_frame_set_child(GTK_FRAME(self->calendar_frame), self->calendar);
gtk_box_append(GTK_BOX(self->main_box), self->calendar_frame);
// Load config
load_reminder_config(self);
// Create information labels
self->label_color = gtk_label_new("Font Color:");
gtk_box_append(GTK_BOX(self->btn_box), self->label_color);
// Create color widgets
self->dialog = gtk_color_dialog_new();
self->color_button = gtk_color_dialog_button_new(self->dialog);
gtk_box_append(GTK_BOX(self->btn_box), self->color_button);
gtk_widget_set_halign(self->btn_box, GTK_ALIGN_CENTER);
gtk_box_append(GTK_BOX(self->main_box), self->btn_box);
// Add button to the main box
self->btnapply = gtk_button_new_with_label("Apply Settings");
g_signal_connect(self->btnapply, "clicked", G_CALLBACK(btnapply_clicked), self);
gtk_widget_set_halign(self->btnapply, GTK_ALIGN_CENTER);
gtk_box_append(GTK_BOX(self->main_box), self->btnapply);
// Set margin
gtk_widget_set_margin_bottom(self->main_box, 10);
gtk_widget_set_margin_end(self->main_box, 10);
gtk_widget_set_margin_start(self->main_box, 10);
gtk_widget_set_margin_top(self->main_box, 10);
gtk_window_set_child(GTK_WINDOW(self), self->main_box);
}
static void my_prefs_class_init(MyPrefsClass *klass)

View File

@ -1,13 +1,8 @@
#include "MyReminder.h"
#include <fstream>
#include "timer.h"
// Default config values
#define default_color "blue"
#define default_year 2023
#define default_month 12
#define default_day 23
#define label_max_length 120
#include "MyPrefs.h"
#include "config.h"
struct _MyReminder
{
@ -63,6 +58,14 @@ static void load_json_data(MyReminder *self)
}
}
static void btnprefs_clicked(GtkWidget *widget, MyReminder *win)
{
// Show the preference window
// Create Preference window
MyPrefs *prefs_window = my_prefs_new(GTK_WINDOW(win));
gtk_window_present(GTK_WINDOW(prefs_window));
}
static void my_reminder_init(MyReminder *self)
{
// Set properties of window
@ -73,12 +76,13 @@ static void my_reminder_init(MyReminder *self)
// Create header bar
self->header = gtk_header_bar_new();
gtk_header_bar_set_decoration_layout(GTK_HEADER_BAR(self->header),
"close,minimize,maximize,icon:menu");
"close,minimize,maximize:menu");
gtk_window_set_titlebar(GTK_WINDOW(self), self->header);
// Create button for preference
self->btn_prefs = gtk_button_new_with_label("Prefs");
gtk_header_bar_pack_end(GTK_HEADER_BAR(self->header), self->btn_prefs);
g_signal_connect(self->btn_prefs, "clicked", G_CALLBACK(btnprefs_clicked), self);
// Load data from json file
load_json_data(self);

View File

@ -0,0 +1,8 @@
#pragma once
// Default config values
#define default_color "blue"
#define default_year 2023
#define default_month 12
#define default_day 23
#define label_max_length 120