Warning, file /office/skrooge/skgbasegui/skgtabwidget.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /***************************************************************************
0002  * SPDX-FileCopyrightText: 2022 S. MANKOWSKI stephane@mankowski.fr
0003  * SPDX-FileCopyrightText: 2022 G. DE BURE support@mankowski.fr
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  ***************************************************************************/
0006 /** @file
0007  * A QTabWidget with more features.
0008  *
0009  * @author Stephane MANKOWSKI / Guillaume DE BURE
0010  */
0011 #include "skgtabwidget.h"
0012 
0013 #include <klocalizedstring.h>
0014 
0015 #include <qicon.h>
0016 #include <qpushbutton.h>
0017 #include <qstringlist.h>
0018 #include <qtabbar.h>
0019 #include <qtimer.h>
0020 
0021 #include "skgmainpanel.h"
0022 #include "skgtabpage.h"
0023 #include "skgtraces.h"
0024 
0025 SKGTabWidget::SKGTabWidget(QWidget* iParent)
0026     : QTabWidget(iParent)
0027 {
0028     m_timerSave.setSingleShot(true);
0029     connect(&m_timerSave, &QTimer::timeout, this, &SKGTabWidget::onRefreshSaveIcon, Qt::QueuedConnection);
0030     connect(tabBar(), &QTabBar::tabMoved, this, &::SKGTabWidget::onMoveTab);
0031     if (iParent != nullptr) {
0032         connect(qobject_cast<SKGMainPanel*>(iParent), &SKGMainPanel::currentPageChanged, this, &SKGTabWidget::onCurrentChanged);
0033     }
0034 
0035     // Use new KDE for moving tabs.
0036     tabBar()->setMovable(true);
0037 
0038     m_timerSave.start(1000);
0039 }
0040 
0041 SKGTabWidget::~SKGTabWidget()
0042     = default;
0043 
0044 void SKGTabWidget::onCurrentChanged()
0045 {
0046     if ((currentWidget() != nullptr) && !m_tabIndexSaveButton.contains(currentWidget())) {
0047         // Build widgets
0048         auto kSave = new QPushButton(this);
0049         kSave->setIcon(SKGServices::fromTheme(QStringLiteral("document-save")));
0050         kSave->setToolTip(i18nc("Verb", "Save this tab"));
0051         kSave->setFlat(true);
0052         kSave->setMaximumSize(QSize(16, 16));
0053         kSave->show();
0054 
0055         connect(kSave, &QPushButton::clicked, this, &SKGTabWidget::onSaveRequested);
0056         tabBar()->setTabButton(currentIndex(), QTabBar::LeftSide, kSave);
0057 
0058         m_tabIndexSaveButton.insert(currentWidget(), kSave);
0059     }
0060 
0061     // Rebuild indexes
0062     QHash<QWidget*, QPushButton*> TabIndexSaveButton2;
0063 
0064     int nb = count();
0065     for (int i = 0; i < nb; ++i) {
0066         QWidget* w = widget(i);
0067         QPushButton* save = m_tabIndexSaveButton.value(w);
0068         if ((w != nullptr) && (save != nullptr)) {
0069             save->setVisible(false);
0070             TabIndexSaveButton2[w] = save;
0071         }
0072     }
0073 
0074     m_tabIndexSaveButton = TabIndexSaveButton2;
0075 
0076     onRefreshSaveIcon();
0077 }
0078 
0079 void SKGTabWidget::removeTab(int index)
0080 {
0081     m_tabIndexSaveButton.clear();
0082     QTabWidget::removeTab(index);
0083 }
0084 
0085 
0086 void SKGTabWidget::onMoveTab(int oldPos, int newPos)
0087 {
0088     Q_UNUSED(oldPos)
0089     Q_UNUSED(newPos)
0090     m_tabIndexSaveButton.clear();
0091     onCurrentChanged();
0092 }
0093 
0094 void SKGTabWidget::onSaveRequested()
0095 {
0096     auto* page = qobject_cast<SKGTabPage*>(currentWidget());
0097     if (page != nullptr) {
0098         page->overwrite(false);
0099         onRefreshSaveIcon();
0100     }
0101 }
0102 
0103 void SKGTabWidget::onRefreshSaveIcon()
0104 {
0105     auto* page = qobject_cast<SKGTabPage*>(currentWidget());
0106     if (page != nullptr) {
0107         QPushButton* save = m_tabIndexSaveButton.value(page);
0108         if (save != nullptr) {
0109             if (page->isOverwriteNeeded()) {
0110                 save->show();
0111                 save->setEnabled(true);
0112                 QStringList overlay;
0113                 if (page->isPin()) {
0114                     overlay.push_back(QStringLiteral("document-encrypt"));
0115                 }
0116                 if (!page->getBookmarkID().isEmpty()) {
0117                     overlay.push_back(QStringLiteral("bookmarks"));
0118                 }
0119                 save->setIcon(SKGServices::fromTheme(QStringLiteral("document-save"), overlay));
0120             } else if (page->isPin()) {
0121                 save->show();
0122                 save->setEnabled(false);
0123                 save->setIcon(SKGServices::fromTheme(QStringLiteral("document-encrypt")));
0124             } else {
0125                 save->hide();
0126             }
0127         }
0128 
0129         m_timerSave.start(1000);
0130     }
0131 }
0132 
0133