'Get the previous/next QModelIndex displayed in the view.
Used by tab_handler.
Args:
upwards: Get previous item, not next.
Return:
A QModelIndex.'

def _next_idx(self, upwards):

    idx = self.selectionModel().currentIndex()
    if (not idx.isValid()):
        if upwards:
            return self.model().last_item()
        else:
            return self.model().first_item()
    while True:
        idx = (self.indexAbove(idx) if upwards else self.indexBelow(idx))
        if ((not idx.isValid()) and upwards):
            return self.model().last_item()
        elif ((not idx.isValid()) and (not upwards)):
            idx = self.model().first_item()
            self.scrollTo(idx.parent())
            return idx
        elif idx.parent().isValid():
            return idx
