qcombobox.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # The code below, as well as the associated test were adapted from
  2. # qt-helpers, which was released under a 3-Clause BSD license:
  3. #
  4. # Copyright (c) 2015, Chris Beaumont and Thomas Robitaille
  5. #
  6. # All rights reserved.
  7. #
  8. # Redistribution and use in source and binary forms, with or without
  9. # modification, are permitted provided that the following conditions are
  10. # met:
  11. #
  12. # * Redistributions of source code must retain the above copyright
  13. # notice, this list of conditions and the following disclaimer.
  14. # * Redistributions in binary form must reproduce the above copyright
  15. # notice, this list of conditions and the following disclaimer in the
  16. # documentation and/or other materials provided with the
  17. # distribution.
  18. # * Neither the name of the Glue project nor the names of its
  19. # contributors may be used to endorse or promote products derived
  20. # from this software without specific prior written permission.
  21. #
  22. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  23. # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  24. # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  25. # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  26. # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27. # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28. # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  29. # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  30. # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  31. # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. def patch_qcombobox(QComboBox):
  34. """
  35. In PySide, using Python objects as userData in QComboBox causes
  36. Segmentation faults under certain conditions. Even in cases where it
  37. doesn't, findData does not work correctly. Likewise, findData also does not
  38. work correctly with Python objects when using PyQt4. On the other hand,
  39. PyQt5 deals with this case correctly. We therefore patch QComboBox when
  40. using PyQt4 and PySide to avoid issues.
  41. """
  42. from ..QtGui import QIcon
  43. from ..QtCore import Qt, QObject
  44. class userDataWrapper():
  45. """
  46. This class is used to wrap any userData object. If we don't do this,
  47. then certain types of objects can cause segmentation faults or issues
  48. depending on whether/how __getitem__ is defined.
  49. """
  50. def __init__(self, data):
  51. self.data = data
  52. _addItem = QComboBox.addItem
  53. def addItem(self, *args, **kwargs):
  54. if len(args) == 3 or (not isinstance(args[0], QIcon)
  55. and len(args) == 2):
  56. args, kwargs['userData'] = args[:-1], args[-1]
  57. if 'userData' in kwargs:
  58. kwargs['userData'] = userDataWrapper(kwargs['userData'])
  59. _addItem(self, *args, **kwargs)
  60. _insertItem = QComboBox.insertItem
  61. def insertItem(self, *args, **kwargs):
  62. if len(args) == 4 or (not isinstance(args[1], QIcon)
  63. and len(args) == 3):
  64. args, kwargs['userData'] = args[:-1], args[-1]
  65. if 'userData' in kwargs:
  66. kwargs['userData'] = userDataWrapper(kwargs['userData'])
  67. _insertItem(self, *args, **kwargs)
  68. _setItemData = QComboBox.setItemData
  69. def setItemData(self, index, value, role=Qt.UserRole):
  70. value = userDataWrapper(value)
  71. _setItemData(self, index, value, role=role)
  72. _itemData = QComboBox.itemData
  73. def itemData(self, index, role=Qt.UserRole):
  74. userData = _itemData(self, index, role=role)
  75. if isinstance(userData, userDataWrapper):
  76. userData = userData.data
  77. return userData
  78. def findData(self, value):
  79. for i in range(self.count()):
  80. if self.itemData(i) == value:
  81. return i
  82. return -1
  83. QComboBox.addItem = addItem
  84. QComboBox.insertItem = insertItem
  85. QComboBox.setItemData = setItemData
  86. QComboBox.itemData = itemData
  87. QComboBox.findData = findData