<commit_before>"""
Created on 20 févr. 2015

@author: inso
"""

import logging
import asyncio

from PyQt5.QtGui import QCursor, QDesktopServices
from PyQt5.QtWidgets import QWidget, QMenu, QAction
from PyQt5.QtCore import Qt, QModelIndex, pyqtSlot, QUrl, QEvent
from ..models.network import NetworkTableModel, NetworkFilterProxyModel
from ucoinpy.api import bma
from ..gen_resources.network_tab_uic import Ui_NetworkTabWidget


class NetworkTabWidget(QWidget, Ui_NetworkTabWidget):
    """
    classdocs
    """

    def __init__(self, app):
        """
        Constructore of a network tab.

        :param sakia.core.Application app: The application
        :return: A new network tab.
        :rtype: NetworkTabWidget
        """
        super().__init__()
        self.app = app
        self.community = None

        self.setupUi(self)
        model = NetworkTableModel(self.community)
        proxy = NetworkFilterProxyModel()
        proxy.setSourceModel(model)
        self.table_network.setModel(proxy)
        self.table_network.sortByColumn(2, Qt.DescendingOrder)
        self.table_network.resizeColumnsToContents()
        model.modelAboutToBeReset.connect(lambda: self.table_network.setEnabled(False))
        model.modelReset.connect(lambda: self.table_network.setEnabled(True))

    def change_community(self, community):
        if self.community:
            self.community.network.nodes_changed.disconnect(self.refresh_nodes)
        if community:
            community.network.nodes_changed.connect(self.refresh_nodes)

        self.community = community
        self.table_network.model().change_community(community)

    @pyqtSlot()
    def refresh_nodes(self):
        logging.debug("Refresh nodes")
        self.table_network.model().sourceModel().refresh_nodes()

    def node_context_menu(self, point):
        index = self.table_network.indexAt(point)
        model = self.table_network.model()
        if index.isValid() and index.row() < model.rowCount(QModelIndex()):
            source_index = model.mapToSource(index)
            is_root_col = model.sourceModel().columns_types.index('is_root')
            is_root_index = model.sourceModel().index(source_index.row(), is_root_col)
            is_root = model.sourceModel().data(is_root_index, Qt.DisplayRole)

            menu = QMenu()
            if is_root:
                unset_root = QAction(self.tr("Unset root node"), self)
                unset_root.triggered.connect(self.unset_root_node)
                unset_root.setData(self.community.network.root_node_index(source_index.row()))
                if len(self.community.network.root_nodes) > 1:
                    menu.addAction(unset_root)
            else:
                set_root = QAction(self.tr("Set as root node"), self)
                set_root.triggered.connect(self.set_root_node)
                set_root.setData(self.community.network.nodes[source_index.row()])
                menu.addAction(set_root)

            if self.app.preferences['expert_mode']:
                open_in_browser = QAction(self.tr("Open in browser"), self)
                open_in_browser.triggered.connect(self.open_node_in_browser)
                open_in_browser.setData(self.community.network.nodes[source_index.row()])
                menu.addAction(open_in_browser)

            # Show the context menu.
            menu.exec_(QCursor.pos())

    @pyqtSlot()
    def set_root_node(self):
        node = self.sender().data()
        self.community.network.add_root_node(node)
        self.table_network.model().sourceModel().refresh_nodes()

    @pyqtSlot()
    def unset_root_node(self):
        index = self.sender().data()
        self.community.network.remove_root_node(index)
        self.table_network.model().sourceModel().refresh_nodes()

    @pyqtSlot()
    def open_node_in_browser(self):
        node = self.sender().data()
        peering = bma.network.Peering(node.endpoint.conn_handler())
        url = QUrl(peering.reverse_url("/peering"))
        QDesktopServices.openUrl(url)

    def manual_nodes_refresh(self):
        self.community.network.refresh_once()
        self.button_manual_refresh.setEnabled(False)
        asyncio.get_event_loop().call_later(15, lambda: self.button_manual_refresh.setEnabled(True))

    def changeEvent(self, event):
        """
        Intercepte LanguageChange event to translate UI
        :param QEvent QEvent: Event
        :return:
        """
        if event.type() == QEvent.LanguageChange:
            self.retranslateUi(self)
            self.refresh_nodes()
        return super(NetworkTabWidget, self).changeEvent(event)
<commit_msg>Fix bug with open in browser action<commit_after>"""
Created on 20 févr. 2015

@author: inso
"""

import logging
import asyncio

