void Variometer::paintTopPixmap() { topPixmap = QPixmap(width(),height()); topPixmap.fill(Qt::transparent); QPainter painter(&topPixmap); setupPainter(&painter); painter.save(); double side = qMin(width(), height()); // Round center painter.setPen(Qt::NoPen); painter.setBrush(darkGrayColor); painter.drawEllipse(QPointF(side/2, side/2), side/5, side/5); painter.setPen(Qt::white); // Unit text QTextOption textOption(Qt::AlignCenter); QFont unitFont = defaultFont; unitFont.setPointSizeF(side/20); painter.setFont(unitFont); painter.drawText(QRect(side/2 + side/5, 0, side/2 - side/5, side), Units::unitName(units), textOption); // Up and down arrows QFont arrowFont = defaultFont; arrowFont.setPointSizeF(side/10); painter.setFont(arrowFont); int textstartX = side/2 - side/5; int textWidth = side/2.5f; int textHeight = side/5.f; painter.drawText(QRect(textstartX, textstartX, textWidth, textHeight), QString::fromUtf8("↑"), textOption); painter.drawText(QRect(textstartX, textstartX + textHeight, textWidth, textHeight), QString::fromUtf8("↓"), textOption); painter.restore(); }
void YTDelegate::layoutText(QTextLayout& textLayout, QString text, QSize constraint) const { QTextOption textOption(Qt::AlignJustify); textLayout.setTextOption(textOption); textLayout.setText(text); textLayout.beginLayout(); int lHeight = 0; while(true){ QTextLine line = textLayout.createLine(); if(!line.isValid()) break; line.setLineWidth(constraint.width()); line.setPosition(QPointF(0, lHeight)); if(lHeight + line.height() > constraint.height()) { QTextLine lastLine = textLayout.lineAt(textLayout.lineCount() - 2); QString lastString = text.mid(lastLine.textStart()); QFontMetrics fm(textLayout.font()); text.chop(lastString.length()); text += fm.elidedText(lastString, Qt::ElideRight, constraint.width()-1); textLayout.endLayout(); layoutText(textLayout, text, constraint); return; } lHeight += line.height(); lHeight += line.leading(); } textLayout.endLayout(); }
void DAbstractSliderSpinBox::paintBreeze(QPainter& painter) { Q_D(DAbstractSliderSpinBox); QStyleOptionSpinBox spinOpts = spinBoxOptions(); QStyleOptionProgressBar progressOpts = progressBarOptions(); QString valueText = progressOpts.text; progressOpts.text = QLatin1String(""); progressOpts.rect.adjust(0, 1, 0, -1); style()->drawComplexControl(QStyle::CC_SpinBox, &spinOpts, &painter, this); style()->drawControl(QStyle::CE_ProgressBarGroove, &progressOpts, &painter, this); painter.save(); QRect leftRect; int progressIndicatorPos = (progressOpts.progress - double(progressOpts.minimum)) / qMax(double(1.0), double(progressOpts.maximum) - progressOpts.minimum) * progressOpts.rect.width(); if (progressIndicatorPos >= 0 && progressIndicatorPos <= progressOpts.rect.width()) { leftRect = QRect(progressOpts.rect.left(), progressOpts.rect.top(), progressIndicatorPos, progressOpts.rect.height()); } else if (progressIndicatorPos > progressOpts.rect.width()) { painter.setPen(palette().highlightedText().color()); } else { painter.setPen(palette().buttonText().color()); } QRegion rightRect = progressOpts.rect; rightRect = rightRect.subtracted(leftRect); painter.setClipRegion(rightRect); QTextOption textOption(Qt::AlignAbsolute | Qt::AlignHCenter | Qt::AlignVCenter); textOption.setWrapMode(QTextOption::NoWrap); if (!(d->edit && d->edit->isVisible())) { painter.drawText(progressOpts.rect, valueText, textOption); } if (!leftRect.isNull()) { painter.setPen(palette().highlightedText().color()); painter.setClipRect(leftRect); style()->drawControl(QStyle::CE_ProgressBarContents, &progressOpts, &painter, this); if (!(d->edit && d->edit->isVisible())) { painter.drawText(progressOpts.rect, valueText, textOption); } } painter.restore(); }
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QColor color(rand() % 255, rand() % 255, rand() % 255, 200); QBrush brush(color); painter->setBackground(brush); painter->setPen(color); painter->drawRect(option.rect); Qt::Alignment alignment = Qt::AlignRight; QTextOption textOption(alignment); QRectF rect = option.rect; qDebug() << index.data(Qt::DisplayRole); painter->drawText(rect, index.data().toString(), textOption); }
void MouseStatWidget::paintEvent(QPaintEvent *) { QPainter painter(this); painter.setPen(Qt::black); painter.drawRect(rect()); QStringList text; text << ((acceptTabletEvent) ? " - tablet events accepted - " : " - tablet events ignored - "); text << QString("Number of tablet events received in the last second: %1").arg(QString::number(receivedTabletEventCountToPaint)); text << QString("Number of mouse events received in the last second: %1").arg(QString::number(receivedMouseEventCountToPaint)); QTextOption textOption(Qt::AlignCenter); textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere); painter.drawText(rect(), text.join("\n"), textOption); }
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { if (option.state & QStyle::State_Selected) { painter->fillRect(option.rect, QBrush("#5990EF")); } QColor color(rand() % 255, rand() % 255, rand() % 255, 200); painter->setPen(color); Qt::Alignment alignment = Qt::AlignRight; QTextOption textOption(alignment); QRectF rect = option.rect; qDebug() << index.data(Qt::DisplayRole); painter->drawText(rect, index.data().toString(), textOption); }
void tst_QStaticText::copyConstructor() { QStaticText text(QLatin1String("My text")); QTextOption textOption(Qt::AlignRight); text.setTextOption(textOption); text.setPerformanceHint(QStaticText::AggressiveCaching); text.setTextWidth(123.456); text.setTextFormat(Qt::PlainText); QStaticText copiedText(text); copiedText.setText(QLatin1String("Other text")); QCOMPARE(copiedText.textOption().alignment(), Qt::AlignRight); QCOMPARE(copiedText.performanceHint(), QStaticText::AggressiveCaching); QCOMPARE(copiedText.textWidth(), 123.456); QCOMPARE(copiedText.textFormat(), Qt::PlainText); QStaticText otherCopiedText(copiedText); otherCopiedText.setTextWidth(789); QCOMPARE(otherCopiedText.text(), QString::fromLatin1("Other text")); }
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { if (!index.isValid()) { return; } bool mouseOver=option.state&QStyle::State_MouseOver; bool selected=option.state&QStyle::State_Selected; if (standard) { if (GtkStyle::isActive()) { bool mouseOver=option.state&QStyle::State_MouseOver; QStyleOptionViewItemV4 opt = option; initStyleOption(&opt, index); if (!underMouse) { mouseOver=false; } if (mouseOver) { opt.showDecorationSelected=true; GtkStyle::drawSelection(option, painter, selected ? 0.75 : 0.25); opt.showDecorationSelected=false; opt.state&=~(QStyle::State_MouseOver|QStyle::State_Selected); opt.backgroundBrush=QBrush(Qt::transparent); if (selected) { opt.palette.setBrush(QPalette::Text, opt.palette.highlightedText()); } } QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget); } else { QStyledItemDelegate::paint(painter, option, index); } return; } bool gtk=mouseOver && GtkStyle::isActive(); bool drawBgnd=true; if (!underMouse) { if (mouseOver && !selected) { drawBgnd=false; } mouseOver=false; } const QString text = index.model()->data(index, Qt::DisplayRole).toString(); const QIcon icon = index.model()->data(index, Qt::DecorationRole).value<QIcon>(); const QPixmap pixmap = icon.pixmap(iconSize, iconSize); QFontMetrics fm = painter->fontMetrics(); #if QT_VERSION >= 0x050100 QSize layoutSize = pixmap.size() / pixmap.devicePixelRatio(); #else QSize layoutSize = pixmap.size(); #endif QTextLayout iconTextLayout(text, option.font); QTextOption textOption(Qt::AlignHCenter); iconTextLayout.setTextOption(textOption); int maxWidth = qMax(3 * layoutSize.width(), 8 * fm.height()); layoutText(&iconTextLayout, maxWidth); QPen pen = painter->pen(); QPalette::ColorGroup cg = option.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled; if (cg == QPalette::Normal && !(option.state & QStyle::State_Active)) { cg = QPalette::Inactive; } QStyleOptionViewItemV4 opt(option); opt.showDecorationSelected = true; QStyle *style = opt.widget ? opt.widget->style() : QApplication::style(); if (drawBgnd) { if (mouseOver && gtk) { GtkStyle::drawSelection(opt, painter, selected ? 0.75 : 0.25); } else { style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget); } } #ifndef Q_OS_WIN if (selected) { painter->setPen(option.palette.color(cg, QPalette::HighlightedText)); } else #endif { painter->setPen(option.palette.color(cg, QPalette::Text)); } painter->drawPixmap(option.rect.x() + (option.rect.width()/2)-(layoutSize.width()/2), option.rect.y() + 5, pixmap); if (!text.isEmpty()) { iconTextLayout.draw(painter, QPoint(option.rect.x() + (option.rect.width()/2)-(maxWidth/2), option.rect.y() + layoutSize.height()+7)); } painter->setPen(pen); drawFocus(painter, option, option.rect); }
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { if (!index.isValid()) { return; } bool mouseOver = option.state & QStyle::State_MouseOver; bool gtk = mouseOver && qApp->style()->inherits("QGtkStyle"); bool selected = option.state & QStyle::State_Selected; bool drawBgnd = true; if (!underMouse) { if (mouseOver && !selected) { drawBgnd = false; } mouseOver = false; } const QString text = index.model()->data(index, Qt::DisplayRole).toString(); const QIcon icon = index.model()->data(index, Qt::DecorationRole).value<QIcon>(); const QPixmap pixmap = icon.pixmap(iconSize, iconSize); QFontMetrics fm = painter->fontMetrics(); int wp = pixmap.width(); int hp = pixmap.height(); QTextLayout iconTextLayout(text, option.font); QTextOption textOption(Qt::AlignHCenter); iconTextLayout.setTextOption(textOption); int maxWidth = qMax(3 * wp, 8 * fm.height()); layoutText(&iconTextLayout, maxWidth); QPen pen = painter->pen(); QPalette::ColorGroup cg = option.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled; if (cg == QPalette::Normal && !(option.state & QStyle::State_Active)) { cg = QPalette::Inactive; } QStyleOptionViewItem opt(option); opt.showDecorationSelected = true; QStyle *style = opt.widget ? opt.widget->style() : QApplication::style(); if (drawBgnd) { if (mouseOver && gtk) { painter->save(); opt.state |= QStyle::State_Selected; painter->setOpacity(selected ? 0.75 : 0.25); } style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget); if (mouseOver && gtk) { painter->restore(); } } if (selected) { painter->setPen(option.palette.color(cg, QPalette::HighlightedText)); } else { painter->setPen(option.palette.color(cg, QPalette::Text)); } painter->drawPixmap(option.rect.x() + (option.rect.width() / 2) - (wp / 2), option.rect.y() + 5, pixmap); if (!text.isEmpty()) { iconTextLayout.draw(painter, QPoint(option.rect.x() + (option.rect.width() / 2) - (maxWidth / 2), option.rect.y() + hp + 7)); } painter->setPen(pen); drawFocus(painter, option, option.rect); }
QSizeF KStandardItemListWidgetInformant::itemSizeHint(int index, const KItemListView* view) const { const QHash<QByteArray, QVariant> values = view->model()->data(index); const KItemListStyleOption& option = view->styleOption(); const int additionalRolesCount = qMax(view->visibleRoles().count() - 1, 0); switch (static_cast<const KStandardItemListView*>(view)->itemLayout()) { case KStandardItemListWidget::IconsLayout: { const QString text = KStringHandler::preProcessWrap(values["text"].toString()); const qreal itemWidth = view->itemSize().width(); const qreal maxWidth = itemWidth - 2 * option.padding; QTextLine line; // Calculate the number of lines required for wrapping the name QTextOption textOption(Qt::AlignHCenter); textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere); qreal textHeight = 0; QTextLayout layout(text, option.font); layout.setTextOption(textOption); layout.beginLayout(); while ((line = layout.createLine()).isValid()) { line.setLineWidth(maxWidth); line.naturalTextWidth(); textHeight += line.height(); } layout.endLayout(); // Add one line for each additional information textHeight += additionalRolesCount * option.fontMetrics.lineSpacing(); const qreal maxTextHeight = option.maxTextSize.height(); if (maxTextHeight > 0 && textHeight > maxTextHeight) { textHeight = maxTextHeight; } return QSizeF(itemWidth, textHeight + option.iconSize + option.padding * 3); } case KStandardItemListWidget::CompactLayout: { // For each row exactly one role is shown. Calculate the maximum required width that is necessary // to show all roles without horizontal clipping. qreal maximumRequiredWidth = 0.0; foreach (const QByteArray& role, view->visibleRoles()) { const QString text = roleText(role, values); const qreal requiredWidth = option.fontMetrics.width(text); maximumRequiredWidth = qMax(maximumRequiredWidth, requiredWidth); } qreal width = option.padding * 4 + option.iconSize + maximumRequiredWidth; const qreal maxWidth = option.maxTextSize.width(); if (maxWidth > 0 && width > maxWidth) { width = maxWidth; } const qreal height = option.padding * 2 + qMax(option.iconSize, (1 + additionalRolesCount) * option.fontMetrics.lineSpacing()); return QSizeF(width, height); } case KStandardItemListWidget::DetailsLayout: { const qreal height = option.padding * 2 + qMax(option.iconSize, option.fontMetrics.height()); return QSizeF(-1, height); } default: Q_ASSERT(false); break; } return QSize(); }
void ColumnItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const { PlayableItem* item = m_model->sourceModel()->itemFromIndex( m_model->mapToSource( index ) ); if ( !item ) return; QTextOption textOption( Qt::AlignVCenter | (Qt::Alignment)index.data( Qt::TextAlignmentRole ).toUInt() ); textOption.setWrapMode( QTextOption::NoWrap ); QString text; if ( !item->artist().isNull() ) { text = item->artist()->name(); } else if ( !item->album().isNull() ) { text = item->album()->name(); } else if ( !item->result().isNull() || !item->query().isNull() ) { float opacity = item->result() && item->result()->isOnline() ? 1.0 : 0.0; opacity = qMax( (float)0.3, opacity ); QColor textColor = TomahawkUtils::alphaBlend( option.palette.color( QPalette::Foreground ), option.palette.color( QPalette::Background ), opacity ); { QStyleOptionViewItemV4 o = option; initStyleOption( &o, QModelIndex() ); painter->save(); o.palette.setColor( QPalette::Text, textColor ); if ( m_view->currentIndex() == index ) o.state |= QStyle::State_Selected; else o.state &= ~QStyle::State_Selected; if ( o.state & QStyle::State_Selected && o.state & QStyle::State_Active ) { o.palette.setColor( QPalette::Text, o.palette.color( QPalette::HighlightedText ) ); } if ( item->isPlaying() ) { textColor = TomahawkStyle::NOW_PLAYING_ITEM_TEXT; o.palette.setColor( QPalette::Highlight, TomahawkStyle::NOW_PLAYING_ITEM ); o.palette.setColor( QPalette::Text, TomahawkStyle::NOW_PLAYING_ITEM_TEXT ); o.state |= QStyle::State_Selected; } int oldX = 0; // if ( m_view->header()->visualIndex( index.column() ) == 0 ) { oldX = o.rect.x(); o.rect.setX( 0 ); } qApp->style()->drawControl( QStyle::CE_ItemViewItem, &o, painter ); if ( oldX > 0 ) o.rect.setX( oldX ); /* if ( m_hoveringOver == index && !index.data().toString().isEmpty() && index.column() == 0 ) { o.rect.setWidth( o.rect.width() - o.rect.height() ); QRect arrowRect( o.rect.x() + o.rect.width(), o.rect.y() + 1, o.rect.height() - 2, o.rect.height() - 2 ); QPixmap infoIcon = TomahawkUtils::defaultPixmap( TomahawkUtils::InfoIcon, TomahawkUtils::Original, arrowRect.size() ); painter->drawPixmap( arrowRect, infoIcon ); m_infoButtonRects[ index ] = arrowRect; }*/ { QRect r = o.rect.adjusted( 3, 0, 0, 0 ); // Paint Now Playing Speaker Icon if ( item->isPlaying() ) { const int pixMargin = 1; const int pixHeight = r.height() - pixMargin * 2; QRect npr = r.adjusted( pixMargin, pixMargin, pixHeight - r.width() + pixMargin, -pixMargin ); painter->drawPixmap( npr, TomahawkUtils::defaultPixmap( TomahawkUtils::NowPlayingSpeaker, TomahawkUtils::Original, npr.size() ) ); r.adjust( pixHeight + 6, 0, 0, 0 ); } painter->setPen( o.palette.text().color() ); QString text = index.data().toString(); if ( item->query()->track()->albumpos() > 0 ) { text = QString( "%1. %2" ) .arg( index.data( PlayableModel::AlbumPosRole ).toString() ) .arg( index.data().toString() ); } text = painter->fontMetrics().elidedText( text, Qt::ElideRight, r.width() - 3 ); painter->drawText( r.adjusted( 0, 1, 0, 0 ), text, textOption ); } painter->restore(); } return; } else return; if ( text.trimmed().isEmpty() ) text = tr( "Unknown" ); QStyleOptionViewItemV4 opt = option; initStyleOption( &opt, QModelIndex() ); const QModelIndex curIndex = m_view->currentIndex(); if ( curIndex == index || curIndex.parent() == index || curIndex.parent().parent() == index ) opt.state |= QStyle::State_Selected; else opt.state &= ~QStyle::State_Selected; qApp->style()->drawControl( QStyle::CE_ItemViewItem, &opt, painter ); if ( opt.state & QStyle::State_Selected ) { opt.palette.setColor( QPalette::Text, opt.palette.color( QPalette::HighlightedText ) ); } QRect arrowRect( m_view->viewport()->width() - option.rect.height(), option.rect.y() + 1, option.rect.height() - 2, option.rect.height() - 2 ); if ( m_hoveringOver.row() == index.row() && m_hoveringOver.parent() == index.parent() ) { QPixmap infoIcon = TomahawkUtils::defaultPixmap( TomahawkUtils::InfoIcon, TomahawkUtils::Original, arrowRect.size() ); painter->drawPixmap( arrowRect, infoIcon ); m_infoButtonRects[ index ] = arrowRect; } if ( index.column() > 0 ) return; painter->save(); painter->setRenderHint( QPainter::TextAntialiasing ); painter->setPen( opt.palette.color( QPalette::Text ) ); QRect r = option.rect.adjusted( 8, 2, -option.rect.width() + option.rect.height() + 4, -2 ); // painter->drawPixmap( r, QPixmap( RESPATH "images/cover-shadow.png" ) ); if ( !m_pixmaps.contains( index ) ) { if ( !item->album().isNull() ) { m_pixmaps.insert( index, QSharedPointer< Tomahawk::PixmapDelegateFader >( new Tomahawk::PixmapDelegateFader( item->album(), r.size(), TomahawkUtils::Original, false ) ) ); _detail::Closure* closure = NewClosure( m_pixmaps[ index ], SIGNAL( repaintRequest() ), const_cast<ColumnItemDelegate*>(this), SLOT( doUpdateIndex( const QPersistentModelIndex& ) ), QPersistentModelIndex( index ) ); closure->setAutoDelete( false ); } else if ( !item->artist().isNull() )
void DiffView::paintCell(QPainter *p, int row, int col) { QFontMetrics fm(font()); DiffViewItem *item = items.at(row); int width = cellWidth(col); int height = cellHeight(); QColor backgroundColor; bool inverted; Qt::Alignment align; int innerborder; QString str; QFont oldFont(p->font()); if (item->type==Separator) { backgroundColor = KColorScheme(QPalette::Active, KColorScheme::Selection).background().color(); p->setPen(KColorScheme(QPalette::Active, KColorScheme::Selection).foreground().color()); inverted = false; align = Qt::AlignLeft; innerborder = 0; if (col == (linenos?1:0) + (marker?1:0)) str = item->line; QFont f(oldFont); f.setBold(true); p->setFont(f); } else if (col == 0 && linenos) { backgroundColor = KColorScheme(QPalette::Active, KColorScheme::Selection).background().color(); p->setPen(KColorScheme(QPalette::Active, KColorScheme::Selection).foreground().color()); inverted = false; align = Qt::AlignLeft; innerborder = 0; if (item->no == -1) str = "+++++"; else str.setNum(item->no); } else if (marker && (col == 0 || col == 1)) { backgroundColor = KColorScheme(QPalette::Active, KColorScheme::View).background(KColorScheme::AlternateBackground).color(); p->setPen(KColorScheme(QPalette::Active, KColorScheme::View).foreground().color()); inverted = false; align = Qt::AlignRight; innerborder = BORDER; str = (item->type==Change)? i18n("Change") : (item->type==Insert)? i18n("Insert") : (item->type==Delete)? i18n("Delete") : QString(); } else { backgroundColor = (item->type==Change)? diffChangeColor : (item->type==Insert)? diffInsertColor : (item->type==Delete)? diffDeleteColor : (item->type==Neutral)? KColorScheme(QPalette::Active, KColorScheme::View).background(KColorScheme::AlternateBackground).color() : KColorScheme(QPalette::Active, KColorScheme::View).background().color(); p->setPen(KColorScheme(QPalette::Active, KColorScheme::View).foreground().color()); inverted = item->inverted; align = Qt::AlignLeft; innerborder = 0; str = item->line; } if (inverted) { p->setPen(backgroundColor); backgroundColor = KColorScheme(QPalette::Active, KColorScheme::View).foreground().color(); QFont f(oldFont); f.setBold(true); p->setFont(f); } p->fillRect(0, 0, width, height, backgroundColor); QTextOption textOption(align); textOption.setTabStop(m_tabWidth * fm.width(' ')); p->drawText(QRectF(innerborder, 0, width-2*innerborder, height), str, textOption); p->setFont(oldFont); }
void DAbstractSliderSpinBox::paintPlastique(QPainter& painter) { Q_D(DAbstractSliderSpinBox); QStyleOptionSpinBox spinOpts = spinBoxOptions(); QStyleOptionProgressBar progressOpts = progressBarOptions(); style()->drawComplexControl(QStyle::CC_SpinBox, &spinOpts, &painter, d->dummySpinBox); painter.save(); QRect rect = progressOpts.rect.adjusted(2, 0, -2, 0); QRect leftRect; int progressIndicatorPos = (progressOpts.progress - double(progressOpts.minimum)) / qMax(double(1.0), double(progressOpts.maximum) - progressOpts.minimum) * rect.width(); if (progressIndicatorPos >= 0 && progressIndicatorPos <= rect.width()) { leftRect = QRect(rect.left(), rect.top(), progressIndicatorPos, rect.height()); } else if (progressIndicatorPos > rect.width()) { painter.setPen(palette().highlightedText().color()); } else { painter.setPen(palette().buttonText().color()); } QRegion rightRect = rect; rightRect = rightRect.subtracted(leftRect); QTextOption textOption(Qt::AlignAbsolute | Qt::AlignHCenter | Qt::AlignVCenter); textOption.setWrapMode(QTextOption::NoWrap); if (!(d->edit && d->edit->isVisible())) { painter.setClipRegion(rightRect); painter.setClipping(true); painter.drawText(rect.adjusted(-2, 0, 2, 0), progressOpts.text, textOption); painter.setClipping(false); } if (!leftRect.isNull()) { painter.setPen(palette().highlight().color()); painter.setBrush(palette().highlight()); painter.drawRect(leftRect.adjusted(0, 0, 0, -1)); if (!(d->edit && d->edit->isVisible())) { painter.setPen(palette().highlightedText().color()); painter.setClipRect(leftRect.adjusted(0, 0, 1, 0)); painter.setClipping(true); painter.drawText(rect.adjusted(-2, 0, 2, 0), progressOpts.text, textOption); painter.setClipping(false); } } painter.restore(); }
void TreeItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const { PlayableItem* item = m_model->sourceModel()->itemFromIndex( m_model->mapToSource( index ) ); if ( !item ) return; QTextOption textOption( Qt::AlignVCenter | (Qt::Alignment)index.data( Qt::TextAlignmentRole ).toUInt() ); textOption.setWrapMode( QTextOption::NoWrap ); QString text; if ( !item->artist().isNull() ) { text = item->artist()->name(); } else if ( !item->album().isNull() ) { text = item->album()->name(); } else if ( !item->result().isNull() || !item->query().isNull() ) { float opacity = item->result().isNull() ? 0.0 : item->result()->score(); opacity = qMax( (float)0.3, opacity ); QColor textColor = TomahawkUtils::alphaBlend( option.palette.color( QPalette::Foreground ), option.palette.color( QPalette::Background ), opacity ); { QStyleOptionViewItemV4 o = option; initStyleOption( &o, QModelIndex() ); painter->save(); o.palette.setColor( QPalette::Text, textColor ); if ( o.state & QStyle::State_Selected && o.state & QStyle::State_Active ) { o.palette.setColor( QPalette::Text, o.palette.color( QPalette::HighlightedText ) ); } if ( item->isPlaying() ) { o.palette.setColor( QPalette::Highlight, o.palette.color( QPalette::Mid ) ); if ( o.state & QStyle::State_Selected ) o.palette.setColor( QPalette::Text, textColor ); o.state |= QStyle::State_Selected; } int oldX = 0; if ( m_view->header()->visualIndex( index.column() ) == 0 ) { oldX = o.rect.x(); o.rect.setX( 0 ); } qApp->style()->drawControl( QStyle::CE_ItemViewItem, &o, painter ); if ( oldX > 0 ) o.rect.setX( oldX ); if ( m_view->hoveredIndex().row() == index.row() && m_view->hoveredIndex().column() == index.column() && ( index.column() == 0 ) ) { o.rect.setWidth( o.rect.width() - 16 ); QRect arrowRect( o.rect.x() + o.rect.width(), o.rect.y() + 1, o.rect.height() - 2, o.rect.height() - 2 ); QPixmap infoIcon = TomahawkUtils::defaultPixmap( TomahawkUtils::InfoIcon, TomahawkUtils::Original, arrowRect.size() ); painter->drawPixmap( arrowRect, infoIcon ); } { QRect r = o.rect.adjusted( 3, 0, 0, 0 ); // Paint Now Playing Speaker Icon if ( item->isPlaying() && m_view->header()->visualIndex( index.column() ) == 0 ) { r.adjust( 0, 0, 0, -3 ); QRect npr = r.adjusted( 3, 1, 18 - r.width(), 1 ); painter->drawPixmap( npr, TomahawkUtils::defaultPixmap( TomahawkUtils::NowPlayingSpeaker, TomahawkUtils::Original, npr.size() ) ); r.adjust( 25, 0, 0, 3 ); } painter->setPen( o.palette.text().color() ); QString text = painter->fontMetrics().elidedText( index.data().toString(), Qt::ElideRight, r.width() - 3 ); painter->drawText( r.adjusted( 0, 1, 0, 0 ), text, textOption ); } painter->restore(); } return; } else return; if ( text.trimmed().isEmpty() ) text = tr( "Unknown" ); QStyleOptionViewItemV4 opt = option; initStyleOption( &opt, QModelIndex() ); qApp->style()->drawControl( QStyle::CE_ItemViewItem, &opt, painter ); if ( option.state & QStyle::State_Selected ) { opt.palette.setColor( QPalette::Text, opt.palette.color( QPalette::HighlightedText ) ); } if ( index.column() > 0 ) return; painter->save(); painter->setRenderHint( QPainter::Antialiasing ); painter->setPen( opt.palette.color( QPalette::Text ) ); QRect r = option.rect.adjusted( 4, 4, -option.rect.width() + option.rect.height() - 4, -4 ); // painter->drawPixmap( r, QPixmap( RESPATH "images/cover-shadow.png" ) ); if ( !m_pixmaps.contains( index ) ) { if ( !item->album().isNull() ) { m_pixmaps.insert( index, QSharedPointer< Tomahawk::PixmapDelegateFader >( new Tomahawk::PixmapDelegateFader( item->album(), r.size(), TomahawkUtils::ScaledCover, false ) ) ); _detail::Closure* closure = NewClosure( m_pixmaps[ index ], SIGNAL( repaintRequest() ), const_cast<TreeItemDelegate*>(this), SLOT( doUpdateIndex( const QPersistentModelIndex& ) ), QPersistentModelIndex( index ) ); closure->setAutoDelete( false ); } else if ( !item->artist().isNull() )
void CollectionListViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { painter->save(); if (option.state & QStyle::State_Selected) { painter->fillRect(option.rect, option.palette.highlight()); painter->setPen(option.palette.highlightedText().color()); } QFont font = QApplication::font(); QFontMetrics fm(font); QFont subFont(font); subFont.setPointSize(font.pointSize() - 2); QFontMetrics sfm(subFont); QString headerText = index.data(CollectionItem::NameRole).toString(); QString subText = index.data(CollectionItem::SizeTextRole).toString() + " | " + \ "subdir: " + index.data(CollectionItem::SubdirTextRole).toString(); QSize trueSize(trueSizeHint(option, index)); QSize size(sizeHint(option, index)); if (trueSize.width() != size.width()) { int n = 0; if (fm.width(headerText) > size.width()) { for (n = headerText.size(); fm.width(QString(headerText.left(n) + "...")) > option.rect.width(); n--); headerText = headerText.left(n) + "..."; } } QTextOption textOption(Qt::AlignHCenter); QRect basicRect = option.rect; if (option.rect.width() > collectionListView->width()) { if (size.width() <= collectionListView->width()) { basicRect.setLeft(0); basicRect.setRight(collectionListView->width()); } } QRect headerRect = basicRect; headerRect.setTop(headerRect.top()+5); headerRect.setBottom(headerRect.top()+fm.height()); QRect subRect = basicRect; subRect.setTop(headerRect.bottom()); subRect.setBottom(subRect.top() + sfm.height()); painter->setFont(font); painter->drawText(headerRect,headerText, textOption); painter->setFont(subFont); painter->drawText(subRect,subText, textOption); painter->restore(); }