-qreal ChatLine::layout(qreal tsw, qreal senderw, qreal textw) {
- tsWidth = tsw; senderWidth = senderw; textWidth = textw;
- if(textw <= 0) return minHeight;
- lineLayouts.clear(); LineLayout line;
- int h = 0;
- int offset = 0; int numWords = 0;
- line.y = 0;
- line.start = 0;
- line.height = minHeight; // first line needs room for ts and sender
- for(uint i = 0; i < (uint)words.count(); i++) {
- int lastpos = charPos[words[i].start + words[i].length]; // We use charPos[lastchar + 1], 'coz last char needs to fit
- if(lastpos - offset <= textw) {
- line.height = qMax(line.height, words[i].height);
- line.length = words[i].start + words[i].length - line.start;
- numWords++;
- } else {
- // we need to wrap!
- if(numWords > 0) {
- // ok, we had some words before, so store the layout and start a new line
- h += line.height;
- line.length = words[i-1].start + words[i-1].length - line.start;
- lineLayouts.append(line);
- line.y += line.height;
- line.start = words[i].start;
- line.height = words[i].height;
- offset = charPos[words[i].start];
- }
- numWords = 1;
- // check if the word fits into the current line
- if(lastpos - offset <= textw) {
- line.length = words[i].length;
- } else {
- // we need to break a word in the middle
- int border = (int)textw + offset; // save some additions
- line.start = words[i].start;
- line.length = 1;
- line.height = charHeights[line.start];
- int j = line.start + 1;
- for(int l = 1; l < words[i].length; j++, l++) {
- if(charPos[j+1] < border) {
- line.length++;
- line.height = qMax(line.height, charHeights[j]);
- continue;
- } else {
- h += line.height;
- lineLayouts.append(line);
- line.y += line.height;
- line.start = j;
- line.height = charHeights[j];
- line.length = 1;
- offset = charPos[j];
- border = (int)textw + offset;
- }
- }
- }
- }
- }
- h += line.height;
- if(numWords > 0) {
- lineLayouts.append(line);
- }
- hght = h;
- return hght;
-}
-
-//!\brief Draw ChatLine on the given QPainter at the given position.
-void ChatLine::draw(QPainter *p, const QPointF &pos) {
- QPalette pal = QApplication::palette();
-
- if(selectionMode == Full) {
- p->setPen(Qt::NoPen);
- p->setBrush(pal.brush(QPalette::Highlight));
- p->drawRect(QRectF(pos, QSizeF(tsWidth + Style::sepTsSender() + senderWidth + Style::sepSenderText() + textWidth, height())));
- } else if(selectionMode == Partial) {
-
- } /*
- p->setClipRect(QRectF(pos, QSizeF(tsWidth, height())));
- tsLayout.draw(p, pos, tsFormat);
- p->setClipRect(QRectF(pos + QPointF(tsWidth + Style::sepTsSender(), 0), QSizeF(senderWidth, height())));
- senderLayout.draw(p, pos + QPointF(tsWidth + Style::sepTsSender(), 0), senderFormat);
- p->setClipping(false);
- textLayout.draw(p, pos + QPointF(tsWidth + Style::sepTsSender() + senderWidth + Style::sepSenderText(), 0), textFormat);
- */
- //p->setClipRect(QRectF(pos, QSizeF(tsWidth, 15)));
- //p->drawRect(QRectF(pos, QSizeF(tsWidth, minHeight)));
- p->setBackgroundMode(Qt::OpaqueMode);
- QPointF tp = pos;
- QRectF rect(pos, QSizeF(tsWidth, minHeight));
- QRectF brect;
- foreach(FormatRange fr, tsFormat) {
- p->setFont(fr.format.font());
- p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background());
- p->drawText(rect, Qt::AlignLeft|Qt::TextSingleLine, styledTimeStamp.text.mid(fr.start, fr.length), &brect);
- rect.setLeft(brect.right());
- }
- rect = QRectF(pos + QPointF(tsWidth + Style::sepTsSender(), 0), QSizeF(senderWidth, minHeight));
- for(int i = senderFormat.count() - 1; i >= 0; i--) {
- FormatRange fr = senderFormat[i];
- p->setFont(fr.format.font()); p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background());
- p->drawText(rect, Qt::AlignRight|Qt::TextSingleLine, styledSender.text.mid(fr.start, fr.length), &brect);
- rect.setRight(brect.left());
- }
- QPointF tpos = pos + QPointF(tsWidth + Style::sepTsSender() + senderWidth + Style::sepSenderText(), 0);
- qreal h = 0; int l = 0;
- rect = QRectF(tpos + QPointF(0, h), QSizeF(textWidth, lineLayouts[l].height));
- int offset = 0;
- foreach(FormatRange fr, textFormat) {
- if(l >= lineLayouts.count()) break;
- p->setFont(fr.format.font()); p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background());
- int start, end, frend, llend;
- do {
- frend = fr.start + fr.length;
- if(frend <= lineLayouts[l].start) break;
- llend = lineLayouts[l].start + lineLayouts[l].length;
- start = qMax(fr.start, lineLayouts[l].start); end = qMin(frend, llend);
- rect.setLeft(tpos.x() + charPos[start] - offset);
- p->drawText(rect, Qt::AlignLeft|Qt::TextSingleLine, styledText.text.mid(start, end - start), &brect);
- if(llend <= end) {
- h += lineLayouts[l].height;
- l++;
- if(l < lineLayouts.count()) {
- rect = QRectF(tpos + QPointF(0, h), QSizeF(textWidth, lineLayouts[l].height));
- offset = charPos[lineLayouts[l].start];
- }
- }
- } while(end < frend && l < lineLayouts.count());
- }