diff --git a/meson.build b/meson.build index e403352..d6e260f 100644 --- a/meson.build +++ b/meson.build @@ -35,7 +35,6 @@ executable( 'src' / 'grade.vala', 'src' / 'subject.vala', 'src' / 'new-grade-dialog.vala', - 'src' / 'new-subject-dialog.vala', 'src' / 'category.vala', 'src' / 'edit-subject-dialog.vala', 'src' / 'add-category-dialog.vala', diff --git a/po/POTFILES b/po/POTFILES index 55fd462..21757ce 100644 --- a/po/POTFILES +++ b/po/POTFILES @@ -4,7 +4,6 @@ src/category.vala src/edit-subject-dialog.vala src/grade.vala src/new-grade-dialog.vala -src/new-subject-dialog.vala src/subject-manager.vala src/subject-page.vala src/subject-parser.vala diff --git a/src/add-category-dialog.vala b/src/add-category-dialog.vala index 57110b2..9db03aa 100644 --- a/src/add-category-dialog.vala +++ b/src/add-category-dialog.vala @@ -11,30 +11,28 @@ public class AddCategoryDialog : Adw.MessageDialog { this.add_response ("cancel", _("Cancel")); this.add_response ("add", _("Add")); - this.set_response_appearance ("add", Adw.ResponseAppearance.SUGGESTED); + this.set_response_appearance ("add", Adw.ResponseAppearance.SUGGESTED); this.set_response_enabled ("add", false); - this.set_close_response ("cancel"); + this.set_close_response ("cancel"); - var lb = new Gtk.ListBox () { css_classes = { "boxed-list" } }; - name_entry = new Adw.EntryRow () { - input_hints = Gtk.InputHints.SPELLCHECK, - title = _("Category Name") - }; - lb.append (name_entry); + var lb = new Gtk.ListBox () { css_classes = { "boxed-list" } }; + name_entry = new Adw.EntryRow () { + input_hints = Gtk.InputHints.SPELLCHECK, + title = _("Category Name") + }; + lb.append (name_entry); - percentage = new Gtk.Adjustment (0, 0, 100, 1, 10, 1); - var percent = new Adw.SpinRow (percentage, 1, 2) { - title = _("Category Percent") - }; - lb.append (percent); + percentage = new Gtk.Adjustment (0, 0, 100, 1, 10, 0); + var percent = new Adw.SpinRow (percentage, 1, 2) { + title = _("Category Percent") + }; + lb.append (percent); - this.set_extra_child (lb); + this.set_extra_child (lb); - name_entry.changed.connect (() => { - this.set_response_enabled ("add", true); - }); + name_entry.changed.connect (() => set_response_enabled ("add", name_entry.text.strip () != "")); - this.present (); + present (); } } diff --git a/src/application.vala b/src/application.vala index 7ba6a0c..317bb8f 100644 --- a/src/application.vala +++ b/src/application.vala @@ -23,15 +23,7 @@ public class MyApp : Adw.Application { } public void on_newsubject_action () { - var dialog = new NewSubjectDialog (main_window); - dialog.close_request.connect (() => { - if (dialog.accept) { - SubjectManager.get_default ().new_subject (dialog.name_entry_box.get_text (), dialog.get_categories ()); - } - dialog.destroy (); - return true; - }); - dialog.present (); + new EditSubjectDialog (main_window, null).present (); } public void on_about_action () { diff --git a/src/edit-subject-dialog.vala b/src/edit-subject-dialog.vala index 4784b27..758a747 100644 --- a/src/edit-subject-dialog.vala +++ b/src/edit-subject-dialog.vala @@ -1,22 +1,26 @@ public class EditSubjectDialog : Adw.Window { - private Subject subject; + public Subject? subject { get; construct; } private List cat_rows; private HashTable categories; private Adw.PreferencesGroup cat_list_box; - public EditSubjectDialog (Adw.ApplicationWindow parent, Subject s) { + private string subject_name; + + public EditSubjectDialog (Adw.ApplicationWindow parent, Subject? subject) { Object ( modal: true, - title: _("Edit Subject"), + title: subject == null ? _("New Subject") : _("Edit Subject"), transient_for: parent, default_height: 400, default_width: 500, width_request: 360, - height_request: 360 + height_request: 360, + subject: subject ); + } - subject = s; - + construct { + categories = new HashTable (str_hash, str_equal); cat_rows = new List (); var cb = new Gtk.Button.with_label (_("Cancel")); @@ -24,9 +28,19 @@ public class EditSubjectDialog : Adw.Window { close (); }); - var ab = new Gtk.Button.with_label (_("Save")) { css_classes = { "suggested-action" } }; + var ab = new Gtk.Button.with_label (_("Save")) { + sensitive = false, + css_classes = { "suggested-action" } + }; ab.clicked.connect (() => { - subject.categories_by_name = categories; + if (subject == null) { + subject = new Subject (subject_name); + subject.categories_by_name = categories; + SubjectManager.get_default ().add_subject (subject); + } else { + subject.name = subject_name; + subject.categories_by_name = categories; + } close (); }); @@ -41,6 +55,18 @@ public class EditSubjectDialog : Adw.Window { hb.pack_start (cb); hb.pack_end (ab); + var name_entry_row = new Adw.EntryRow () { + input_hints = SPELLCHECK, + title = _("Subject Title"), + }; + + var name_list_box = new Gtk.ListBox () { + margin_start = 1, + margin_end = 1 + }; + name_list_box.add_css_class ("boxed-list"); + name_list_box.append (name_entry_row); + var new_cat_button = new Gtk.Button () { icon_name = "list-add-symbolic", tooltip_text = _("Add New Category"), @@ -48,6 +74,7 @@ public class EditSubjectDialog : Adw.Window { }; cat_list_box = new Adw.PreferencesGroup () { + margin_top = 20, margin_start = 1, margin_end = 1, hexpand = true, @@ -56,13 +83,19 @@ public class EditSubjectDialog : Adw.Window { }; cat_list_box.set_header_suffix (new_cat_button); - var subject_delete_button = new Gtk.Button.with_label (_("Delete Subject…")) { hexpand = false, halign = Gtk.Align.START, margin_top = 20 }; + var subject_delete_button = new Gtk.Button.with_label (_("Delete Subject…")) { + hexpand = false, + halign = START, + margin_top = 20, + visible = subject != null + }; subject_delete_button.add_css_class ("destructive-action"); var main_box = new Gtk.Box (VERTICAL, 0) { margin_top = 20, margin_bottom = 20 }; + main_box.append (name_list_box); main_box.append (cat_list_box); main_box.append (subject_delete_button); @@ -86,15 +119,22 @@ public class EditSubjectDialog : Adw.Window { content = tbv; - categories = new HashTable (str_hash, str_equal); - subject.categories_by_name.@foreach ((key, val) => { - categories[key] = val; - }); + name_entry_row.changed.connect (() => { + subject_name = name_entry_row.text; + ab.sensitive = subject_name.strip () != ""; //TODO: check whether another subject with the same name exists + }); + + if (subject != null) { + name_entry_row.text = subject.name; + subject.categories_by_name.@foreach ((key, val) => { + categories[key] = val; + }); + } load_list (); subject_delete_button.clicked.connect (() => { - string n = s.name; + string n = subject.name; ///TRANSLATORS: %s is the name of a school subject var message_dialog = new Adw.MessageDialog(this, _("Delete %s?").printf( n), null); message_dialog.set_body (_("If you delete %s, its information will be deleted permanently.").printf( n)); diff --git a/src/new-subject-dialog.vala b/src/new-subject-dialog.vala deleted file mode 100644 index 5f83fe2..0000000 --- a/src/new-subject-dialog.vala +++ /dev/null @@ -1,133 +0,0 @@ -public class NewSubjectDialog : Adw.Window { - private Category[] categories = {}; - public Gtk.Box main_box; - public Adw.EntryRow name_entry_box; - private Gtk.Button new_cat_button; - public bool accept; - - public NewSubjectDialog (Adw.ApplicationWindow parent) { - Object ( - modal: true, - title: _("New Subject"), - transient_for: parent, - default_height: 400, - default_width: 500, - width_request: 360, - height_request: 360 - ); - - var tbv = new Adw.ToolbarView (); - this.set_content (tbv); - - var hb = new Adw.HeaderBar () { - show_end_title_buttons = false, - show_start_title_buttons = false, - }; - - var cb = new Gtk.Button.with_label (_("Cancel")); - cb.clicked.connect (() => { - accept = false; - this.close (); - }); - - var ab = new Gtk.Button.with_label (_("Save")) { css_classes = { "suggested-action" }, sensitive = false }; - ab.clicked.connect (() => { - accept = true; - this.close (); - }); - - hb.pack_start (cb); - hb.pack_end (ab); - - tbv.add_top_bar (hb); - - Adw.Clamp ac = new Adw.Clamp () { - margin_start = 19, - margin_end = 19, - maximum_size = 500, - tightening_threshold = 400 - }; - - var sw = new Gtk.ScrolledWindow (); - main_box = new Gtk.Box (VERTICAL, 0) { - margin_top = 20, - margin_bottom = 20 - }; - tbv.set_content (sw); - - this.set_content (tbv); - sw.set_child (ac); - ac.set_child (main_box); - - Gtk.ListBox lisbox = new Gtk.ListBox () {margin_start = 1, margin_end = 1}; - lisbox.add_css_class ("boxed-list"); - name_entry_box = new Adw.EntryRow () { - input_hints = Gtk.InputHints.SPELLCHECK, - title = _("Subject Title"), - }; - lisbox.append (name_entry_box); - main_box.append (lisbox); - - - name_entry_box.changed.connect (() => { - ab.sensitive = true; - }); - - categories_list_ui (); - } - - public void add_cat (string n, double p) { - categories += new Category (n, p); - categories_list_ui (); - } - - public Category[] get_categories () { - return categories; - } - - public void categories_list_ui () { - if (main_box.get_first_child ().get_next_sibling () != null && main_box.get_first_child ().get_next_sibling ().name == "AdwPreferencesGroup") { - main_box.remove (main_box.get_first_child ().get_next_sibling ()); - } - - var cat_list_box = new Adw.PreferencesGroup () { - margin_top = 20, - margin_start = 1, - margin_end = 1, - hexpand = true, - vexpand = true, - title = _("Subject Categories"), - }; - - new_cat_button = new Gtk.Button () { - icon_name = "list-add-symbolic", - tooltip_text = _("Add New Category"), - css_classes = { "flat" } - }; - - new_cat_button.clicked.connect (() => { - var dialog = new AddCategoryDialog (this); - - dialog.response.connect ((response_id) => { - if (response_id == "add") { - add_cat (dialog.name_entry.get_text (), dialog.percentage.get_value ()); - } - dialog.destroy (); - }); - - dialog.present (); - }); - - cat_list_box.set_header_suffix (new_cat_button); - main_box.insert_child_after (cat_list_box, main_box.get_first_child ()); - - - for (int i = 0; i < categories.length && categories[i] != null; i++) { - var cat_row = new Adw.ActionRow () { - title = categories[i].name, - subtitle = categories[i].percentage.to_string () + "%" - }; - cat_list_box.add (cat_row); - } - } -} diff --git a/src/subject-manager.vala b/src/subject-manager.vala index 2f8b1e2..b2f31dc 100644 --- a/src/subject-manager.vala +++ b/src/subject-manager.vala @@ -88,17 +88,7 @@ public class SubjectManager : Object { } } - public void new_subject (string name, Category[] c) { - var subject = new Subject (name); - - foreach (var cat in c) { - subject.categories_by_name[cat.name] = cat; - } - - add_subject (subject); - } - - private void add_subject (Subject subject) { + public void add_subject (Subject subject) { subjects.append (subject); subject.notify["deleted"].connect (() => { uint pos; diff --git a/src/window.vala b/src/window.vala index 61d9010..0072ae9 100644 --- a/src/window.vala +++ b/src/window.vala @@ -31,8 +31,10 @@ public class Window : Adw.ApplicationWindow { var navigation_sidebar = new Gtk.ListBox (); navigation_sidebar.add_css_class ("navigation-sidebar"); navigation_sidebar.bind_model (subject_manager.subjects, (obj) => { - var sub = (Subject) obj; - return new Gtk.Label (sub.name) { xalign = 0 }; + var subject = (Subject) obj; + var label = new Gtk.Label (subject.name) { xalign = 0 }; + subject.notify["name"].connect (() => label.label = subject.name); + return label; }); var scrolled_window = new Gtk.ScrolledWindow () {