File indexing completed on 2025-02-02 04:11:32

0001 /*
0002  * SPDX-FileCopyrightText: 2019-2023 Mattia Basaglia <dev@dragon.best>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #include "tab_bar_close_button.hpp"
0008 
0009 #include <QStyle>
0010 #include <QTabBar>
0011 #include <QPainter>
0012 #include <QStyleOptionTab>
0013 #include <KLocalizedString>
0014 
0015 using namespace glaxnimate::gui;
0016 
0017 
0018 static void initStyleOption(QTabBar* q, QStyleOptionTab *option, int tabIndex)
0019 {
0020     const int totalTabs = q->count();
0021 
0022     if (!option || (tabIndex < 0 || tabIndex >= totalTabs))
0023         return;
0024 
0025     option->initFrom(q);
0026     option->rect = q->tabRect(tabIndex);
0027     option->row = 0;
0028     option->shape = q->shape();
0029     option->iconSize = q->iconSize();
0030     option->documentMode = q->documentMode();
0031     option->selectedPosition = QStyleOptionTab::NotAdjacent;
0032     option->position = QStyleOptionTab::Middle;
0033     QRect textRect = q->style()->subElementRect(QStyle::SE_TabBarTabText, option, q);
0034     option->text = q->fontMetrics().elidedText(option->text, q->elideMode(), textRect.width(), Qt::TextShowMnemonic);
0035 }
0036 
0037 
0038 void TabBarCloseButton::add_button(QTabBar* bar, int index)
0039 {
0040     QStyleOptionTab opt;
0041     initStyleOption(bar, &opt, index);
0042     QTabBar::ButtonPosition close_side = QTabBar::ButtonPosition(bar->style()->styleHint(QStyle::SH_TabBar_CloseButtonPosition, nullptr, bar));
0043     QAbstractButton *button = new TabBarCloseButton(bar);
0044     connect(button, &QAbstractButton::clicked, bar, [bar, button, close_side]{
0045         for ( int i = 0; i < bar->count(); i++ )
0046         {
0047             if ( bar->tabButton(i, close_side) == button )
0048                 bar->tabCloseRequested(i);
0049         }
0050     });
0051     bar->setTabButton(index, close_side, button);
0052 }
0053 
0054 
0055 TabBarCloseButton::TabBarCloseButton(QWidget *parent)
0056     : QAbstractButton(parent)
0057 {
0058     setFocusPolicy(Qt::NoFocus);
0059     setCursor(Qt::ArrowCursor);
0060     setToolTip(i18n("Delete Composition"));
0061 
0062     resize(sizeHint());
0063 }
0064 
0065 QSize TabBarCloseButton::sizeHint() const
0066 {
0067     ensurePolished();
0068     int width = style()->pixelMetric(QStyle::PM_TabCloseIndicatorWidth, nullptr, this);
0069     int height = style()->pixelMetric(QStyle::PM_TabCloseIndicatorHeight, nullptr, this);
0070     return QSize(width, height);
0071 }
0072 
0073 #if QT_VERSION_MAJOR < 6
0074 void TabBarCloseButton::enterEvent(QEvent * event)
0075 #else
0076 void TabBarCloseButton::enterEvent(QEnterEvent * event)
0077 #endif
0078 {
0079     if (isEnabled())
0080         update();
0081     QAbstractButton::enterEvent(event);
0082 }
0083 
0084 void TabBarCloseButton::leaveEvent(QEvent *event)
0085 {
0086     if (isEnabled())
0087         update();
0088     QAbstractButton::leaveEvent(event);
0089 }
0090 
0091 void TabBarCloseButton::paintEvent(QPaintEvent *)
0092 {
0093     QPainter p(this);
0094     QStyleOption opt;
0095     opt.initFrom(this);
0096     opt.state |= QStyle::State_AutoRaise;
0097     if (isEnabled() && underMouse() && !isChecked() && !isDown())
0098         opt.state |= QStyle::State_Raised;
0099     if (isChecked())
0100         opt.state |= QStyle::State_On;
0101     if (isDown())
0102         opt.state |= QStyle::State_Sunken;
0103 
0104     if (const QTabBar *tb = qobject_cast<const QTabBar *>(parent())) {
0105         int index = tb->currentIndex();
0106         QTabBar::ButtonPosition position = (QTabBar::ButtonPosition)style()->styleHint(QStyle::SH_TabBar_CloseButtonPosition, nullptr, tb);
0107         if (tb->tabButton(index, position) == this)
0108             opt.state |= QStyle::State_Selected;
0109     }
0110 
0111     style()->drawPrimitive(QStyle::PE_IndicatorTabClose, &opt, &p, this);
0112 }