Hi.
I am writing an sms app in python and pyqt.
I've made custom delegate for showing message "bubbles". I'm showing these in QListView. It works, but I want it in other posistion. Maybe photo will show it.moje.png
Now when I have many "bubbles", they are aligned to up of QListView(green arrow). There is free space in down(red arrrow).The same when I have one "bubble". But I want it reversed. When there is one "bubble", then it should be in down part of QListView. When there are many "bubbles", they should be aligned to down. There could be free space on top, or one bubble could be cropped.
I post my delegate code, maybe it could help to help me:
Qt Code:
  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # vim:fenc=utf-8
  4.  
  5. from PyQt4.QtCore import Qt, QSize, QSettings, QPointF
  6.  
  7.  
  8. class bubbleDelegate(QItemDelegate):
  9. def __init__(self, parent = None):
  10. super(bubbleDelegate, self).__init__(parent)
  11. settings = QSettings()
  12. self.errorColor = QColor(settings.value("bubble/errorColor", QColor(Qt.red)))
  13.  
  14. def minimumSizeHint(self,option, index):
  15. return QSize(10,10)
  16.  
  17. def sizeHint(self, option, index):
  18. text = index.data(Qt.UserRole +1).toString().split('\n')
  19. textH = 0
  20. for i in text:
  21. textH = textH + option.fontMetrics.boundingRect(0,0,option.rect.width()/2-4,0, Qt.TextWordWrap, i).height()
  22. #text = option.fontMetrics.boundingRect(option.rect.x(), option.rect.y(), option.rect.width()/2, 100, Qt.TextWrapAnywhere, index.data(Qt.UserRole + 1).toString())
  23. font = QFont()
  24. font.setPointSize(6)
  25. fontM = QFontMetrics(font)
  26. hour = fontM.boundingRect(option.rect.x(), option.rect.y(), option.rect.width()/2, 100, Qt.TextWrapAnywhere, index.data(Qt.UserRole + 2).toString())
  27. return QSize(option.rect.width(),textH+hour.height()+2)
  28.  
  29. def paint(self, painter, option, index):
  30. hourFont = QFont()
  31. hourFont.setPointSize(6)
  32. hourFontM = QFontMetrics(hourFont)
  33. hour = hourFontM.boundingRect(option.rect.x(), option.rect.y(), option.rect.width()/2, 100, Qt.TextWrapAnywhere, index.data(Qt.UserRole + 2).toString())
  34. textFont = QFont()
  35. painter.setRenderHint(QPainter.Antialiasing, True)
  36. painter.setPen(Qt.black)
  37. text = index.data(Qt.UserRole +1).toString().split('\n')
  38. h = 0
  39. textH=0
  40. if index.data(Qt.UserRole + 3).toBool() == True:
  41. painter.setBrush(QColor(index.data(Qt.UserRole + 4)))
  42. painter.drawRoundedRect(option.rect.x(), option.rect.y(), option.rect.width()/2 -1 ,option.rect.height(),5,5)
  43. painter.setFont(textFont)
  44. for i in text:
  45. textH = textH + option.fontMetrics.boundingRect(0,0,option.rect.width()/2-4,0, Qt.TextWordWrap, i).height()
  46. painter.drawText(option.rect.x()+2, option.rect.y()+2+h, option.rect.width()/2 -1, option.rect.height(),Qt.TextWordWrap,i)
  47. h += textH
  48. y = option.rect.y() +option.rect.height() - hour.height()-1
  49. painter.setFont(hourFont)
  50. painter.drawText(option.rect.x()+2, y, option.rect.width()/2 -1, option.rect.height(),Qt.TextWrapAnywhere,index.data(Qt.UserRole + 2).toString())
  51. else:
  52. painter.setBrush(QColor(index.data(Qt.UserRole + 4)))
  53. painter.drawRoundedRect(option.rect.width()/2+1, option.rect.y(), option.rect.width()/2 -1 ,option.rect.height(),5,5)
  54. painter.setFont(textFont)
  55. h = 0
  56. textH = 0
  57. for i in text:
  58. textH = textH + option.fontMetrics.boundingRect(0,0,option.rect.width()/2-4,0, Qt.TextWordWrap, i).height()
  59. #painter.drawText(option.rect.x()+2, option.rect.y()+2+h, option.rect.width()/2 -1, option.rect.height(),Qt.TextWordWrap,i)
  60. painter.drawText(option.rect.width()/2+2, option.rect.y()+2+h, option.rect.width()/2 -1, option.rect.height(),Qt.TextWordWrap,i)
  61. h += textH
  62. #painter.drawText(option.rect.width()/2+2, option.rect.y()+2, option.rect.width()/2 -1, option.rect.height(),Qt.TextWrapAnywhere,index.data(Qt.UserRole + 1).toString())
  63. y = option.rect.y() +option.rect.height() - hour.height()-1
  64. painter.setFont(hourFont)
  65. painter.drawText(option.rect.width() - hour.width() - 2, y, option.rect.width()/2 -1, option.rect.height(),Qt.TextWrapAnywhere,index.data(Qt.UserRole + 2).toString())
  66. status = index.data(Qt.UserRole + 5)
  67. if (status == 1):
  68. painter.setBrush(Qt.green)
  69. painter.setPen(Qt.green)
  70. polygon = QPolygonF()
  71. polygon.append(QPointF(0.0,3.0))
  72. polygon.append(QPointF(5.0,8.0))
  73. polygon.append(QPointF(10.0,0.0))
  74. polygon.append(QPointF(5.0,5.0))
  75. polygon.append(QPointF(0.0,3.0))
  76. polygon.translate(option.rect.width()/2+4, y)
  77. painter.drawPolygon(polygon)
  78. def updateColors(self,received, sended):
  79. self.received = received
  80. self.sended = sended
To copy to clipboard, switch view to plain text mode 
I know, that this is poor code. I will write it better.