Debian Codegraph @

src/Gui/Dialogs/DlgCreateNewPreferencePackImp.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2021 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 #include <QPushButton>
25 #include <QRegularExpression>
26 #include <QRegularExpressionMatch>
27
28 #include "Dialogs/DlgCreateNewPreferencePackImp.h"
29 #include "ui_DlgCreateNewPreferencePack.h"
30
31 #include "FileDialog.h"
32
33 using namespace Gui::Dialog;
34
35 const auto TemplateRole = Qt::UserRole;
36
37 /* TRANSLATOR Gui::Dialog::DlgCreateNewPreferencePackImp */
38
39 /**
40 * Constructs a Gui::Dialog::DlgCreateNewPreferencePackImp as a child of 'parent'
41 */
42 DlgCreateNewPreferencePackImp::DlgCreateNewPreferencePackImp(QWidget* parent)
43 : QDialog(parent)
44 , ui(new Ui_DlgCreateNewPreferencePack)
45 {
46 ui->setupUi(this);
47
48 QRegularExpression validNames(QStringLiteral(R"([^/\\?%*:|"<>]+)"));
49 _nameValidator.setRegularExpression(validNames);
50 ui->lineEdit->setValidator(&_nameValidator);
51 ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
52 connect(ui->treeWidget, &QTreeWidget::itemChanged, this, &DlgCreateNewPreferencePackImp::onItemChanged);
53 connect(
54 ui->lineEdit,
55 &QLineEdit::textEdited,
56 this,
57 &DlgCreateNewPreferencePackImp::onLineEditTextEdited
58 );
59 connect(
60 ui->pushButton,
61 &QPushButton::clicked,
62 this,
63 &DlgCreateNewPreferencePackImp::onBrowseButtonClicked
64 );
65 }
66
67
68 DlgCreateNewPreferencePackImp::~DlgCreateNewPreferencePackImp() = default;
69
70 void DlgCreateNewPreferencePackImp::setPreferencePackTemplates(
71 const std::vector<Gui::PreferencePackManager::TemplateFile>& availableTemplates
72 )
73 {
74 ui->treeWidget->clear();
75 _groups.clear();
76
77 ui->treeWidget->header()->setDefaultSectionSize(250);
78
79 _templates = availableTemplates;
80 for (const auto& t : _templates) {
81
82 QTreeWidgetItem* group;
83 if (auto foundGroup = _groups.find(t.group); foundGroup != _groups.end()) {
84 group = foundGroup->second;
85 }
86 else {
87 group = new QTreeWidgetItem(ui->treeWidget, QStringList(QString::fromStdString(t.group)));
88 group->setCheckState(0, Qt::Checked);
89 group->setExpanded(true);
90 _groups.insert(std::make_pair(t.group, group));
91 }
92
93 QStringList itemColumns;
94 itemColumns.push_back(QString::fromStdString(t.name));
95 auto newItem = new QTreeWidgetItem(group, itemColumns);
96 newItem->setCheckState(0, Qt::Checked);
97 if (group->checkState(0) != newItem->checkState(0)) {
98 group->setCheckState(0, Qt::PartiallyChecked);
99 }
100 newItem->setData(0, TemplateRole, QVariant::fromValue(t));
101 group->addChild(newItem);
102 }
103 }
104
105 void Gui::Dialog::DlgCreateNewPreferencePackImp::setPreferencePackNames(
106 const std::vector<std::string>& usedNames
107 )
108 {
109 _existingPackNames = usedNames;
110 }
111
112 std::vector<Gui::PreferencePackManager::TemplateFile> DlgCreateNewPreferencePackImp::selectedTemplates() const
113 {
114 std::vector<Gui::PreferencePackManager::TemplateFile> results;
115
116 for (const auto& group : _groups) {
117 for (int childIndex = 0; childIndex < group.second->childCount(); ++childIndex) {
118 if (auto child = group.second->child(childIndex); child->checkState(0) == Qt::Checked) {
119 if (child->data(0, TemplateRole).canConvert<Gui::PreferencePackManager::TemplateFile>()) {
120 results.push_back(
121 child->data(0, TemplateRole).value<Gui::PreferencePackManager::TemplateFile>()
122 );
123 }
124 }
125 }
126 }
127
128 return results;
129 }
130
131 std::string DlgCreateNewPreferencePackImp::preferencePackName() const
132 {
133 return ui->lineEdit->text().toStdString();
134 }
135
136 std::string Gui::Dialog::DlgCreateNewPreferencePackImp::preferencePackDirectory() const
137 {
138 return _cfgFileDirectory.toStdString();
139 }
140
141 void DlgCreateNewPreferencePackImp::onItemChanged(QTreeWidgetItem* item, int column)
142 {
143 Q_UNUSED(column);
144 const QSignalBlocker blocker(ui->treeWidget);
145 if (auto group = item->parent(); group) {
146 // Child clicked
147 bool firstItemChecked = false;
148 for (int childIndex = 0; childIndex < group->childCount(); ++childIndex) {
149 auto child = group->child(childIndex);
150 if (childIndex == 0) {
151 firstItemChecked = child->checkState(0) == Qt::Checked;
152 }
153 else {
154 bool thisItemChecked = child->checkState(0) == Qt::Checked;
155 if (firstItemChecked != thisItemChecked) {
156 group->setCheckState(0, Qt::PartiallyChecked);
157 return;
158 }
159 }
160 }
161 group->setCheckState(0, firstItemChecked ? Qt::Checked : Qt::Unchecked);
162 }
163 else {
164 // Group clicked:
165 auto groupCheckState = item->checkState(0);
166 for (int childIndex = 0; childIndex < item->childCount(); ++childIndex) {
167 auto child = item->child(childIndex);
168 child->setCheckState(0, groupCheckState);
169 }
170 }
171 }
172
173 void DlgCreateNewPreferencePackImp::onLineEditTextEdited(const QString& text)
174 {
175 ui->buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty());
176 }
177
178 void DlgCreateNewPreferencePackImp::onBrowseButtonClicked()
179 {
180 _cfgFileDirectory
181 = FileDialog::getExistingDirectory(this, tr("Export configuration"), _cfgFileDirectory);
182 }
183
184 void Gui::Dialog::DlgCreateNewPreferencePackImp::accept()
185 {
186 // Ensure that the chosen name is either unique, or that the user actually wants to overwrite
187 // the old one
188 if (const auto chosenName = ui->lineEdit->text().toStdString();
189 std::ranges::find(_existingPackNames, chosenName) != _existingPackNames.end()) {
190 const auto result = QMessageBox::warning(
191 this,
192 tr("Pack already exists"),
193 tr("A preference pack with that name already exists. Overwrite it?"),
194 QMessageBox::Yes | QMessageBox::Cancel
195 );
196 if (result == QMessageBox::Cancel) {
197 return;
198 }
199 }
200 QDialog::accept();
201 }
202
203
204 #include "moc_DlgCreateNewPreferencePackImp.cpp"