PDA

View Full Version : QListView items up-down alignment.



thinus
21st November 2013, 22:03
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.9805
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:rolleyes::

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8

from PyQt4.QtCore import Qt, QSize, QSettings, QPointF
from PyQt4.QtGui import QItemDelegate, QPainter, QStyle, QFont, QFontMetrics, QColor, QPolygonF


class bubbleDelegate(QItemDelegate):
def __init__(self, parent = None):
super(bubbleDelegate, self).__init__(parent)
settings = QSettings()
self.errorColor = QColor(settings.value("bubble/errorColor", QColor(Qt.red)))

def minimumSizeHint(self,option, index):
return QSize(10,10)

def sizeHint(self, option, index):
text = index.data(Qt.UserRole +1).toString().split('\n')
textH = 0
for i in text:
textH = textH + option.fontMetrics.boundingRect(0,0,option.rect.wi dth()/2-4,0, Qt.TextWordWrap, i).height()
#text = option.fontMetrics.boundingRect(option.rect.x(), option.rect.y(), option.rect.width()/2, 100, Qt.TextWrapAnywhere, index.data(Qt.UserRole + 1).toString())
font = QFont()
font.setPointSize(6)
fontM = QFontMetrics(font)
hour = fontM.boundingRect(option.rect.x(), option.rect.y(), option.rect.width()/2, 100, Qt.TextWrapAnywhere, index.data(Qt.UserRole + 2).toString())
return QSize(option.rect.width(),textH+hour.height()+2)

def paint(self, painter, option, index):
hourFont = QFont()
hourFont.setPointSize(6)
hourFontM = QFontMetrics(hourFont)
hour = hourFontM.boundingRect(option.rect.x(), option.rect.y(), option.rect.width()/2, 100, Qt.TextWrapAnywhere, index.data(Qt.UserRole + 2).toString())
textFont = QFont()
painter.setRenderHint(QPainter.Antialiasing, True)
painter.setPen(Qt.black)
text = index.data(Qt.UserRole +1).toString().split('\n')
h = 0
textH=0
if index.data(Qt.UserRole + 3).toBool() == True:
painter.setBrush(QColor(index.data(Qt.UserRole + 4)))
painter.drawRoundedRect(option.rect.x(), option.rect.y(), option.rect.width()/2 -1 ,option.rect.height(),5,5)
painter.setFont(textFont)
for i in text:
textH = textH + option.fontMetrics.boundingRect(0,0,option.rect.wi dth()/2-4,0, Qt.TextWordWrap, i).height()
painter.drawText(option.rect.x()+2, option.rect.y()+2+h, option.rect.width()/2 -1, option.rect.height(),Qt.TextWordWrap,i)
h += textH
y = option.rect.y() +option.rect.height() - hour.height()-1
painter.setFont(hourFont)
painter.drawText(option.rect.x()+2, y, option.rect.width()/2 -1, option.rect.height(),Qt.TextWrapAnywhere,index.dat a(Qt.UserRole + 2).toString())
else:
painter.setBrush(QColor(index.data(Qt.UserRole + 4)))
painter.drawRoundedRect(option.rect.width()/2+1, option.rect.y(), option.rect.width()/2 -1 ,option.rect.height(),5,5)
painter.setFont(textFont)
h = 0
textH = 0
for i in text:
textH = textH + option.fontMetrics.boundingRect(0,0,option.rect.wi dth()/2-4,0, Qt.TextWordWrap, i).height()
#painter.drawText(option.rect.x()+2, option.rect.y()+2+h, option.rect.width()/2 -1, option.rect.height(),Qt.TextWordWrap,i)
painter.drawText(option.rect.width()/2+2, option.rect.y()+2+h, option.rect.width()/2 -1, option.rect.height(),Qt.TextWordWrap,i)
h += textH
#painter.drawText(option.rect.width()/2+2, option.rect.y()+2, option.rect.width()/2 -1, option.rect.height(),Qt.TextWrapAnywhere,index.dat a(Qt.UserRole + 1).toString())
y = option.rect.y() +option.rect.height() - hour.height()-1
painter.setFont(hourFont)
painter.drawText(option.rect.width() - hour.width() - 2, y, option.rect.width()/2 -1, option.rect.height(),Qt.TextWrapAnywhere,index.dat a(Qt.UserRole + 2).toString())
status = index.data(Qt.UserRole + 5)
if (status == 1):
painter.setBrush(Qt.green)
painter.setPen(Qt.green)
polygon = QPolygonF()
polygon.append(QPointF(0.0,3.0))
polygon.append(QPointF(5.0,8.0))
polygon.append(QPointF(10.0,0.0))
polygon.append(QPointF(5.0,5.0))
polygon.append(QPointF(0.0,3.0))
polygon.translate(option.rect.width()/2+4, y)
painter.drawPolygon(polygon)
def updateColors(self,received, sended):
self.received = received
self.sended = sended

I know, that this is poor code. I will write it better.