| 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 "PluginsSettings.h" |
| 21 | |
| 22 | PluginsSettings::PluginsSettings( |
| 23 | const QSharedPointer<IConfig> &config, |
| 24 | const QSharedPointer<IFileDialogService> &fileDialogService, |
| 25 | const QSharedPointer<IPluginFinder> &pluginFinder) : |
| 26 | mConfig(config), |
| 27 | mFileDialogService(fileDialogService), |
| 28 | mPluginFinder(pluginFinder), |
| 29 | mLayout(new QGridLayout), |
| 30 | mPluginPathLabel(new QLabel(this)), |
| 31 | mPluginPathLineEdit(new QLineEdit(this)), |
| 32 | mBrowseButton(new QPushButton(this)), |
| 33 | mDetectButton(new QPushButton(this)), |
| 34 | mTableWidget(new QTableWidget(5, 2, this)), |
| 35 | mDefaultSearchPathRadioButton(new QRadioButton(this)), |
| 36 | mCustomSearchPathRadioButton(new QRadioButton(this)) |
| 37 | { |
| 38 | initGui(); |
| 39 | loadConfig(); |
| 40 | } |
| 41 | |
| 42 | void PluginsSettings::saveSettings() |
| 43 | { |
| 44 | mConfig->setPluginInfos(mDetectedPlugins); |
| 45 | mConfig->setPluginPath(mPluginPathLineEdit->text()); |
| 46 | mConfig->setCustomPluginSearchPathEnabled(mCustomSearchPathRadioButton->isChecked()); |
| 47 | } |
| 48 | |
| 49 | void PluginsSettings::initGui() |
| 50 | { |
| 51 | mPluginPathLabel->setText(tr("Search Path") + QLatin1String(":")); |
| 52 | |
| 53 | mDefaultSearchPathRadioButton->setText(tr("Default")); |
| 54 | connect(mDefaultSearchPathRadioButton, &QRadioButton::clicked, this, &PluginsSettings::searchPathSelectionChanged); |
| 55 | mDefaultSearchPathRadioButton->setChecked(true); |
| 56 | |
| 57 | connect(mCustomSearchPathRadioButton, &QRadioButton::clicked, this, &PluginsSettings::searchPathSelectionChanged); |
| 58 | |
| 59 | mPluginPathLineEdit->setToolTip(tr("The directory where the plugins are located.")); |
| 60 | |
| 61 | mBrowseButton->setText(tr("Browse")); |
| 62 | connect(mBrowseButton, &QPushButton::clicked, this, &PluginsSettings::choosePluginDirectory); |
| 63 | |
| 64 | mTableWidget->setHorizontalHeaderLabels(QStringList{ tr("Name"), tr("Version") }); |
| 65 | mTableWidget->horizontalHeader()->setStretchLastSection(true); |
| 66 | mTableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); |
| 67 | mTableWidget->verticalHeader()->setVisible(false); |
| 68 | |
| 69 | mDetectButton->setText(tr("Detect")); |
| 70 | connect(mDetectButton, &QPushButton::clicked, this, &PluginsSettings::detectPlugins); |
| 71 | |
| 72 | mLayout->setAlignment(Qt::AlignTop); |
| 73 | mLayout->setColumnMinimumWidth(0, 18); |
| 74 | mLayout->addWidget(mPluginPathLabel, 0, 0, 1, 4); |
| 75 | mLayout->addWidget(mDefaultSearchPathRadioButton, 1, 0, 1, 4); |
| 76 | mLayout->addWidget(mCustomSearchPathRadioButton, 2, 0, 1, 4); |
| 77 | mLayout->addWidget(mPluginPathLineEdit, 2, 1, 1, 3); |
| 78 | mLayout->addWidget(mBrowseButton, 2, 4); |
| 79 | mLayout->addWidget(mTableWidget, 4, 0, 2, 4); |
| 80 | mLayout->addWidget(mDetectButton, 4, 4); |
| 81 | |
| 82 | setTitle(tr("Plugin Settings")); |
| 83 | setLayout(mLayout); |
| 84 | } |
| 85 | |
| 86 | void PluginsSettings::loadConfig() |
| 87 | { |
| 88 | mPluginPathLineEdit->setText(mConfig->pluginPath()); |
| 89 | mCustomSearchPathRadioButton->setChecked(mConfig->customPluginSearchPathEnabled()); |
| 90 | mDetectedPlugins = mConfig->pluginInfos(); |
| 91 | |
| 92 | updatePluginTable(); |
| 93 | searchPathSelectionChanged(); |
| 94 | } |
| 95 | |
| 96 | void PluginsSettings::choosePluginDirectory() |
| 97 | { |
| 98 | auto path = mFileDialogService->getExistingDirectory(this, tr("Plugin location"), mConfig->saveDirectory()); |
| 99 | |
| 100 | if(!path.isEmpty()) { |
| 101 | mPluginPathLineEdit->setText(path); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void PluginsSettings::detectPlugins() |
| 106 | { |
| 107 | if(mDefaultSearchPathRadioButton->isChecked()) { |
| 108 | mDetectedPlugins = mPluginFinder->find(); |
| 109 | } else { |
| 110 | auto pluginPath = mPluginPathLineEdit->text(); |
| 111 | if (!pluginPath.isEmpty()) { |
| 112 | mDetectedPlugins = mPluginFinder->find(pluginPath); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | updatePluginTable(); |
| 117 | } |
| 118 | |
| 119 | void PluginsSettings::updatePluginTable() |
| 120 | { |
| 121 | auto pluginCount = mDetectedPlugins.count(); |
| 122 | for (auto i = 0; i < pluginCount; i++) { |
| 123 | auto pluginInfo = mDetectedPlugins[i]; |
| 124 | auto name = new QTableWidgetItem(PathHelper::extractFilename(pluginInfo.path())); |
| 125 | auto version = new QTableWidgetItem(pluginInfo.version()); |
| 126 | mTableWidget->setItem(i, 0, name); |
| 127 | mTableWidget->setItem(i, 1, version); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | void PluginsSettings::searchPathSelectionChanged() |
| 132 | { |
| 133 | auto isCustomSelected = mCustomSearchPathRadioButton->isChecked(); |
| 134 | mPluginPathLineEdit->setEnabled(isCustomSelected); |
| 135 | mBrowseButton->setEnabled(isCustomSelected); |
| 136 | } |