Skip to content

Commit

Permalink
DRY new subject dialog (#27)
Browse files Browse the repository at this point in the history
* Formatting and sizegroup

* Allow 100% categories

* Some formatting

* Remove new-subject-dialog and move its function to edit-subject-dialog
  • Loading branch information
leolost2605 committed Dec 24, 2023
1 parent d4bb7b3 commit 3cf4774
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 189 deletions.
1 change: 0 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 0 additions & 1 deletion po/POTFILES
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 16 additions & 18 deletions src/add-category-dialog.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
}
}
10 changes: 1 addition & 9 deletions src/application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
68 changes: 54 additions & 14 deletions src/edit-subject-dialog.vala
Original file line number Diff line number Diff line change
@@ -1,32 +1,46 @@
public class EditSubjectDialog : Adw.Window {
private Subject subject;
public Subject? subject { get; construct; }
private List<Adw.ActionRow> cat_rows;
private HashTable<string, Category> 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<string, Category> (str_hash, str_equal);
cat_rows = new List<Adw.ActionRow> ();

var cb = new Gtk.Button.with_label (_("Cancel"));
cb.clicked.connect (() => {
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 ();
});

Expand All @@ -41,13 +55,26 @@ 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"),
css_classes = { "flat" }
};

cat_list_box = new Adw.PreferencesGroup () {
margin_top = 20,
margin_start = 1,
margin_end = 1,
hexpand = true,
Expand All @@ -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);

Expand All @@ -86,15 +119,22 @@ public class EditSubjectDialog : Adw.Window {

content = tbv;

categories = new HashTable<string, Category> (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));
Expand Down
133 changes: 0 additions & 133 deletions src/new-subject-dialog.vala

This file was deleted.

12 changes: 1 addition & 11 deletions src/subject-manager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions src/window.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 3cf4774

Please sign in to comment.