Exemple #1
0
    def _start(self, is_new=False):
        """
            starts or opens a project depending on
            [is_new] parameter
        """

        if not is_new:
            dir_name = str(QtGui.QFileDialog.getExistingDirectory(self, 'Open Project'))
        else:
            dir_name = str(QtGui.QFileDialog.getSaveFileName(self, 'Start Project'))

        if not dir_name:
            return

        try:
            self.current_project = GUIProject(dir_name)
            self.current_project.set_up(self, is_new)

            self._disable_enable_project_buttons(True)

            if is_new:
                self.configure()

        except InvalidProjectError, e:

            print "%s" % e

            self._disable_enable_project_buttons(False)
Exemple #2
0
class BrowserTab(BaseBrowserTab):
    """
        A Browser Tab representation

        This class overrides all the methods of the
        base class.
    """

    def __init__(self, parent):

        BaseBrowserTab.__init__(self, parent)
        self.url = None
        self.crawler = OffLineCrawler()

    def load_bar(self, value):
        """
            Load the progress bar
        """

        self.pg_load.setValue(value)

    def loaded_bar(self, state):
        """
            Triggered when the bar finish the loading
        """

        self.pg_load.hide()
        index = self.parent.tab_pages.indexOf(self)
        self.parent.tab_pages.setTabText(index, self.html.title())
        self.parent.tab_pages.setTabIcon(index, QtWebKit.QWebSettings.iconForUrl(QtCore.QUrl(self.url)))

    def load_start(self):
        """
            Show the progress bar
        """

        self.pg_load.show()

    def load_url(self, url, selected_nodes=None):
        """
            Load the requested url in the webwiew
        """

        self.url = str(url)
        html = self.crawler._get_response(self.url)

        with open(get_full_template_path("html_template"), "r") as f:
            template = f.read()
            html = template % {'content': html, 'css_class': SELECTED_CLASS }

        if selected_nodes is not None:
            html = self._highlight_nodes(html, selected_nodes)

        self.html.setHtml(html)
        self.html.show()

    def _highlight_nodes(self, html, nodes):
        """
            Highlights the nodes selected by the user in the current page
        """

        html_tree = XPathExtractor().get_object(html)

        for xpath in nodes:

            tags = html_tree.xpath(xpath)

            if tags:

                tag = tags[0]

                classes = tag.attrib.get("class", "")
                classes = "%s %s" % (classes, SELECTED_CLASS)
                tag.attrib["class"] = classes.strip()
                tag.attrib["id"] = xpath

        return etree.tostring(html_tree.getroot(), pretty_print=True, method="html")

    def url_changed(self, url):
        """
            Update the url text box
        """

        if self.is_current():
            self.parent.tb_url.setText(self.url)
        self.url = url.toString()

    def back(self):
        """
            Back to previous page
        """

        if self.is_current():
            self.html.back()

    def ahead(self):
        """
            Go to next page
        """

        if self.is_current():
            self.html.forward()

    def reload(self):
        """
            Reload page
        """

        if self.is_current():
            self.html.reload()

    def start(self):
        """
            Starts a new project
        """

        self._start(is_new=True)

    def open(self):
        """
            Opens an existing project
        """

        self._start()

    def _start(self, is_new=False):
        """
            starts or opens a project depending on
            [is_new] parameter
        """

        if not is_new:
            dir_name = str(QtGui.QFileDialog.getExistingDirectory(self, 'Open Project'))
        else:
            dir_name = str(QtGui.QFileDialog.getSaveFileName(self, 'Start Project'))

        if not dir_name:
            return

        try:
            self.current_project = GUIProject(dir_name)
            self.current_project.set_up(self, is_new)

            self._disable_enable_project_buttons(True)

            if is_new:
                self.configure()

        except InvalidProjectError, e:

            print "%s" % e

            self._disable_enable_project_buttons(False)