]>
Commit | Line | Data |
---|---|---|
1 | import gi | |
2 | gi.require_version("Gtk", "3.0") | |
3 | from gi.repository import Gtk | |
4 | from notebook import Notebook | |
5 | from newlensdialog import NewlensDialog | |
6 | from db import DB | |
7 | ||
8 | dirty = False | |
9 | name = "Lens Database" | |
10 | version = "0.1" | |
11 | db = None | |
12 | ||
13 | class SearchDialog(Gtk.Dialog): | |
14 | def __init__(self, builder): | |
15 | self.builder = builder | |
16 | Gtk.Dialog.__init__(self, parent=None) | |
17 | self.set_title("Search for lens") | |
18 | self.set_default_size(200, 100) | |
19 | self.add_button("_OK", Gtk.ResponseType.OK) | |
20 | btn = self.add_button("_Cancel", Gtk.ResponseType.CANCEL) | |
21 | self.connect("response", self.on_response) | |
22 | ||
23 | box = self.get_content_area() | |
24 | makerLabel= Gtk.Label(label="Lens Maker") | |
25 | box.add(makerLabel) | |
26 | self.makerEntry = Gtk.Entry() | |
27 | self.makerEntry.set_placeholder_text("Lens Maker") | |
28 | self.makerEntry.set_tooltip_text("Enter possible name of lens maker") | |
29 | box.add(self.makerEntry) | |
30 | dataLabel = Gtk.Label(label="Lens data") | |
31 | box.add(dataLabel) | |
32 | self.dataEntry = Gtk.Entry() | |
33 | self.dataEntry.set_placeholder_text("Lens data") | |
34 | self.dataEntry.set_tooltip_text("Enter all or parts of lens data") | |
35 | box.add(self.dataEntry) | |
36 | self.show_all() | |
37 | btn.grab_focus() | |
38 | ||
39 | def on_response(self, dialog, response): | |
40 | if response == Gtk.ResponseType.OK: | |
41 | from searchwindow import SearchResultDialog | |
42 | search = SearchResultDialog(self.builder, db) | |
43 | maker = self.makerEntry.get_text() | |
44 | data = self.dataEntry.get_text() | |
45 | self.destroy() | |
46 | search.addLensData((maker, data)) | |
47 | #return True | |
48 | elif response == Gtk.ResponseType.CANCEL: | |
49 | pass | |
50 | else: | |
51 | pass | |
52 | ||
53 | class RemoveMakerDialog(Gtk.Dialog): | |
54 | def __init__(self, notebook): | |
55 | self.nb = notebook | |
56 | self.maker = self.nb.getCurrentPageLabel() | |
57 | Gtk.Dialog.__init__(self, parent=None) | |
58 | self.set_title("Remove Lens Maker") | |
59 | self.set_default_size(100, 100) | |
60 | self.add_button("_OK", Gtk.ResponseType.OK) | |
61 | btn = self.add_button("_Cancel", Gtk.ResponseType.CANCEL) | |
62 | self.connect("response", self.on_response) | |
63 | ||
64 | box = self.get_content_area() | |
65 | makerLabel= Gtk.Label() | |
66 | makerLabel.set_markup('<span foreground="red" size="x-large"><b>Remove [' + self.maker + '] and all lenses?</b></span>') | |
67 | box.add(makerLabel) | |
68 | self.show_all() | |
69 | btn.grab_focus() | |
70 | ||
71 | def on_response(self, dialog, response): | |
72 | if response == Gtk.ResponseType.OK: | |
73 | try: | |
74 | db.deleteMaker(self.maker) | |
75 | self.nb.deletePage(self.nb.getCurrentPage()) | |
76 | except Exception as err: | |
77 | print(err) | |
78 | raise | |
79 | elif response == Gtk.ResponseType.CANCEL: | |
80 | pass | |
81 | else: | |
82 | pass | |
83 | dialog.destroy() | |
84 | ||
85 | class NewlensmakerDialog(Gtk.Dialog): | |
86 | def __init__(self, builder): | |
87 | self.builder = builder | |
88 | Gtk.Dialog.__init__(self, parent=None) | |
89 | self.set_title("New Lens Maker") | |
90 | self.set_default_size(200, 100) | |
91 | self.add_button("_OK", Gtk.ResponseType.OK) | |
92 | btn = self.add_button("_Cancel", Gtk.ResponseType.CANCEL) | |
93 | self.connect("response", self.on_response) | |
94 | ||
95 | box = self.get_content_area() | |
96 | label = Gtk.Label(label="Lens Maker") | |
97 | box.add(label) | |
98 | self.entry = Gtk.Entry() | |
99 | self.entry.set_placeholder_text("Lens Maker") | |
100 | self.entry.set_tooltip_text("Enter name of lens maker") | |
101 | box.add(self.entry) | |
102 | self.show_all() | |
103 | btn.grab_focus() | |
104 | ||
105 | def on_response(self, dialog, response): | |
106 | if response == Gtk.ResponseType.OK: | |
107 | notebook = Notebook(self.builder, db) | |
108 | notebook.addPage(self.entry.get_text()) | |
109 | elif response == Gtk.ResponseType.CANCEL: | |
110 | pass | |
111 | else: | |
112 | pass | |
113 | dialog.destroy() | |
114 | ||
115 | class Handler: | |
116 | def __init__(self, builder): | |
117 | self.builder = builder | |
118 | ||
119 | def on_destroy(self, *args): | |
120 | Gtk.main_quit() | |
121 | ||
122 | def on_app_quit(self, *args): | |
123 | if dirty: | |
124 | """ show warning """ | |
125 | self.on_destroy(self, args) | |
126 | ||
127 | def on_show_about(self, menuitem, data=None): | |
128 | about = self.builder.get_object("about_dialog") | |
129 | about.run() | |
130 | about.hide() | |
131 | return True | |
132 | ||
133 | def on_find_lens_clicked(self, *args): | |
134 | """ Find lens""" | |
135 | dialog = SearchDialog(self.builder) | |
136 | dialog.run() | |
137 | return True | |
138 | ||
139 | def on_add_lens_clicked(self, *args): | |
140 | """ Add lens""" | |
141 | dialog = NewlensDialog(self.builder, db) | |
142 | dialog.run() | |
143 | return True | |
144 | ||
145 | def on_new_maker_clicked(self, *args): | |
146 | """ New lens maker""" | |
147 | dialog = NewlensmakerDialog(self.builder) | |
148 | dialog.run() | |
149 | return True | |
150 | ||
151 | def on_remove_maker_clicked(self, *args): | |
152 | """ Remove lens maker""" | |
153 | notebook = Notebook(self.builder, db) | |
154 | dialog = RemoveMakerDialog(notebook) | |
155 | dialog.run() | |
156 | return True | |
157 | ||
158 | class MainWindow: | |
159 | def __init__(self, path): | |
160 | global db | |
161 | db = DB(path); | |
162 | self.builder = Gtk.Builder() | |
163 | self.builder.add_from_file(path + "/lensdatabase.ui") | |
164 | self.builder.connect_signals(Handler(self.builder)) | |
165 | ||
166 | main_window = self.builder.get_object("main_window") | |
167 | aboutdialog = self.builder.get_object("about_dialog") | |
168 | aboutdialog.set_program_name(name) | |
169 | aboutdialog.set_version(version) | |
170 | self.load() | |
171 | main_window.show_all() | |
172 | ||
173 | def load(self): | |
174 | makers = db.getMakers() | |
175 | notebook = Notebook(self.builder, None) | |
176 | for row in makers: | |
177 | for maker in row: | |
178 | lenses = db.getLenses(maker) | |
179 | notebook.addPage(maker) | |
180 | for lens in lenses: | |
181 | notebook.addRow(maker, lens) | |
182 | notebook.setDB(db) |