def __init__(self):
        super(EmployeeWindow, self).__init__()
        self.ui = Ui_employeeWindow()
        self.ui.setupUi(self)
        self.lst = self.ui.listWidget

        self.cur_rec_id = None
        self.investigators = [emp for emp in Dataset.employees if Dataset.employees[emp].investigator]

        self._load_list(Dataset.employees.keys())

        self.lst.itemClicked.connect(self._rec_selected)
        self.ui.investigatorFilterBox.clicked.connect(self._filter_clicked)
        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.vistaButton.clicked.connect(self._vista_clicked)

        self.ui.nameEdit.editingFinished.connect(self._name_edited)
        self.ui.investigatorBox.clicked.connect(self._investigator_clicked)
        self.ui.investigatorBox.setEnabled(False)
        self.ui.gradeEdit.editingFinished.connect(self._grade_edited)

        self.setMinimumWidth(800)
class EmployeeWindow(QWidget):

    # noinspection PyUnresolvedReferences
    def __init__(self):
        super(EmployeeWindow, self).__init__()
        self.ui = Ui_employeeWindow()
        self.ui.setupUi(self)
        self.lst = self.ui.listWidget

        self.cur_rec_id = None
        self.investigators = [emp for emp in Dataset.employees if Dataset.employees[emp].investigator]

        self._load_list(Dataset.employees.keys())

        self.lst.itemClicked.connect(self._rec_selected)
        self.ui.investigatorFilterBox.clicked.connect(self._filter_clicked)
        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.vistaButton.clicked.connect(self._vista_clicked)

        self.ui.nameEdit.editingFinished.connect(self._name_edited)
        self.ui.investigatorBox.clicked.connect(self._investigator_clicked)
        self.ui.investigatorBox.setEnabled(False)
        self.ui.gradeEdit.editingFinished.connect(self._grade_edited)

        self.setMinimumWidth(800)

    def _load_list(self, names, selected=None):
        self.lst.clear()
        if not names:
            return
        selected_index = 0
        for idx, name in enumerate(sorted(names)):
            item = QListWidgetItem(name)
            self.lst.addItem(item)
            if name == selected:
                selected_index = idx
        self.lst.setCurrentItem(self.lst.item(selected_index))
        self._rec_selected()

    def _rec_selected(self):
        rec = Dataset.employees[self.lst.currentItem().text()]
        self.ui.nameEdit.setText(rec.name)
        self.ui.nameEdit.setCursorPosition(0)
        self.ui.gradeEdit.setText(str(rec.grade))
        self.ui.stepEdit.setText(str(rec.step))
        self.ui.fteEdit.setText(str(rec.fte))
        if rec.cp == '015':
            self.ui.cp15Button.setChecked(True)
        else:
            self.ui.cp16Button.setChecked(True)
        self.ui.investigatorBox.setChecked(rec.investigator == 1)
        self.ui.notesEdit.setPlainText(rec.notes)
        self.cur_rec_id = rec.id

    def _add_clicked(self):
        self.cur_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.gradeEdit.setText(None)
        self.ui.stepEdit.setText(None)
        self.ui.fteEdit.setText(None)
        self.ui.investigatorBox.setChecked(False)
        self.ui.cp15Button.setChecked(False)
        self.ui.cp16Button.setChecked(False)
        self.ui.notesEdit.setPlainText(None)

    def _save_clicked(self):
        rec = self._valid_emp()
        if rec:
            try:
                rec.save()
            except AllocatException as e:
                QMessageBox.critical(QMessageBox(), 'Input Error', e.msg)
                self.ui.nameEdit.setFocus()
                return
            Dataset.employees = Employee.get_all()
            self._load_list(Dataset.employees.keys(), rec.name)
        self.ui.addBtn.setEnabled(True)

    def _valid_emp(self):
        name = self.ui.nameEdit.text()
        if not Employee.is_valid_name(name):
            msg = 'Employee name is invalid'
            QMessageBox.critical(QMessageBox(), 'Input Error', msg)
            self.ui.nameEdit.setFocus()
            return None
        grade = self.ui.gradeEdit.text()
        if not Employee.is_valid_grade_step(grade):
            msg = 'Employee grade is invalid'
            QMessageBox.critical(QMessageBox(), 'Input Error', msg)
            self.ui.gradeEdit.setFocus()
            return None
        step = self.ui.stepEdit.text()
        if not Employee.is_valid_grade_step(step):
            msg = 'Employee step is invalid'
            QMessageBox.critical(QMessageBox(), 'Input Error', msg)
            self.ui.stepEdit.setFocus()
            return None
        fte = self.ui.fteEdit.text()
        if not Employee.is_valid_fte(fte):
            msg = 'Employee FTE is invalid'
            QMessageBox.critical(QMessageBox(), 'Input Error', msg)
            self.ui.fteEdit.setFocus()
            return None
        cp = '015' if self.ui.cp15Button.isChecked() else '016'
        investigator = self.ui.investigatorBox.isChecked()
        notes = self.ui.notesEdit.toPlainText()
        return Employee([
            self.cur_rec_id,
            name,
            int(grade),
            int(step),
            int(fte),
            cp,
            investigator,
            notes
        ])

    def _remove_clicked(self):
        name = self.ui.nameEdit.text()
        msg = 'Are you sure you want to remove employee ' + name + '?'
        reply = QMessageBox.question(QMessageBox(), 'Double check', msg)
        if reply == QMessageBox.Yes:
            rec = Dataset.employees[name]
            rec.remove()
            del Dataset.employees[name]
            self._load_list(Dataset.employees.keys())

    def _filter_clicked(self):
        if self.ui.investigatorFilterBox.isChecked():
            self._load_list(self.investigators)
        else:
            self._load_list(Dataset.employees)

    def _investigator_clicked(self, checked):
        grade = self.ui.gradeEdit.text()
        if (checked and grade < 13) or \
                (not checked and grade > 12):
            msg = 'Investigator with grade %d?' % (grade,)
            QMessageBox.warning(QMessageBox(), 'Input Warning', msg)

    def _name_edited(self):
        self.ui.nameEdit.setText(self.ui.nameEdit.text().replace(', ', ',').upper())

    def _grade_edited(self):
        grade = int(self.ui.gradeEdit.text())
        self.ui.investigatorBox.setChecked(grade > 12)
        self.ui.investigatorBox.setEnabled(True)

    def _asns_clicked(self):
        name = self.ui.nameEdit.text()
        emp = Dataset.employees[name]
        emp.set_assignments()

        from views import AssignmentDialog
        assignment_dialog = AssignmentDialog(emp)
        assignment_dialog.exec_()

    def _vista_clicked(self):
        pass