Debian Codegraph @

src/Gui/Dialogs/DlgPreferencePackManagementImp.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2022 Chris Hennes <[email protected]> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23 #include <QMessageBox>
24
25 #include "Dialogs/DlgPreferencePackManagementImp.h"
26 #include "ui_DlgPreferencePackManagement.h"
27 #include "Application.h"
28 #include "Command.h"
29 #include "PreferencePackManager.h"
30
31
32 using namespace Gui::Dialog;
33 namespace fs = std::filesystem;
34
35 /* TRANSLATOR Gui::Dialog::DlgPreferencePackManagementImp */
36
37 /**
38 * Constructs a Gui::Dialog::DlgPreferencePackManagementImp as a child of 'parent'
39 */
40 DlgPreferencePackManagementImp::DlgPreferencePackManagementImp(QWidget* parent)
41 : QDialog(parent)
42 , ui(new Ui_DlgPreferencePackManagement)
43 {
44 ui->setupUi(this);
45 if (Application::Instance->commandManager().getCommandByName("Std_AddonMgr")) {
46 connect(
47 ui->pushButtonOpenAddonManager,
48 &QPushButton::clicked,
49 this,
50 &DlgPreferencePackManagementImp::showAddonManager
51 );
52 }
53 else {
54 ui->pushButtonOpenAddonManager->setDisabled(true);
55 }
56 connect(
57 this,
58 &DlgPreferencePackManagementImp::packVisibilityChanged,
59 this,
60 &DlgPreferencePackManagementImp::updateTree
61 );
62 updateTree();
63 }
64
65 void DlgPreferencePackManagementImp::updateTree()
66 {
67 // Separate out user-saved packs from installed packs: we can remove individual user-saved
68 // packs, but can only disable individual installed packs (though we can completely uninstall
69 // the pack's containing Addon by redirecting to the Addon Manager).
70 auto savedPreferencePacksDirectory
71 = Application::Instance->prefPackManager()->getSavedPreferencePacksPath();
72 auto modDirectories = Application::Instance->prefPackManager()->modPaths();
73 auto resourcePath = Application::Instance->prefPackManager()->getResourcePreferencePacksPath();
74
75 // The displayed tree has two levels: at the toplevel is either "User-Saved Packs" or the name
76 // of the addon containing the pack. Beneath those are the individual packs themselves. The tree
77 // view shows "Hide"/"Show" for packs installed as a Mod, and "Delete" for packs in the
78 // user-saved pack section.
79 auto userPacks = Application::Instance->prefPackManager()->getPacksFromDirectory(
80 savedPreferencePacksDirectory
81 );
82
83 auto builtinPacks = Application::Instance->prefPackManager()->getPacksFromDirectory(resourcePath);
84
85 std::map<std::string, std::vector<std::string>> installedPacks;
86 for (const auto& modDirectory : modDirectories) {
87 if (fs::exists(modDirectory) && fs::is_directory(modDirectory)) {
88 for (const auto& mod : fs::directory_iterator(modDirectory)) {
89 auto packs = Application::Instance->prefPackManager()->getPacksFromDirectory(mod);
90 if (!packs.empty()) {
91 auto modName = Base::FileInfo::pathToString(mod.path().filename());
92 installedPacks.emplace(modName, packs);
93 }
94 }
95 }
96 }
97
98 ui->treeWidget->clear(); // Begin by clearing whatever is there
99 ui->treeWidget->header()->setDefaultAlignment(Qt::AlignLeft);
100 ui->treeWidget->setColumnCount(2);
101 ui->treeWidget->setSelectionMode(QAbstractItemView::SelectionMode::NoSelection);
102 ui->treeWidget->header()->setStretchLastSection(false);
103 ui->treeWidget->header()->setSectionResizeMode(0, QHeaderView::ResizeMode::Stretch);
104 ui->treeWidget->header()->setSectionResizeMode(1, QHeaderView::ResizeMode::ResizeToContents);
105
106 if (!userPacks.empty()) {
107 addTreeNode(tr("User-Saved Preference Packs").toStdString(), userPacks, TreeWidgetType::USER);
108 }
109
110 if (!builtinPacks.empty()) {
111 addTreeNode(tr("Built-In Preference Packs").toStdString(), builtinPacks, TreeWidgetType::BUILTIN);
112 }
113
114 for (const auto& installedPack : installedPacks) {
115 addTreeNode(installedPack.first, installedPack.second, TreeWidgetType::ADDON);
116 }
117 }
118
119 void DlgPreferencePackManagementImp::addTreeNode(
120 const std::string& name,
121 const std::vector<std::string>& contents,
122 TreeWidgetType twt
123 )
124 {
125 static const auto iconIsVisible = QIcon(QLatin1String(":/icons/dagViewVisible.svg"));
126 static const auto iconIsInvisible = QIcon(QLatin1String(":/icons/Invisible.svg"));
127 auto packRoot = new QTreeWidgetItem();
128 packRoot->setText(0, QString::fromStdString(name));
129 std::vector<QTreeWidgetItem*> items;
130 for (const auto& packName : contents) {
131 auto pack = new QTreeWidgetItem(packRoot);
132 pack->setText(0, QString::fromStdString(packName));
133 items.push_back(pack);
134 }
135 ui->treeWidget->addTopLevelItem(packRoot);
136 packRoot->setExpanded(true);
137 for (const auto item : items) {
138 auto button = new QPushButton();
139 button->setFlat(true);
140 switch (twt) {
141 break;
142 case TreeWidgetType::BUILTIN:
143 // The button is a "hide" button
144 if (Application::Instance->prefPackManager()
145 ->isVisible("##BUILT_IN##", item->text(0).toStdString())) {
146 button->setIcon(iconIsVisible);
147 }
148 else {
149 button->setIcon(iconIsInvisible);
150 }
151 button->setToolTip(
152 tr("Toggle visibility of built-in preference pack '%1'").arg(item->text(0))
153 );
154 connect(button, &QPushButton::clicked, [this, item]() {
155 this->hideBuiltInPack(item->text(0).toStdString());
156 });
157 break;
158 case TreeWidgetType::USER:
159 // The button is a "delete" button
160 button->setIcon(QIcon(QLatin1String(":/icons/delete.svg")));
161 button->setToolTip(
162 tr("Deletes the user-saved preference pack '%1'").arg(item->text(0))
163 );
164 connect(button, &QPushButton::clicked, [this, item]() {
165 this->deleteUserPack(item->text(0).toStdString());
166 });
167 break;
168 case TreeWidgetType::ADDON:
169 // The button is a "hide" button
170 if (Application::Instance->prefPackManager()
171 ->isVisible(name, item->text(0).toStdString())) {
172 button->setIcon(iconIsVisible);
173 }
174 else {
175 button->setIcon(iconIsInvisible);
176 }
177 button->setToolTip(tr("Toggles the visibility of the addon preference pack '%1' "
178 "(use the Addon Manager to remove permanently)")
179 .arg(item->text(0)));
180 connect(button, &QPushButton::clicked, [this, name, item]() {
181 this->hideInstalledPack(name, item->text(0).toStdString());
182 });
183 }
184 ui->treeWidget->setItemWidget(item, 1, button);
185 }
186 }
187
188 void DlgPreferencePackManagementImp::deleteUserPack(const std::string& name)
189 {
190 // Do the deletion here...
191 auto result = QMessageBox::warning(
192 this,
193 tr("Delete saved preference pack?"),
194 tr("Delete the preference pack named '%1'? This cannot be undone.")
195 .arg(QString::fromStdString(name)),
196 QMessageBox::Yes | QMessageBox::Cancel,
197 QMessageBox::Cancel
198 );
199 if (result == QMessageBox::Yes) {
200 Application::Instance->prefPackManager()->deleteUserPack(name);
201 Q_EMIT packVisibilityChanged();
202 }
203 }
204
205 void DlgPreferencePackManagementImp::hideBuiltInPack(const std::string& prefPackName)
206 {
207 Application::Instance->prefPackManager()->toggleVisibility("##BUILT_IN##", prefPackName);
208 Q_EMIT packVisibilityChanged();
209 }
210
211 void DlgPreferencePackManagementImp::hideInstalledPack(
212 const std::string& addonName,
213 const std::string& prefPackName
214 )
215 {
216 Application::Instance->prefPackManager()->toggleVisibility(addonName, prefPackName);
217 Q_EMIT packVisibilityChanged();
218 }
219
220 void DlgPreferencePackManagementImp::showAddonManager()
221 {
222 // Configure the view to show all preference packs (installed and uninstalled)
223 auto pref = App::GetApplication().GetParameterGroupByPath(
224 "User parameter:BaseApp/Preferences/Addons"
225 );
226 pref->SetInt("PackageTypeSelection", 3);
227 pref->SetInt("StatusSelection", 0);
228
229 CommandManager& rMgr = Application::Instance->commandManager();
230 rMgr.runCommandByName("Std_AddonMgr");
231 close();
232 }
233
234 DlgPreferencePackManagementImp::~DlgPreferencePackManagementImp() = default;
235
236
237 #include "moc_DlgPreferencePackManagementImp.cpp"