<commit_before>from PyQt5.QtWidgets import QDialog, QListWidgetItem, QLineEdit, QListWidget, QPushButton
from PyQt5.Qt import Qt, QFont, QDesktopWidget, QSizePolicy
import os
from Paradox.Mod import Mod

class ProfileEditor(QDialog):
    def __init__(self, app, profile=None):
        """
        Init
        """
        self.app = app
        self.profile = profile
        self.game_path = os.path.expanduser('~\\Documents\\Paradox Interactive\\Stellaris')
        self.game_mods_path = os.path.join('\\', self.game_path, 'mod')

        super(ProfileEditor, self).__init__()

        # define UI elements
        self.profileName = QLineEdit(self)
        self.listOfMods = QListWidget(self)
        self.saveButton = QPushButton(self)
        self.cancelButton = QPushButton(self)
        # self.reloadModsButton = QPushButton(self)

        # initialize the UI
        self.init_ui()

        """
        if not profile:
            self.add_mods()
        else:
            self.profileName.setText(profile[0])
            self.add_mods(profile[1])
        """

    def init_ui(self):
        """
        Initialize the actual UI
        :return: 
        """
        # Main window
        desktop = QDesktopWidget()
        screen_width = desktop.screen().width()
        screen_height = desktop.screen().height()
        app_width = 450
        app_height = 420
        self.setWindowTitle('Stellaris Profile Manager')
        self.setGeometry(  # this centers the window
            (screen_width / 2) - (app_width / 2),
            (screen_height / 2) - (app_height / 2),
            app_width,
            app_height
        )
        self.setFixedSize(self.size())
        self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)

        # elements
        self.create_profile_name_input()
        self.create_list_of_mods()
        self.create_save_button()
        self.create_cancel_button()

    def create_profile_name_input(self):
        """
        Configure the field for setting the profile name
        :return: 
        """
        self.profileName.setGeometry(20, 20, 410, 30)
        self.profileName.setPlaceholderText('Profile name')
        if self.profile:
            self.profileName.setText(self.profile[0])

    def create_list_of_mods(self):
        """
        Configure the list of mods
        :return: 
        """
        self.listOfMods.setGeometry(20, 55, 410, 300)
        if self.profile:
            self.load_mods(self.profile[1])
        else:
            self.load_mods()

    def create_save_button(self):
        """
        Configure the save button
        :return: 
        """
        self.saveButton.setGeometry(270, 360, 80, 30)
        self.saveButton.setText('Save')
        self.saveButton.clicked.connect(self.on_save)

    def create_cancel_button(self):
        """
        Configure the cancel button
        :return: 
        """
        self.cancelButton.setGeometry(351, 360, 80, 30)
        self.cancelButton.setText('Cancel')
        self.cancelButton.setDefault(True)
        self.cancelButton.clicked.connect(self.on_cancel)

    def load_mods(self, mods=[]):
        """
        Load mods into the list
        :param mods: 
        :return: 
        """
        for m in self.get_available_mods():
            item = QListWidgetItem()
            item.setFlags(item.flags() | Qt.ItemIsUserCheckable)
            if m['path'] in mods:
                item.setCheckState(Qt.Checked)
            else:
                item.setCheckState(Qt.Unchecked)
            item.setText(m['name'])
            item.setData(Qt.UserRole, m['path'])
            self.listOfMods.addItem(item)

        self.listOfMods.sortItems(Qt.AscendingOrder)

    def get_available_mods(self):
        mods = []
        for m in os.listdir(self.game_mods_path):
            p = os.path.join(self.game_mods_path, m)
            if os.path.isfile(p):
                mod = Mod(p)
                info = mod.get_info()
                info['path'] = 'mod/' + m
                mods.append(info)

        return mods

    def on_save(self):
        """
        Save the changes. It can be either creating a new profile
        or updating an existing one.<commit_msg>Adds reload button for list of mods<commit_after>from PyQt5.QtWidgets import QDialog, QListWidgetItem, QLineEdit, QListWidget, QPushButton
from PyQt5.Qt import Qt, QFont, QDesktopWidget, QSizePolicy
import os
from Paradox.Mod import Mod


class ProfileEditor(QDialog):
    def __init__(self, app, profile=None):
        """
        Init
        """
        self.app = app
        self.profile = profile
        self.game_path = os.path.expanduser('~\\Documents\\Paradox Interactive\\Stellaris')
        self.game_mods_path = os.path.join('\\', self.game_path, 'mod')

        super(ProfileEditor, self).__init__()

        # define UI elements
        self.profileName = QLineEdit(self)
        self.listOfMods = QListWidget(self)
        self.saveButton = QPushButton(self)
        self.cancelButton = QPushButton(self)
        self.reloadModsButton = QPushButton(self)

        # initialize the UI
        self.init_ui()

    def init_ui(self):
        """
        Initialize the actual UI
        :return: 
        """
        # Main window
        desktop = QDesktopWidget()
        screen_width = desktop.screen().width()
        screen_height = desktop.screen().height()
        app_width = 450
        app_height = 420
        self.setWindowTitle('Stellaris Profile Manager')
        self.setGeometry(  # this centers the window
            (screen_width / 2) - (app_width / 2),
            (screen_height / 2) - (app_height / 2),
            app_width,
            app_height
        )
        self.setFixedSize(self.size())
        self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)

        # elements
        self.create_profile_name_input()
        self.create_list_of_mods()
        self.create_save_button()
        self.create_cancel_button()
        self.create_reload_mods_button()

    def create_profile_name_input(self):
        """
        Configure the field for setting the profile name
        :return: 
        """
        self.profileName.setGeometry(20, 20, 410, 30)
        self.profileName.setPlaceholderText('Profile name')
        if self.profile:
            self.profileName.setText(self.profile[0])

    def create_list_of_mods(self):
        """
        Configure the list of mods
        :return: 
        """
        self.listOfMods.setGeometry(20, 55, 410, 300)
        if self.profile:
            self.load_mods(self.profile[1])
        else:
            self.load_mods()

    def create_save_button(self):
        """
        Configure the save button
        :return: 
        """
        self.saveButton.setGeometry(270, 360, 80, 30)
        self.saveButton.setText('Save')
        self.saveButton.clicked.connect(self.on_save)

    def create_cancel_button(self):
        """
        Configure the cancel button
        :return: 
        """
        self.cancelButton.setGeometry(351, 360, 80, 30)
        self.cancelButton.setText('Cancel')
        self.cancelButton.setDefault(True)
        self.cancelButton.clicked.connect(self.on_cancel)

    def create_reload_mods_button(self):
        """
        Configure the reload mods button
        :return: 
        """
        self.reloadModsButton.setGeometry(20, 360, 80, 30)
        self.reloadModsButton.setText('Reload mods')
        self.reloadModsButton.clicked.connect(self.reload_mods)

    def reload_mods(self):
        """
        Reload mods
        :return: 
        """
        if self.profile:
            self.load_mods(self.profile[1])
        else:
            self.load_mods()

    def load_mods(self, mods=[]):
        """
        Load mods into the list
        :param mods: 
        :return: 
        """
        self.listOfMods.clear()
        for m in self.get_available_mods():
            item = QListWidgetItem()
            item.setFlags(item.flags() | Qt.ItemIsUserCheckable)
            if m['path'] in mods:
                item.setCheckState(Qt.Checked)
            else:
                item.setCheckState(Qt.Unchecked)
            item.setText(m['name'])
            item.setData(Qt.UserRole, m['path'])
            self.listOfMods.addItem(item)

        self.listOfMods.sortItems(Qt.AscendingOrder)

    def get_available_mods(self):
        mods = []
        for m in os.listdir(self.game_mods_path):
            p = os.path.join(self.game_mods_path, m)
            if os.path.isfile(p):
                mod = Mod(p)
                info = mod.get_info()
                info['path'] = 'mod/' + m
                mods.append(info)

        return mods

    def on_save(self):
        """
        Save the changes. It can be either creating a new profile
        or updating an existing one.
{'fraction_non_alphanumeric': 0.10517378014264689, 'fraction_numerical': 0.013358994679044492, 'mean_word_length': 1.9203305785123967, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4398340', 'n_tokens_mistral': 2762, 'n_tokens_neox': 2592, 'n_words': 515}