def __init__(self): super(ProjectWindow, self).__init__() self.ui = Ui_projectWindow() self.ui.setupUi(self) self.lst = self.ui.listWidget self.current_rec_id = None self._set_prj_by_emp() self._load_list(Dataset.projects.keys()) self._load_emp_list() self.lst.itemClicked.connect(self._rec_selected) self.ui.addBtn.clicked.connect(self._add_clicked) self.ui.saveBtn.clicked.connect(self._save_clicked) self.ui.removeBtn.clicked.connect(self._remove_clicked) self.ui.asnsBtn.clicked.connect(self._asns_clicked) self.ui.empList.currentIndexChanged.connect(self._emp_filter) self.ui.allBtn.clicked.connect(self._all_clicked) self.setMinimumWidth(800)
class ProjectWindow(QWidget): # noinspection PyUnresolvedReferences def __init__(self): super(ProjectWindow, self).__init__() self.ui = Ui_projectWindow() self.ui.setupUi(self) self.lst = self.ui.listWidget self.current_rec_id = None self._set_prj_by_emp() self._load_list(Dataset.projects.keys()) self._load_emp_list() self.lst.itemClicked.connect(self._rec_selected) self.ui.addBtn.clicked.connect(self._add_clicked) self.ui.saveBtn.clicked.connect(self._save_clicked) self.ui.removeBtn.clicked.connect(self._remove_clicked) self.ui.asnsBtn.clicked.connect(self._asns_clicked) self.ui.empList.currentIndexChanged.connect(self._emp_filter) self.ui.allBtn.clicked.connect(self._all_clicked) self.setMinimumWidth(800) def _set_prj_by_emp(self): d = {} for asn in Dataset.assignments: if asn.employee_name not in d: d[asn.employee_name] = [] d[asn.employee_name].append(asn.project_name) self.prj_by_emp = d def _load_list(self, nicknames, selected=None): self.lst.clear() if not nicknames: return selected_index = 0 for idx, nickname in enumerate(sorted(nicknames)): item = QListWidgetItem(nickname) self.lst.addItem(item) if nickname == selected: selected_index = idx self.lst.setCurrentItem(self.lst.item(selected_index)) self._rec_selected() def _load_emp_list(self): self.ui.empList.clear() emps = sorted(Dataset.employees.keys()) for name in emps: self.ui.empList.addItem(name) def _emp_filter(self): emp_name = self.ui.empList.currentText() projects = self.prj_by_emp[emp_name] self._load_list(projects) def _rec_selected(self): rec = Dataset.projects[self.lst.currentItem().text()] self.ui.nameEdit.setText(rec.name) self.ui.nameEdit.setToolTip(rec.name) self.ui.nameEdit.setCursorPosition(0) self.ui.nicknameEdit.setText(rec.nickname) self.ui.firstMonthEdit.setText(Monkey.prettify(rec.first_month)) self.ui.lastMonthEdit.setText(Monkey.prettify(rec.last_month)) self.ui.notesEdit.setPlainText(rec.notes) self.ui.billingEdit.setPlainText(rec.billing) self.current_rec_id = rec.id def _all_clicked(self): self._load_list(Dataset.projects.keys()) def _add_clicked(self): self.current_rec_id = None self._clear_form() self.ui.nameEdit.setFocus() self.ui.addBtn.setEnabled(False) def _clear_form(self): self.ui.nameEdit.setText(None) self.ui.nicknameEdit.setText(None) self.ui.firstMonthEdit.setText(None) self.ui.lastMonthEdit.setText(None) self.ui.notesEdit.setPlainText(None) self.ui.billingEdit.setPlainText(None) def _save_clicked(self): interval = self._validate_monkeys() if not interval: return rec = Project([ self.current_rec_id, self.ui.nicknameEdit.text(), self.ui.nameEdit.text(), interval.first_month, interval.last_month, self.ui.notesEdit.toPlainText(), self.ui.billingEdit.toPlainText() ]) try: rec.save() except AllocatException as e: QMessageBox.critical(QMessageBox(), 'Input Error', e.msg) self.ui.nameEdit.setFocus() return Dataset.projects = Project.get_all() self._load_list(Dataset.projects.keys(), rec.nickname) self.ui.addBtn.setEnabled(True) def _validate_monkeys(self): first_month = Monkey.unslash(self.ui.firstMonthEdit.text()) last_month = Monkey.unslash(self.ui.lastMonthEdit.text()) if not Monkey.is_valid_pair(first_month, last_month): msg = 'Invalid time frame: invalid month or first month after last month!' QMessageBox.critical(QMessageBox(), 'Input Error', msg) self.ui.firstMonthEdit.setFocus() return None return TimeInterval(first_month=first_month, last_month=last_month) def _remove_clicked(self): nickname = self.ui.nicknameEdit.text() msg = 'Are you sure you want to remove project ' + nickname + '?' reply = QMessageBox.question(QMessageBox(), 'Double check', msg) if reply == QMessageBox.Yes: rec = Dataset.projects[nickname] rec.remove() del Dataset.projects[nickname] self._load_list(Dataset.projects.keys()) def _asns_clicked(self): nickname = self.ui.nicknameEdit.text() prj = Dataset.projects[nickname] prj.set_assignments() from views import AssignmentDialog assignment_dialog = AssignmentDialog(prj) assignment_dialog.exec_()