def edit(self): """Create edition frame to edit current category name""" self.mainFrame.editState = self.mainFrame.EDITING self.label.grid_forget() message = 'Edit Category Name:' self.newName = tk.StringVar() self.editFrame = TextEditFrame(master=self, textVar=self.newName, labelText=message, buttonText='Edit', buttonAction=self.editCategory, cancelButtonAction=self.cancelEditCategory) self.editFrame.grid(columnspan=2) self.config(bd=2, relief=tk.SUNKEN)
def add_subcategory(self): """Create edition frame to add subcategory to current one""" self.mainFrame.editState = self.mainFrame.ADDING if self.id == -1: message = 'New category:' else: message = 'New Subcategory:' self.catName = tk.StringVar() self.editFrame = TextEditFrame(master=self, textVar=self.catName, labelText=message, buttonText='Add', buttonAction=self.addCategory, cancelButtonAction=self.cancelAddCategory) self.editFrame.grid(row=0, column=1, columnspan=2, rowspan=3) self.config(bd=2, relief=tk.SUNKEN)
def add_brandOrCompany(self): """Create edition frame to add a new brand or company""" self.mainFrame.editState = self.mainFrame.ADDING if self.id == -1: message = "New Company:" else: message = "New Brand:" self.newName = tk.StringVar() self.editFrame = TextEditFrame(master=self, textVar=self.newName, labelText=message, buttonText='Add', buttonAction=self.addNewBrandOrCompany, cancelButtonAction=self.cancelAdding) self.editFrame.grid(row=0, column=1, columnspan=2, rowspan=3) self.config(bd=2, relief=tk.SUNKEN)
class CategoryLabel(tk.Frame): """Labels with right-click menu for editing""" def __init__(self, master=None, name='', id=-1, mainWidget=None): self.textVar = tk.StringVar() self.textVar.set(str(id) + ': ' + name) self.id = id tk.Frame.__init__(self, master) self.label = tk.Label(self, textvariable=self.textVar, bg='white', bd=1, relief=tk.RAISED) self.label.grid(sticky=tk.N) # create right-click menu self.menu = tk.Menu(self, tearoff=0) if self.id!=-1: # id=-1 for buttons only self.menu.add_command(label='Edit', command=self.edit) self.menu.add_command(label='Add subcategory', command=self.add_subcategory) if self.id!=-1: self.menu.add_command(label='Delete', command=self.delete) self.label.bind('<Button-3>', self.openMenu) # shortcut self.mainFrame = mainWidget def openMenu(self, event): """Opens the right click menu for the label""" if self.mainFrame.editState == self.mainFrame.WAITING: self.menu.post(event.x_root, event.y_root) def edit(self): """Create edition frame to edit current category name""" self.mainFrame.editState = self.mainFrame.EDITING self.label.grid_forget() message = 'Edit Category Name:' self.newName = tk.StringVar() self.editFrame = TextEditFrame(master=self, textVar=self.newName, labelText=message, buttonText='Edit', buttonAction=self.editCategory, cancelButtonAction=self.cancelEditCategory) self.editFrame.grid(columnspan=2) self.config(bd=2, relief=tk.SUNKEN) def editCategory(self): """when the TextEditFrame.addButton is clicked""" res = self.mainFrame.db.updateLineFromId('category', 'name', self.newName.get().encode('utf-8'), self.id) if res==True: self.mainFrame.updateMainFrame() self.mainFrame.editState = self.mainFrame.WAITING else: tkMessageBox.showerror('Edit Category Error', 'Category could not be edited\n' + str(res)) def cancelEditCategory(self): """when the TextEditFrame.cancelButton is clicked""" self.editFrame.destroy() self.label.grid() self.mainFrame.editState = self.mainFrame.WAITING self.config(bd=0, relief=tk.FLAT) def add_subcategory(self): """Create edition frame to add subcategory to current one""" self.mainFrame.editState = self.mainFrame.ADDING if self.id == -1: message = 'New category:' else: message = 'New Subcategory:' self.catName = tk.StringVar() self.editFrame = TextEditFrame(master=self, textVar=self.catName, labelText=message, buttonText='Add', buttonAction=self.addCategory, cancelButtonAction=self.cancelAddCategory) self.editFrame.grid(row=0, column=1, columnspan=2, rowspan=3) self.config(bd=2, relief=tk.SUNKEN) def addCategory(self): """when the TextEditFrame.addButton is clicked""" headers = ('name', 'parent_id') if self.id == -1: values = (self.catName.get().encode('utf-8'), None) else: values = (self.catName.get().encode('utf-8'), self.id) res = self.mainFrame.db.simpleInsert('category', headers, values) if res==True: self.mainFrame.updateMainFrame() self.mainFrame.editState = self.mainFrame.WAITING else: tkMessageBox.showerror('Add Category Error', 'Category could not be created\n' + str(res)) def cancelAddCategory(self): """when the TextEditFrame.cancelButton is clicked""" self.editFrame.destroy() self.mainFrame.editState = self.mainFrame.WAITING self.config(bd=0, relief=tk.FLAT) def delete(self): """Delete the category if user confirms""" self.mainFrame.editState = self.mainFrame.DELETING if self.mainFrame.hasChildren(self.id): tkMessageBox.showerror('DeleteCategory Error', 'Can not delete ' + self.textVar.get() + '\nDelete subcategories first') elif tkMessageBox.askyesno('Delete Category Warning', 'Warning: if you delete a category\n' + 'it can create broken links\n' + 'Confirm delete?', icon=tkMessageBox.WARNING): res = self.mainFrame.db.deleteLineFromId('category', self.id) if res==True: tkMessageBox.showinfo('Delete Category Success', 'Category was successfully deleted') self.mainFrame.updateMainFrame() else: tkMessageBox.showerror('Delete Category Error', 'Category could not be deleted\n' + str(res)) self.mainFrame.editState = self.mainFrame.WAITING
class BrandLabel(tk.Frame): """Labels with right-click menu for editing""" def __init__(self, name, id, navbar, type, master=None, mainWidget=None): """ master: the parent widget name: the name of the brand/company (str) id: the id of the brand/company, -1 for the "add company" label (int) navbar: in_navbar status (bool) type: 'company' or 'brand' """ tk.Frame.__init__(self, master) # create name label self.textVar = tk.StringVar() self.textVar.set(str(id) + ': ' + str(name)) self.id = id self.navbar = navbar self.type = type self.label = tk.Label(self, textvariable=self.textVar, bg='white', bd=1, relief=tk.RAISED) self.label.grid(row=0, sticky=tk.N) # create navbar checkbox if self.id != -1: self.navLabel = tk.Label(self, text="In Navbar?") self.navLabel.grid(row=0, column=1) self.navState = tk.IntVar() self.navState.set(self.navbar*1) self.navbarCheck = tk.Checkbutton(self, variable=self.navState) self.navbarCheck.grid(row=0, column=2) self.navbarCheck.bind('<Button-1>', self.navbarCheckHandler) # create right-click menu self.menu = tk.Menu(self, tearoff=0) if self.id == -1: self.menu.add_command(label='Add Company', command=self.add_brandOrCompany) else: self.menu.add_command(label='Edit', command=self.edit) if self.type == 'company': self.menu.add_command(label='Add brand', command=self.add_brandOrCompany) self.menu.add_command(label='Delete', command=self.delete) self.label.bind('<Button-3>', self.openMenu) # shortcut self.mainFrame = mainWidget def navbarCheckHandler(self, event): """Edits the in_navbar info when checkbutton is clicked""" self.mainFrame.editState == self.mainFrame.EDITING res = self.mainFrame.db.updateLineFromId(table=self.type, column='in_navbar', newValue=(not self.navbar), id=self.id) if res==True: self.mainFrame.updateMainFrame() else: tkMessageBox.showerror('Edit Company/Brand Error', 'Navbar setting could not be edited\n' + str(res)) self.mainFrame.editState = self.mainFrame.WAITING def openMenu(self, event): """Opens the right click menu for the label""" if self.mainFrame.editState == self.mainFrame.WAITING: self.menu.post(event.x_root, event.y_root) def edit(self): """Create edition frame to edit current category name""" self.mainFrame.editState = self.mainFrame.EDITING self.label.grid_forget() self.navLabel.grid_forget() self.navbarCheck.grid_forget() message = 'Edit {type} Name:'.format(type=self.type) self.newName = tk.StringVar() self.editFrame = TextEditFrame(master=self, textVar=self.newName, labelText=message, buttonText='Edit', buttonAction=self.editName, cancelButtonAction=self.cancelEditName) self.editFrame.grid(columnspan=2) self.config(bd=2, relief=tk.SUNKEN) def editName(self): """when the TextEditFrame.edit button is clicked""" value = self.newName.get().encode('utf-8') res = self.mainFrame.db.updateLineFromId(table=self.type, column='name', newValue=value, id=self.id) if res==True: self.mainFrame.updateMainFrame() self.mainFrame.editState = self.mainFrame.WAITING else: tkMessageBox.showerror('Edit Company/Brand Error', 'Name could not be edited\n' + str(res)) def cancelEditName(self): """when the TextEditFrame.cancelButton is clicked""" self.editFrame.destroy() self.label.grid(row=0, column=0) self.navLabel.grid(row=0, column=1) self.navbarCheck.grid(row=0, column=2) self.mainFrame.editState = self.mainFrame.WAITING self.config(bd=0, relief=tk.FLAT) def add_brandOrCompany(self): """Create edition frame to add a new brand or company""" self.mainFrame.editState = self.mainFrame.ADDING if self.id == -1: message = "New Company:" else: message = "New Brand:" self.newName = tk.StringVar() self.editFrame = TextEditFrame(master=self, textVar=self.newName, labelText=message, buttonText='Add', buttonAction=self.addNewBrandOrCompany, cancelButtonAction=self.cancelAdding) self.editFrame.grid(row=0, column=1, columnspan=2, rowspan=3) self.config(bd=2, relief=tk.SUNKEN) def addNewBrandOrCompany(self): """when TextEditFrame.addButton is clicked""" if self.id == -1: tab = 'company' headers = ('name',) values = (self.newName.get().encode('utf-8'),) else: tab = 'brand' headers = ('name', 'company_id') values = (self.newName.get().encode('utf-8'), self.id) res = self.mainFrame.db.simpleInsert(table=tab, headers=headers, values=values) if res == True: self.mainFrame.updateMainFrame() self.mainFrame.editState = self.mainFrame.WAITING else: tkMessageBox.showerror('Add Brand/Company Error', 'Brand or Company could not be created\n' + str(res)) def cancelAdding(self): """when the TextEditFrame.cancelButton is clicked""" self.editFrame.destroy() self.mainFrame.editState = self.mainFrame.WAITING self.config(bd=0, relief=tk.FLAT) def delete(self): """Delete the brand/company if user confirms""" self.mainFrame.editState = self.mainFrame.DELETING if self.type == 'company' and self.mainFrame.hasBrand(self.id): tkMessageBox.showerror('Delete Company Error', 'Can not delete ' + self.textVar.get() + '\nDelete associated brands first') elif tkMessageBox.askyesno('Delete Warning', 'Warning: if you delete a ' + self.type + '\n' + 'it can create broken links\n' + 'Confirm delete?', icon=tkMessageBox.WARNING): res = self.mainFrame.db.deleteLineFromId(table=self.type, id=self.id) if res==True: tkMessageBox.showinfo('Delete ' + self.type + ' Success', '' + self.type + ' was successfully deleted') self.mainFrame.updateMainFrame() else: tkMessageBox.showerror('Delete ' + self.type + ' Error', '' + self.type + ' could not be deleted\n' + str(res)) self.mainFrame.editState = self.mainFrame.WAITING