multipage.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. from PyQt4 import QtGui, QtCore
  2. from groupedelements import Elements, MenuItems
  3. import kcgwidget as kcgw
  4. from .. import config
  5. class RightSwitch(kcgw.ClickableSVG):
  6. """
  7. Buttons to change the Page in a MultiPage
  8. """
  9. def __init__(self, pagesWidget, width=10, height=20, wwidth=None, hidden=False):
  10. """
  11. Initialise a RightSwitch
  12. :param pagesWidget: (MultiPage) The MultiPage widget to switch pages upon press
  13. :param width: (int) width of the icon shown on the switch
  14. :param height: (int) height of the icon shown on the switch
  15. :param wwidth: (int) Width of the switch
  16. :param hidden: (bool) whether this switch is shown or hidden
  17. :return: -
  18. """
  19. super(RightSwitch, self).__init__(config.install_path+"icons/chevron-right.svg", width, height, wwidth)
  20. self.setObjectName("right_switch")
  21. if hidden:
  22. self.hide()
  23. self.clicked.connect(lambda: pagesWidget.setCurrentIndex(pagesWidget.currentIndex()+1))
  24. class LeftSwitch(kcgw.ClickableSVG):
  25. """
  26. Buttons to change the Page in a MultiPage Widget
  27. """
  28. def __init__(self, pagesWidget, width=10, height=20, wwidth=None, hidden=False):
  29. """
  30. Initialise a LeftSwitch
  31. :param pagesWidget: (MultiPage) The MultiPage widget to switch pages upon press
  32. :param width: (int) width of the icon shown on the switch
  33. :param height: (int) height of the icon shown on the switch
  34. :param wwidth: (int) Width of the switch
  35. :param hidden: (bool) whether this switch is shown or hidden
  36. :return: -
  37. """
  38. super(LeftSwitch, self).__init__(config.install_path+"icons/chevron-left.svg", width, height, wwidth)
  39. self.setObjectName("left_switch")
  40. if hidden:
  41. self.hide()
  42. self.clicked.connect(lambda: pagesWidget.setCurrentIndex(pagesWidget.currentIndex()-1))
  43. class LeftRightSwitch(QtGui.QWidget):
  44. """
  45. Small Buttons to change the Page in a MultiPage Widget
  46. """
  47. def __init__(self, pagesWidget):
  48. """
  49. Initialise a combination of left and right switch
  50. :param pagesWidget: (MultiPage) The multipage widget instance to change pages on
  51. """
  52. super(LeftRightSwitch, self).__init__()
  53. self.right = RightSwitch(pagesWidget, width=10, height=10)
  54. self.right.setObjectName("leftright")
  55. self.left = LeftSwitch(pagesWidget, width=10, height=10)
  56. self.left.setObjectName("leftright")
  57. self.layout = QtGui.QHBoxLayout()
  58. self.setLayout(self.layout)
  59. self.layout.addWidget(self.left)
  60. self.layout.addWidget(self.right)
  61. def disable_left(self):
  62. """
  63. Disable the switch to the page left of the current one
  64. :return: -
  65. """
  66. # self.left.setStyleSheet("border-radius: 4px; background-color: lightgrey;")
  67. self.left.setStyleSheet("#leftright:hover { background-color: none;}")
  68. self.left.changeSvg(config.install_path+"icons/chevron-left-grey.svg")
  69. def enable_left(self):
  70. """
  71. Enable the switch to the page left of the current one
  72. :return: -
  73. """
  74. self.left.setStyleSheet("")
  75. self.left.changeSvg(config.install_path+"icons/chevron-left.svg")
  76. def disable_right(self):
  77. """
  78. Disable the switch to the page right of the current one
  79. :return: -
  80. """
  81. # self.right.setStyleSheet("border-radius: 4px; background-color: lightgrey;")
  82. self.right.setStyleSheet("#leftright:hover { background-color: none;}")
  83. self.right.changeSvg(config.install_path+"icons/chevron-right-grey.svg")
  84. def enable_right(self):
  85. """
  86. Enable the switch to the page right of the current one
  87. :return: -
  88. """
  89. self.right.setStyleSheet("")
  90. self.right.changeSvg(config.install_path+"icons/chevron-right.svg")
  91. class MultiPage(QtGui.QStackedWidget):
  92. """
  93. Implementation of a Paginated View Widget
  94. """
  95. def __init__(self, parent=None):
  96. super(MultiPage, self).__init__(parent)
  97. self.pages = []
  98. self.numOfPages = -1
  99. self.leftright = LeftRightSwitch(self)
  100. self.leftright.hide()
  101. self.setCurrentIndex(0)
  102. self.leftShortcut = QtGui.QShortcut(QtGui.QKeySequence("Ctrl+Left"), self, self.left)
  103. self.rightShortcut = QtGui.QShortcut(QtGui.QKeySequence("Ctrl+Right"), self, self.right)
  104. self.lSwitches = []
  105. self.rSwitches = []
  106. def addPage(self, NewPage, name=None, set_to_first=True):
  107. """
  108. Add a page (a Widget) to the MultiPage Widget
  109. :param NewPage: widget to add as new page
  110. :param name: name of that page (e.g. to show in the status bar)
  111. :param bool set_to_first: Set the current page to first page
  112. :return: -
  113. """
  114. self.leftright.show()
  115. self.numOfPages += 1
  116. self.pages.append(QtGui.QWidget())
  117. self.pages[-1].widget = NewPage
  118. self.rSwitches.append(RightSwitch(self, wwidth=20, hidden=True))
  119. self.lSwitches.append(LeftSwitch(self, wwidth=20, hidden=True))
  120. NewPage.index = len(self.pages)-1
  121. self.pages[-1].name = name
  122. self.pages[-1].layout = QtGui.QHBoxLayout()
  123. self.pages[-1].setLayout(self.pages[-1].layout)
  124. if len(self.pages) == 1:
  125. self.pages[-1].layout.addWidget(NewPage)
  126. if len(self.pages) > 1:
  127. self.rSwitches[-2].show()
  128. self.pages[-2].layout.addWidget(self.rSwitches[-2])
  129. self.lSwitches[-1].show()
  130. self.pages[-1].layout.addWidget(self.lSwitches[-1])
  131. self.pages[-1].layout.addWidget(NewPage)
  132. self.addWidget(self.pages[-1])
  133. if set_to_first:
  134. self.setCurrentIndex(0)
  135. else:
  136. name = self.pages[self.currentIndex()].name+" " if self.pages[self.currentIndex()].name else " "
  137. self.window().pageIndicator.setText(name+"| "+str(self.currentIndex()+1)+"/"+str(self.numOfPages+1))
  138. def removePage(self, page):
  139. """
  140. Removes a page from the pages widget and adjusts switches accordingly
  141. :param page: what page to remove
  142. :return: -
  143. """
  144. if self.numOfPages == 1:
  145. raise IndexError("Not enough pages left")
  146. self.numOfPages -=1
  147. idx = page.index
  148. self.removeWidget(self.pages[idx])
  149. del self.pages[idx]
  150. del self.lSwitches[idx]
  151. del self.rSwitches[idx]
  152. self.pages[-1].layout.removeWidget(self.rSwitches[-1])
  153. self.rSwitches[-1].hide()
  154. name = self.pages[self.currentIndex()].name+" " if self.pages[self.currentIndex()].name else " "
  155. self.window().pageIndicator.setText(name+"| "+str(self.currentIndex()+1)+"/"+str(self.numOfPages+1))
  156. def left(self):
  157. self.setCurrentIndex(self.currentIndex()-1)
  158. def right(self):
  159. self.setCurrentIndex(self.currentIndex()+1)
  160. def setCurrentIndex(self, p_int):
  161. """
  162. Set te current Index of the MultiPage Widget (e.g. set the current page)
  163. :param p_int: (int) what page
  164. :return: -
  165. """
  166. if self.currentIndex() >= 0 and p_int <= self.numOfPages:
  167. MenuItems.setEnabled(self.pages[self.currentIndex()].name.strip(), False)
  168. if p_int > self.numOfPages or p_int < 0:
  169. return
  170. if p_int <= 0:
  171. self.leftright.disable_left()
  172. else:
  173. self.leftright.enable_left()
  174. if p_int >= self.numOfPages:
  175. self.leftright.disable_right()
  176. else:
  177. self.leftright.enable_right()
  178. name = self.pages[p_int].name+" " if self.pages[p_int].name else " "
  179. self.window().pageIndicator.setText(name+"| "+str(p_int+1)+"/"+str(self.numOfPages+1))
  180. super(MultiPage, self).setCurrentIndex(p_int)
  181. MenuItems.setEnabled(name.strip(), True)
  182. if getattr(self.pages[self.currentIndex()].widget, 'pages_update_function', None) is not None:
  183. self.pages[self.currentIndex()].widget.pages_update_function()