def __init__(self,
                 api_data=None,
                 preferences=None,
                 post_limit=None,
                 parent=None):
        """Initialize a new ThumbnailArea. api_data is a reference to a
        Danbooru object, while preferences is a reference to a
        KConfigXT instance."""

        super(DanbooruTabWidget, self).__init__(parent)
        loadUi(WIDGET_UI, self)

        self.preferences = preferences
        self.api_data = api_data
        self.__pages = list()
        self.__firstpage = True
        self.__current_index = 0
        self.post_limit = post_limit

        # Generate and set the two widgets

        self.fetchwidget = FetchWidget(
            limit=preferences.thumbnail_no,
            default_rating=preferences.max_allowed_rating,
            parent=self.parent())

        self.connectwidget = ConnectWidget(preferences.boards_list, self)
        self.messagewidget = KMessageWidget(self)

        # Ugly hacks to have the widgets in proper positions

        self.gridLayout.removeWidget(self.thumbnailTabWidget)
        spacer = self.gridLayout.itemAtPosition(1, 0)
        self.gridLayout.removeItem(spacer)
        self.gridLayout.removeWidget(self.nextPageButton)

        self.gridLayout.addWidget(self.messagewidget, 0, 0, 1, -1)
        self.gridLayout.addWidget(self.connectwidget, 1, 0, 1, -1)
        self.gridLayout.addWidget(self.fetchwidget, 2, 0, 1, -1)
        self.gridLayout.addWidget(self.thumbnailTabWidget, 3, 0, 1, 2)
        self.gridLayout.addItem(spacer, 4, 0, 1, 1)
        self.gridLayout.addWidget(self.nextPageButton, 5, 1, 1, 1)

        self.connectwidget.hide()
        self.fetchwidget.hide()
        self.messagewidget.hide()

        self.messagewidget.setMessageType(KMessageWidget.Error)

        KAcceleratorManager.setNoAccel(self.thumbnailTabWidget)
        self.nextPageButton.setDisabled(True)

        button_toggle = partial(self.nextPageButton.setDisabled, False)

        self.api_data.postDownloadFinished.connect(button_toggle)
        self.api_data.postDownloadFinished.connect(self.__check)
        self.nextPageButton.clicked.connect(self.update_search_results)
        self.api_data.downloadError.connect(self.display_error)

        self.new_page()
    def __init__(self, api_data=None, preferences=None, post_limit=None,
                 parent=None):

        """Initialize a new ThumbnailArea. api_data is a reference to a
        Danbooru object, while preferences is a reference to a
        KConfigXT instance."""

        super(DanbooruTabWidget, self).__init__(parent)
        loadUi(WIDGET_UI, self)

        self.preferences = preferences
        self.api_data = api_data
        self.__pages = list()
        self.__firstpage = True
        self.__current_index = 0
        self.post_limit = post_limit

        # Generate and set the two widgets

        self.fetchwidget = FetchWidget(
            limit=preferences.thumbnail_no,
            default_rating=preferences.max_allowed_rating,
            parent=self.parent()
        )

        self.connectwidget = ConnectWidget(preferences.boards_list, self)
        self.messagewidget = KMessageWidget(self)

        # Ugly hacks to have the widgets in proper positions

        self.gridLayout.removeWidget(self.thumbnailTabWidget)
        spacer = self.gridLayout.itemAtPosition(1,0)
        self.gridLayout.removeItem(spacer)
        self.gridLayout.removeWidget(self.nextPageButton)

        self.gridLayout.addWidget(self.messagewidget, 0, 0, 1, -1)
        self.gridLayout.addWidget(self.connectwidget, 1, 0, 1, -1)
        self.gridLayout.addWidget(self.fetchwidget, 2, 0, 1, -1)
        self.gridLayout.addWidget(self.thumbnailTabWidget, 3, 0, 1, 2)
        self.gridLayout.addItem(spacer, 4, 0, 1, 1)
        self.gridLayout.addWidget(self.nextPageButton, 5, 1, 1, 1)

        self.connectwidget.hide()
        self.fetchwidget.hide()
        self.messagewidget.hide()

        self.messagewidget.setMessageType(KMessageWidget.Error)

        KAcceleratorManager.setNoAccel(self.thumbnailTabWidget)
        self.nextPageButton.setDisabled(True)

        button_toggle = partial(self.nextPageButton.setDisabled, False)

        self.api_data.postDownloadFinished.connect(button_toggle)
        self.api_data.postDownloadFinished.connect(self.__check)
        self.nextPageButton.clicked.connect(self.update_search_results)
        self.api_data.downloadError.connect(self.display_error)

        self.new_page()
