+import gi
+gi.require_version("Gtk", "3.0")
+from gi.repository import Gtk
+from notebook import Notebook
+from newlensdialog import NewlensDialog
+from db import DB
+
+dirty = False
+name = "Lens Database"
+version = "0.1"
+db = None
+
+class SearchDialog(Gtk.Dialog):
+ def __init__(self, builder):
+ self.builder = builder
+ Gtk.Dialog.__init__(self, parent=None)
+ self.set_title("Search for lens")
+ self.set_default_size(200, 100)
+ self.add_button("_OK", Gtk.ResponseType.OK)
+ btn = self.add_button("_Cancel", Gtk.ResponseType.CANCEL)
+ self.connect("response", self.on_response)
+
+ box = self.get_content_area()
+ makerLabel= Gtk.Label(label="Lens Maker")
+ box.add(makerLabel)
+ self.makerEntry = Gtk.Entry()
+ self.makerEntry.set_placeholder_text("Lens Maker")
+ self.makerEntry.set_tooltip_text("Enter possible name of lens maker")
+ box.add(self.makerEntry)
+ dataLabel = Gtk.Label(label="Lens data")
+ box.add(dataLabel)
+ self.dataEntry = Gtk.Entry()
+ self.dataEntry.set_placeholder_text("Lens data")
+ self.dataEntry.set_tooltip_text("Enter all or parts of lens data")
+ box.add(self.dataEntry)
+ self.show_all()
+ btn.grab_focus()
+
+ def on_response(self, dialog, response):
+ if response == Gtk.ResponseType.OK:
+ from searchwindow import SearchResultDialog
+ search = SearchResultDialog(self.builder, db)
+ maker = self.makerEntry.get_text()
+ data = self.dataEntry.get_text()
+ self.destroy()
+ search.addLensData((maker, data))
+ #return True
+ elif response == Gtk.ResponseType.CANCEL:
+ pass
+ else:
+ pass
+
+class RemoveMakerDialog(Gtk.Dialog):
+ def __init__(self, notebook):
+ self.nb = notebook
+ self.maker = self.nb.getCurrentPageLabel()
+ Gtk.Dialog.__init__(self, parent=None)
+ self.set_title("Remove Lens Maker")
+ self.set_default_size(100, 100)
+ self.add_button("_OK", Gtk.ResponseType.OK)
+ btn = self.add_button("_Cancel", Gtk.ResponseType.CANCEL)
+ self.connect("response", self.on_response)
+
+ box = self.get_content_area()
+ makerLabel= Gtk.Label()
+ makerLabel.set_markup('<span foreground="red" size="x-large"><b>Remove [' + self.maker + '] and all lenses?</b></span>')
+ box.add(makerLabel)
+ self.show_all()
+ btn.grab_focus()
+
+ def on_response(self, dialog, response):
+ if response == Gtk.ResponseType.OK:
+ try:
+ db.deleteMaker(self.maker)
+ self.nb.deletePage(self.nb.getCurrentPage())
+ except Exception as err:
+ print(err)
+ raise
+ elif response == Gtk.ResponseType.CANCEL:
+ pass
+ else:
+ pass
+ dialog.destroy()
+
+class NewlensmakerDialog(Gtk.Dialog):
+ def __init__(self, builder):
+ self.builder = builder
+ Gtk.Dialog.__init__(self, parent=None)
+ self.set_title("New Lens Maker")
+ self.set_default_size(200, 100)
+ self.add_button("_OK", Gtk.ResponseType.OK)
+ btn = self.add_button("_Cancel", Gtk.ResponseType.CANCEL)
+ self.connect("response", self.on_response)
+
+ box = self.get_content_area()
+ label = Gtk.Label(label="Lens Maker")
+ box.add(label)
+ self.entry = Gtk.Entry()
+ self.entry.set_placeholder_text("Lens Maker")
+ self.entry.set_tooltip_text("Enter name of lens maker")
+ box.add(self.entry)
+ self.show_all()
+ btn.grab_focus()
+
+ def on_response(self, dialog, response):
+ if response == Gtk.ResponseType.OK:
+ notebook = Notebook(self.builder, db)
+ notebook.addPage(self.entry.get_text())
+ elif response == Gtk.ResponseType.CANCEL:
+ pass
+ else:
+ pass
+ dialog.destroy()
+
+class Handler:
+ def __init__(self, builder):
+ self.builder = builder
+
+ def on_destroy(self, *args):
+ Gtk.main_quit()
+
+ def on_app_quit(self, *args):
+ if dirty:
+ """ show warning """
+ self.on_destroy(self, args)
+
+ def on_show_about(self, menuitem, data=None):
+ about = self.builder.get_object("about_dialog")
+ about.run()
+ about.hide()
+ return True
+
+ def on_find_lens_clicked(self, *args):
+ """ Find lens"""
+ dialog = SearchDialog(self.builder)
+ dialog.run()
+ return True
+
+ def on_add_lens_clicked(self, *args):
+ """ Add lens"""
+ dialog = NewlensDialog(self.builder, db)
+ dialog.run()
+ return True
+
+ def on_new_maker_clicked(self, *args):
+ """ New lens maker"""
+ dialog = NewlensmakerDialog(self.builder)
+ dialog.run()
+ return True
+
+ def on_remove_maker_clicked(self, *args):
+ """ Remove lens maker"""
+ notebook = Notebook(self.builder, db)
+ dialog = RemoveMakerDialog(notebook)
+ dialog.run()
+ return True
+
+class MainWindow:
+ def __init__(self, path):
+ global db
+ db = DB(path);
+ self.builder = Gtk.Builder()
+ self.builder.add_from_file(path + "/lensdatabase.ui")
+ self.builder.connect_signals(Handler(self.builder))
+
+ main_window = self.builder.get_object("main_window")
+ aboutdialog = self.builder.get_object("about_dialog")
+ aboutdialog.set_program_name(name)
+ aboutdialog.set_version(version)
+ self.load()
+ main_window.show_all()
+
+ def load(self):
+ makers = db.getMakers()
+ notebook = Notebook(self.builder, None)
+ for row in makers:
+ for maker in row:
+ lenses = db.getLenses(maker)
+ notebook.addPage(maker)
+ for lens in lenses:
+ notebook.addRow(maker, lens)
+ notebook.setDB(db)