~bookworm-team/bookworm/trunk

« back to all changes in this revision

Viewing changes to src/Library/PropertyDialog.vala

  • Committer: Raphael Isemann
  • Date: 2013-11-28 11:16:16 UTC
  • mfrom: (21.2.1 bookworm)
  • Revision ID: teemperor@googlemail.com-20131128111616-xacgees1lx04vvf1
First version of the Property Dialog

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using Gtk;
 
2
 
 
3
namespace BookWorm {
 
4
 
 
5
        public class PropertyDialog : Granite.Widgets.LightWindow {
 
6
 
 
7
                private Library library;
 
8
                private Book book;
 
9
                private EventBox cover_box;
 
10
                private bool loading_cover = false;
 
11
 
 
12
 
 
13
                public PropertyDialog(Gtk.Window parent, Library lib, Book b) {
 
14
                        library = lib;
 
15
                        book = b;
 
16
                        set_transient_for (parent);
 
17
                        set_title (b.name);
 
18
 
 
19
                        Grid main_grid = new Grid ();
 
20
 
 
21
                        Image cover = new Image.from_pixbuf (b.cover);
 
22
                        cover_box = new EventBox();
 
23
                        cover_box.width_request = LibraryView.ICON_W;
 
24
                        cover_box.height_request = LibraryView.ICON_H;
 
25
                        cover_box.button_press_event.connect (() => {
 
26
                                request_cover_change ();
 
27
                                return true;
 
28
                        });
 
29
                        cover_box.add (cover);
 
30
                        book.cover_changed.connect (() => {
 
31
                                Image new_cover = new Image.from_pixbuf (b.cover);
 
32
                                cover_box.remove (cover_box.get_child ());
 
33
                                cover_box.add (new_cover);
 
34
                                show_all ();
 
35
                        });
 
36
 
 
37
 
 
38
                        Grid cover_grid = new Grid();
 
39
                        cover_grid.attach (cover_box, 1, 1, 1, 1);
 
40
                        main_grid.attach (cover_grid, 0, 0, 1, 2);
 
41
 
 
42
                        main_grid.attach (make_field (N_("Name")), 1, 0, 1, 1);
 
43
                        main_grid.attach (make_field (N_("Author")), 1, 1, 1, 1);
 
44
                        Gtk.Separator separator_top_desc = new Gtk.Separator (Gtk.Orientation.HORIZONTAL);
 
45
                        separator_top_desc.hexpand = true;
 
46
                        main_grid.attach (separator_top_desc, 0, 2, 2, 1);
 
47
                        main_grid.attach (make_field (N_("Description")), 0, 3, 2, 1);
 
48
 
 
49
                        Gtk.Separator separator_bot_desc = new Gtk.Separator (Gtk.Orientation.HORIZONTAL);
 
50
                        separator_bot_desc.hexpand = true;
 
51
                        main_grid.attach (separator_bot_desc, 0, 4, 2, 1);
 
52
 
 
53
                        main_grid.margin = 8;
 
54
                        add (main_grid);
 
55
 
 
56
                        show_all ();
 
57
                }
 
58
 
 
59
                private Grid make_field (string name) {
 
60
                        Grid result = new Grid ();
 
61
 
 
62
                        Gtk.Grid spanner = new Grid();
 
63
                        spanner.hexpand = true;
 
64
 
 
65
                        Label label = new Label (_(name));
 
66
                        label.margin = 4;
 
67
                        result.attach (label, 1, 0, 1, 1);
 
68
 
 
69
                        Widget value_field = null;
 
70
                        switch(name) {
 
71
                                case N_("Name"):
 
72
                                        Entry entry = new Entry ();
 
73
                                        entry.text = book.name;
 
74
                                        entry.changed.connect (() => {
 
75
                                                book.name = entry.text;
 
76
                                        });
 
77
                                        value_field = entry;
 
78
                                        result.attach (spanner, 0, 0, 1, 1);
 
79
                                        break;
 
80
                                case N_("Description"):
 
81
                                        TextView entry = new TextView ();
 
82
                                        entry.margin = 4;
 
83
                                        entry.expand = true;
 
84
                                        entry.buffer.text = book.desc;
 
85
                                        entry.buffer.changed.connect (() => {
 
86
                                                book.desc = entry.buffer.text;
 
87
                                        });
 
88
                                        value_field = entry;
 
89
                                break;
 
90
                                case N_("Author"):
 
91
                                        Entry entry = new Entry ();
 
92
                                        entry.text = book.author;
 
93
                                        entry.changed.connect (() => {
 
94
                                                book.author = entry.text;
 
95
                                        });
 
96
                                        value_field = entry;
 
97
                                        result.attach (spanner, 0, 0, 1, 1);
 
98
                                break;
 
99
                        }
 
100
                        result.attach (value_field, 2, 0, 1, 1);
 
101
                        result.show_all ();
 
102
                        return result;
 
103
                }
 
104
 
 
105
                public void request_cover_change () {
 
106
                        if(loading_cover)
 
107
                                return;
 
108
                        loading_cover = true;
 
109
 
 
110
 
 
111
                        string filename = "";
 
112
                        var filechooser = new Gtk.FileChooserDialog ("Title", this,
 
113
                                        Gtk.FileChooserAction.OPEN, Gtk.Stock.CANCEL,
 
114
                                        Gtk.ResponseType.CANCEL, Gtk.Stock.OPEN, Gtk.ResponseType.OK);
 
115
 
 
116
                        if (filechooser.run () == Gtk.ResponseType.OK)
 
117
                        {
 
118
                                filename = filechooser.get_filename ();
 
119
                        }
 
120
                        filechooser.destroy ();
 
121
 
 
122
                        if (filename == "") {
 
123
                                loading_cover = false;
 
124
                                return;
 
125
                        }
 
126
 
 
127
                        File file1 = File.new_for_path (filename);
 
128
                        File file2 = File.new_for_path (BookWorm.get_data_dir()
 
129
                                                                        + "icon_lib/" + book.id.to_string ());
 
130
                        try {
 
131
                                Spinner spinner = new Spinner ();
 
132
                                spinner.width_request = LibraryView.ICON_W;
 
133
                                spinner.height_request = LibraryView.ICON_H;
 
134
                                cover_box.remove (cover_box.get_child ());
 
135
                                cover_box.add (spinner);
 
136
                                show_all ();
 
137
 
 
138
                                file1.copy_async.begin (file2, FileCopyFlags.OVERWRITE, Priority.DEFAULT,
 
139
                                                                                                null, null, (obj, res) => {
 
140
                                        book.reload_cover ();
 
141
                                        loading_cover = false;
 
142
                                });
 
143
                        } catch (Error e) {
 
144
                                warning ("Errddor: %s\n", e.message);
 
145
                        }
 
146
                }
 
147
        }
 
148
}