class DanbooruTabWidget(QWidget):
    """Class that provides an area where individual ThumbnailViews (from
    thumbnailview.py) can be placed in, using a tabbed interface. The class uses
    an internal list for each page added, to avoid garbage collection issues.
    Methods to create tabs are not called directly, but are instead slots called
    upon by signal.
    """
    def __init__(self,
                 api_data=None,
                 preferences=None,
                 post_limit=None,
                 parent=None):
        """Initialize a new ThumbnailArea. api_data is a reference to a
        Danbooru object, while preferences is a reference to a
        KConfigXT instance."""

        super(DanbooruTabWidget, self).__init__(parent)
        loadUi(WIDGET_UI, self)

        self.preferences = preferences
        self.api_data = api_data
        self.__pages = list()
        self.__firstpage = True
        self.__current_index = 0
        self.post_limit = post_limit

        # Generate and set the two widgets

        self.fetchwidget = FetchWidget(
            limit=preferences.thumbnail_no,
            default_rating=preferences.max_allowed_rating,
            parent=self.parent())

        self.connectwidget = ConnectWidget(preferences.boards_list, self)
        self.messagewidget = KMessageWidget(self)

        # Ugly hacks to have the widgets in proper positions

        self.gridLayout.removeWidget(self.thumbnailTabWidget)
        spacer = self.gridLayout.itemAtPosition(1, 0)
        self.gridLayout.removeItem(spacer)
        self.gridLayout.removeWidget(self.nextPageButton)

        self.gridLayout.addWidget(self.messagewidget, 0, 0, 1, -1)
        self.gridLayout.addWidget(self.connectwidget, 1, 0, 1, -1)
        self.gridLayout.addWidget(self.fetchwidget, 2, 0, 1, -1)
        self.gridLayout.addWidget(self.thumbnailTabWidget, 3, 0, 1, 2)
        self.gridLayout.addItem(spacer, 4, 0, 1, 1)
        self.gridLayout.addWidget(self.nextPageButton, 5, 1, 1, 1)

        self.connectwidget.hide()
        self.fetchwidget.hide()
        self.messagewidget.hide()

        self.messagewidget.setMessageType(KMessageWidget.Error)

        KAcceleratorManager.setNoAccel(self.thumbnailTabWidget)
        self.nextPageButton.setDisabled(True)

        button_toggle = partial(self.nextPageButton.setDisabled, False)

        self.api_data.postDownloadFinished.connect(button_toggle)
        self.api_data.postDownloadFinished.connect(self.__check)
        self.nextPageButton.clicked.connect(self.update_search_results)
        self.api_data.downloadError.connect(self.display_error)

        self.new_page()

    def __iter__(self):

        "Yields every stored page in the thumbnail area."

        for item in self.__pages:
            yield item

    def __check(self):
        """Check whether results were received or not."""

        current_index = self.thumbnailTabWidget.currentIndex()

        widget = self.thumbnailTabWidget.widget(current_index)

        if not widget:

            self.setUpdatesEnabled(False)
            self.thumbnailTabWidget.removeTab(current_index)
            label = QLabel(
                i18n("No matching posts found for this page."
                     "\nWere you looking for a tag in the sidebar?"))
            label.setAlignment(Qt.AlignCenter | Qt.AlignVCenter)
            text = i18n("Page %1 (empty)", current_index + 1)

            index = self.thumbnailTabWidget.addTab(label, text)
            self.thumbnailTabWidget.setCurrentIndex(index)
            self.nextPageButton.setDisabled(False)
            self.setUpdatesEnabled(True)

    def new_page(self):

        "Slot used to create a new page."

        if self.__firstpage:
            self.create_tab()
            self.__firstpage = False
        else:
            self.create_tab()
            self.nextPageButton.setDisabled(True)

    def create_tab(self):
        """Creates a new tab in the tab widget, and adds it to the internal
        lists. Returns the inserted widget and the index it was inserted in."""

        current_page = self.thumbnailTabWidget.currentIndex() + 1
        next_page = 1 if current_page == 0 else current_page + 1
        page_name = i18n("Page %1", next_page)

        view = thumbnailview.DanbooruPostView(self.api_data, self.preferences)

        # We add the item to a list to keep a reference of it around

        self.__pages.append(view)

        index = self.thumbnailTabWidget.addTab(view, page_name)
        self.thumbnailTabWidget.setCurrentIndex(index)
        self.__current_index = index + 1

    def clear(self):

        "Removes all pages in the widget."

        self.thumbnailTabWidget.clear()
        self.__pages = list()
        self.__firstpage = True
        self.__current_index = 0
        self.nextPageButton.setDisabled(True)
        self.new_page()

    def selected_images(self):

        "Returns a list of the selected images in all pages."

        images = list()

        for view in self:

            selected = view.selected_images()

            if selected:
                images.extend(selected)

        return images

    def update_search_results(self):
        """Update the search results using the same parameters as originally
        supplied."""

        self.nextPageButton.setDisabled(True)
        self.new_page()
        current_page = self.__current_index + 1

        self.api_data.get_post_list(limit=self.post_limit,
                                    tags=self.api_data.current_tags,
                                    page=current_page,
                                    blacklist=self.preferences.tag_blacklist,
                                    rating=self.preferences.max_allowed_rating)

    def display_error(self, error_string):
        """Display errors from the API."""

        self.messagewidget.setText(error_string)
        self.messagewidget.animatedShow()
