File indexing completed on 2024-05-12 05:25:41

0001 /*
0002    SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "sieveeditortabwidget.h"
0008 
0009 #include <KActionCollection>
0010 #include <KLocalizedString>
0011 #include <QIcon>
0012 #include <QMenu>
0013 
0014 #include <QAction>
0015 #include <QTabBar>
0016 
0017 SieveEditorTabWidget::SieveEditorTabWidget(KActionCollection *ac, QWidget *parent)
0018     : QTabWidget(parent)
0019 {
0020     initActions(ac);
0021     setMovable(true);
0022     setTabsClosable(true);
0023     connect(this, &QTabWidget::tabCloseRequested, this, &SieveEditorTabWidget::tabCloseRequestedIndex);
0024     setContextMenuPolicy(Qt::CustomContextMenu);
0025     connect(this, &SieveEditorTabWidget::customContextMenuRequested, this, &SieveEditorTabWidget::slotTabContextMenuRequest);
0026 }
0027 
0028 SieveEditorTabWidget::~SieveEditorTabWidget() = default;
0029 
0030 void SieveEditorTabWidget::slotTabContextMenuRequest(const QPoint &pos)
0031 {
0032     QTabBar *bar = tabBar();
0033     if (count() < 1) {
0034         return;
0035     }
0036 
0037     const int indexBar = bar->tabAt(bar->mapFrom(this, pos));
0038     if (indexBar == -1) {
0039         return;
0040     }
0041 
0042     QMenu menu(this);
0043     QAction *closeTab = menu.addAction(i18nc("@action:inmenu", "Close Tab"));
0044     closeTab->setIcon(QIcon::fromTheme(QStringLiteral("tab-close")));
0045 
0046     const bool hasSeveralTabs = (count() > 1);
0047     QAction *allOther = menu.addAction(i18nc("@action:inmenu", "Close All Other Tabs"));
0048     allOther->setEnabled(hasSeveralTabs);
0049     allOther->setIcon(QIcon::fromTheme(QStringLiteral("tab-close-other")));
0050 
0051     QAction *allTab = menu.addAction(i18nc("@action:inmenu", "Close All Tabs"));
0052     allTab->setEnabled(hasSeveralTabs);
0053     allTab->setIcon(QIcon::fromTheme(QStringLiteral("tab-close")));
0054 
0055     QAction *action = menu.exec(mapToGlobal(pos));
0056 
0057     if (action == allOther) { // Close all other tabs
0058         Q_EMIT tabRemoveAllExclude(indexBar);
0059     } else if (action == closeTab) {
0060         Q_EMIT tabCloseRequestedIndex(indexBar);
0061     } else if (action == allTab) {
0062         Q_EMIT tabCloseAllTab();
0063     }
0064 }
0065 
0066 void SieveEditorTabWidget::initActions(KActionCollection *ac)
0067 {
0068     if (ac) {
0069         auto closeCurrentTabAct = new QAction(i18nc("@action:inmenu", "Close Tab"), this);
0070         ac->addAction(QStringLiteral("close_current_tab"), closeCurrentTabAct);
0071         ac->setDefaultShortcut(closeCurrentTabAct, QKeySequence(Qt::CTRL | Qt::Key_W));
0072         closeCurrentTabAct->setIcon(QIcon::fromTheme(QStringLiteral("tab-close")));
0073         connect(closeCurrentTabAct, &QAction::triggered, this, &SieveEditorTabWidget::slotCloseCurrentTab);
0074     }
0075 }
0076 
0077 void SieveEditorTabWidget::slotCloseCurrentTab()
0078 {
0079     const int curIndex = currentIndex();
0080     if (curIndex != -1) {
0081         Q_EMIT tabCloseRequestedIndex(curIndex);
0082     }
0083 }
0084 
0085 #include "moc_sieveeditortabwidget.cpp"