QSize KsirkChatItem::sizeHint(const QStyleOptionViewItem &option) { unsigned int w = 0; QTextDocument fake; // used to allow to compute lines height fake.setHtml("gpl"); fake.setDefaultFont(option.font); fake.adjustSize(); fake.setTextWidth ( -1 ); qreal h = fake.size().height(); for (int i = 0 ; i < m_order.size(); i++) { QTextDocument rt; rt.setHtml(m_strings[i]); rt.adjustSize(); rt.setTextWidth ( -1 ); QPixmap px(rt.size().toSize()); switch (m_order[i]) { case Text: w += px.width(); break; case Pixmap: if (! m_pixmaps[i].isNull()) { QPixmap scaled = m_pixmaps[i].scaledToHeight((int)h); w+= scaled.width(); } break; default: ; } } // kDebug() << "KsirkChatItem::sizeHint: " << QSize(w,h) << endl; return QSize(w,(int)h); }
void drawHtmlLine(QPainter *painter, const QFont font, QRect rect, QString text, bool multiline, bool leftAligned) { if(!painter){return;} painter->save(); QTextDocument displayDoc; setUpDisplayDoc(displayDoc, font); if(!leftAligned){ text = QString("<div align=\"right\">") + text + "</div>"; } displayDoc.setHtml(text); //multiline == false - Normally if(multiline){ displayDoc.setTextWidth(rect.width()); } else {displayDoc.setTextWidth(-1);} painter->translate(rect.topLeft()); if(multiline){ displayDoc.adjustSize(); } rect.moveTopLeft(QPoint(0,0)); displayDoc.drawContents(painter, rect); painter->restore(); }
void KsirkChatItem::paint(QPainter* p, const QStyleOptionViewItem &option, int row) { // kError() << "KsirkChatItem::paint"; Q_UNUSED(row); QTextDocument fake; // used to allow to compute lines height fake.setHtml("gpl"); fake.setDefaultFont(option.font); fake.adjustSize(); fake.setTextWidth ( -1 ); qreal h = fake.size().height(); unsigned int x = 0; for (int i = 0 ; i < m_order.size(); i++) { QTextDocument rt; rt.setHtml(m_strings[i]); rt.setDefaultFont(option.font); rt.adjustSize(); rt.setTextWidth ( -1 ); QPixmap px(rt.size().toSize()); px.fill(); QPainter pa(&px); rt.drawContents(&pa); switch (m_order[i]) { case Text: // kDebug() << " paint string '" << m_strings[i] << "' at " << x << ", " << row*h << endl; p->drawPixmap(option.rect.x()+x,option.rect.y(),px); x += px.width(); break; case Pixmap: if (! m_pixmaps[i].isNull()) { // kDebug() << " paint pixmap at " << x << ", " << row*h << endl; QPixmap scaled = m_pixmaps[i].scaledToHeight((int)h); p->drawPixmap(option.rect.x()+x,option.rect.y(),scaled); x+= scaled.width(); } break; default: ; } } }
QSize AppClassTreeDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const { QStyleOptionViewItemV4 options = option; initStyleOption(&options, index); QTextDocument doc; doc.setHtml(options.text); doc.setTextWidth(options.rect.width()); doc.adjustSize(); return QSize(doc.idealWidth(), doc.size().height()); }
void MeasureToolPlugin::drawInfobox( GeoPainter *painter ) const { QString boxContent; if (m_paintMode == Polygon) { boxContent += "<strong>" + tr("Polygon Ruler") + ":</strong><br/>\n"; } else /* Circular */ { boxContent += "<strong>" + tr("Circle Ruler") + ":</strong><br/>\n"; } if (m_paintMode == Polygon) { boxContent += tr("Total Distance: %1<br/>\n").arg( meterToPreferredUnit(m_totalDistance) ); if (m_showPolygonArea) boxContent += tr("Area: %1<br/>\n").arg( meterToPreferredUnit(m_polygonArea, true) ); if (m_showPerimeter) boxContent += tr("Perimeter: %1<br/>\n").arg( meterToPreferredUnit(m_perimeter) ); } else /* Circular */ { if (m_showRadius) boxContent += tr("Radius: %1<br/>\n").arg( meterToPreferredUnit(m_radius) ); if (m_showCircumference) boxContent += tr("Circumference: %1<br/>\n").arg( meterToPreferredUnit(m_circumference) ); if (m_showCircularArea) boxContent += tr("Area: %1<br/>\n").arg( meterToPreferredUnit(m_circularArea, true) ); } painter->setPen( QColor( Qt::black ) ); painter->setBrush( QColor( 192, 192, 192, 192 ) ); QTextDocument doc; doc.setHtml(boxContent); doc.setDefaultFont(m_font_regular); doc.adjustSize(); QSizeF pageSize = doc.size(); painter->drawRect( 10, 105, 10 + pageSize.width(), pageSize.height() ); QTransform transform; transform.translate(15, 110); painter->setTransform(transform); doc.drawContents(painter); painter->setTransform(QTransform()); }
void QStaticTextPrivate::paintText(const QPointF &pos, QPainter *p) { bool preferRichText = textFormat == Qt::RichText || (textFormat == Qt::AutoText && Qt::mightBeRichText(text)); if (!preferRichText) { if (maximumSize.isValid()) { QRectF boundingRect; p->drawText(QRectF(pos, maximumSize), Qt::TextWordWrap, text, &boundingRect); actualSize = boundingRect.size(); needsClipRect = boundingRect.width() > maximumSize.width() || boundingRect.height() > maximumSize.height(); } else { p->drawText(pos, text); needsClipRect = false; QFontMetrics fm(font); actualSize = fm.boundingRect(text).size(); } } else { QTextDocument document; document.setDefaultFont(font); document.setHtml(text); QRectF rect = maximumSize.isValid() ? QRectF(pos, maximumSize) : QRectF(); document.adjustSize(); p->save(); p->translate(pos); document.drawContents(p, rect); p->restore(); actualSize = document.size(); needsClipRect = maximumSize.isValid() && (actualSize.width() > maximumSize.width() || actualSize.height() > maximumSize.height()); } }
void QStaticTextPrivate::paintText(const QPointF &topLeftPosition, QPainter *p) { bool preferRichText = textFormat == Qt::RichText || (textFormat == Qt::AutoText && Qt::mightBeRichText(text)); if (!preferRichText) { QTextLayout textLayout; textLayout.setText(text); textLayout.setFont(font); textLayout.setTextOption(textOption); qreal leading = QFontMetricsF(font).leading(); qreal height = -leading; textLayout.beginLayout(); while (1) { QTextLine line = textLayout.createLine(); if (!line.isValid()) break; if (textWidth >= 0.0) line.setLineWidth(textWidth); height += leading; line.setPosition(QPointF(0.0, height)); height += line.height(); } textLayout.endLayout(); actualSize = textLayout.boundingRect().size(); textLayout.draw(p, topLeftPosition); } else { QTextDocument document; #ifndef QT_NO_CSSPARSER QColor color = p->pen().color(); document.setDefaultStyleSheet(QString::fromLatin1("body { color: #%1%2%3 }") .arg(QString::number(color.red(), 16), 2, QLatin1Char('0')) .arg(QString::number(color.green(), 16), 2, QLatin1Char('0')) .arg(QString::number(color.blue(), 16), 2, QLatin1Char('0'))); #endif document.setDefaultFont(font); document.setDocumentMargin(0.0); #ifndef QT_NO_TEXTHTMLPARSER document.setHtml(text); #else document.setPlainText(text); #endif if (textWidth >= 0.0) document.setTextWidth(textWidth); else document.adjustSize(); document.setDefaultTextOption(textOption); p->save(); p->translate(topLeftPosition); QAbstractTextDocumentLayout::PaintContext ctx; ctx.palette.setColor(QPalette::Text, p->pen().color()); document.documentLayout()->draw(p, ctx); p->restore(); if (textWidth >= 0.0) document.adjustSize(); // Find optimal size actualSize = document.size(); } }
QSize AboutLabel::minimumSizeHint() const { QTextDocument *doc = document(); doc->adjustSize(); return QSize(int(doc->size().width()), int(doc->size().height())); }
void QTextDocumentProto::adjustSize() { QTextDocument *item = qscriptvalue_cast<QTextDocument*>(thisObject()); if (item) item->adjustSize(); }
BalloonMsg::BalloonMsg(void *param, const QString &text, QStringList &btn, QWidget *parent, const QRect *rcParent, bool bModal, bool bAutoHide, unsigned bwidth, const QString &box_msg, bool *bChecked) : QDialog(parent, (bAutoHide ? Qt::Popup : Qt::Window | Qt::WindowStaysOnTopHint) | Qt::FramelessWindowHint | Qt::Tool) , m_check(NULL) , m_bAutoHide(bAutoHide) , m_bYes(false) , m_bChecked(bChecked) , m_param(param) , frm(new QFrame(this)) , vlay(new QVBoxLayout(frm)) , lay(new QHBoxLayout()) { setModal(bModal); setAttribute(Qt::WA_DeleteOnClose); setPalette(QToolTip::palette()); bool bTailDown = true; //QFrame *frm = new QFrame(this); //QVBoxLayout *vlay = new QVBoxLayout(frm); vlay->setMargin(0); if (!box_msg.isEmpty()){ m_check = new QCheckBox(box_msg, frm); vlay->addWidget(m_check); if (m_bChecked) m_check->setChecked(*m_bChecked); } //QHBoxLayout *lay = new QHBoxLayout(); vlay->addLayout(lay); lay->setSpacing(5); lay->addStretch(); unsigned id = 0; bool bFirst = true; for (QStringList::Iterator it = btn.begin(); it != btn.end(); ++it, id++){ BalloonButton *b = new BalloonButton(*it, frm, id); connect(b, SIGNAL(action(int)), this, SLOT(action(int))); lay->addWidget(b); if (bFirst){ b->setDefault(true); bFirst = false; } } //setButtonsPict(this); lay->addStretch(); int hButton = frm->minimumSizeHint().height(); int txtWidth = bwidth; QRect rc; if (rcParent){ rc = *rcParent; }else{ QPoint p = parent->mapToGlobal(parent->rect().topLeft()); rc = QRect(p.x(), p.y(), parent->width(), parent->height()); } if (rc.width() > txtWidth) txtWidth = rc.width(); QTextDocument doc; doc.setHtml(text); doc.adjustSize(); doc.setDefaultFont(font()); QSizeF s = doc.size(); QSize sMin = frm->minimumSizeHint(); if (s.width() < sMin.width()) s.setWidth(sMin.width()); int BALLOON_SHADOW = BALLOON_SHADOW_DEF; resize((int)s.width() + BALLOON_R * 2 + BALLOON_SHADOW, (int)s.height() + BALLOON_R * 2 + BALLOON_TAIL + BALLOON_SHADOW + hButton + BALLOON_MARGIN); int w = width() - BALLOON_SHADOW; int tailX = w / 2; int posX = rc.left() + rc.width() / 2 + BALLOON_TAIL_WIDTH - tailX; if (posX <= screenGeometry().left()) posX = 1; QRect rcScreen = screenGeometry(); if (posX + width() >= rcScreen.width()) posX = rcScreen.width() - 1 - width(); int tx = posX + tailX - BALLOON_TAIL_WIDTH; if (tx < rc.left()) tx = rc.left(); if (tx > rc.left() + rc.width()) tx = rc.left() + rc.width(); tailX = tx + BALLOON_TAIL_WIDTH - posX; if (tailX < BALLOON_R) tailX = BALLOON_R; if (tailX > width() - BALLOON_R - BALLOON_TAIL_WIDTH) tailX = width() - BALLOON_R - BALLOON_TAIL_WIDTH; if (rc.top() <= height() + 2){ bTailDown = false; move(posX, rc.top() + rc.height() + 1); }else{ move(posX, rc.top() - height() - 1); } int pos = 0; int h = height() - BALLOON_SHADOW - BALLOON_TAIL; if (!bTailDown) pos += BALLOON_TAIL; frm->resize((int)s.width(), hButton); frm->move(BALLOON_R, pos + h - BALLOON_R - hButton); QPixmap pm(width(), height()); // fill with transparent color pm.fill(Qt::transparent); QPainter p(&pm); p.setRenderHints(QPainter::TextAntialiasing); // we need it more than one time const QBrush backBrush = palette().brush(QPalette::ToolTipBase); p.setPen(palette().color(QPalette::ToolTipText)); p.setBrush(backBrush); // the four corners p.drawEllipse(0, pos, BALLOON_R * 2, BALLOON_R * 2); p.drawEllipse(w - BALLOON_R * 2, pos, BALLOON_R * 2, BALLOON_R * 2); p.drawEllipse(w - BALLOON_R * 2, pos + h - BALLOON_R * 2, BALLOON_R * 2, BALLOON_R * 2); p.drawEllipse(0, pos + h - BALLOON_R * 2, BALLOON_R * 2, BALLOON_R * 2); // tail QPolygon arr(3); arr.setPoint(0, tailX, bTailDown ? h - 1 : pos + 1); arr.setPoint(1, tailX + BALLOON_TAIL_WIDTH, bTailDown ? h - 1 : pos + 1); arr.setPoint(2, tailX - BALLOON_TAIL_WIDTH, bTailDown ? height() - BALLOON_SHADOW : 0); p.drawPolygon(arr); // the inner rest (after tail!) p.fillRect(0, pos + BALLOON_R, w, h - BALLOON_R * 2, backBrush); p.fillRect(BALLOON_R, pos, w - BALLOON_R * 2, h, backBrush); p.setPen(palette().color(QPalette::ToolTipText)); // black lines left and right p.drawLine(0, pos + BALLOON_R, 0, pos + h - BALLOON_R); p.drawLine(w, pos + BALLOON_R, w, pos + h - BALLOON_R); // black lines left and right if (bTailDown){ // upper line p.drawLine(BALLOON_R, 0, w - BALLOON_R, 0); // lower left p.drawLine(BALLOON_R, h, tailX, h); // lower right p.drawLine(tailX + BALLOON_TAIL_WIDTH, h, w - BALLOON_R, h); }else{ // lower line p.drawLine(BALLOON_R, pos + h, w - BALLOON_R, pos + h); // upper left p.drawLine(BALLOON_R, pos, tailX, pos); // upper right p.drawLine(tailX + BALLOON_TAIL_WIDTH, pos, w - BALLOON_R, pos); } // center text p.translate((pm.width() - s.width()) / 2, pos + BALLOON_R); doc.drawContents(&p); p.end(); m_backgroundPixmap = pm; setMask(pm.mask()); if (!bAutoHide) setFocusPolicy(Qt::NoFocus); QWidget *top = NULL; if (parent) top = parent->topLevelWidget(); if (top){ //raiseWindow(top); top->installEventFilter(this); } }