| 1 | /* |
| 2 | * Copyright (C) 2021 Damir Porobic <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include "ActionsSettings.h" |
| 21 | |
| 22 | ActionsSettings::ActionsSettings(const QList<CaptureModes> &captureModes, const QSharedPointer<IPlatformChecker> &platformChecker, const QSharedPointer<IConfig> &config) : |
| 23 | mConfig(config), |
| 24 | mPlatformChecker(platformChecker), |
| 25 | mLayout(new QVBoxLayout(this)), |
| 26 | mTabWidget(new QTabWidget(this)), |
| 27 | mCaptureModes(captureModes) |
| 28 | { |
| 29 | Q_ASSERT(mConfig != nullptr); |
| 30 | |
| 31 | initGui(); |
| 32 | loadConfig(); |
| 33 | |
| 34 | mTabWidget->setCurrentIndex(0); |
| 35 | } |
| 36 | |
| 37 | ActionsSettings::~ActionsSettings() |
| 38 | { |
| 39 | delete mTabWidget; |
| 40 | delete mLayout; |
| 41 | } |
| 42 | |
| 43 | void ActionsSettings::saveSettings() |
| 44 | { |
| 45 | QList<Action> actions; |
| 46 | auto count = mTabWidget->count(); |
| 47 | for (int index = 0; index < count; ++index) { |
| 48 | auto tabContent = dynamic_cast<ActionSettingTab *>(mTabWidget->widget(index)); |
| 49 | if(tabContent != nullptr) { |
| 50 | actions.append(tabContent->action()); |
| 51 | } |
| 52 | } |
| 53 | mConfig->setActions(actions); |
| 54 | } |
| 55 | |
| 56 | void ActionsSettings::initGui() |
| 57 | { |
| 58 | auto addButton = new QPushButton(); |
| 59 | addButton->setText(tr("Add")); |
| 60 | connect(addButton, &QPushButton::clicked, this, &ActionsSettings::addEmptyTab); |
| 61 | |
| 62 | auto addTabIndex = mTabWidget->addTab(new EmptyActionSettingTab, QString()); |
| 63 | mTabWidget->setTabEnabled(addTabIndex, false); |
| 64 | mTabWidget->tabBar()->setTabButton(addTabIndex, QTabBar::RightSide, addButton); |
| 65 | |
| 66 | mTabWidget->setTabsClosable(true); |
| 67 | connect(mTabWidget, &QTabWidget::tabCloseRequested, this, &ActionsSettings::closeTab); |
| 68 | |
| 69 | mLayout->addWidget(mTabWidget); |
| 70 | |
| 71 | setTitle(tr("Actions Settings")); |
| 72 | setLayout(mLayout); |
| 73 | } |
| 74 | |
| 75 | void ActionsSettings::loadConfig() |
| 76 | { |
| 77 | auto actions = mConfig->actions(); |
| 78 | for(const auto& action : actions) { |
| 79 | auto tabContent = new ActionSettingTab(action, mCaptureModes, mPlatformChecker); |
| 80 | insertActionTab(tabContent, action.name()); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void ActionsSettings::insertActionTab(ActionSettingTab *tabContent, const QString &name) |
| 85 | { |
| 86 | auto index = mTabWidget->insertTab(mTabWidget->count() - 1, tabContent, name); |
| 87 | connect(tabContent, &ActionSettingTab::nameChanged, [this, index](const QString &name) |
| 88 | { |
| 89 | mTabWidget->setTabText(index, name); |
| 90 | }); |
| 91 | |
| 92 | mTabWidget->setCurrentIndex(index); |
| 93 | } |
| 94 | |
| 95 | void ActionsSettings::addEmptyTab() |
| 96 | { |
| 97 | auto name = tr("Action") + QLatin1String(" ") + QString::number(mTabWidget->count()); |
| 98 | auto tabContent = new ActionSettingTab(name, mCaptureModes, mPlatformChecker); |
| 99 | insertActionTab(tabContent, name); |
| 100 | } |
| 101 | |
| 102 | void ActionsSettings::closeTab(int index) |
| 103 | { |
| 104 | mTabWidget->removeTab(index); |
| 105 | } |