36 setUniformItemSizes(
true);
37 setDropIndicatorShown(
true);
38 setMouseTracking(
true);
39 setFlow(QListView::LeftToRight);
40 setFocusPolicy(Qt::NoFocus);
41 setFrameShape(QFrame::NoFrame);
42 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
43 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
44 setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
47 setItemDelegate(m_delegate);
54 return m_hideWhenEmpty;
59 m_hideWhenEmpty = enable;
65 QRect rect = visualRect(index);
66 if (!rect.isValid()) {
70 rect.moveTop(rect.y() - rect.height() / 2);
71 rect.setHeight(rect.height() * 2);
72 viewport()->update(rect);
77 const QModelIndex index = option->index;
79 option->state.setFlag(QStyle::State_Active,
true);
80 option->state.setFlag(QStyle::State_HasFocus,
false);
83 if (!index.isValid()) {
84 option->viewItemPosition = QStyleOptionViewItem::Invalid;
85 }
else if (model()->rowCount() == 1) {
86 option->viewItemPosition = QStyleOptionViewItem::OnlyOne;
89 option->viewItemPosition = QStyleOptionViewItem::Beginning;
91 option->viewItemPosition = QStyleOptionViewItem::End;
93 option->viewItemPosition = QStyleOptionViewItem::Middle;
100 if (!index.isValid()) {
103 const QRect rect = visualRect(index);
104 return indexAt(QPoint(rect.right() + rect.width() / 2, rect.y()));
109 if (!index.isValid()) {
112 const QRect rect = visualRect(index);
113 return indexAt(QPoint(rect.left() - rect.width() / 2, rect.y()));
116void TabListView::currentChanged(
const QModelIndex ¤t,
const QModelIndex &previous)
119 QListView::currentChanged(current, previous);
121 setCurrentIndex(previous);
125void TabListView::dataChanged(
const QModelIndex &topLeft,
const QModelIndex &bottomRight,
const QVector<int> &roles)
127 QListView::dataChanged(topLeft, bottomRight, roles);
130 setCurrentIndex(topLeft);
134void TabListView::rowsInserted(
const QModelIndex &parent,
int start,
int end)
136 QListView::rowsInserted(parent, start, end);
141void TabListView::rowsAboutToBeRemoved(
const QModelIndex &parent,
int start,
int end)
143 QListView::rowsAboutToBeRemoved(parent, start, end);
145 QTimer::singleShot(0,
this, &TabListView::updateVisibility);
148bool TabListView::viewportEvent(QEvent *event)
150 switch (event->type()) {
151 case QEvent::MouseButtonPress: {
152 auto *me =
static_cast<QMouseEvent*
>(event);
153 const QModelIndex index = indexAt(me->pos());
155 if (me->buttons() == Qt::MiddleButton && tab) {
158 if (me->buttons() != Qt::LeftButton) {
159 m_pressedIndex = QModelIndex();
160 m_pressedButton = NoButton;
163 m_pressedIndex = index;
164 m_pressedButton = buttonAt(me->pos(), m_pressedIndex);
165 if (m_pressedButton == NoButton && tab) {
166 tab->makeCurrentTab();
171 case QEvent::MouseButtonRelease: {
172 auto *me =
static_cast<QMouseEvent*
>(event);
173 if (me->buttons() != Qt::NoButton) {
176 const QModelIndex index = indexAt(me->pos());
177 if (m_pressedIndex != index) {
180 DelegateButton button = buttonAt(me->pos(), index);
181 if (m_pressedButton == button) {
183 if (tab && m_pressedButton == AudioButton) {
190 case QEvent::ToolTip: {
191 auto *he =
static_cast<QHelpEvent*
>(event);
192 const QModelIndex index = indexAt(he->pos());
193 DelegateButton button = buttonAt(he->pos(), index);
194 if (button == AudioButton) {
196 QToolTip::showText(he->globalPos(), muted ? tr(
"Unmute Tab") : tr(
"Mute Tab"),
this, visualRect(index));
199 }
else if (button == NoButton) {
200 QToolTip::showText(he->globalPos(), index.data().toString(),
this, visualRect(index));
207 case QEvent::ContextMenu: {
208 auto *ce =
static_cast<QContextMenuEvent*
>(event);
209 const QModelIndex index = indexAt(ce->pos());
211 const int tabIndex = tab ? tab->
tabIndex() : -1;
214 menu.exec(ce->globalPos());
218 case QEvent::StyleChange:
225 return QListView::viewportEvent(event);
228TabListView::DelegateButton TabListView::buttonAt(
const QPoint &pos,
const QModelIndex &index)
const
236void TabListView::updateVisibility()
238 setVisible(!m_hideWhenEmpty || model()->rowCount() > 0);
241void TabListView::updateHeight()
243 QStyleOptionViewItem option;
244 initViewItemOption(&option);
245 setFixedHeight(m_delegate->
sizeHint(option, QModelIndex()).height());
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
QRect audioButtonRect(const QModelIndex &index) const
void adjustStyleOption(QStyleOptionViewItem *option)
TabListView(BrowserWindow *window, QWidget *parent=nullptr)
QModelIndex indexBefore(const QModelIndex &index) const
void updateIndex(const QModelIndex &index)
bool isHidingWhenEmpty() const
QModelIndex indexAfter(const QModelIndex &index) const
void setHideWhenEmpty(bool enable)