#ifndef QT_NO_ITEMVIEWS
case CE_ItemViewItem:
if (const QStyleOptionViewItemV4 *vopt = qstyleoption_cast<const QStyleOptionViewItemV4 *>(opt)) {
p->save();
p->setClipRect(opt->rect);
QRect checkRect
= subElementRect
(SE_ItemViewItemCheckIndicator, vopt, widget
);
QRect iconRect
= subElementRect
(SE_ItemViewItemDecoration, vopt, widget
);
QRect textRect
= subElementRect
(SE_ItemViewItemText, vopt, widget
);
// draw the background
proxy()->drawPrimitive(PE_PanelItemViewItem, opt, p, widget);
// draw the check mark
QStyleOptionViewItemV4 option(*vopt);
option.rect = checkRect;
option.
state = option.
state & ~
QStyle::State_HasFocus;
switch (vopt->checkState) {
case Qt::Unchecked:
option.
state |
= QStyle::State_Off;
break;
case Qt::PartiallyChecked:
option.
state |
= QStyle::State_NoChange;
break;
case Qt::Checked:
option.
state |
= QStyle::State_On;
break;
}
proxy
()->drawPrimitive
(QStyle::PE_IndicatorViewItemCheck,
&option, p, widget
);
}
// draw the icon
if (!(vopt
->state
& QStyle::State_Enabled)) else if (vopt
->state
& QStyle::State_Selected) vopt->icon.paint(p, iconRect, vopt->decorationAlignment, mode, state);
// draw the text
if (!vopt->text.isEmpty()) {
if (vopt
->state
& QStyle::State_Selected) { p
->setPen
(vopt
->palette.
color(cg,
QPalette::HighlightedText));
} else {
p
->setPen
(vopt
->palette.
color(cg,
QPalette::Text));
}
if (vopt
->state
& QStyle::State_Editing) { p
->setPen
(vopt
->palette.
color(cg,
QPalette::Text));
p->drawRect(textRect.adjusted(0, 0, -1, -1));
}
d->viewItemDrawText(p, vopt, textRect);
}
// draw the focus rect
if (vopt
->state
& QStyle::State_HasFocus) { o.rect = proxy()->subElementRect(SE_ItemViewItemFocusRect, vopt, widget);
o.
state |
= QStyle::State_KeyboardFocusChange;
o.
state |
= QStyle::State_Item;
o.
backgroundColor = vopt
->palette.
color(cg,
(vopt
->state
& QStyle::State_Selected) proxy
()->drawPrimitive
(QStyle::PE_FrameFocusRect,
&o, p, widget
);
}
p->restore();
}
break;
#ifndef QT_NO_ITEMVIEWS
case CE_ItemViewItem:
if (const QStyleOptionViewItemV4 *vopt = qstyleoption_cast<const QStyleOptionViewItemV4 *>(opt)) {
p->save();
p->setClipRect(opt->rect);
QRect checkRect = subElementRect(SE_ItemViewItemCheckIndicator, vopt, widget);
QRect iconRect = subElementRect(SE_ItemViewItemDecoration, vopt, widget);
QRect textRect = subElementRect(SE_ItemViewItemText, vopt, widget);
// draw the background
proxy()->drawPrimitive(PE_PanelItemViewItem, opt, p, widget);
// draw the check mark
if (vopt->features & QStyleOptionViewItemV2::HasCheckIndicator) {
QStyleOptionViewItemV4 option(*vopt);
option.rect = checkRect;
option.state = option.state & ~QStyle::State_HasFocus;
switch (vopt->checkState) {
case Qt::Unchecked:
option.state |= QStyle::State_Off;
break;
case Qt::PartiallyChecked:
option.state |= QStyle::State_NoChange;
break;
case Qt::Checked:
option.state |= QStyle::State_On;
break;
}
proxy()->drawPrimitive(QStyle::PE_IndicatorViewItemCheck, &option, p, widget);
}
// draw the icon
QIcon::Mode mode = QIcon::Normal;
if (!(vopt->state & QStyle::State_Enabled))
mode = QIcon::Disabled;
else if (vopt->state & QStyle::State_Selected)
mode = QIcon::Selected;
QIcon::State state = vopt->state & QStyle::State_Open ? QIcon::On : QIcon::Off;
vopt->icon.paint(p, iconRect, vopt->decorationAlignment, mode, state);
// draw the text
if (!vopt->text.isEmpty()) {
QPalette::ColorGroup cg = vopt->state & QStyle::State_Enabled
? QPalette::Normal : QPalette::Disabled;
if (cg == QPalette::Normal && !(vopt->state & QStyle::State_Active))
cg = QPalette::Inactive;
if (vopt->state & QStyle::State_Selected) {
p->setPen(vopt->palette.color(cg, QPalette::HighlightedText));
} else {
p->setPen(vopt->palette.color(cg, QPalette::Text));
}
if (vopt->state & QStyle::State_Editing) {
p->setPen(vopt->palette.color(cg, QPalette::Text));
p->drawRect(textRect.adjusted(0, 0, -1, -1));
}
d->viewItemDrawText(p, vopt, textRect);
}
// draw the focus rect
if (vopt->state & QStyle::State_HasFocus) {
QStyleOptionFocusRect o;
o.QStyleOption::operator=(*vopt);
o.rect = proxy()->subElementRect(SE_ItemViewItemFocusRect, vopt, widget);
o.state |= QStyle::State_KeyboardFocusChange;
o.state |= QStyle::State_Item;
QPalette::ColorGroup cg = (vopt->state & QStyle::State_Enabled)
? QPalette::Normal : QPalette::Disabled;
o.backgroundColor = vopt->palette.color(cg, (vopt->state & QStyle::State_Selected)
? QPalette::Highlight : QPalette::Window);
proxy()->drawPrimitive(QStyle::PE_FrameFocusRect, &o, p, widget);
}
p->restore();
}
break;
To copy to clipboard, switch view to plain text mode
void QCommonStylePrivate
::viewItemDrawText(QPainter *p,
const QStyleOptionViewItemV4
*option,
const QRect &rect
) const {
const QWidget *widget
= option
->widget;
const int textMargin
= proxyStyle
->pixelMetric
(QStyle::PM_FocusFrameHMargin,
0, widget
) + 1;
QRect textRect
= rect.
adjusted(textMargin,
0,
-textMargin,
0);
// remove width padding textOption.setTextDirection(option->direction);
textOption.
setAlignment(QStyle::visualAlignment(option
->direction, option
->displayAlignment
));
textLayout.setTextOption(textOption);
textLayout.setFont(option->font);
textLayout.setText(option->text);
viewItemTextLayout(textLayout, textRect.width());
qreal height = 0;
qreal width = 0;
int elidedIndex = -1;
const int lineCount = textLayout.lineCount();
for (int j = 0; j < lineCount; ++j) {
if (j + 1 <= lineCount - 1) {
const QTextLine nextLine
= textLayout.
lineAt(j
+ 1);
if ((nextLine.y() + nextLine.height()) > textRect.height()) {
int start = line.textStart();
int length = line.textLength() + nextLine.textLength();
const QStackTextEngine engine(textLayout.text().mid(start, length), option->font);
elidedText = engine.elidedText(option->textElideMode, textRect.width());
height += line.height();
width = textRect.width();
elidedIndex = j;
break;
}
}
if (line.naturalTextWidth() > textRect.width()) {
int start = line.textStart();
int length = line.textLength();
const QStackTextEngine engine(textLayout.text().mid(start, length), option->font);
elidedText = engine.elidedText(option->textElideMode, textRect.width());
height += line.height();
width = textRect.width();
elidedIndex = j;
break;
}
width = qMax<qreal>(width, line.width());
height += line.height();
}
const QRect layoutRect
= QStyle::alignedRect(option
->direction, option
->displayAlignment,
QSize(int(width
),
int(height
)), textRect
);
const QPointF position
= layoutRect.
topLeft();
for (int i = 0; i < lineCount; ++i) {
if (i == elidedIndex) {
qreal x = position.x() + line.x();
qreal y = position.y() + line.y() + line.ascent();
p->save();
p->setFont(option->font);
p
->drawText
(QPointF(x, y
), elidedText
);
p->restore();
break;
}
line.draw(p, position);
}
}
void QCommonStylePrivate::viewItemDrawText(QPainter *p, const QStyleOptionViewItemV4 *option, const QRect &rect) const
{
const QWidget *widget = option->widget;
const int textMargin = proxyStyle->pixelMetric(QStyle::PM_FocusFrameHMargin, 0, widget) + 1;
QRect textRect = rect.adjusted(textMargin, 0, -textMargin, 0); // remove width padding
const bool wrapText = option->features & QStyleOptionViewItemV2::WrapText;
QTextOption textOption;
textOption.setWrapMode(wrapText ? QTextOption::WordWrap : QTextOption::ManualWrap);
textOption.setTextDirection(option->direction);
textOption.setAlignment(QStyle::visualAlignment(option->direction, option->displayAlignment));
QTextLayout textLayout;
textLayout.setTextOption(textOption);
textLayout.setFont(option->font);
textLayout.setText(option->text);
viewItemTextLayout(textLayout, textRect.width());
QString elidedText;
qreal height = 0;
qreal width = 0;
int elidedIndex = -1;
const int lineCount = textLayout.lineCount();
for (int j = 0; j < lineCount; ++j) {
const QTextLine line = textLayout.lineAt(j);
if (j + 1 <= lineCount - 1) {
const QTextLine nextLine = textLayout.lineAt(j + 1);
if ((nextLine.y() + nextLine.height()) > textRect.height()) {
int start = line.textStart();
int length = line.textLength() + nextLine.textLength();
const QStackTextEngine engine(textLayout.text().mid(start, length), option->font);
elidedText = engine.elidedText(option->textElideMode, textRect.width());
height += line.height();
width = textRect.width();
elidedIndex = j;
break;
}
}
if (line.naturalTextWidth() > textRect.width()) {
int start = line.textStart();
int length = line.textLength();
const QStackTextEngine engine(textLayout.text().mid(start, length), option->font);
elidedText = engine.elidedText(option->textElideMode, textRect.width());
height += line.height();
width = textRect.width();
elidedIndex = j;
break;
}
width = qMax<qreal>(width, line.width());
height += line.height();
}
const QRect layoutRect = QStyle::alignedRect(option->direction, option->displayAlignment,
QSize(int(width), int(height)), textRect);
const QPointF position = layoutRect.topLeft();
for (int i = 0; i < lineCount; ++i) {
const QTextLine line = textLayout.lineAt(i);
if (i == elidedIndex) {
qreal x = position.x() + line.x();
qreal y = position.y() + line.y() + line.ascent();
p->save();
p->setFont(option->font);
p->drawText(QPointF(x, y), elidedText);
p->restore();
break;
}
line.draw(p, position);
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks