X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fqtui%2Fchatline-old.cpp;h=3b0dae5cfd269616cb4b497586fcb68f90d523e7;hp=d2f55be962f1df5641cf9bdd04adf744a788fbac;hb=63af36e607d250637aaba0bba14d2158e79c7bed;hpb=e733408e4759473bf38831f498f48a0f2f5e6dc7 diff --git a/src/qtui/chatline-old.cpp b/src/qtui/chatline-old.cpp index d2f55be9..3b0dae5c 100644 --- a/src/qtui/chatline-old.cpp +++ b/src/qtui/chatline-old.cpp @@ -19,41 +19,79 @@ ***************************************************************************/ #include "chatline-old.h" +#include "client.h" +#include "network.h" #include "qtui.h" -//! Construct a ChatLine object from a message. +#include "qtuisettings.h" + +//! Construct a ChatLineOld object from a message. /** * \param m The message to be layouted and rendered */ -ChatLine::ChatLine(Message m) { +QColor ChatLineOld::_highlightColor; +ChatLineOld::ChatLineOld(const Message &m) { hght = 0; - //networkName = m.buffer.network(); - //bufferName = m.buffer.buffer(); msg = m; selectionMode = None; + isHighlight = false; formatMsg(msg); -} -ChatLine::~ChatLine() { + if(!_highlightColor.isValid()) { + QtUiSettings s("QtUi/Colors"); + _highlightColor = s.value("highlightColor", QVariant(QColor("lightcoral"))).value(); + } +} +ChatLineOld::~ChatLineOld() { } -void ChatLine::formatMsg(Message msg) { +void ChatLineOld::formatMsg(Message msg) { + isHighlight = msg.flags() & Message::Highlight; QTextOption tsOption, senderOption, textOption; styledTimeStamp = QtUi::style()->styleString(msg.formattedTimestamp()); styledSender = QtUi::style()->styleString(msg.formattedSender()); - styledText = QtUi::style()->styleString(msg.formattedText()); + styledContents = QtUi::style()->styleString(msg.formattedText()); precomputeLine(); } +QList ChatLineOld::calcFormatRanges(const UiStyle::StyledText &fs) { + QTextLayout::FormatRange additional; + additional.start = additional.length = 0; + return calcFormatRanges(fs, additional); +} + // This function is almost obsolete, since with the new style engine, we already get a list of formats... -// We don't know yet if we keep this implementation of ChatLine, so I won't bother making this actually nice. -// Also, the additional format is ignored for now, which means that you won't see a selection... -// FIXME TODO -QList ChatLine::calcFormatRanges(const UiStyle::StyledText &fs, QTextLayout::FormatRange additional) { +// We don't know yet if we keep this implementation of ChatLineOld, so I won't bother making this actually nice. +QList ChatLineOld::calcFormatRanges(const UiStyle::StyledText &_fs, + const QTextLayout::FormatRange &additional) { + UiStyle::StyledText fs = _fs; QList ranges; - foreach(QTextLayout::FormatRange f, fs.formats) { + if(additional.length > 0) { + for(int i = 0; i < fs.formatList.count(); i++) { + int oldend = fs.formatList[i].start + fs.formatList[i].length - 1; + int addend = additional.start + additional.length - 1; + if(oldend < additional.start) continue; + fs.formatList[i].length = additional.start - fs.formatList[i].start; + QTextLayout::FormatRange addfmtrng = fs.formatList[i]; + addfmtrng.format.merge(additional.format); + addfmtrng.start = additional.start; + addfmtrng.length = qMin(oldend, addend) - additional.start + 1; + fs.formatList.insert(++i, addfmtrng); + if(addend == oldend) break; + if(addend < oldend) { + QTextLayout::FormatRange restfmtrng = fs.formatList[i-1]; + restfmtrng.start = addend + 1; + restfmtrng.length = oldend - addend; + fs.formatList.insert(++i, restfmtrng); + break; + } + } + } + + foreach(QTextLayout::FormatRange f, fs.formatList) { + if(f.length <= 0) continue; FormatRange range; range.start = f.start; range.length = f.length; @@ -62,40 +100,10 @@ QList ChatLine::calcFormatRanges(const UiStyle::StyledTex range.height = metrics.lineSpacing(); ranges.append(range); } - /* - QList formats = fs.formats; - formats.append(additional); - int cur = -1; - FormatRange range, lastrange; - for(int i = 0; i < fs.text.length(); i++) { - QTextCharFormat format; - foreach(QTextLayout::FormatRange f, formats) { - if(i >= f.start && i < f.start + f.length) format.merge(f.format); - } - if(cur < 0) { - range.start = 0; range.length = 1; range.format= format; - cur = 0; - } else { - if(format == range.format) range.length++; - else { - QFontMetrics metrics(range.format.font()); - range.height = metrics.lineSpacing(); - ranges.append(range); - range.start = i; range.length = 1; range.format = format; - cur++; - } - } - } - if(cur >= 0) { - QFontMetrics metrics(range.format.font()); - range.height = metrics.lineSpacing(); - ranges.append(range); - } - */ return ranges; } -void ChatLine::setSelection(SelectionMode mode, int start, int end) { +void ChatLineOld::setSelection(SelectionMode mode, int start, int end) { selectionMode = mode; //tsFormat.clear(); senderFormat.clear(); textFormat.clear(); QPalette pal = QApplication::palette(); @@ -104,7 +112,7 @@ void ChatLine::setSelection(SelectionMode mode, int start, int end) { case None: tsFormat = calcFormatRanges(styledTimeStamp); senderFormat = calcFormatRanges(styledSender); - textFormat = calcFormatRanges(styledText); + textFormat = calcFormatRanges(styledContents); break; case Partial: selectionStart = qMin(start, end); selectionEnd = qMax(start, end); @@ -112,65 +120,63 @@ void ChatLine::setSelection(SelectionMode mode, int start, int end) { textSel.format.setBackground(pal.brush(QPalette::Highlight)); textSel.start = selectionStart; textSel.length = selectionEnd - selectionStart; - //textFormat.append(textSel); - textFormat = calcFormatRanges(styledText, textSel); - foreach(FormatRange fr, textFormat); + textFormat = calcFormatRanges(styledContents, textSel); break; case Full: tsSel.format.setForeground(pal.brush(QPalette::HighlightedText)); tsSel.format.setBackground(pal.brush(QPalette::Highlight)); - tsSel.start = 0; tsSel.length = styledTimeStamp.text.length(); + tsSel.start = 0; tsSel.length = styledTimeStamp.plainText.length(); tsFormat = calcFormatRanges(styledTimeStamp, tsSel); senderSel.format.setForeground(pal.brush(QPalette::HighlightedText)); senderSel.format.setBackground(pal.brush(QPalette::Highlight)); - senderSel.start = 0; senderSel.length = styledSender.text.length(); + senderSel.start = 0; senderSel.length = styledSender.plainText.length(); senderFormat = calcFormatRanges(styledSender, senderSel); textSel.format.setForeground(pal.brush(QPalette::HighlightedText)); textSel.format.setBackground(pal.brush(QPalette::Highlight)); - textSel.start = 0; textSel.length = styledText.text.length(); - textFormat = calcFormatRanges(styledText, textSel); + textSel.start = 0; textSel.length = styledContents.plainText.length(); + textFormat = calcFormatRanges(styledContents, textSel); break; } } -MsgId ChatLine::msgId() const { +MsgId ChatLineOld::msgId() const { return msg.msgId(); } -BufferInfo ChatLine::bufferInfo() const { - return msg.buffer(); +BufferInfo ChatLineOld::bufferInfo() const { + return msg.bufferInfo(); } -QDateTime ChatLine::timestamp() const { +QDateTime ChatLineOld::timestamp() const { return msg.timestamp(); } -QString ChatLine::sender() const { - return styledSender.text; +QString ChatLineOld::sender() const { + return styledSender.plainText; } -QString ChatLine::text() const { - return styledText.text; +QString ChatLineOld::text() const { + return styledContents.plainText; } -bool ChatLine::isUrl(int c) const { +bool ChatLineOld::isUrl(int c) const { if(c < 0 || c >= charUrlIdx.count()) return false;; return charUrlIdx[c] >= 0; } -QUrl ChatLine::getUrl(int c) const { +QUrl ChatLineOld::getUrl(int c) const { if(c < 0 || c >= charUrlIdx.count()) return QUrl(); int i = charUrlIdx[c]; - if(i >= 0) return styledText.urls[i].url; + if(i >= 0) return styledContents.urls[i].url; else return QUrl(); } //!\brief Return the cursor position for the given coordinate pos. /** - * \param pos The position relative to the ChatLine + * \param pos The position relative to the ChatLineOld * \return The cursor position, [or -3 for invalid,] or -2 for timestamp, or -1 for sender */ -int ChatLine::posToCursor(QPointF pos) { +int ChatLineOld::posToCursor(QPointF pos) { if(pos.x() < tsWidth + (int)QtUi::style()->sepTsSender()/2) return -2; qreal textStart = tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText(); if(pos.x() < textStart) return -1; @@ -188,21 +194,21 @@ int ChatLine::posToCursor(QPointF pos) { return 0; } -void ChatLine::precomputeLine() { +void ChatLineOld::precomputeLine() { tsFormat = calcFormatRanges(styledTimeStamp); senderFormat = calcFormatRanges(styledSender); - textFormat = calcFormatRanges(styledText); + textFormat = calcFormatRanges(styledContents); minHeight = 0; foreach(FormatRange fr, tsFormat) minHeight = qMax(minHeight, fr.height); foreach(FormatRange fr, senderFormat) minHeight = qMax(minHeight, fr.height); words.clear(); - charPos.resize(styledText.text.length() + 1); - charHeights.resize(styledText.text.length()); - charUrlIdx.fill(-1, styledText.text.length()); - for(int i = 0; i < styledText.urls.count(); i++) { - QtUiStyle::UrlInfo url = styledText.urls[i]; + charPos.resize(styledContents.plainText.length() + 1); + charHeights.resize(styledContents.plainText.length()); + charUrlIdx.fill(-1, styledContents.plainText.length()); + for(int i = 0; i < styledContents.urls.count(); i++) { + QtUiStyle::UrlInfo url = styledContents.urls[i]; for(int j = url.start; j < url.end; j++) charUrlIdx[j] = i; } if(!textFormat.count()) return; @@ -210,10 +216,10 @@ void ChatLine::precomputeLine() { QFontMetrics metrics(textFormat[0].format.font()); Word wr; wr.start = -1; wr.trailing = -1; - for(int i = 0; i < styledText.text.length(); ) { + for(int i = 0; i < styledContents.plainText.length(); ) { charPos[i] = w; charHeights[i] = textFormat[idx].height; - w += metrics.charWidth(styledText.text, i); - if(!styledText.text[i].isSpace()) { + w += metrics.charWidth(styledContents.plainText, i); + if(!styledContents.plainText[i].isSpace()) { if(wr.trailing >= 0) { // new word after space words.append(wr); @@ -231,17 +237,17 @@ void ChatLine::precomputeLine() { wr.trailing++; } } - if(++i < styledText.text.length() && ++cnt >= textFormat[idx].length) { + if(++i < styledContents.plainText.length() && ++cnt >= textFormat[idx].length) { cnt = 0; idx++; Q_ASSERT(idx < textFormat.count()); metrics = QFontMetrics(textFormat[idx].format.font()); } } - charPos[styledText.text.length()] = w; + charPos[styledContents.plainText.length()] = w; if(wr.start >= 0) words.append(wr); } -qreal ChatLine::layout(qreal tsw, qreal senderw, qreal textw) { +qreal ChatLineOld::layout(qreal tsw, qreal senderw, qreal textw) { tsWidth = tsw; senderWidth = senderw; textWidth = textw; if(textw <= 0) return minHeight; lineLayouts.clear(); LineLayout line; @@ -306,16 +312,23 @@ qreal ChatLine::layout(qreal tsw, qreal senderw, qreal textw) { return hght; } -//!\brief Draw ChatLine on the given QPainter at the given position. -void ChatLine::draw(QPainter *p, const QPointF &pos) { +//!\brief Draw ChatLineOld on the given QPainter at the given position. +void ChatLineOld::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 + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText() + textWidth, height()))); - } else if(selectionMode == Partial) { + } else { + if(isHighlight) { + p->setPen(Qt::NoPen); + p->setBrush(_highlightColor /*pal.brush(QPalette::AlternateBase) */); + p->drawRect(QRectF(pos, QSizeF(tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText() + textWidth, height()))); + } + if(selectionMode == Partial) { + } } /* p->setClipRect(QRectF(pos, QSizeF(tsWidth, height()))); tsLayout.draw(p, pos, tsFormat); @@ -333,18 +346,19 @@ void ChatLine::draw(QPainter *p, const QPointF &pos) { 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); + p->drawText(rect, Qt::AlignLeft|Qt::TextSingleLine, styledTimeStamp.plainText.mid(fr.start, fr.length), &brect); rect.setLeft(brect.right()); } rect = QRectF(pos + QPointF(tsWidth + QtUi::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); + p->drawText(rect, Qt::AlignRight|Qt::TextSingleLine, styledSender.plainText.mid(fr.start, fr.length), &brect); rect.setRight(brect.left()); } QPointF tpos = pos + QPointF(tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText(), 0); qreal h = 0; int l = 0; + if(lineLayouts.count() == 0) return; // how can this happen? rect = QRectF(tpos + QPointF(0, h), QSizeF(textWidth, lineLayouts[l].height)); int offset = 0; foreach(FormatRange fr, textFormat) { @@ -357,7 +371,7 @@ void ChatLine::draw(QPainter *p, const QPointF &pos) { 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); + p->drawText(rect, Qt::AlignLeft|Qt::TextSingleLine, styledContents.plainText.mid(start, end - start), &brect); if(llend <= end) { h += lineLayouts[l].height; l++;