def finish_form(self):
        """Assign new category indices and exit loop."""
        ### reference category path map locally for quick
        ### and easier access
        category_path_map = APP_REFS.category_path_map

        ### iterate over color widgets, assigning the
        ### color indices to the respectives categories

        for widget in self.widgets:

            if not isinstance(widget, List2D):
                continue

            color_widget = widget[0]
            widget_color = color_widget.color

            index = next(
                index
                for index, color in (enumerate(NODE_CATEGORY_COLORS))
                if color == widget_color
            )

            ###

            category_id = color_widget.category_id

            category_path = category_path_map[category_id]

            category_metadata_path = category_path / NODE_CATEGORY_METADATA_FILENAME

            if category_metadata_path.is_file():

                try:
                    metadata = load_pyl(category_metadata_path)

                except Exception as err:

                    error_msg = "Error while loading category metadata."

                    logger.exception(error_msg)
                    USER_LOGGER.exception(error_msg)

                    dialog_msg = error_msg + (
                        " Check the user log for more info (press"
                        " <Ctrl+Shift+j> after leaving this form or access"
                        " the \"Help > Show user log\" option on menubar)."
                    )

                    create_and_show_dialog(dialog_msg, level_name="error")

                    return

                metadata["color_index"] = index

            else:
                metadata = {"color_index": index}

            ###

            try:
                save_pyl(metadata, category_metadata_path)

            except Exception as err:
                
                error_msg = "Error while saving category metadata."

                logger.exception(error_msg)
                USER_LOGGER.exception(error_msg)

                dialog_msg = error_msg + (
                    " Check the user log for more info (press <Ctrl+Shift+j>"
                    " after leaving this form or access the \"Help > Show user"
                    " log\" option on menubar)."
                )

                create_and_show_dialog(dialog_msg, level_name="error")

                return

        ### notify user via dialog and status message

        message = (
            "Category colors changed. Restart the application to see the"
            " changes."
        )

        set_status_message(message)

        create_and_show_dialog(message, level_name='info')
Assign new category indices and exit loop.
nodezator/editing/categorycolors.py