123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- from PyQt4 import QtGui, QtCore
- from groupedelements import Elements, MenuItems
- import kcgwidget as kcgw
- from .. import config
- class RightSwitch(kcgw.ClickableSVG):
- """
- Buttons to change the Page in a MultiPage
- """
- def __init__(self, pagesWidget, width=10, height=20, wwidth=None, hidden=False):
- """
- Initialise a RightSwitch
- :param pagesWidget: (MultiPage) The MultiPage widget to switch pages upon press
- :param width: (int) width of the icon shown on the switch
- :param height: (int) height of the icon shown on the switch
- :param wwidth: (int) Width of the switch
- :param hidden: (bool) whether this switch is shown or hidden
- :return: -
- """
- super(RightSwitch, self).__init__(config.install_path+"icons/chevron-right.svg", width, height, wwidth)
- self.setObjectName("right_switch")
- if hidden:
- self.hide()
- self.clicked.connect(lambda: pagesWidget.setCurrentIndex(pagesWidget.currentIndex()+1))
- class LeftSwitch(kcgw.ClickableSVG):
- """
- Buttons to change the Page in a MultiPage Widget
- """
- def __init__(self, pagesWidget, width=10, height=20, wwidth=None, hidden=False):
- """
- Initialise a LeftSwitch
- :param pagesWidget: (MultiPage) The MultiPage widget to switch pages upon press
- :param width: (int) width of the icon shown on the switch
- :param height: (int) height of the icon shown on the switch
- :param wwidth: (int) Width of the switch
- :param hidden: (bool) whether this switch is shown or hidden
- :return: -
- """
- super(LeftSwitch, self).__init__(config.install_path+"icons/chevron-left.svg", width, height, wwidth)
- self.setObjectName("left_switch")
- if hidden:
- self.hide()
- self.clicked.connect(lambda: pagesWidget.setCurrentIndex(pagesWidget.currentIndex()-1))
- class LeftRightSwitch(QtGui.QWidget):
- """
- Small Buttons to change the Page in a MultiPage Widget
- """
- def __init__(self, pagesWidget):
- """
- Initialise a combination of left and right switch
- :param pagesWidget: (MultiPage) The multipage widget instance to change pages on
- """
- super(LeftRightSwitch, self).__init__()
- self.right = RightSwitch(pagesWidget, width=10, height=10)
- self.right.setObjectName("leftright")
- self.left = LeftSwitch(pagesWidget, width=10, height=10)
- self.left.setObjectName("leftright")
- self.layout = QtGui.QHBoxLayout()
- self.setLayout(self.layout)
- self.layout.addWidget(self.left)
- self.layout.addWidget(self.right)
- def disable_left(self):
- """
- Disable the switch to the page left of the current one
- :return: -
- """
- # self.left.setStyleSheet("border-radius: 4px; background-color: lightgrey;")
- self.left.setStyleSheet("#leftright:hover { background-color: none;}")
- self.left.changeSvg(config.install_path+"icons/chevron-left-grey.svg")
- def enable_left(self):
- """
- Enable the switch to the page left of the current one
- :return: -
- """
- self.left.setStyleSheet("")
- self.left.changeSvg(config.install_path+"icons/chevron-left.svg")
- def disable_right(self):
- """
- Disable the switch to the page right of the current one
- :return: -
- """
- # self.right.setStyleSheet("border-radius: 4px; background-color: lightgrey;")
- self.right.setStyleSheet("#leftright:hover { background-color: none;}")
- self.right.changeSvg(config.install_path+"icons/chevron-right-grey.svg")
- def enable_right(self):
- """
- Enable the switch to the page right of the current one
- :return: -
- """
- self.right.setStyleSheet("")
- self.right.changeSvg(config.install_path+"icons/chevron-right.svg")
- class MultiPage(QtGui.QStackedWidget):
- """
- Implementation of a Paginated View Widget
- """
- def __init__(self, parent=None):
- super(MultiPage, self).__init__(parent)
- self.pages = []
- self.numOfPages = -1
- self.leftright = LeftRightSwitch(self)
- self.leftright.hide()
- self.setCurrentIndex(0)
- self.leftShortcut = QtGui.QShortcut(QtGui.QKeySequence("Ctrl+Left"), self, self.left)
- self.rightShortcut = QtGui.QShortcut(QtGui.QKeySequence("Ctrl+Right"), self, self.right)
- self.lSwitches = []
- self.rSwitches = []
- def addPage(self, NewPage, name=None, set_to_first=True):
- """
- Add a page (a Widget) to the MultiPage Widget
- :param NewPage: widget to add as new page
- :param name: name of that page (e.g. to show in the status bar)
- :param bool set_to_first: Set the current page to first page
- :return: -
- """
- self.leftright.show()
- self.numOfPages += 1
- self.pages.append(QtGui.QWidget())
- self.pages[-1].widget = NewPage
- self.rSwitches.append(RightSwitch(self, wwidth=20, hidden=True))
- self.lSwitches.append(LeftSwitch(self, wwidth=20, hidden=True))
- NewPage.index = len(self.pages)-1
- self.pages[-1].name = name
- self.pages[-1].layout = QtGui.QHBoxLayout()
- self.pages[-1].setLayout(self.pages[-1].layout)
- if len(self.pages) == 1:
- self.pages[-1].layout.addWidget(NewPage)
- if len(self.pages) > 1:
- self.rSwitches[-2].show()
- self.pages[-2].layout.addWidget(self.rSwitches[-2])
- self.lSwitches[-1].show()
- self.pages[-1].layout.addWidget(self.lSwitches[-1])
- self.pages[-1].layout.addWidget(NewPage)
- self.addWidget(self.pages[-1])
- if set_to_first:
- self.setCurrentIndex(0)
- else:
- name = self.pages[self.currentIndex()].name+" " if self.pages[self.currentIndex()].name else " "
- self.window().pageIndicator.setText(name+"| "+str(self.currentIndex()+1)+"/"+str(self.numOfPages+1))
- def removePage(self, page):
- """
- Removes a page from the pages widget and adjusts switches accordingly
- :param page: what page to remove
- :return: -
- """
- if self.numOfPages == 1:
- raise IndexError("Not enough pages left")
- self.numOfPages -=1
- idx = page.index
- self.removeWidget(self.pages[idx])
- del self.pages[idx]
- del self.lSwitches[idx]
- del self.rSwitches[idx]
- self.pages[-1].layout.removeWidget(self.rSwitches[-1])
- self.rSwitches[-1].hide()
- name = self.pages[self.currentIndex()].name+" " if self.pages[self.currentIndex()].name else " "
- self.window().pageIndicator.setText(name+"| "+str(self.currentIndex()+1)+"/"+str(self.numOfPages+1))
- def left(self):
- self.setCurrentIndex(self.currentIndex()-1)
- def right(self):
- self.setCurrentIndex(self.currentIndex()+1)
- def setCurrentIndex(self, p_int):
- """
- Set te current Index of the MultiPage Widget (e.g. set the current page)
- :param p_int: (int) what page
- :return: -
- """
- if self.currentIndex() >= 0 and p_int <= self.numOfPages:
- MenuItems.setEnabled(self.pages[self.currentIndex()].name.strip(), False)
- if p_int > self.numOfPages or p_int < 0:
- return
- if p_int <= 0:
- self.leftright.disable_left()
- else:
- self.leftright.enable_left()
- if p_int >= self.numOfPages:
- self.leftright.disable_right()
- else:
- self.leftright.enable_right()
- name = self.pages[p_int].name+" " if self.pages[p_int].name else " "
- self.window().pageIndicator.setText(name+"| "+str(p_int+1)+"/"+str(self.numOfPages+1))
- super(MultiPage, self).setCurrentIndex(p_int)
- MenuItems.setEnabled(name.strip(), True)
- if getattr(self.pages[self.currentIndex()].widget, 'pages_update_function', None) is not None:
- self.pages[self.currentIndex()].widget.pages_update_function()
|