QVector<QRectF> Painter::textExtents(const char * const *strs, int n, Qt::Alignment alignment) { QVector<QRectF> rects(n); QFontMetrics metrics = QFontMetrics(font()); QTransform rtform = transform().inverted(); for (int i = 0; i < n; i++) { QString qstr = QString::fromLocal8Bit(strs[i]); if (qstr.count('\n')) rects[i] = metrics.boundingRect(0, 0, 0, 0, alignment, qstr); // Apparently this is necessary for good vertical bounds // It's noted to be slow on Windows, so might want strWidth method else rects[i] = metrics.tightBoundingRect(qstr); rects[i] = rtform.mapRect(rects[i]); // map from pixels // no real position, so force it to sit on 0 for consistency rects[i].moveTop(0); } return rects; }
void IntervalProgressDisplay::LinearPaintStrategy::drawPoint(qreal x, qreal y, qreal size, QPainter &painter, int value, const QBrush &bgPaint, bool small) { painter.setBrush(bgPaint); painter.drawEllipse(QPointF(x, y), size/2, size/2); painter.setPen(Qt::NoPen); painter.drawEllipse(QPointF(x, y), size/2, size/2); bool drawText = !small; if (drawText) { QString valueString = QString::number(value); painter.setFont(font); QFontMetrics fontMetrics = painter.fontMetrics(); QRect boundingRect = fontMetrics.tightBoundingRect(valueString); qreal textX = x - fontMetrics.width(valueString)/2.0; qreal textY = y + boundingRect.height()/2.0; painter.setPen(Qt::black); painter.drawText(QPointF(textX, textY), valueString); } }
void ContactItemDelegate::paint(QPainter *p, const QStyleOptionViewItem &opt, const QModelIndex &index) const { QStyleOptionViewItemV4 ropt = opt; QRect r = opt.rect.adjusted(5, 5, -5, -8); p->save(); /* Selection (behind the avatar) */ ropt.rect.adjust(0, 0, 0, -3); SelectionState sst = NoSelectionState; if (opt.state & QStyle::State_Selected) sst = Selected; else if (opt.state & QStyle::State_MouseOver) sst = MouseOver; else if (index.data(ContactsModel::AlertRole).toBool()) { sst = Alert; p->setOpacity(parentDelegate->alertOpacity()); parentDelegate->startAlertAnimation(); } if (sst != NoSelectionState) { p->drawPixmap(ropt.rect.topLeft(), customSelectionRect(ropt.rect.size(), sst)); if (sst == Alert) p->setOpacity(1); } /* Avatar */ QPixmap avatar = index.data(Qt::DecorationRole).value<QPixmap>(); QPoint avatarPos = QPoint(r.x() + (35 - avatar.width()) / 2, r.y() + (35 - avatar.height()) / 2); if (!avatar.isNull()) { if (avatar.width() > 35 || avatar.height() > 35) avatar = avatar.scaled(QSize(35, 35), Qt::KeepAspectRatio, Qt::SmoothTransformation); p->drawPixmap(avatarPos, avatar); } /* Status */ QPixmap status = index.data(ContactsModel::StatusIndicator).value<QPixmap>(); if (!status.isNull()) p->drawPixmap(avatarPos - QPoint(status.width()/2-2, status.height()/2-3), status); /* Page switch buttons */ int textWidth = 0; if ((opt.state & QStyle::State_Selected) || (opt.state & QStyle::State_MouseOver)) { ContactsView::Page activePage = parentDelegate->view->activePage(); bool isActive = (parentDelegate->view->currentIndex() == index); /* Chat page */ QRect iconRect(r.right()-16+1, r.top()-1, 16, 16); QPixmap pm; if (isActive && activePage == ContactsView::ContactChatPage) pm = QPixmap(QLatin1String(":/icons/chat-active.png")); else if (iconRect.contains(ropt.widget->mapFromGlobal(QCursor::pos()))) pm = QPixmap(QLatin1String(":/icons/chat-hover.png")); else pm = QPixmap(QLatin1String(":/icons/chat-inactive.png")); p->drawPixmap(iconRect.topLeft(), pm); /* Info page */ iconRect = QRect(r.right()-16+1, r.bottom()-16+1, 16, 16); if (isActive && activePage == ContactsView::ContactInfoPage) pm = QPixmap(QLatin1String(":/icons/info-active.png")); else if (iconRect.contains(ropt.widget->mapFromGlobal(QCursor::pos()))) pm = QPixmap(QLatin1String(":/icons/info-hover.png")); else pm = QPixmap(QLatin1String(":/icons/info-inactive.png")); p->drawPixmap(iconRect.topLeft(), pm); textWidth -= 14; } /* Draw nickname */ r.adjust(41, 0, 0, 0); textWidth += r.width(); QString nickname = index.data().toString(); QFont nickFont = p->font(); nickFont.setPixelSize(12); p->setFont(nickFont); /* Caution: horrifically slow */ QFontMetrics metrics = p->fontMetrics(); QRect nickRect = metrics.tightBoundingRect(nickname); p->drawText(r.topLeft() + QPoint(0, nickRect.height()+1), nickname); /* Draw info text */ QModelIndex infoIndex(index.model()->index(index.row(), 2, index.parent())); QString infoText = infoIndex.data(Qt::DisplayRole).toString(); QFont infoFont = QFont(QLatin1String("Arial")); infoFont.setPixelSize(10); infoFont.setStyleHint(QFont::SansSerif); p->setFont(infoFont); infoText = QFontMetrics(infoFont).elidedText(infoText, Qt::ElideRight, textWidth); p->setPen(infoIndex.data(Qt::ForegroundRole).value<QColor>()); p->drawText(r.bottomLeft() - QPoint(0, 1), infoText); p->restore(); }