buttons.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. ########################################################################
  2. # Searx-qt - Lightweight desktop application for SearX.
  3. # Copyright (C) 2020 CYBERDEViL
  4. #
  5. # This file is part of Searx-qt.
  6. #
  7. # Searx-qt is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # Searx-qt is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. #
  20. ########################################################################
  21. from PyQt5.QtWidgets import (
  22. QToolButton,
  23. QFrame,
  24. QHBoxLayout,
  25. QLabel,
  26. QMenu,
  27. QWidgetAction,
  28. QCheckBox
  29. )
  30. from PyQt5.QtCore import Qt
  31. from PyQt5.QtGui import QCursor
  32. from searxqt.translations import _
  33. class Button(QToolButton):
  34. """ QToolButton with option to set text at creation.
  35. """
  36. def __init__(self, text="", parent=None):
  37. QToolButton.__init__(self, parent=parent)
  38. self.setText(text)
  39. class CheckboxOptionsButton(QFrame):
  40. def __init__(self, labelName="", parent=None):
  41. QFrame.__init__(self, parent=parent)
  42. # Keep a list of disabled keys set with setKeyDisabled()
  43. self.__disabledKeys = []
  44. layout = QHBoxLayout(self)
  45. layout.setContentsMargins(3, 3, 3, 3)
  46. self._labelName = labelName
  47. self.setFrameShape(QFrame.StyledPanel)
  48. openButton = Button("+", self)
  49. layout.addWidget(openButton, 0, Qt.AlignLeft | Qt.AlignTop)
  50. self._label = QLabel("", self)
  51. self._label.setWordWrap(True)
  52. layout.addWidget(self._label, 0, Qt.AlignLeft | Qt.AlignTop)
  53. openButton.clicked.connect(self.__toggleMenu)
  54. self.__genLabel()
  55. def keyDisabled(self, key):
  56. """
  57. @return: Returns True if the given key is disabled. Flase if
  58. enabled.
  59. @rtype: bool
  60. """
  61. return bool(key in self.__disabledKeys)
  62. def setKeyDisabled(self, key):
  63. """ Disable the given key from being toggled.
  64. """
  65. self.__disabledKeys.append(key)
  66. def setKeyEnabled(self, key):
  67. """ Re-enable the given key so the user may toggle it again.
  68. """
  69. self.__disabledKeys.remove(key)
  70. def reGenerate(self):
  71. self.__genLabel()
  72. @property
  73. def labelName(self):
  74. return self._labelName
  75. @labelName.setter
  76. def labelName(self, name):
  77. self._labelName = name
  78. def __genLabel(self):
  79. str_ = _("All")
  80. checkedOptions = self.getCheckedOptionNames()
  81. if checkedOptions:
  82. str_ = ", ".join(checkedOptions)
  83. self._label.setText("<b>" + self._labelName + "</b>: " + str_)
  84. self._label.setToolTip(str_)
  85. def __addWidgets(self, menu):
  86. for key, name, state in self.getOptions():
  87. action = QWidgetAction(menu)
  88. widget = QCheckBox(name, menu)
  89. widget.setTristate(False)
  90. widget.setChecked(state)
  91. action.setDefaultWidget(widget)
  92. if key in self.__disabledKeys:
  93. widget.setEnabled(False)
  94. widget.stateChanged.connect(
  95. lambda state, key=key:
  96. self.__checkBoxStateChanged(key, state)
  97. )
  98. menu.addAction(action)
  99. def __toggleMenu(self):
  100. menu = QMenu(self)
  101. menu.setStyleSheet("QMenu { menu-scrollable: 1; }")
  102. self.addCustomWidgetsTop(menu)
  103. self.__addWidgets(menu)
  104. menu.exec(QCursor.pos())
  105. def __checkBoxStateChanged(self, key, state):
  106. self.optionToggled(key, state == 2)
  107. self.__genLabel()
  108. """ Methods below may be reimplemented.
  109. """
  110. def addCustomWidgetsTop(self, menu):
  111. pass
  112. """ Methods below should be reimplemented.
  113. """
  114. def getCheckedOptionNames(self):
  115. """ Should return a list with checked option names. This will
  116. be used to generate the label.
  117. @return: should return a list with strings.
  118. @rtype: list
  119. """
  120. # Re-implement this!
  121. return []
  122. def getOptions(self):
  123. """ Should return a list with options tuple(key, name, state)
  124. This will be used to generate the options.
  125. """
  126. # Re-implement this!
  127. return []
  128. def optionToggled(self, key, state):
  129. # Re-implement to do stuff here.
  130. return