<commit_before>"""
This module contains the main window implementation.
"""
import os
import platform
import sys
from pyqode.core.api import TextHelper
from pyqode.core.qt import QtCore
from pyqode.core.qt import QtWidgets
from pyqode.core import widgets
from pyqode.python.backend import server
from pyqode.python.code_edit import PyCodeEdit
from pyqode.python import modes
from .utils import get_interpreters
from .settings import Settings
from .ui.main_window_ui import Ui_MainWindow


class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        # Load our UI (made in Qt Designer)
        self.setupUi(self)
        self.dockWidget.hide()
        self.setup_recent_files_menu()
        self.setup_actions()
        self.setup_status_bar_widgets()
        self.on_current_tab_changed()

    def setup_status_bar_widgets(self):
        self.lbl_interpreter = QtWidgets.QLabel()
        self.lbl_filename = QtWidgets.QLabel()
        self.lbl_encoding = QtWidgets.QLabel()
        self.lbl_cursor_pos = QtWidgets.QLabel()
        self.statusbar.addPermanentWidget(self.lbl_filename, 200)
        self.statusbar.addPermanentWidget(self.lbl_interpreter, 100)
        self.statusbar.addPermanentWidget(self.lbl_encoding, 20)
        self.statusbar.addPermanentWidget(self.lbl_cursor_pos, 20)

    def setup_actions(self):
        """ Connects slots to signals """
        self.actionOpen.triggered.connect(self.on_open)
        self.actionNew.triggered.connect(self.on_new)
        self.actionSave.triggered.connect(self.tabWidget.save_current)
        self.actionSave_as.triggered.connect(self.on_save_as)
        self.actionClose_tab.triggered.connect(self.tabWidget.close)
        self.actionClose_other_tabs.triggered.connect(
            self.tabWidget.close_others)
        self.actionClose_all_tabs.triggered.connect(self.tabWidget.close_all)
        self.actionQuit.triggered.connect(
            QtWidgets.QApplication.instance().quit)
        self.tabWidget.currentChanged.connect(self.on_current_tab_changed)
        self.actionAbout.triggered.connect(self.on_about)
        self.actionRun.triggered.connect(self.on_run)
        self.interactiveConsole.process_finished.connect(
            self.on_process_finished)
        self.actionConfigure_run.triggered.connect(self.on_configure_run)

    def setup_recent_files_menu(self):
        """ Setup the recent files menu and manager """
        self.recent_files_manager = widgets.RecentFilesManager(
            'pyqode', 'qidle')
        self.menu_recents = widgets.MenuRecentFiles(
            self.menuFile, title='Recents',
            recent_files_manager=self.recent_files_manager)
        self.menu_recents.open_requested.connect(self.open_file)
        self.menuFile.insertMenu(self.actionSave, self.menu_recents)
        self.menuFile.insertSeparator(self.actionSave)

    def setup_menu_interpreters(self):
        mnu = QtWidgets.QMenu('Select Python interpreter', self.menuEdit)
        group = QtWidgets.QActionGroup(self)
        group.triggered.connect(self.on_interpreter_changed)
        for interpreter in get_interpreters():
            a = QtWidgets.QAction(mnu)
            a.setText(interpreter)
            a.setCheckable(True)
            if interpreter == Settings().interpreter:
                a.setChecked(True)
            group.addAction(a)
            mnu.addAction(a)
        self.menuEdit.addSeparator()
        self.menuEdit.addMenu(mnu)

    def closeEvent(self, QCloseEvent):
        """
        Delegates the close event to the tabWidget to be sure we do not quit
        the application while there are some still some unsaved tabs.
        """
        self.tabWidget.closeEvent(QCloseEvent)

    def setup_editor(self, editor):
        """
        Setup the python editor, run the server and connect a few signals.

        :param editor: editor to setup.
        """
        editor.cursorPositionChanged.connect(self.on_cursor_pos_changed)
        zip_path = os.path.join(os.getcwd(), 'libraries.zip')
        if not os.path.exists(zip_path):
            if platform.system().lower() == 'linux':
                zip_path = '/usr/share/qidle/libraries.zip'
        if hasattr(sys, "frozen"):
            server_path = os.path.join(os.getcwd(), 'server.py')
            editor.backend.start(server_path,
                                 interpreter=Settings().interpreter,
                                 args=['-s', zip_path])
        else:
            editor.backend.start(server.__file__,
                                 interpreter=Settings().interpreter,
                                 args=['-s', zip_path])
        m = editor.modes.get(modes.GoToAssignmentsMode)
        assert isinstance(m, modes.GoToAssignmentsMode)
        m.out_of_doc.connect(self.on_goto_out_of_doc)

    @QtCore.Slot(str)
    def open_file(self, path):
        """
        Creates a new GenericCodeEdit, opens the requested file and adds it
        to the tab widget.

        :param path: Path of the file to open<commit_msg>Do not use zip when running example from python3<commit_after>"""
