qpygui_qmap.sip 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // This is the SIP interface definition for the QMap mapped types specific to
  2. // QtGui.
  3. //
  4. // Copyright (c) 2026 Riverbank Computing Limited <info@riverbankcomputing.com>
  5. //
  6. // This file is part of PyQt6.
  7. //
  8. // This file may be used under the terms of the GNU General Public License
  9. // version 3.0 as published by the Free Software Foundation and appearing in
  10. // the file LICENSE included in the packaging of this file. Please review the
  11. // following information to ensure the GNU General Public License version 3.0
  12. // requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  13. //
  14. // If you do not wish to use this file under the terms of the GPL version 3.0
  15. // then you may purchase a commercial license. For more information contact
  16. // info@riverbankcomputing.com.
  17. //
  18. // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  19. // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20. %If (Qt_6_11_0 -)
  21. template<_TYPE_, quint32>
  22. %MappedType QMap<_TYPE_, quint32>
  23. /TypeHint="Dict[_TYPE_, int]", TypeHintValue="{}"/
  24. {
  25. %TypeHeaderCode
  26. #include <qfont.h>
  27. #include <qmap.h>
  28. %End
  29. %ConvertFromTypeCode
  30. PyObject *d = PyDict_New();
  31. if (!d)
  32. return 0;
  33. QMap<_TYPE_, quint32>::const_iterator it = sipCpp->constBegin();
  34. QMap<_TYPE_, quint32>::const_iterator end = sipCpp->constEnd();
  35. while (it != end)
  36. {
  37. _TYPE_ *k = new _TYPE_(it.key());
  38. PyObject *kobj = sipConvertFromNewType(k, sipType__TYPE_,
  39. sipTransferObj);
  40. if (!kobj)
  41. {
  42. delete k;
  43. Py_DECREF(d);
  44. return 0;
  45. }
  46. PyObject *vobj = PyLong_FromUnsignedLong(it.value());
  47. if (!vobj)
  48. {
  49. Py_DECREF(kobj);
  50. Py_DECREF(d);
  51. return 0;
  52. }
  53. int rc = PyDict_SetItem(d, kobj, vobj);
  54. Py_DECREF(vobj);
  55. Py_DECREF(kobj);
  56. if (rc < 0)
  57. {
  58. Py_DECREF(d);
  59. return 0;
  60. }
  61. ++it;
  62. }
  63. return d;
  64. %End
  65. %ConvertToTypeCode
  66. if (!sipIsErr)
  67. return PyDict_Check(sipPy);
  68. QMap<_TYPE_, quint32> *qm = new QMap<_TYPE_, quint32>;
  69. Py_ssize_t pos = 0;
  70. PyObject *kobj, *vobj;
  71. while (PyDict_Next(sipPy, &pos, &kobj, &vobj))
  72. {
  73. int kstate;
  74. _TYPE_ *k = reinterpret_cast<_TYPE_ *>(
  75. sipForceConvertToType(kobj, sipType__TYPE_, sipTransferObj,
  76. SIP_NOT_NONE, &kstate, sipIsErr));
  77. if (*sipIsErr)
  78. {
  79. PyErr_Format(PyExc_TypeError,
  80. "a dict key has type '%s' but '_TYPE_' is expected",
  81. sipPyTypeName(Py_TYPE(kobj)));
  82. delete qm;
  83. return 0;
  84. }
  85. quint32 v = sipLong_AsUnsignedInt(vobj);
  86. if (PyErr_Occurred())
  87. {
  88. if (PyErr_ExceptionMatches(PyExc_TypeError))
  89. PyErr_Format(PyExc_TypeError,
  90. "a dict value has type '%s' but 'int' is expected",
  91. sipPyTypeName(Py_TYPE(vobj)));
  92. delete qm;
  93. *sipIsErr = 1;
  94. return 0;
  95. }
  96. qm->insert(*k, v);
  97. sipReleaseType(k, sipType__TYPE_, kstate);
  98. }
  99. *sipCppPtr = qm;
  100. return sipGetState(sipTransferObj);
  101. %End
  102. };
  103. template<_TYPE_, float>
  104. %MappedType QMap<_TYPE_, float>
  105. /TypeHint="Dict[_TYPE_, float]", TypeHintValue="{}"/
  106. {
  107. %TypeHeaderCode
  108. #include <qfont.h>
  109. #include <qmap.h>
  110. %End
  111. %ConvertFromTypeCode
  112. PyObject *d = PyDict_New();
  113. if (!d)
  114. return 0;
  115. QMap<_TYPE_, float>::const_iterator it = sipCpp->constBegin();
  116. QMap<_TYPE_, float>::const_iterator end = sipCpp->constEnd();
  117. while (it != end)
  118. {
  119. _TYPE_ *k = new _TYPE_(it.key());
  120. PyObject *kobj = sipConvertFromNewType(k, sipType__TYPE_,
  121. sipTransferObj);
  122. if (!kobj)
  123. {
  124. delete k;
  125. Py_DECREF(d);
  126. return 0;
  127. }
  128. PyObject *vobj = PyFloat_FromDouble(it.value());
  129. if (!vobj)
  130. {
  131. Py_DECREF(kobj);
  132. Py_DECREF(d);
  133. return 0;
  134. }
  135. int rc = PyDict_SetItem(d, kobj, vobj);
  136. Py_DECREF(vobj);
  137. Py_DECREF(kobj);
  138. if (rc < 0)
  139. {
  140. Py_DECREF(d);
  141. return 0;
  142. }
  143. ++it;
  144. }
  145. return d;
  146. %End
  147. %ConvertToTypeCode
  148. if (!sipIsErr)
  149. return PyDict_Check(sipPy);
  150. QMap<_TYPE_, float> *qm = new QMap<_TYPE_, float>;
  151. Py_ssize_t pos = 0;
  152. PyObject *kobj, *vobj;
  153. while (PyDict_Next(sipPy, &pos, &kobj, &vobj))
  154. {
  155. int kstate;
  156. _TYPE_ *k = reinterpret_cast<_TYPE_ *>(
  157. sipForceConvertToType(kobj, sipType__TYPE_, sipTransferObj,
  158. SIP_NOT_NONE, &kstate, sipIsErr));
  159. if (*sipIsErr)
  160. {
  161. PyErr_Format(PyExc_TypeError,
  162. "a dict key has type '%s' but '_TYPE_' is expected",
  163. sipPyTypeName(Py_TYPE(kobj)));
  164. delete qm;
  165. return 0;
  166. }
  167. float v = PyFloat_AsDouble(vobj);
  168. if (PyErr_Occurred())
  169. {
  170. if (PyErr_ExceptionMatches(PyExc_TypeError))
  171. PyErr_Format(PyExc_TypeError,
  172. "a dict value has type '%s' but 'float' is expected",
  173. sipPyTypeName(Py_TYPE(vobj)));
  174. delete qm;
  175. *sipIsErr = 1;
  176. return 0;
  177. }
  178. qm->insert(*k, v);
  179. sipReleaseType(k, sipType__TYPE_, kstate);
  180. }
  181. *sipCppPtr = qm;
  182. return sipGetState(sipTransferObj);
  183. %End
  184. };
  185. %End