28#include <QApplication>
32 : QAbstractButton(parent)
34 int width = style()->pixelMetric(QStyle::PM_TabCloseIndicatorWidth,
nullptr,
this);
35 int height = style()->pixelMetric(QStyle::PM_TabCloseIndicatorHeight,
nullptr,
this);
36 resize(width, height);
41 return m_showOnNormal;
46 m_showOnNormal = show;
51 return m_showOnHovered;
56 m_showOnHovered = show;
61 return m_showOnSelected;
66 m_showOnSelected = show;
71 if (hovered && selected) {
72 return m_showOnHovered || m_showOnSelected;
73 }
else if (selected) {
74 return m_showOnSelected;
76 return m_showOnHovered;
78 return m_showOnNormal;
82void TabTreeCloseButton::paintEvent(QPaintEvent *)
88 : QStyledItemDelegate()
91 m_padding = qMax(5, m_view->style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1);
98 auto *tabBar =
new QTabBar(m_view);
99 tabBar->setObjectName(
QSL(
"tabtree_tabbar"));
103 m_closeButton->lower();
106static int indexDepth(QModelIndex index)
109 while (index.parent().isValid()) {
110 index = index.parent();
118 const QRect rect = m_view->visualRect(index);
119 const int depth = indexDepth(index);
120 return QRect(m_indentation * depth, rect.y(), m_indentation, rect.height());
128 const QRect rect = m_view->visualRect(index);
129 const int center = rect.height() / 2 + rect.top();
130 const int rightPosition = rect.right() - m_padding * 2 - 16;
131 return QRect(rightPosition - 16, center - 16 / 2, 16, 16);
136 const QRect rect = m_view->visualRect(index);
137 const int center = rect.height() / 2 + rect.top();
138 QSize size = m_closeButton->size();
139 size.setHeight(qMin(rect.height() - m_padding, size.height()));
140 return QRect(QPoint(rect.right() - m_padding - size.width(), center - size.height() / 2), size);
145 const QWidget *w = option.widget;
146 const QStyle *style = w ? w->style() : m_view->style();
148 const bool expanded = m_view->isExpanded(index);
149 const bool children = m_view->model()->rowCount(index) > 0;
150 const int depth = indexDepth(index);
153 QStyleOptionViewItem opt = option;
154 initStyleOption(&opt, index);
157 const int height = opt.rect.height();
158 const int center = height / 2 + opt.rect.top();
160 int leftPosition = opt.rect.left() + m_indentation + m_indentation * depth + m_padding;
161 int rightPosition = opt.rect.right() - m_padding * 2 - m_closeButton->size().width();
163 const QPalette::ColorRole colorRole = opt.state & QStyle::State_Selected ? QPalette::HighlightedText : QPalette::Text;
165 QPalette::ColorGroup cg = opt.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
166 if (!isRestoredTab) {
167 cg = QPalette::Disabled;
169 if (cg == QPalette::Normal && !(opt.state & QStyle::State_Active)) {
170 cg = QPalette::Inactive;
174 opt.palette.setColor(QPalette::All, QPalette::HighlightedText, opt.palette.color(QPalette::Active, QPalette::Text));
175 opt.palette.setColor(QPalette::All, QPalette::Highlight, opt.palette.base().color().darker(108));
178 QPalette textPalette = opt.palette;
179 textPalette.setCurrentColorGroup(cg);
181 const bool hovered = opt.state.testFlag(QStyle::State_MouseOver);
182 const bool selected = opt.state.testFlag(QStyle::State_Selected);
186 opt.rect.moveLeft(m_indentation * depth);
187 opt.rect.setWidth(opt.rect.width() - m_indentation * depth);
189 style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, w);
193 QStyleOptionViewItem o = opt;
194 o.state &= ~QStyle::State_MouseOver;
195 o.rect.moveLeft(m_indentation * depth);
196 o.rect.setWidth(m_indentation);
197 style->drawPrimitive(expanded ? QStyle::PE_IndicatorArrowDown : QStyle::PE_IndicatorArrowRight, &o, painter, w);
201 const int iconSize = 16;
202 const int iconYPos = center - (iconSize / 2);
203 QRect iconRect(leftPosition, iconYPos, iconSize, iconSize);
206 pixmap = m_loadingAnimator->
pixmap(index);
208 pixmap = index.data(Qt::DecorationRole).value<QIcon>().pixmap(iconSize);
210 painter->drawPixmap(iconRect, pixmap);
211 leftPosition += iconRect.width() + m_padding;
214 if (m_closeButton->
isVisible(hovered, selected)) {
215 QStyleOptionButton o;
216 o.initFrom(m_closeButton);
218 const bool hovered =
closeButtonRect(index).contains(m_view->viewport()->mapFromGlobal(QCursor::pos()));
219 const bool pressed = hovered && QApplication::mouseButtons() == Qt::LeftButton;
221 QSize closeSize = QSize(o.rect.size().width(), qMin(height - m_padding, o.rect.size().height()));
222 QPoint pos(opt.rect.right() - m_padding - closeSize.width(), center - closeSize.height() / 2);
223 o.rect = QRect(pos, closeSize);
224 o.state |= QStyle::State_AutoRaise | QStyle::State_Enabled | QStyle::State_Selected;
225 o.state.setFlag(QStyle::State_Raised, hovered && !pressed);
226 o.state.setFlag(QStyle::State_Sunken, pressed);
227 o.state.setFlag(QStyle::State_MouseOver, hovered);
228 style->drawPrimitive(QStyle::PE_IndicatorTabClose, &o, painter, m_closeButton);
234 if (audioMuted || audioPlaying) {
235 QSize audioSize(16, 16);
236 QPoint pos(rightPosition - audioSize.width(), center - audioSize.height() / 2);
237 QRect audioRect(pos, audioSize);
239 rightPosition -= audioSize.width() + m_padding;
243 QRect titleRect(leftPosition, center - opt.fontMetrics.height() / 2, opt.rect.width(), opt.fontMetrics.height());
244 titleRect.setRight(rightPosition - m_padding);
245 QString title = opt.fontMetrics.elidedText(index.data().toString(), Qt::ElideRight, titleRect.width());
246 style->drawItemText(painter, titleRect, Qt::AlignLeft, textPalette,
true, title, colorRole);
251 QStyleOptionViewItem opt(option);
252 initStyleOption(&opt, index);
254 return QSize(200, m_padding * 2 + opt.fontMetrics.height());
void updateIndex(const QModelIndex &index)
QPixmap pixmap(const QModelIndex &index)
QRect audioButtonRect(const QModelIndex &index) const
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
QRect closeButtonRect(const QModelIndex &index) const
TabTreeDelegate(TabTreeView *view)
QRect expandButtonRect(const QModelIndex &index) const
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
int backgroundIndentation
void updateIndex(const QModelIndex &index)
void adjustStyleOption(QStyleOptionViewItem *option)