1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 01:22:10 +01:00

VerticalTabs: Fix QSS styling issues

It is now possible to replicate tabbar from Windows theme.
This commit is contained in:
David Rosca 2018-02-03 15:33:51 +01:00
parent ad387fe712
commit 36eb1f5523
No known key found for this signature in database
GPG Key ID: EBC3FC294452C6D8
6 changed files with 91 additions and 7 deletions

View File

@ -52,9 +52,8 @@ void TabListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &optio
const QStyle *style = w ? w->style() : m_view->style();
QStyleOptionViewItem opt = option;
opt.state.setFlag(QStyle::State_Active, true);
opt.state.setFlag(QStyle::State_HasFocus, false);
opt.state.setFlag(QStyle::State_Selected, index.data(TabModel::CurrentTabRole).toBool());
initStyleOption(&opt, index);
m_view->adjustStyleOption(&opt);
const int height = opt.rect.height();
const int center = height / 2 + opt.rect.top();
@ -91,7 +90,6 @@ void TabListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &optio
painter->drawEllipse(audioRect);
painter->drawPixmap(audioRect, audioMuted ? TabIcon::data()->audioMutedPixmap : TabIcon::data()->audioPlayingPixmap);
}
}

View File

@ -45,6 +45,30 @@ TabListView::TabListView(QWidget *parent)
setFixedHeight(m_delegate->sizeHint(viewOptions(), QModelIndex()).height());
}
void TabListView::adjustStyleOption(QStyleOptionViewItem *option)
{
const QModelIndex index = option->index;
option->state.setFlag(QStyle::State_Active, true);
option->state.setFlag(QStyle::State_HasFocus, false);
option->state.setFlag(QStyle::State_Selected, index.data(TabModel::CurrentTabRole).toBool());
if (!index.isValid()) {
option->viewItemPosition = QStyleOptionViewItem::Invalid;
} else if (model()->rowCount() == 1) {
option->viewItemPosition = QStyleOptionViewItem::OnlyOne;
} else {
const QRect rect = visualRect(index);
if (!indexAt(QPoint(rect.x() - rect.width() / 2, rect.y())).isValid()) {
option->viewItemPosition = QStyleOptionViewItem::Beginning;
} else if (!indexAt(QPoint(rect.x() + rect.width() / 2, rect.y())).isValid()) {
option->viewItemPosition = QStyleOptionViewItem::End;
} else {
option->viewItemPosition = QStyleOptionViewItem::Middle;
}
}
}
void TabListView::currentChanged(const QModelIndex &current, const QModelIndex &previous)
{
if (current.data(TabModel::CurrentTabRole).toBool()) {

View File

@ -28,6 +28,8 @@ class TabListView : public QListView
public:
explicit TabListView(QWidget *parent = nullptr);
void adjustStyleOption(QStyleOptionViewItem *option);
private:
void currentChanged(const QModelIndex &current, const QModelIndex &previous) override;
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int>()) override;

View File

@ -100,9 +100,8 @@ void TabTreeDelegate::paint(QPainter *painter, const QStyleOptionViewItem &optio
const int depth = indexDepth(index);
QStyleOptionViewItem opt = option;
opt.state.setFlag(QStyle::State_Active, true);
opt.state.setFlag(QStyle::State_HasFocus, false);
opt.state.setFlag(QStyle::State_Selected, index.data(TabModel::CurrentTabRole).toBool());
initStyleOption(&opt, index);
m_view->adjustStyleOption(&opt);
const int height = opt.rect.height();
const int center = height / 2 + opt.rect.top();
@ -184,6 +183,11 @@ void TabTreeDelegate::paint(QPainter *painter, const QStyleOptionViewItem &optio
QRect titleRect(leftPosition, center - opt.fontMetrics.height() / 2, opt.rect.width(), opt.fontMetrics.height());
titleRect.setRight(rightPosition - m_padding);
QString title = opt.fontMetrics.elidedText(index.data().toString(), Qt::ElideRight, titleRect.width());
if (opt.state.testFlag(QStyle::State_Selected) && m_view->selectedColor().isValid()) {
textPalette.setColor(cg, colorRole, m_view->selectedColor());
} else if (opt.state.testFlag(QStyle::State_MouseOver) && m_view->hoverColor().isValid()) {
textPalette.setColor(cg, colorRole, m_view->hoverColor());
}
style->drawItemText(painter, titleRect, Qt::AlignLeft, textPalette, true, title, colorRole);
}

View File

@ -51,6 +51,26 @@ TabTreeView::TabTreeView(QWidget *parent)
viewport()->setAttribute(Qt::WA_Hover);
}
QColor TabTreeView::hoverColor() const
{
return m_hoverColor;
}
void TabTreeView::setHoverColor(const QColor &color)
{
m_hoverColor = color;
}
QColor TabTreeView::selectedColor() const
{
return m_selectedColor;
}
void TabTreeView::setSelectedColor(const QColor &color)
{
m_selectedColor = color;
}
bool TabTreeView::areTabsInOrder() const
{
return m_tabsInOrder;
@ -61,6 +81,29 @@ void TabTreeView::setTabsInOrder(bool enable)
m_tabsInOrder = enable;
}
void TabTreeView::adjustStyleOption(QStyleOptionViewItem *option)
{
const QModelIndex index = option->index;
option->state.setFlag(QStyle::State_Active, true);
option->state.setFlag(QStyle::State_HasFocus, false);
option->state.setFlag(QStyle::State_Selected, index.data(TabModel::CurrentTabRole).toBool());
if (!index.isValid()) {
option->viewItemPosition = QStyleOptionViewItem::Invalid;
} else if (model()->rowCount() == 1) {
option->viewItemPosition = QStyleOptionViewItem::OnlyOne;
} else {
if (!indexAbove(index).isValid()) {
option->viewItemPosition = QStyleOptionViewItem::Beginning;
} else if (!indexBelow(index).isValid()) {
option->viewItemPosition = QStyleOptionViewItem::End;
} else {
option->viewItemPosition = QStyleOptionViewItem::Middle;
}
}
}
void TabTreeView::drawBranches(QPainter *, const QRect &, const QModelIndex &) const
{
// Disable drawing branches

View File

@ -24,14 +24,24 @@ class TabTreeDelegate;
class TabTreeView : public QTreeView
{
Q_OBJECT
Q_PROPERTY(QColor hoverColor READ hoverColor WRITE setHoverColor)
Q_PROPERTY(QColor selectedColor READ selectedColor WRITE setSelectedColor)
public:
explicit TabTreeView(QWidget *parent = nullptr);
QColor hoverColor() const;
void setHoverColor(const QColor &color);
QColor selectedColor() const;
void setSelectedColor(const QColor &color);
// In TabBar order
bool areTabsInOrder() const;
void setTabsInOrder(bool enable);
void adjustStyleOption(QStyleOptionViewItem *option);
private:
void drawBranches(QPainter *painter, const QRect &rect, const QModelIndex &index) const override;
void currentChanged(const QModelIndex &current, const QModelIndex &previous) override;
@ -48,6 +58,9 @@ private:
DelegateButton buttonAt(const QPoint &pos, const QModelIndex &index) const;
QColor m_hoverColor;
QColor m_selectedColor;
TabTreeDelegate *m_delegate;
DelegateButton m_pressedButton = NoButton;
QPersistentModelIndex m_pressedIndex;