SIP4 python bindings for TQt
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sqlsubclass5.py 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/usr/bin/env python
  2. #****************************************************************************
  3. #** $Id: sqlsubclass5.py,v 1.3 2002/07/06 13:35:41 phil Exp $
  4. #**
  5. #** Copyright (C) 1992-1998 Troll Tech AS. All rights reserved.
  6. #**
  7. #** This file is part of an example program for PyTQt. This example
  8. #** program may be used, distributed and modified without limitation.
  9. #**
  10. #*****************************************************************************/
  11. import sys
  12. from python_tqt.qt import *
  13. from python_tqt.qtsql import *
  14. from dbconnect import createConnection
  15. TRUE = 1
  16. FALSE = 0
  17. class CustomTable(TQDataTable):
  18. def __init__(self, cursor, autoPopulate = FALSE, parent = None, name = None):
  19. TQDataTable.__init__(self, cursor, autoPopulate, parent, name)
  20. def paintField(self, p, field, cr, b):
  21. if not field:
  22. return
  23. fn = str(field.name())
  24. if fn == "pricesid":
  25. query = TQSqlQuery("SELECT name FROM prices WHERE id=%s" %
  26. field.value().toString())
  27. value = ""
  28. if query.next():
  29. value = query.value(0).toString()
  30. p.drawText(2, 2, cr.width()-4, cr.height()-4,
  31. self.fieldAlignment(field), value)
  32. elif fn == "quantity":
  33. p.drawText(2, 2, cr.width()-6, cr.height()-4,
  34. TQt.AlignRight|TQt.AlignVCenter, field.value().toString())
  35. elif fn in ("price", "cost"):
  36. v = field.value().toDouble()
  37. if v < 0:
  38. p.setPen(TQColor("red"))
  39. value = TQString(u"%.2f \u20ac" % v)
  40. p.drawText(2, 2, cr.width()-6, cr.height()-4,
  41. TQt.AlignRight|TQt.AlignVCenter, value)
  42. elif fn == "paiddate":
  43. if field.value().toDate().isNull():
  44. v = TQString("not yet")
  45. p.setPen(TQColor("red"))
  46. else:
  47. v = field.value().toDate().toString(TQt.LocalDate)
  48. p.drawText(2, 2, cr.width()-4, cr.height()-4,
  49. TQt.AlignHCenter|TQt.AlignVCenter, v)
  50. else:
  51. TQDataTable.paintField(self, p, field, cr, b)
  52. class InvoiceItemCursor(TQSqlCursor):
  53. def __init__(self):
  54. TQSqlCursor.__init__(self, "invoiceitem")
  55. productPrice = TQSqlFieldInfo("price", TQVariant.Double)
  56. self.append(productPrice)
  57. self.setCalculated(productPrice.name(), TRUE)
  58. productCost = TQSqlFieldInfo("cost", TQVariant.Double)
  59. self.append(productCost)
  60. self.setCalculated(productCost.name(), TRUE)
  61. def calculateField(self, name):
  62. fn = str(name)
  63. if fn == "productname":
  64. query = TQSqlQuery("SELECT name FROM prices WHERE id=%d;" %
  65. (self.field("pricesid").value().toInt()))
  66. if query.next():
  67. return query.value(0)
  68. elif fn == "price":
  69. query = TQSqlQuery("SELECT price FROM prices WHERE id=%d;" %
  70. (self.field("pricesid").value().toInt()))
  71. if query.next():
  72. return query.value(0)
  73. elif fn == "cost":
  74. query = TQSqlQuery("SELECT price FROM prices WHERE id=%d;" %
  75. (self.field("pricesid").value().toInt()))
  76. if query.next():
  77. return TQVariant(query.value(0).toDouble() *
  78. self.value("quantity").toDouble())
  79. return TQVariant(TQString.null)
  80. def primeInsert(self):
  81. buffer = self.editBuffer()
  82. buffer.setValue("id", TQVariant(0))
  83. buffer.setValue("paiddate", TQVariant(TQDate.currentDate()))
  84. buffer.setValue("quantity", TQVariant(1))
  85. return buffer
  86. class ProductPicker(TQComboBox):
  87. def __init__(self, parent = None, name = None):
  88. TQComboBox.__init__(self, parent, name)
  89. cur = TQSqlCursor("prices")
  90. cur.select(cur.index("id"))
  91. while cur.next():
  92. self.insertItem(cur.value("name").toString(), cur.value("id").toInt())
  93. class CustomSqlEditorFactory(TQSqlEditorFactory):
  94. def __init__(self):
  95. TQSqlEditorFactory.__init__(self)
  96. def createEditor(self, parent, field):
  97. try:
  98. if str(field.name()) == "pricesid":
  99. return ProductPicker(parent)
  100. except AttributeError:
  101. pass
  102. return TQSqlEditorFactory.createEditor(self, parent, field)
  103. class Table(CustomTable):
  104. def __init__(self):
  105. self.invoiceItemCursor = InvoiceItemCursor()
  106. TQDataTable.__init__(self, self.invoiceItemCursor)
  107. self.propMap = TQSqlPropertyMap()
  108. self.editorFactory = CustomSqlEditorFactory()
  109. self.propMap.insert("ProductPicker", "pricesid")
  110. self.installPropertyMap(self.propMap)
  111. self.installEditorFactory(self.editorFactory)
  112. for cn, ch in (("pricesid", "Product"),
  113. ("quantity", "Quantity"),
  114. ("price", "Price" ),
  115. ("cost", "Cost"),
  116. ("paiddate", "Paid")):
  117. self.addColumn(cn, ch)
  118. self.setColumnWidth(0 , 150)
  119. self.setColumnWidth(1, 70)
  120. self.resize(600, 250)
  121. self.refresh()
  122. if __name__=='__main__':
  123. app = TQApplication(sys.argv)
  124. if createConnection():
  125. t = Table()
  126. app.setMainWidget(t)
  127. t.show()
  128. app.exec_loop()