from PyQt5.QtGui import QCursor, QDesktopServices
from PyQt5.QtWidgets import QWidget, QMenu, QAction
from PyQt5.QtCore import Qt, QModelIndex, pyqtSlot, QUrl, QEvent
from ..models.network import NetworkTableModel, NetworkFilterProxyModel
from ucoinpy.api import bma
from ..gen_resources.network_tab_uic import Ui_NetworkTabWidget


class NetworkTabWidget(QWidget, Ui_NetworkTabWidget):
    """
    classdocs
    """

    def __init__(self, app):
        """
        Constructore of a network tab.

        :param sakia.core.Application app: The application
        :return: A new network tab.
        :rtype: NetworkTabWidget
        """
        super().__init__()
        self.app = app
        self.community = None

        self.setupUi(self)
        model = NetworkTableModel(self.community)
        proxy = NetworkFilterProxyModel()
        proxy.setSourceModel(model)
        self.table_network.setModel(proxy)
        self.table_network.sortByColumn(2, Qt.DescendingOrder)
        self.table_network.resizeColumnsToContents()
        model.modelAboutToBeReset.connect(lambda: self.table_network.setEnabled(False))
        model.modelReset.connect(lambda: self.table_network.setEnabled(True))

    def change_community(self, community):
        if self.community:
            self.community.network.nodes_changed.disconnect(self.refresh_nodes)
        if community:
            community.network.nodes_changed.connect(self.refresh_nodes)

        self.community = community
        self.table_network.model().change_community(community)

    @pyqtSlot()
    def refresh_nodes(self):
        logging.debug("Refresh nodes")
        self.table_network.model().sourceModel().refresh_nodes()

    def node_context_menu(self, point):
        index = self.table_network.indexAt(point)
        model = self.table_network.model()
        if index.isValid() and index.row() < model.rowCount(QModelIndex()):
            source_index = model.mapToSource(index)
            is_root_col = model.sourceModel().columns_types.index('is_root')
            is_root_index = model.sourceModel().index(source_index.row(), is_root_col)
            is_root = model.sourceModel().data(is_root_index, Qt.DisplayRole)

            menu = QMenu()
            if is_root:
                unset_root = QAction(self.tr("Unset root node"), self)
                unset_root.triggered.connect(self.unset_root_node)
                unset_root.setData(self.community.network.root_node_index(source_index.row()))
                if len(self.community.network.root_nodes) > 1:
                    menu.addAction(unset_root)
            else:
                set_root = QAction(self.tr("Set as root node"), self)
                set_root.triggered.connect(self.set_root_node)
                set_root.setData(self.community.network.nodes[source_index.row()])
                menu.addAction(set_root)

            if self.app.preferences['expert_mode']:
                open_in_browser = QAction(self.tr("Open in browser"), self)
                open_in_browser.triggered.connect(self.open_node_in_browser)
                open_in_browser.setData(self.community.network.nodes[source_index.row()])
                menu.addAction(open_in_browser)

            # Show the context menu.
            menu.exec_(QCursor.pos())

    @pyqtSlot()
    def set_root_node(self):
        node = self.sender().data()
        self.community.network.add_root_node(node)
        self.table_network.model().sourceModel().refresh_nodes()

    @pyqtSlot()
    def unset_root_node(self):
        index = self.sender().data()
        self.community.network.remove_root_node(index)
        self.table_network.model().sourceModel().refresh_nodes()

    @pyqtSlot()
    def open_node_in_browser(self):
        node = self.sender().data()
        peering = bma.network.Peering(node.endpoint.conn_handler())
        url = QUrl(peering.reverse_url("http", "/peering"))
        QDesktopServices.openUrl(url)

    def manual_nodes_refresh(self):
        self.community.network.refresh_once()
        self.button_manual_refresh.setEnabled(False)
        asyncio.get_event_loop().call_later(15, lambda: self.button_manual_refresh.setEnabled(True))

    def changeEvent(self, event):
        """
        Intercepte LanguageChange event to translate UI
        :param QEvent QEvent: Event
        :return:
        """
        if event.type() == QEvent.LanguageChange:
            self.retranslateUi(self)
            self.refresh_nodes()
        return super(NetworkTabWidget, self).changeEvent(event)

{'fraction_non_alphanumeric': 0.10421596042159605, 'fraction_numerical': 0.002796300279630028, 'mean_word_length': 2.731540930979133, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5189664', 'n_tokens_mistral': 2776, 'n_tokens_neox': 2669, 'n_words': 447}