class MainScreen: def __init__(self, master, connection): #Reset Database if necessary Globals.ClearUserTables(connection) self.Config = configparser.ConfigParser() self.Config.read('config.ini') #Main Window self.Master = master self.Master.title("Hour Registration") #Database connection self.dbConnection = connection #Initialize Cache self.Cache = Cache.Cache(connection) #Initialize String Vars self.RecordTypeValue = StringVar() self.ProjectValue = StringVar() self.DescriptionValue = StringVar() self.LastLogon = StringVar() self.LastLogon.set(Globals.GetLastLogon()) #Designer self.DaysCombo = ttk.Combobox(master) self.DaysCombo.grid(row=0, column=0, sticky='NSEW', columnspan=3) self.RecordButton = Button(master, text="Start Recording", command=self.StartRecording) self.RecordButton.grid(row=1, column=0, sticky='NSEW') self.RecordIcon = PhotoImage(file=".\\Resources\\add.png") self.RecordButton.config(image=self.RecordIcon, width="32", height="32") self.DeleteRecordButton = Button(master, text="Delete Record", command=self.DeleteRecord) self.DeleteRecordButton.grid(row=1, column=1, sticky='NSEW') self.DeleteRecordIcon = PhotoImage(file=".\\Resources\\delete.png") self.DeleteRecordButton.config(image=self.DeleteRecordIcon, width="32", height="32") self.StopRecordButton = Button(master, text="Stop Recording", command=self.StopRecording) self.StopRecordButton.grid(row=1, column=2, sticky='NSEW') self.StopRecordIcon = PhotoImage(file=".\\Resources\\stop.png") self.StopRecordButton.config(image=self.StopRecordIcon, width="32", height="32") self.CopyToCodexButton = Button(master, text="Copy To Codex", command=self.CopyToCodex) self.CopyToCodexButton.grid(row=2, column=0, sticky='NSEW') self.CopyIcon = PhotoImage(file=".\\Resources\\copyCodex.png") self.CopyToCodexButton.config(image=self.CopyIcon, width="32", height="32") self.ExcelButton = Button(master, text="Export", command=self.ExportToExcel) self.ExcelButton.grid(row=2, column=1, sticky='NSEW') self.ExcelIcon = PhotoImage(file=".\\Resources\\excel.png") self.ExcelButton.config(image=self.ExcelIcon, width="32", height="32") self.ProjectButton = Button(master, text="Project", command=self.OpenProjectListForm) self.ProjectButton.grid(row=2, column=2, sticky='NSEW') self.ProjectIcon = PhotoImage(file=".\\Resources\\add_project.png") self.ProjectButton.config(image=self.ProjectIcon, width="32", height="32") self.CopyRecordButton = Button(master, text="CopyRecord", command=self.CopyRecord) self.CopyRecordButton.grid(row=3, column=0, sticky='NSEW') self.CopyRecordIcon = PhotoImage(file=".\\Resources\\copy.png") self.CopyRecordButton.config(image=self.CopyRecordIcon, width="32", height="32") self.OneNoteButton = Button(master, text="OneNote", command=self.OpenInOneNote) self.OneNoteButton.grid(row=3, column=1, sticky='NSEW') self.OneNoteIcon = PhotoImage(file=".\\Resources\\onenote.png") self.OneNoteButton.config(image=self.OneNoteIcon, width="32", height="32") self.AddTimeRecordButton = Button(master, text="AddTimeRecordButton", command=self.ShowNewEditForm) self.AddTimeRecordButton.grid(row=3, column=2, sticky="NSEW") self.AddTimeRecordIcon = PhotoImage( file=".\\Resources\\application_add.png") self.AddTimeRecordButton.config(image=self.AddTimeRecordIcon, width="32", height="32") self.BackupButton = Button(master, text="BackupButton", command=self.DatabaseBackup) self.BackupButton.grid(row=4, column=0, sticky="NSEW") self.BackupButtonIcon = PhotoImage(file=".\\Resources\\angel.png") self.BackupButton.config(image=self.BackupButtonIcon, width="32", height="32") self.RecordTypeButton = Button(master, text="RecordType", command=self.OpenRecordTypeListForm) self.RecordTypeButton.grid(row=4, column=1, sticky="NSEW") self.RecordTypeButtonIcon = PhotoImage( file=".\\Resources\\recordType.png") self.RecordTypeButton.config(image=self.RecordTypeButtonIcon, width="32", height="32") self.ExportToGraphsButton = Button(master, text="ExportToGraphs", command=self.ExportToGraph) self.ExportToGraphsButton.grid(row=4, column=2, sticky="NSEW") self.ExportToGraphsButtonIcon = PhotoImage( file=".\\Resources\\chart.png") self.ExportToGraphsButton.config(image=self.ExportToGraphsButtonIcon, width="32", height="32") self.ProjectsCombo = ttk.Combobox(master, textvariable=self.ProjectValue) self.ProjectsCombo.grid(row=0, column=3, columnspan=2, sticky='NSEW') self.RecordTypeCombo = ttk.Combobox(master, textvariable=self.RecordTypeValue) self.RecordTypeCombo.grid(row=1, column=3, columnspan=2, sticky='NSEW') self.DescriptionTextBox = Entry(master, textvariable=self.DescriptionValue) self.DescriptionTextBox.grid(row=2, column=3, columnspan=2, sticky='NSEW') self.RecordsListBox = Listbox(master, width=50) self.RecordsListBox.grid(row=3, column=3, rowspan=7, columnspan=1, sticky='NSEW') self.CommentListBox = Listbox(master, width=70) self.CommentListBox.grid(row=3, column=4, rowspan=7, columnspan=2, sticky='NSEW') self.CommentListBox.bindtags((self.CommentListBox, master, "all")) self.EventLogExplanationLabel = Label(master, text="Laatst aangemeld op: ") self.EventLogExplanationLabel.grid(row=0, column=5) self.EventLogLabel = Label(master, textvariable=self.LastLogon) self.EventLogLabel.grid(row=1, column=5) self.DaysCombo.bind("<<ComboboxSelected>>", self.DaysCombo_SelectedItemChanged) self.RecordsListBox.bind("<<ListboxSelect>>", self.RecordsListBox_SelectedItemChanged) self.RecordsListBox.bind('<Double-1>', lambda x: self.ShowEditForm()) #End Designer #Set Form Controls self.FillCombos() self.SetButtonsEnabled() self.Queue = queue.Queue(10) self.KillEvent = threading.Event() self.ControllerThread = threading.Thread(target=self.ctrl, args=(self.Queue, self.KillEvent)) self.ControllerThread.start() Logger.LogInfo(self.ControllerThread.getName() + ' started.') self.CheckForUpdatesFromController() def ctrl(self, queue, killEvent): dac = DAController.DAController(queue, killEvent) dac.Listen() def DatabaseBackup(self): source = self.Config['DEFAULT']['databasename'] destination = self.Config['DEFAULT'][ 'databasebackuplocation'] + '{}.db'.format( time.strftime('%Y%m%d%H%M')) copyfile(source, destination) messagebox.showinfo('Backup', 'Database backup made') def CheckForUpdatesFromController(self): if not self.Queue.empty(): queue = self.Queue.get() blPr = BLProject.BLProject(self.dbConnection) project = blPr.GetByButton(queue) if project is not None: print(project.Description) recordType = 1 blTr = BLTimeRecord.BLTimeRecord(self.dbConnection) for record in self.Cache.TimeRecords: if record.StatusID == TimeRecordStatusEnum.TimeRecordStatusEnum.Gestart.value: record.StatusID = TimeRecordStatusEnum.TimeRecordStatusEnum.Gestopt.value record.EndHour = Globals.GetCurrentTime() blTr.Update(record) timeRecord = TimeRecord.TimeRecord( None, Globals.GetCurrentTime(), None, project.ID, recordType, 'Automatically generated', TimeRecordStatusEnum.TimeRecordStatusEnum.Gestart.value, 0, None, None) valid = TimeRecordValidation.TimeRecordValidation() validationMessage = valid.ValidateOnCreation(timeRecord) if not len(validationMessage) == 0: errorMessage = '' for i in validationMessage: errorMessage = errorMessage + i + '\n' messagebox.showerror('Error', errorMessage) else: index = self.DaysCombo.current() blTr.Create(timeRecord) self.Cache.RefreshAllStaticData() self.FillCombos() if index == -1: index = 0 self.DaysCombo.current(index) self.RefreshTimeRecords() self.Master.after(500, self.CheckForUpdatesFromController) def OpenInOneNote(self): sel = self.RecordsListBox.curselection()[0] timeRecordView = self.Cache.TimeRecordViews[sel] os.system("start " + timeRecordView.OneNoteLink) def CopyRecord(self): blTr = BLTimeRecord.BLTimeRecord(self.dbConnection) sel = self.RecordsListBox.curselection()[0] timeRecordView = self.Cache.TimeRecordViews[sel] timeRecord = blTr.GetById(timeRecordView.ID) blTr.CopyTimeRecord(timeRecord) self.Refresh() def Refresh(self): index = self.DaysCombo.current() self.DaysCombo.current(0) self.Cache.RefreshAllStaticData() self.FillCombos() self.DaysCombo.current(index) self.RefreshTimeRecords() self.SetButtonsEnabled() def RecordsListBox_SelectedItemChanged(self, eventObject): self.SetButtonsEnabled() def DaysCombo_SelectedItemChanged(self, eventObject): self.RefreshTimeRecords() self.SetButtonsEnabled() def RefreshTimeRecords(self): index = self.DaysCombo.current() if not index == -1: date = self.Cache.DayViews[index].Date self.Cache.RefreshTimeRecordsForDate(date) self.FillTimeRecords(self.Cache.TimeRecordViews) else: self.RecordsListBox.delete(0, END) self.CommentListBox.delete(0, END) def Show(self): self.Master.mainloop() def FillCombos(self): self.FillProjectCombo() self.FillRecordTypeCombo() self.FillDays() def FillProjectCombo(self): self.ProjectsCombo['value'] = self.Cache.ActiveProjects def FillRecordTypeCombo(self): self.RecordTypeCombo['value'] = self.Cache.RecordTypes def FillDays(self): self.DaysCombo['value'] = self.Cache.DayViews def FillTimeRecords(self, timeRecordViews): self.RecordsListBox.delete(0, END) self.CommentListBox.delete(0, END) for item in timeRecordViews: self.RecordsListBox.insert(END, item) self.CommentListBox.insert(END, item.Description) for i in range(0, self.RecordsListBox.size()): item = timeRecordViews[i] itemStatus = item.Status if itemStatus == 'Gestart': self.RecordsListBox.itemconfig(i, {'bg': 'red'}) elif itemStatus == 'Gestopt': self.RecordsListBox.itemconfig(i, {'bg': 'green'}) elif itemStatus == 'Gekopieerd': self.RecordsListBox.itemconfig(i, {'bg': 'orange'}) def StartRecording(self): recordIndex = self.RecordTypeCombo.current() projectIndex = self.ProjectsCombo.current() if recordIndex == -1: recordType = '' else: recordType = self.Cache.RecordTypes[recordIndex].ID if projectIndex == -1: project = None else: project = self.Cache.ActiveProjects[projectIndex].ID timeRecord = TimeRecord.TimeRecord( None, Globals.GetCurrentTime(), None, project, recordType, self.DescriptionValue.get(), TimeRecordStatusEnum.TimeRecordStatusEnum.Gestart.value, 0, None, None) valid = TimeRecordValidation.TimeRecordValidation() validationMessage = valid.ValidateOnCreation(timeRecord) if not len(validationMessage) == 0: errorMessage = '' for i in validationMessage: errorMessage = errorMessage + i + '\n' messagebox.showerror('Error', errorMessage) else: index = self.DaysCombo.current() blTr = BLTimeRecord.BLTimeRecord(self.dbConnection) blTr.Create(timeRecord) self.Cache.RefreshAllStaticData() self.FillCombos() if index == -1: index = 0 self.DaysCombo.current(index) self.RefreshTimeRecords() def StopRecording(self): blTr = BLTimeRecord.BLTimeRecord(self.dbConnection) sel = self.RecordsListBox.curselection()[0] timeRecordView = self.Cache.TimeRecordViews[sel] timeRecord = blTr.GetById(timeRecordView.ID) timeRecord.EndHour = Globals.GetCurrentTime() timeRecord.StatusID = TimeRecordStatusEnum.TimeRecordStatusEnum.Gestopt.value blTr.Update(timeRecord) index = self.DaysCombo.current() self.DaysCombo.current(0) self.Cache.RefreshAllStaticData() self.FillCombos() self.DaysCombo.current(index) self.RefreshTimeRecords() self.SetButtonsEnabled() def CopyToCodex(self): self.Master.clipboard_clear() index = self.DaysCombo.current() date = self.Cache.DayViews[index].Date self.Master.clipboard_append( Globals.CopyToCodex(self.dbConnection, date)) self.RefreshTimeRecords() def ShowEditForm(self): timeRecordView = self.Cache.TimeRecordViews[ self.RecordsListBox.curselection()[0]] edit = TimeRecordEditForm(self.dbConnection, timeRecordView, self.Cache) edit.Show() index = self.DaysCombo.current() self.DaysCombo.current(0) self.Cache.RefreshAllStaticData() self.FillCombos() self.DaysCombo.current(index) self.RefreshTimeRecords() edit.Master.destroy() def ShowNewEditForm(self): tr = TimeRecordView.TimeRecordView(None, None, None, None, None, None, None, None, None, None, None) index = self.DaysCombo.current() tr.Date = self.Cache.DayViews[index].Date edit = TimeRecordEditForm(self.dbConnection, tr, self.Cache) edit.Show() index = self.DaysCombo.current() self.DaysCombo.current(0) self.Cache.RefreshAllStaticData() self.FillCombos() self.DaysCombo.current(index) self.RefreshTimeRecords() edit.Master.destroy() def OpenProjectListForm(self): projectListForm = ProjectListForm(self.Cache, self.dbConnection) projectListForm.Show() self.Cache.RefreshAllStaticData() self.FillCombos() projectListForm.Master.destroy() def OpenRecordTypeListForm(self): recordTypeListForm = RecordTypeListForm(self.Cache, self.dbConnection) recordTypeListForm.Show() self.Cache.RefreshAllStaticData() self.FillCombos() recordTypeListForm.Master.destroy() def ExportToExcel(self): excel = ExportToExcelForm(self.dbConnection) excel.Show() excel.Master.destroy() def ExportToGraph(self): graph = ExportToGraphsForm(self.dbConnection, self.Cache) graph.Show() graph.Master.destroy() def DeleteRecord(self): bl = BLTimeRecord.BLTimeRecord(self.dbConnection) indexRecordsListBox = self.RecordsListBox.curselection()[0] record = self.Cache.TimeRecordViews[indexRecordsListBox] bl.DeleteByID(record.ID) index = self.DaysCombo.current() self.Cache.RefreshAllStaticData() self.FillCombos() #Hier schiet nog 1 record over; het is nog niet verwijderd uit Cache op dit moment if len(self.Cache.TimeRecordViews) == 1: self.DaysCombo.set('') else: self.DaysCombo.current(index) self.RefreshTimeRecords() self.SetButtonsEnabled() def SetButtonsEnabled(self): enableStop = True enableCopyToCodex = True enableDelete = True enableCopyRecord = True enableOpenOneNote = True indexDaysCombo = self.DaysCombo.current() indexRecordsListBox = self.RecordsListBox.curselection() current = Globals.GetCurrentDay() if indexDaysCombo == -1: enableStop = False enableCopyToCodex = False else: date = self.Cache.DayViews[indexDaysCombo].Date if not current == date: enableStop = False bl = BLTimeRecord.BLTimeRecord(self.dbConnection) records = bl.GetAllForDate(date) for record in records: if record.StatusID == TimeRecordStatusEnum.TimeRecordStatusEnum.Gestart.value: enableCopyToCodex = False if len(indexRecordsListBox) == 0: enableStop = False enableDelete = False enableCopyRecord = False enableOpenOneNote = False else: trView = self.Cache.TimeRecordViews[indexRecordsListBox[0]] if trView.OneNoteLink == 'None' or trView.OneNoteLink == "": enableOpenOneNote = False self.SetButton(enableStop, self.StopRecordButton) self.SetButton(enableCopyToCodex, self.CopyToCodexButton) self.SetButton(enableDelete, self.DeleteRecordButton) self.SetButton(enableCopyRecord, self.CopyRecordButton) self.SetButton(enableOpenOneNote, self.OneNoteButton) def SetButton(self, enabled, button): if enabled: button.config(state=NORMAL) else: button.config(state=DISABLED)
class ItemList(object): def __init__(self, title, width=115, height=10, bg="white", fg="black", font=("Consolas", 10), icon=None, selection=False): # Cria janela. self.__root = Tk() # Configura a janela. self.__root.resizable(False, False) self.__root.title(title) self.__root.iconbitmap(icon) self.__root.focus_force() # Cria uma barra de rolagem. scrollbar = Scrollbar(self.__root) scrollbar.pack(side="right", fill="y") # Cria lista de itens. self.__listbox = Listbox(self.__root, width=width, height=height, font=font) self.__listbox.config(yscrollcommand=scrollbar.set) self.__listbox.pack() # Configura a barra de rolagem. scrollbar.config(command=self.__listbox.yview) # Desativa o mecanismo de seleção da lista caso "selection" seja False. if not selection: self.__listbox.bindtags((self.__listbox, self.__root, "all")) else: self.__listbox.config(selectmode="SINGLE") self.__selection = selection def __close(self, event): """ Método do evento para fechar janela. """ self.__root.destroy() def run(self, list_, stopFunction, spacing=30): """ Coloca as strings dentro da lista e a executa dentro de um mainloop. """ # Evento para fechar a janela. self.__stopFunction = stopFunction self.__root.bind("<Escape>", self.__close) # Coloca os itens dentro da lista. for item in list_: # Caso tenha sido definido um espaçamento, será separado o título da descrição. if spacing and not isinstance(item, str): if len(item[0]) < spacing: item[0] += " " * (spacing - len(item[0])) else: item[0] = item[0][:spacing - 3] + "..." self.__listbox.insert(0, item[0] + " " + item[1]) # Caso não tenha sido definido um espaçamento, será colocado simplesmente o item à lista. else: self.__listbox.insert(0, item) # Atualiza a janela. self.__item = None self.__updater() self.__root.mainloop() # Retorna um item selecionado. return self.__item def __updater(self): """ Método para obter um item selecionado e verificar se o usuário deseja fechar a janela. """ # Obtém um item selecionado. if self.__selection: item = self.__listbox.curselection() if item: self.__item = self.__listbox.get(item[0]).strip() self.__root.destroy() # Caso o retorno da função seja True, ele irá parar de atualizar a janela. if not self.__stopFunction(): return self.__root.after(1, self.__updater) else: self.__root.destroy()
class FileDialog: """Standard file selection dialog -- no checks on selected file. Usage: d = FileDialog(master) fname = d.go(dir_or_file, pattern, default, key) if fname is None: ...canceled... else: ...open file... All arguments to go() are optional. The 'key' argument specifies a key in the global dictionary 'dialogstates', which keeps track of the values for the directory and pattern arguments, overriding the values passed in (it does not keep track of the default argument!). If no key is specified, the dialog keeps no memory of previous state. Note that memory is kept even when the dialog is canceled. (All this emulates the behavior of the Macintosh file selection dialogs.) """ title = "File Selection Dialog" def __init__(self, master, title=None): if title is None: title = self.title self.master = master self.directory = None self.top = Toplevel(master) self.top.title(title) self.top.iconname(title) _setup_dialog(self.top) self.botframe = Frame(self.top) self.botframe.pack(side=BOTTOM, fill=X) self.selection = Entry(self.top) self.selection.pack(side=BOTTOM, fill=X) self.selection.bind('<Return>', self.ok_event) self.filter = Entry(self.top) self.filter.pack(side=TOP, fill=X) self.filter.bind('<Return>', self.filter_command) self.midframe = Frame(self.top) self.midframe.pack(expand=YES, fill=BOTH) self.filesbar = Scrollbar(self.midframe) self.filesbar.pack(side=RIGHT, fill=Y) self.files = Listbox(self.midframe, exportselection=0, yscrollcommand=(self.filesbar, 'set')) self.files.pack(side=RIGHT, expand=YES, fill=BOTH) btags = self.files.bindtags() self.files.bindtags(btags[1:] + btags[:1]) self.files.bind('<ButtonRelease-1>', self.files_select_event) self.files.bind('<Double-ButtonRelease-1>', self.files_double_event) self.filesbar.config(command=(self.files, 'yview')) self.dirsbar = Scrollbar(self.midframe) self.dirsbar.pack(side=LEFT, fill=Y) self.dirs = Listbox(self.midframe, exportselection=0, yscrollcommand=(self.dirsbar, 'set')) self.dirs.pack(side=LEFT, expand=YES, fill=BOTH) self.dirsbar.config(command=(self.dirs, 'yview')) btags = self.dirs.bindtags() self.dirs.bindtags(btags[1:] + btags[:1]) self.dirs.bind('<ButtonRelease-1>', self.dirs_select_event) self.dirs.bind('<Double-ButtonRelease-1>', self.dirs_double_event) self.ok_button = Button(self.botframe, text="OK", command=self.ok_command) self.ok_button.pack(side=LEFT) self.filter_button = Button(self.botframe, text="Filter", command=self.filter_command) self.filter_button.pack(side=LEFT, expand=YES) self.cancel_button = Button(self.botframe, text="Cancel", command=self.cancel_command) self.cancel_button.pack(side=RIGHT) self.top.protocol('WM_DELETE_WINDOW', self.cancel_command) # XXX Are the following okay for a general audience? self.top.bind('<Alt-w>', self.cancel_command) self.top.bind('<Alt-W>', self.cancel_command) def go(self, dir_or_file=os.curdir, pattern="*", default="", key=None): if key and key in dialogstates: self.directory, pattern = dialogstates[key] else: dir_or_file = os.path.expanduser(dir_or_file) if os.path.isdir(dir_or_file): self.directory = dir_or_file else: self.directory, default = os.path.split(dir_or_file) self.set_filter(self.directory, pattern) self.set_selection(default) self.filter_command() self.selection.focus_set() self.top.wait_visibility() # window needs to be visible for the grab self.top.grab_set() self.how = None self.master.mainloop() # Exited by self.quit(how) if key: directory, pattern = self.get_filter() if self.how: directory = os.path.dirname(self.how) dialogstates[key] = directory, pattern self.top.destroy() return self.how def quit(self, how=None): self.how = how self.master.quit() # Exit mainloop() def dirs_double_event(self, event): self.filter_command() def dirs_select_event(self, event): dir, pat = self.get_filter() subdir = self.dirs.get('active') dir = os.path.normpath(os.path.join(self.directory, subdir)) self.set_filter(dir, pat) def files_double_event(self, event): self.ok_command() def files_select_event(self, event): file = self.files.get('active') self.set_selection(file) def ok_event(self, event): self.ok_command() def ok_command(self): self.quit(self.get_selection()) def filter_command(self, event=None): dir, pat = self.get_filter() try: names = os.listdir(dir) except OSError: self.master.bell() return self.directory = dir self.set_filter(dir, pat) names.sort() subdirs = [os.pardir] matchingfiles = [] for name in names: fullname = os.path.join(dir, name) if os.path.isdir(fullname): subdirs.append(name) elif fnmatch.fnmatch(name, pat): matchingfiles.append(name) self.dirs.delete(0, END) for name in subdirs: self.dirs.insert(END, name) self.files.delete(0, END) for name in matchingfiles: self.files.insert(END, name) head, tail = os.path.split(self.get_selection()) if tail == os.curdir: tail = '' self.set_selection(tail) def get_filter(self): filter = self.filter.get() filter = os.path.expanduser(filter) if filter[-1:] == os.sep or os.path.isdir(filter): filter = os.path.join(filter, "*") return os.path.split(filter) def get_selection(self): file = self.selection.get() file = os.path.expanduser(file) return file def cancel_command(self, event=None): self.quit() def set_filter(self, dir, pat): if not os.path.isabs(dir): try: pwd = os.getcwd() except OSError: pwd = None if pwd: dir = os.path.join(pwd, dir) dir = os.path.normpath(dir) self.filter.delete(0, END) self.filter.insert(END, os.path.join(dir or os.curdir, pat or "*")) def set_selection(self, file): self.selection.delete(0, END) self.selection.insert(END, os.path.join(self.directory, file))