class DanbooruTabWidget(QWidget):

    """Class that provides an area where individual ThumbnailViews (from
    thumbnailview.py) can be placed in, using a tabbed interface. The class uses
    an internal list for each page added, to avoid garbage collection issues.
    Methods to create tabs are not called directly, but are instead slots called
    upon by signal.
    """

    def __init__(self, api_data=None, preferences=None, post_limit=None,
                 parent=None):

        """Initialize a new ThumbnailArea. api_data is a reference to a
        Danbooru object, while preferences is a reference to a
        KConfigXT instance."""

        super(DanbooruTabWidget, self).__init__(parent)
        loadUi(WIDGET_UI, self)

        self.preferences = preferences
        self.api_data = api_data
        self.__pages = list()
        self.__firstpage = True
        self.__current_index = 0
        self.post_limit = post_limit

        # Generate and set the two widgets

        self.fetchwidget = FetchWidget(
            limit=preferences.thumbnail_no,
            default_rating=preferences.max_allowed_rating,
            parent=self.parent()
        )

        self.connectwidget = ConnectWidget(preferences.boards_list, self)
        self.messagewidget = KMessageWidget(self)

        # Ugly hacks to have the widgets in proper positions

        self.gridLayout.removeWidget(self.thumbnailTabWidget)
        spacer = self.gridLayout.itemAtPosition(1,0)
        self.gridLayout.removeItem(spacer)
        self.gridLayout.removeWidget(self.nextPageButton)

        self.gridLayout.addWidget(self.messagewidget, 0, 0, 1, -1)
        self.gridLayout.addWidget(self.connectwidget, 1, 0, 1, -1)
        self.gridLayout.addWidget(self.fetchwidget, 2, 0, 1, -1)
        self.gridLayout.addWidget(self.thumbnailTabWidget, 3, 0, 1, 2)
        self.gridLayout.addItem(spacer, 4, 0, 1, 1)
        self.gridLayout.addWidget(self.nextPageButton, 5, 1, 1, 1)

        self.connectwidget.hide()
        self.fetchwidget.hide()
        self.messagewidget.hide()

        self.messagewidget.setMessageType(KMessageWidget.Error)

        KAcceleratorManager.setNoAccel(self.thumbnailTabWidget)
        self.nextPageButton.setDisabled(True)

        button_toggle = partial(self.nextPageButton.setDisabled, False)

        self.api_data.postDownloadFinished.connect(button_toggle)
        self.api_data.postDownloadFinished.connect(self.__check)
        self.nextPageButton.clicked.connect(self.update_search_results)
        self.api_data.downloadError.connect(self.display_error)

        self.new_page()

    def __iter__(self):

        "Yields every stored page in the thumbnail area."

        for item in self.__pages:
            yield item

    def __check(self):

        """Check whether results were received or not."""

        current_index = self.thumbnailTabWidget.currentIndex()

        widget = self.thumbnailTabWidget.widget(current_index)

        if not widget:

            self.setUpdatesEnabled(False)
            self.thumbnailTabWidget.removeTab(current_index)
            label = QLabel(i18n("No matching posts found for this page."
                                "\nWere you looking for a tag in the sidebar?"))
            label.setAlignment(Qt.AlignCenter | Qt.AlignVCenter)
            text = i18n("Page %1 (empty)", current_index + 1)

            index = self.thumbnailTabWidget.addTab(label, text)
            self.thumbnailTabWidget.setCurrentIndex(index)
            self.nextPageButton.setDisabled(False)
            self.setUpdatesEnabled(True)

    def new_page(self):

        "Slot used to create a new page."

        if self.__firstpage:
            self.create_tab()
            self.__firstpage = False
        else:
            self.create_tab()
            self.nextPageButton.setDisabled(True)

    def create_tab(self):

        """Creates a new tab in the tab widget, and adds it to the internal
        lists. Returns the inserted widget and the index it was inserted in."""

        current_page = self.thumbnailTabWidget.currentIndex() + 1
        next_page = 1 if current_page == 0 else current_page + 1
        page_name = i18n("Page %1", next_page)

        view = thumbnailview.DanbooruPostView(self.api_data, self.preferences)

        # We add the item to a list to keep a reference of it around

        self.__pages.append(view)

        index = self.thumbnailTabWidget.addTab(view, page_name)
        self.thumbnailTabWidget.setCurrentIndex(index)
        self.__current_index = index + 1

    def clear(self):

        "Removes all pages in the widget."

        self.thumbnailTabWidget.clear()
        self.__pages = list()
        self.__firstpage = True
        self.__current_index = 0
        self.nextPageButton.setDisabled(True)
        self.new_page()

    def selected_images(self):

        "Returns a list of the selected images in all pages."

        images = list()

        for view in self:

            selected = view.selected_images()

            if selected:
                images.extend(selected)

        return images

    def update_search_results(self):

        """Update the search results using the same parameters as originally
        supplied."""

        self.nextPageButton.setDisabled(True)
        self.new_page()
        current_page = self.__current_index + 1

        self.api_data.get_post_list(limit=self.post_limit,
                                    tags=self.api_data.current_tags,
                                    page=current_page,
                                    blacklist=self.preferences.tag_blacklist,
                                    rating=self.preferences.max_allowed_rating)

    def display_error(self, error_string):

        """Display errors from the API."""

        self.messagewidget.setText(error_string)
        self.messagewidget.animatedShow()