File indexing completed on 2025-01-19 05:11:26

0001 /*
0002 SPDX-FileCopyrightText: 2021 Hamed Masafi <hamed.masfi@gmail.com>
0003 
0004 SPDX-License-Identifier: GPL-3.0-or-later
0005 */
0006 
0007 #include "multipagewidget.h"
0008 #include "widgetbase.h"
0009 
0010 #include <QAction>
0011 #include <QActionGroup>
0012 #include <QToolButton>
0013 
0014 Git::Manager *MultiPageWidget::defaultGitManager() const
0015 {
0016     return mDefaultGitManager;
0017 }
0018 
0019 void MultiPageWidget::setDefaultGitManager(Git::Manager *newDefaultGitManager)
0020 {
0021     mDefaultGitManager = newDefaultGitManager;
0022 }
0023 
0024 int MultiPageWidget::count() const
0025 {
0026     return m_actionGroup->actions().size();
0027 }
0028 
0029 MultiPageWidget::MultiPageWidget(QWidget *parent)
0030     : QWidget(parent)
0031     , m_actionGroup(new QActionGroup(this))
0032 {
0033     Q_SET_OBJECT_NAME(m_actionGroup);
0034 
0035     setupUi(this);
0036 
0037     connect(m_actionGroup, &QActionGroup::triggered, this, &MultiPageWidget::slotPageSelected);
0038     auto styleSheet = QString(QStringLiteral(R"CSS(
0039         #scrollAreaWidgetContents {
0040             background-color: #%1;
0041         }
0042         QToolButton {
0043             background-color: #%1;
0044             border: none;
0045             padding-top: 10px;
0046             padding-bottom: 10px;
0047             height: 48px;
0048         }
0049 
0050         QToolButton:hover {
0051             background-color: #%2;
0052         }
0053 
0054         QToolButton:checked {
0055             background-color: #%3;
0056             color: white;
0057         }
0058 
0059 )CSS"))
0060                           .arg(palette().color(QPalette::Base).rgba(), 0, 16)
0061                           .arg(palette().color(QPalette::Highlight).lighter().rgba(), 0, 16)
0062                           .arg(palette().color(QPalette::Highlight).rgba(), 0, 16);
0063 
0064     scrollAreaWidgetContents->setStyleSheet(styleSheet);
0065 }
0066 
0067 void MultiPageWidget::addPage(const QString &title, const QIcon &icon, WidgetBase *widget)
0068 {
0069     const QList<Qt::Key> keys = {Qt::Key_0, Qt::Key_1, Qt::Key_2, Qt::Key_3, Qt::Key_4, Qt::Key_5, Qt::Key_6, Qt::Key_7, Qt::Key_8, Qt::Key_9};
0070     auto btn = new QToolButton(this);
0071     btn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
0072     btn->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed));
0073 
0074     auto action = new QAction(this);
0075 
0076     action->setText(title);
0077     action->setIcon(icon);
0078     action->setCheckable(true);
0079     action->setData(m_actionGroup->actions().size());
0080     if (m_actionGroup->actions().size() < 10)
0081         action->setShortcut(QKeySequence(Qt::CTRL + keys[m_actionGroup->actions().size()]));
0082     btn->setDefaultAction(action);
0083     m_actionGroup->addAction(action);
0084 
0085     stackedWidget->addWidget(widget);
0086 
0087     widget->layout()->setContentsMargins(0, 0, 0, 0);
0088 
0089     verticalLayoutButtons->insertWidget(m_actionGroup->actions().size() - 1, btn);
0090 }
0091 
0092 void MultiPageWidget::addPage(WidgetBase *widget, QAction *action)
0093 {
0094     auto btn = new QToolButton(this);
0095     btn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
0096     btn->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed));
0097 
0098     if (!action)
0099         action = new QAction(this);
0100     action->setText(widget->windowTitle());
0101     action->setIcon(widget->windowIcon());
0102     action->setCheckable(true);
0103     action->setData(m_actionGroup->actions().size());
0104     btn->setDefaultAction(action);
0105     m_actionGroup->addAction(action);
0106 
0107     stackedWidget->addWidget(widget);
0108 
0109     widget->layout()->setContentsMargins(0, 0, 0, 0);
0110 
0111     verticalLayoutButtons->insertWidget(m_actionGroup->actions().size() - 1, btn);
0112 }
0113 
0114 void MultiPageWidget::setCurrentIndex(int index)
0115 {
0116     m_actionGroup->actions().at(index)->trigger();
0117 }
0118 
0119 QList<QAction *> MultiPageWidget::actions() const
0120 {
0121     return m_actionGroup->actions();
0122 }
0123 
0124 void MultiPageWidget::slotPageSelected(QAction *action)
0125 {
0126     stackedWidget->setCurrentIndex(action->data().toInt());
0127     labelTitle->setText(action->text().remove(QLatin1Char('&')));
0128     labelPageIcon->setPixmap(action->icon().pixmap({32, 32}));
0129 }
0130 
0131 #include "moc_multipagewidget.cpp"