This module contains the main window implementation.
"""
import os
import platform
import sys
from pyqode.core.api import TextHelper
from pyqode.core.qt import QtCore
from pyqode.core.qt import QtWidgets
from pyqode.core import widgets
from pyqode.python.backend import server
from pyqode.python.code_edit import PyCodeEdit
from pyqode.python import modes
from .utils import get_interpreters
from .settings import Settings
from .ui.main_window_ui import Ui_MainWindow


class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        # Load our UI (made in Qt Designer)
        self.setupUi(self)
        self.dockWidget.hide()
        self.setup_recent_files_menu()
        self.setup_actions()
        self.setup_status_bar_widgets()
        self.on_current_tab_changed()

    def setup_status_bar_widgets(self):
        self.lbl_interpreter = QtWidgets.QLabel()
        self.lbl_filename = QtWidgets.QLabel()
        self.lbl_encoding = QtWidgets.QLabel()
        self.lbl_cursor_pos = QtWidgets.QLabel()
        self.statusbar.addPermanentWidget(self.lbl_filename, 200)
        self.statusbar.addPermanentWidget(self.lbl_interpreter, 100)
        self.statusbar.addPermanentWidget(self.lbl_encoding, 20)
        self.statusbar.addPermanentWidget(self.lbl_cursor_pos, 20)

    def setup_actions(self):
        """ Connects slots to signals """
        self.actionOpen.triggered.connect(self.on_open)
        self.actionNew.triggered.connect(self.on_new)
        self.actionSave.triggered.connect(self.tabWidget.save_current)
        self.actionSave_as.triggered.connect(self.on_save_as)
        self.actionClose_tab.triggered.connect(self.tabWidget.close)
        self.actionClose_other_tabs.triggered.connect(
            self.tabWidget.close_others)
        self.actionClose_all_tabs.triggered.connect(self.tabWidget.close_all)
        self.actionQuit.triggered.connect(
            QtWidgets.QApplication.instance().quit)
        self.tabWidget.currentChanged.connect(self.on_current_tab_changed)
        self.actionAbout.triggered.connect(self.on_about)
        self.actionRun.triggered.connect(self.on_run)
        self.interactiveConsole.process_finished.connect(
            self.on_process_finished)
        self.actionConfigure_run.triggered.connect(self.on_configure_run)

    def setup_recent_files_menu(self):
        """ Setup the recent files menu and manager """
        self.recent_files_manager = widgets.RecentFilesManager(
            'pyqode', 'qidle')
        self.menu_recents = widgets.MenuRecentFiles(
            self.menuFile, title='Recents',
            recent_files_manager=self.recent_files_manager)
        self.menu_recents.open_requested.connect(self.open_file)
        self.menuFile.insertMenu(self.actionSave, self.menu_recents)
        self.menuFile.insertSeparator(self.actionSave)

    def setup_menu_interpreters(self):
        mnu = QtWidgets.QMenu('Select Python interpreter', self.menuEdit)
        group = QtWidgets.QActionGroup(self)
        group.triggered.connect(self.on_interpreter_changed)
        for interpreter in get_interpreters():
            a = QtWidgets.QAction(mnu)
            a.setText(interpreter)
            a.setCheckable(True)
            if interpreter == Settings().interpreter:
                a.setChecked(True)
            group.addAction(a)
            mnu.addAction(a)
        self.menuEdit.addSeparator()
        self.menuEdit.addMenu(mnu)

    def closeEvent(self, QCloseEvent):
        """
        Delegates the close event to the tabWidget to be sure we do not quit
        the application while there are some still some unsaved tabs.
        """
        self.tabWidget.closeEvent(QCloseEvent)

    def setup_editor(self, editor):
        """
        Setup the python editor, run the server and connect a few signals.

        :param editor: editor to setup.
        """
        editor.cursorPositionChanged.connect(self.on_cursor_pos_changed)
        zip_path = os.path.join(os.getcwd(), 'libraries.zip')
        if not os.path.exists(zip_path):
            if platform.system().lower() == 'linux':
                zip_path = '/usr/share/qidle/libraries.zip'
        if hasattr(sys, "frozen") or not '3' in Settings().interpreter:
            server_path = os.path.join(os.getcwd(), 'server.py')
            editor.backend.start(server_path,
                                 interpreter=Settings().interpreter,
                                 args=['-s', zip_path])
        else:
            editor.backend.start(server.__file__,
                                 interpreter=Settings().interpreter,
                                 args=['-s', ''])
        m = editor.modes.get(modes.GoToAssignmentsMode)
        assert isinstance(m, modes.GoToAssignmentsMode)
        m.out_of_doc.connect(self.on_goto_out_of_doc)

    @QtCore.Slot(str)
    def open_file(self, path):
        """
        Creates a new GenericCodeEdit, opens the requested file and adds it
        to the tab widget.

        :param path: Path of the file to open
{'fraction_non_alphanumeric': 0.09172061997253286, 'fraction_numerical': 0.002158132234647832, 'mean_word_length': 2.668585822238215, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5306453', 'n_tokens_mistral': 3035, 'n_tokens_neox': 2994, 'n_words': 549}