X-Git-Url: http://git.datanom.net/lensdatabase.git/blobdiff_plain/365c79d46c3804dd4b3f796bd2fcd4e3ddf1186b..29f0e748a8cd062a091bc4cca710fa91f720c11c:/notebook.py?ds=sidebyside diff --git a/notebook.py b/notebook.py new file mode 100644 index 0000000..d5a4a02 --- /dev/null +++ b/notebook.py @@ -0,0 +1,101 @@ +import gi +gi.require_version("Gtk", "3.0") +from gi.repository import Gtk +from newlensdialog import NewlensDialog + +pages = {} + +class Notebook: + __instance = None + def __new__(cls, *args, **kwargs): + if (cls.__instance is None): + cls.__instance = super(Notebook, cls).__new__(cls) + return cls.__instance + + def __init__(self, builder, db): + self.builder = builder + self.db = db + self.nb = self.builder.get_object("lens_maker_notebook") + + def setDB(self, db): + self.db = db + + def addPage(self, name): + if pages.get(name) is not None: + raise Exception("{page}: Page exists".format(page = name)) + label = Gtk.Label() + label.set_text(name) + liststore = Gtk.ListStore(int, str, str, int) + treeview = Gtk.TreeView() + treeview.connect("row-activated", self.on_row_activated) + treeview.set_model(liststore) + for i, title in enumerate(["Serial No", "Lens", "Notes"]): + renderer = Gtk.CellRendererText() + column = Gtk.TreeViewColumn(title, renderer, text=i) + treeview.append_column(column) + scrollable_treelist = Gtk.ScrolledWindow() + scrollable_treelist.set_vexpand(True) + scrollable_treelist.add(treeview) + index = self.nb.append_page(scrollable_treelist, label) + if index < 0: + raise Exception("{page}: Page not added".format(page = name)) + if self.db is not None: + self.db.addMaker(name) + pages[name] = liststore + self.nb.show_all() + + def addRow(self, tab, tuple): + page = pages.get(tab) + if page is None: + raise Exception("{name}: Page does not exist".format(name = tab)) + try: + if self.db is not None: + id = self.db.addLens(tab, tuple) + page.append(tuple + (id, )) + else: + page.append(tuple) + except Exception as err: + print(err) + raise + + def updateRow(self, path, tuple): + try: + model = path[0] + row = path[1] + for i in range(0, len(tuple)): + model[row][i] = tuple[i] + if self.db is not None: + self.db.updateLens(tuple) + except Exception as err: + print(err) + raise + + def deleteRow(self, path, id): + try: + model = path[0] + iter = model.get_iter(path[1]) + model.remove(iter) + if self.db is not None: + self.db.deleteLens(id) + except Exception as err: + print(err) + raise + + def deletePage(self, pagenum): + self.nb.remove_page(pagenum) + + def getCurrentPage(self): + return self.nb.get_current_page() + + def getCurrentPageLabel(self): + page = self.nb.get_nth_page(self.getCurrentPage()) + label = self.nb.get_tab_label_text(page) + return label + + def on_row_activated(self, widget, row, col): + model = widget.get_model() + lensdata = [] + for i in range(0, 4): + lensdata.append(model[row][i]) + lensDialog = NewlensDialog(self.builder, self.db, (model, row)) + lensDialog.setText(lensdata)