void KCRowHeader::drawText(QPainter& painter, const QFont& font, const QPointF& location, const QString& text) const { register KCSheet * const sheet = m_pView->activeSheet(); if (!sheet) return; const double scaleX = POINT_TO_INCH(double(KoDpi::dpiX())); const double scaleY = POINT_TO_INCH(double(KoDpi::dpiY())); // Qt scales the font already with the logical resolution. Do not do it twice! painter.save(); painter.scale(1.0 / scaleX, 1.0 / scaleY); QTextLayout textLayout(text, font); textLayout.beginLayout(); forever { QTextLine line = textLayout.createLine(); if (!line.isValid()) break; line.setLineWidth(width() * scaleX); } textLayout.endLayout(); QPointF loc(location.x() * scaleX, location.y() * scaleY); textLayout.draw(&painter, loc); painter.restore(); }
void PluginListDelegate::drawDisplay(QPainter* painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text) const { QTextDocument textDocument; textDocument.setHtml(text); QTextLayout textLayout(textDocument.begin()); textLayout.setFont(option.font); const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin, 0) + 1; QRect textRect = rect.adjusted(textMargin, 0, -textMargin, 0); // remove width padding textLayout.beginLayout(); qreal height = 0; while (1) { QTextLine line = textLayout.createLine(); if (!line.isValid()) { break; } line.setLineWidth(textRect.width()); height += 3; line.setPosition(QPoint(0, height)); height += line.height(); } textLayout.endLayout(); textLayout.draw(painter, QPointF(textRect.left(), textRect.top())); }
QRectF STSimpleEditView::microFocusRect() const{ //QString text=m_text; bool preedit=!m_preeditString.isEmpty(); int cursorPos=m_cursorPos; if(preedit){ //text=text.left(m_cursorPos)+m_preeditString; cursorPos=preeditCursorPos(); if(cursorPos==-1){ return QRectF(-10.f, -10.f, 0.f, 0.f); } cursorPos+=m_cursorPos; } const STFont::Layout& layout=textLayout();//STFont::defaultFont()->layoutString(text); QPointF origin=textOrigin(); foreach(const STFont::LayoutGlyph& glyph, layout){ if(glyph.charIndex==cursorPos){ float pos=glyph.rect.left()+origin.x(); if(pos<1.f && pos>=0.f)pos=1.f; if(pos>(float)m_size.width()-1.f && pos<=(float)m_size.width())pos=m_size.width()-1; return QRectF(roundf(pos)-1.f, glyph.rect.top()-1.f+origin.y(), 2.f, 12.f); } } return QRectF(0.f, 0.f, 1.f, 12.f); }
void FileMetaDataToolTip::setName(const QString& name) { QTextOption textOption; textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere); const QString processedName = Qt::mightBeRichText(name) ? name : KStringHandler::preProcessWrap(name); QTextLayout textLayout(processedName); textLayout.setFont(m_name->font()); textLayout.setTextOption(textOption); QString wrappedText; wrappedText.reserve(processedName.length()); // wrap the text to fit into the maximum width of m_name textLayout.beginLayout(); QTextLine line = textLayout.createLine(); while (line.isValid()) { line.setLineWidth(m_name->maximumWidth()); wrappedText += processedName.midRef(line.textStart(), line.textLength()); line = textLayout.createLine(); if (line.isValid()) { wrappedText += QChar::LineSeparator; } } textLayout.endLayout(); m_name->setText(wrappedText); }
void DegreesLabel270::paintEvent( QPaintEvent* event ) { QStyleOption opt; opt.init(this); QPainter p(this); style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); QTextLayout textLayout(content, p.font()); textLayout.beginLayout(); drawRotatedText(&p, ROTATE270, width(), height()/2, content); }
void STSimpleEditView::lineNavigation(bool shift, int delta) { const STFont::Layout& layout=textLayout(); float currentLineY; int currentLine; if(!m_lineNavigationMode){ foreach(const STFont::LayoutGlyph& glyph, layout){ if(glyph.charIndex==m_cursorPos){ float pos=glyph.rect.left(); m_lineNavigationX=pos; break; } } m_lineNavigationMode=true; }
void Window::paintEvent(QPaintEvent *event) { //! [0] QTextLayout textLayout(text, font); qreal margin = 10; qreal radius = qMin(width()/2.0, height()/2.0) - margin; QFontMetrics fm(font); qreal lineHeight = fm.height(); qreal y = 0; textLayout.beginLayout(); while (1) { // create a new line QTextLine line = textLayout.createLine(); if (!line.isValid()) break; qreal x1 = qMax(0.0, pow(pow(radius,2)-pow(radius-y,2), 0.5)); qreal x2 = qMax(0.0, pow(pow(radius,2)-pow(radius-(y+lineHeight),2), 0.5)); qreal x = qMax(x1, x2) + margin; qreal lineWidth = (width() - margin) - x; line.setLineWidth(lineWidth); line.setPosition(QPointF(x, margin+y)); y += line.height(); } textLayout.endLayout(); QPainter painter; painter.begin(this); painter.setRenderHint(QPainter::Antialiasing); painter.fillRect(rect(), Qt::white); painter.setBrush(QBrush(Qt::black)); painter.setPen(QPen(Qt::black)); textLayout.draw(&painter, QPoint(0,0)); painter.setBrush(QBrush(QColor("#a6ce39"))); painter.setPen(QPen(Qt::black)); painter.drawEllipse(QRectF(-radius, margin, 2*radius, 2*radius)); painter.end(); //! [0] }
//! [2] void ElidedLabel::paintEvent(QPaintEvent *event) { QFrame::paintEvent(event); QPainter painter(this); QFontMetrics fontMetrics = painter.fontMetrics(); bool didElide = false; int lineSpacing = fontMetrics.lineSpacing(); int y = 0; QTextLayout textLayout(content, painter.font()); textLayout.beginLayout(); forever { QTextLine line = textLayout.createLine(); if (!line.isValid()) break; line.setLineWidth(width()); int nextLineY = y + lineSpacing; if (height() >= nextLineY + lineSpacing) { line.draw(&painter, QPoint(0, y)); y = nextLineY; //! [2] //! [3] } else { QString lastLine = content.mid(line.textStart()); QString elidedLastLine = fontMetrics.elidedText(lastLine, Qt::ElideRight, width()); painter.drawText(QPoint(0, y + fontMetrics.ascent()), elidedLastLine); line = textLayout.createLine(); didElide = line.isValid(); break; } } textLayout.endLayout(); //! [3] //! [4] if (didElide != elided) { elided = didElide; emit elisionChanged(didElide); } }
void ElidedLabel::paintEvent(QPaintEvent *event) { QLabel::paintEvent(event); QPainter painter(this); QFontMetrics fontMetrics = painter.fontMetrics(); QRect cr = contentsRect(); cr.adjust(margin(), margin(), -margin(), -margin()); bool didElide = false; int lineSpacing = fontMetrics.lineSpacing(); int x, y = x =cr.top()+(cr.height()-lineSpacing)/2; QTextLayout textLayout(mContent, painter.font()); textLayout.beginLayout(); forever { QTextLine line = textLayout.createLine(); if (!line.isValid()) break; line.setLineWidth(cr.width()+2*x); int nextLineY = y + lineSpacing; if (cr.height() >= nextLineY + lineSpacing) { line.draw(&painter, QPoint(x, y)); y = nextLineY; } else { QString lastLine = mContent.mid(line.textStart()); QString elidedLastLine = fontMetrics.elidedText(lastLine, Qt::ElideRight, cr.width()); painter.drawText(QPoint(x, y + fontMetrics.ascent()), elidedLastLine); line = textLayout.createLine(); didElide = line.isValid(); break; } } textLayout.endLayout(); if (didElide != mElided) { mElided = didElide; emit elisionChanged(didElide); } }
void PluginListDelegate::drawDisplay(QPainter* painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text) const { QPalette::ColorGroup cg = option.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled; if (cg == QPalette::Normal && !(option.state & QStyle::State_Active)) { cg = QPalette::Inactive; } if (option.state & QStyle::State_Selected) { painter->fillRect(rect, option.palette.brush(cg, QPalette::Highlight)); painter->setPen(option.palette.color(cg, QPalette::HighlightedText)); } else { painter->setPen(option.palette.color(cg, QPalette::Text)); } QTextDocument textDocument; textDocument.setHtml(text); QTextLayout textLayout(textDocument.begin()); textLayout.setFont(option.font); const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin, 0) + 1; QRect textRect = rect.adjusted(textMargin, 0, -textMargin, 0); // remove width padding textLayout.beginLayout(); qreal height = 0; QTextLine line = textLayout.createLine(); while (line.isValid()) { line.setLineWidth(textRect.width()); height += 3; line.setPosition(QPoint(0, height)); height += line.height(); line = textLayout.createLine(); } textLayout.endLayout(); textLayout.draw(painter, QPointF(textRect.left(), textRect.top())); }
QStringList StatusEventItemDelegate::layoutText(const QString &text, const QFont &font, int maxLineWidth, int maxLines, int *textHeight) { QTextLayout textLayout(text, font); QFontMetrics fontMetrics(font); QStringList lines; qreal height = 0.0; textLayout.beginLayout(); while (lines.size() < maxLines) { QTextLine line = textLayout.createLine(); if (! line.isValid()) break; if (maxLines <= 0 || lines.size() < maxLines-1) { // Wrap the current line at or below the maximum line width line.setLineWidth(maxLineWidth); lines.append(text.mid(line.textStart(), line.textLength())); } else { // Set the line width beyond the max line width, and then elide it // so the user has a visible indication that the full message is // longer than what is visible. line.setLineWidth(2 * maxLineWidth); lines.append(fontMetrics.elidedText(text.mid(line.textStart()), Qt::ElideRight, maxLineWidth)); } height += fontMetrics.leading() + line.height(); } textLayout.endLayout(); if (textHeight) *textHeight = qRound(height); return lines; }
void ElidedLabel::paintEvent( QPaintEvent* event ) { QFrame::paintEvent( event ); QPainter p( this ); QRect r = contentsRect(); r.adjust( m_margin, m_margin, -m_margin, -m_margin ); if ( m_multiLine ) { QTextLayout textLayout( m_text ); textLayout.setFont( p.font() ); int widthUsed = 0; int lineCount = 0; int lineLimit = r.height() / fontMetrics().height(); textLayout.beginLayout(); while ( ++lineCount < lineLimit ) { QTextLine line = textLayout.createLine(); if ( !line.isValid() ) break; line.setLineWidth( r.width() ); widthUsed += line.naturalTextWidth(); } textLayout.endLayout(); widthUsed += r.width(); const QString elidedText = fontMetrics().elidedText( m_text, Qt::ElideRight, widthUsed ); p.drawText( r, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, elidedText ); } else { const QString elidedText = fontMetrics().elidedText( m_text, m_mode, r.width() ); p.drawText( r, m_align, elidedText ); } }
} foreach(const STFont::LayoutGlyph& glyph, layout){ if(glyph.charIndex==m_cursorPos){ currentLineY=glyph.rect.center().y(); currentLine=glyph.line; break; } } int maxLine=layout.last().line; currentLineY+=(float)delta*14.f; currentLine+=delta; int newCursorPos=STFont::defaultFont()->cursorPosAtPoint(textLayout(), QPoint(m_lineNavigationX, currentLineY)); if(currentLine<0)newCursorPos=0; if(currentLine>maxLine)newCursorPos=m_text.size(); m_cursorPos=newCursorPos; if(!shift)m_markPos=m_cursorPos; ensureCursorVisible(); blinkCursor(); updateCursor(); update(); }
QRect StyleHelper::drawText(QPainter* p, const QRect& rc, QString& str, int nLines, int nFlags, const QColor& color, const QFont& font, bool bElided) { if (str.isEmpty()) { qDebug() << "[WARNING]: the text should not be empty when drawing!"; return QRect(); } QFontMetrics fm(font); if (rc.height() < (fm.height() + fm.leading()) * nLines) { qDebug() << "[WARNING]: space is not enough for drawing! text: " << str.left(30) << "..."; } //if (rc.width() * nLines < fm.width(str)) { // qDebug() << "[WARNING]: width should bigger than font metrics when drawing! text:" << str.left(30) << "..."; //} p->save(); p->setPen(color); p->setFont(font); int nWidth = 0; int nHeight = 0; int nHeightLine = p->fontMetrics().height() + leading(); QRect rcRet(rc.x(), rc.y(), rc.width(), nHeightLine); rcRet.adjust(margin(), 0, -margin(), 0); QTextLayout textLayout(str, p->font()); QTextOption opt = textLayout.textOption(); opt.setWrapMode(QTextOption::WrapAnywhere); textLayout.setTextOption(opt); textLayout.beginLayout(); while (nLines) { QTextLine line = textLayout.createLine(); if (!line.isValid()) { break; } line.setLineWidth(rcRet.width()); QString lineText; if (nLines == 1 && bElided) { // the last line lineText = p->fontMetrics().elidedText(str, Qt::ElideRight, rcRet.width()); nWidth = qMax<int>(p->fontMetrics().width(lineText), nWidth); } else { lineText = str.left(line.textLength()); nWidth = qMax<int>(line.width(), nWidth); } str.remove(0, line.textLength()); p->drawText(rcRet, nFlags, lineText); nHeight += nHeightLine; rcRet.setRect(rc.x(), rc.y() + nHeight, nWidth, nHeightLine); rcRet.adjust(margin(), 0, -margin(), 0); nLines--; } textLayout.endLayout(); rcRet.setRect(rc.x() + margin(), rc.y(), nWidth + margin(), nHeight); //rcRet.adjust(margin(), 0, -margin(), 0); p->restore(); return rcRet; }
void YTDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { painter->save(); bool white = false; if(index.data(YTDialog::Clicked).toBool()) { painter->drawPixmap(option.rect.adjusted(0,0,0, -1), hoverBackground ); white = true; } else { if(index.row() % 2 == 0) { painter->fillRect(option.rect, QColor("#f0f6ff")); } else { painter->fillRect(option.rect, Qt::white); } } SingleVideoItem* item = qVariantValue<SingleVideoItem*>(index.data()); QSize sz = option.rect.size(); --sz.rheight(); painter->translate(option.rect.x() , option.rect.y()); painter->drawPixmap(0, 0, PIXWIDTH, sz.height(), item->pix ); if(index.data(YTDialog::Clicked).toBool()) { painter->drawPixmap(0, 0, PIXWIDTH, sz.height(), pixBorderOverlayHover); QPoint pop = QRect(0,0,PIXWIDTH, sz.height()).center() - playOverlay.rect().center(); painter->drawPixmap(pop, playOverlay); } else { painter->drawPixmap(0, 0, PIXWIDTH, sz.height(), index.row() %2 == 0 ? pixBorderOverlay1 : pixBorderOverlay2 ); } QFont font = option.font; font.setPixelSize(11); QFontMetrics fm(font); // total time QString totalTime = timeString(item->totalTime); QRect ttRect = fm.boundingRect(totalTime); ttRect.adjust(-2 , 0, 4, 2); ttRect.moveBottomRight(QPoint(PIXWIDTH -9, sz.height() -11 )); painter->setPen(Qt::white); painter->setFont(font); painter->setOpacity(0.7); painter->drawPixmap(ttRect, durationBackground); ttRect.adjust(1, 1, 1, 0); painter->drawText(ttRect, Qt::AlignHCenter | Qt::AlignTop, totalTime); painter->setOpacity(1); // Header font.setPixelSize(14); fm = QFontMetrics(font); painter->setFont(font); painter->setPen(cr(Qt::black, white)); QRect headerRect = fm.boundingRect(item->header); headerRect = QRect( PIXWIDTH + 4 , 7, sz.width() - PIXWIDTH - 8, headerRect.height() + 4 ); painter->drawText(headerRect, Qt::AlignTop | Qt::AlignLeft | Qt::TextSingleLine, fm.elidedText(item->header, Qt::ElideRight, headerRect.width()), &headerRect ); // Footer font.setPixelSize(11); fm = QFontMetrics(font); painter->setPen(cr(QColor("#636363"), white)); painter->setFont(font); QRect footerRect(PIXWIDTH + 4, ttRect.top(), sz.width() - PIXWIDTH - 8, ttRect.height()); painter->drawText(footerRect, Qt::AlignVCenter | Qt::AlignLeft, item->date.toString("yyyy-MM-dd")); painter->drawText(footerRect, Qt::AlignVCenter | Qt::AlignRight, tr("%L1 views").arg(item->views)); //Description font.setPixelSize(12); painter->setFont(font); QRect descRect( PIXWIDTH + 4, headerRect.bottom() + 7, sz.width() - PIXWIDTH - 8, ttRect.top() - headerRect.bottom() - 7); QTextLayout textLayout(item->desc, font); layoutText(textLayout, item->desc, descRect.size()); textLayout.draw(painter, descRect.topLeft()); QPen sepLinePen(QColor("#e3e3e3")); painter->setPen(sepLinePen); painter->drawLine(0, 90, sz.width(), 90); painter->restore(); }