Ejemplo n.º 1
0
class SportOrgImportDialog(QDialog):
    def __init__(self, races, current_race=0):
        super().__init__(GlobalAccess().get_main_window())
        self.races = races
        self.current_race = current_race

    def exec_(self):
        self.init_ui()
        self.set_values()
        return super().exec_()

    def init_ui(self):
        self.setWindowTitle(_('Import'))
        self.setWindowIcon(QIcon(config.ICON))
        self.setSizeGripEnabled(False)
        self.setModal(True)

        self.layout = QFormLayout(self)

        self.item_races = AdvComboBox()
        self.layout.addRow(QLabel(_('Choose race')), self.item_races)

        self.unique_id_box = QGroupBox(_('Unique id'))
        self.unique_id_box_layout = QFormLayout()
        self.unique_id_item_id = QRadioButton(_('Id'))
        self.unique_id_item_id.setChecked(True)
        self.unique_id_box_layout.addRow(self.unique_id_item_id)
        self.unique_id_item_name = QRadioButton(_('Name'))
        self.unique_id_item_name.setDisabled(True)
        self.unique_id_box_layout.addRow(self.unique_id_item_name)
        self.unique_id_box.setLayout(self.unique_id_box_layout)
        self.layout.addRow(self.unique_id_box)

        self.import_action_box = QGroupBox(_('Action'))
        self.import_action_box_layout = QFormLayout()
        self.import_action_item_add = QRadioButton(_('Add'))
        self.import_action_box_layout.addRow(self.import_action_item_add)
        self.import_action_item_overwrite = QRadioButton(_('Overwrite'))
        self.import_action_item_overwrite.setChecked(True)
        self.import_action_box_layout.addRow(self.import_action_item_overwrite)
        self.import_action_box.setLayout(self.import_action_box_layout)
        self.layout.addRow(self.import_action_box)

        def cancel_changes():
            self.close()

        def apply_changes():
            try:
                self.apply_changes_impl()
            except Exception as e:
                logging.error(str(e))
            self.close()

        button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
        self.button_ok = button_box.button(QDialogButtonBox.Ok)
        self.button_ok.setText(_('OK'))
        self.button_ok.clicked.connect(apply_changes)
        self.button_cancel = button_box.button(QDialogButtonBox.Cancel)
        self.button_cancel.setText(_('Cancel'))
        self.button_cancel.clicked.connect(cancel_changes)
        self.layout.addRow(button_box)

        self.show()

    def set_values(self):
        self.fill_race_list()

    def apply_changes_impl(self):
        import_race = self.races[self.item_races.currentIndex()]
        unique_id = 'id'
        if self.unique_id_item_name.isChecked():
            unique_id = 'name'
        action = 'add'
        if self.import_action_item_overwrite.isChecked():
            action = 'overwrite'

        obj = race()

        if unique_id == 'id' and action == 'overwrite':
            import_race.id = obj.id
            obj.update_data(import_race.to_dict())
            return

        if unique_id == 'id' and action == 'add':
            organizations = []
            for org in import_race.organizations:
                old_org = find(obj.organizations, id=org.id)
                old_org_by_name = find(obj.organizations, name=org.name)
                if old_org is None:
                    if old_org_by_name is not None:
                        org.name = '_' + org.name
                    organizations.append(org)
            obj.organizations.extend(organizations)

            courses = []
            for course in import_race.courses:
                old_course = find(obj.courses, id=course.id)
                old_course_by_name = find(obj.courses, name=course.name)
                if old_course is None:
                    if old_course_by_name is not None:
                        course.name = '_' + course.name
                    courses.append(course)
            obj.courses.extend(courses)

            groups = []
            for group in import_race.groups:
                old_group = find(obj.groups, id=group.id)
                old_group_by_name = find(obj.groups, name=group.name)
                if old_group is None:
                    if old_group_by_name is not None:
                        group.name = '_' + group.name
                    if group.course:
                        group.course = find(obj.courses, id=group.course.id)
                    groups.append(group)
            obj.groups.extend(groups)

            persons = []
            for person in import_race.persons:
                if find(obj.persons, id=person.id) is None:
                    if person.group:
                        person.group = find(obj.groups, id=person.group.id)
                    if person.organization:
                        person.organization = find(obj.organizations, id=person.organization.id)
                    persons.append(person)
            obj.persons.extend(persons)

            results = []
            for result in import_race.results:
                if find(obj.results, id=result.id) is None:
                    if result.person:
                        result.person = find(obj.persons, id=result.person.id)
                    results.append(result)
            obj.results.extend(results)
            return

    def fill_race_list(self):
        race_list = []

        self.item_races.clear()
        for cur_race in self.races:
            assert isinstance(cur_race, Race)
            race_list.append(str(cur_race.data.get_start_datetime()))
        self.item_races.addItems(race_list)

        self.item_races.setCurrentIndex(self.current_race)
Ejemplo n.º 2
0
class MultidayTab(Tab):
    def __init__(self, parent):
        self.widget = QWidget()
        self.layout = QFormLayout(parent)

        self.item_races = AdvComboBox()
        self.fill_race_list()

        max_button_width = 100

        def select_race():
            index = self.item_races.currentIndex()
            set_current_race_index(index)
            GlobalAccess().get_main_window().refresh()

        self.item_races.currentIndexChanged.connect(select_race)
        self.layout.addRow(self.item_races)

        def add_race_function():
            add_race()
            self.fill_race_list()

        self.item_new = QPushButton(_('New'))
        self.item_new.clicked.connect(add_race_function)
        self.item_new.setMaximumWidth(max_button_width)
        self.layout.addRow(self.item_new)

        def copy_race_function():
            copy_race()
            self.fill_race_list()

        self.item_copy = QPushButton(_('Copy'))
        self.item_copy.clicked.connect(copy_race_function)
        self.item_copy.setMaximumWidth(max_button_width)
        self.layout.addRow(self.item_copy)

        def move_up_race_function():
            move_up_race()
            self.fill_race_list()

        self.item_move_up = QPushButton(_('Move up'))
        self.item_move_up.clicked.connect(move_up_race_function)
        self.item_move_up.setMaximumWidth(max_button_width)
        self.layout.addRow(self.item_move_up)

        def move_down_race_function():
            move_down_race()
            self.fill_race_list()

        self.item_move_down = QPushButton(_('Move down'))
        self.item_move_down.clicked.connect(move_down_race_function)
        self.item_move_down.setMaximumWidth(max_button_width)
        self.layout.addRow(self.item_move_down)

        def del_race_function():
            del_race()
            self.fill_race_list()

        self.item_del = QPushButton(_('Delete'))
        self.item_del.clicked.connect(del_race_function)
        self.item_del.setMaximumWidth(max_button_width)
        self.layout.addRow(self.item_del)

        self.widget.setLayout(self.layout)

    def save(self):
        pass

    def fill_race_list(self):
        race_list = []
        index = get_current_race_index()

        self.item_races.clear()
        for cur_race in races():
            assert isinstance(cur_race, Race)
            race_list.append(str(cur_race.data.get_start_datetime()))
        self.item_races.addItems(race_list)

        self.item_races.setCurrentIndex(index)