Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make TableView rendering consistent with what Qt native does #84

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 34 additions & 14 deletions lib/src/style/QlementineStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -861,8 +861,31 @@ void QlementineStyle::drawPrimitive(PrimitiveElement pe, const QStyleOption* opt
const auto focus =
widgetHasFocus && selection == SelectionState::Selected ? FocusState::Focused : FocusState::NotFocused;
const auto active = getActiveState(itemState);
const auto& color = listItemBackgroundColor(mouse, selection, focus, active, optItem->index, w);
p->fillRect(rect, color);
if (const QStyleOptionViewItem *vopt = qstyleoption_cast<const QStyleOptionViewItem*>(opt)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use auto as much as possible 😊

Suggested change
if (const QStyleOptionViewItem *vopt = qstyleoption_cast<const QStyleOptionViewItem*>(opt)) {
if (const auto *vopt = qstyleoption_cast<const QStyleOptionViewItem*>(opt)) {

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, why do you cast again opt? It's already made a few lines above?
So the code in the else statement will never be called... So you broke the styling made by Qlementine.

QPalette::ColorGroup cg = (w ? w->isEnabled() : (vopt->state & QStyle::State_Enabled))
? QPalette::Normal : QPalette::Disabled;
if (cg == QPalette::Normal && !(vopt->state & QStyle::State_Active))
cg = QPalette::Inactive;
Comment on lines +867 to +868
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use brackets 😊

Suggested change
if (cg == QPalette::Normal && !(vopt->state & QStyle::State_Active))
cg = QPalette::Inactive;
if (cg == QPalette::Normal && !(vopt->state & QStyle::State_Active)) {
cg = QPalette::Inactive;
}


if (vopt->showDecorationSelected && (vopt->state & QStyle::State_Selected)) {
p->fillRect(vopt->rect, vopt->palette.brush(cg, QPalette::Highlight));
} else {
if (vopt->backgroundBrush.style() != Qt::NoBrush) {
QPointF oldBO = p->brushOrigin();
p->setBrushOrigin(vopt->rect.topLeft());
p->fillRect(vopt->rect, vopt->backgroundBrush);
p->setBrushOrigin(oldBO);
}

if (vopt->state & QStyle::State_Selected) {
QRect textRect = subElementRect(QStyle::SE_ItemViewItemText, opt, w);
p->fillRect(textRect, vopt->palette.brush(cg, QPalette::Highlight));
}
}
} else {
const auto &color = listItemBackgroundColor(mouse, selection, focus, active, optItem->index, w);
p->fillRect(rect, color);
}

// Border on the left if necessary.
if (column == 0) {
Expand Down Expand Up @@ -3935,18 +3958,16 @@ QSize QlementineStyle::sizeFromContents(
}
const auto lineW = _impl->theme.borderWidth;
const auto iconExtent = pixelMetric(PM_SmallIconSize, opt);
const auto fm = QFontMetrics(font);
const auto textW = qlementine::textWidth(fm, optHeader->text);
const QSize textSize = optHeader->fontMetrics.size(0, optHeader->text);
const auto& icon = optHeader->icon;
const auto hasIcon = !icon.isNull();
const auto iconW = hasIcon ? iconExtent + spacing : 0;
const auto hasArrow = optHeader->sortIndicator != QStyleOptionHeader::SortIndicator::None;
const auto arrowW = hasArrow ? iconExtent + spacing : 0;
const auto paddingH = pixelMetric(PM_HeaderMargin);
const auto paddingV = paddingH / 2;
const auto textH = fm.height();
const auto w = lineW + paddingH + iconW + textW + arrowW + paddingH + lineW;
const auto h = lineW + paddingV + std::max(iconExtent, textH) + paddingV + lineW;
const auto w = lineW + paddingH + iconW + textSize.width() + arrowW + paddingH + lineW;
const auto h = lineW + paddingV + std::max(iconExtent, textSize.height()) + paddingV + lineW;
return QSize{ w, h };
}
break;
Expand Down Expand Up @@ -3982,19 +4003,18 @@ QSize QlementineStyle::sizeFromContents(
const auto& iconSize = hasIcon ? optItem->decorationSize : QSize{ 0, 0 };

const auto hasText = features.testFlag(QStyleOptionViewItem::HasDisplay) && !optItem->text.isEmpty();
const auto textH = hasText ? optItem->fontMetrics.height() : 0;
QSize textSize(0,0);
if (hasText) {
textSize = optItem->fontMetrics.size(0, optItem->text);
}

const auto hasCheck = features.testFlag(QStyleOptionViewItem::HasCheckIndicator);
const auto& checkSize = hasCheck ? _impl->theme.iconSize : QSize{ 0, 0 };

auto font = QFont(widget->font());
const auto fm = QFontMetrics(font);
const auto textW = qlementine::textWidth(fm, optItem->text);

const auto w = textW + 2 * hPadding + (iconSize.width() > 0 ? iconSize.width() + spacing : 0)
const auto w = textSize.width() + 2 * hPadding + (iconSize.width() > 0 ? iconSize.width() + spacing : 0)
+ (checkSize.width() > 0 ? checkSize.width() + spacing : 0);
const auto defaultH = _impl->theme.controlHeightLarge;
const auto h = std::max({ iconSize.height() + spacing, textH + spacing, defaultH });
const auto h = std::max({ iconSize.height() + spacing, textSize.height() + spacing, defaultH });
return QSize{ w, h };
}
break;
Expand Down