1
/***************************************************************************
2
* Copyright (c) 2021 Chris Hennes <chennes@pioneerlibrarysystem.org> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
***************************************************************************/
23
#include "PreCompiled.h"
26
# include <QMessageBox>
27
# include <QPushButton>
28
# include <QRegularExpression>
29
# include <QRegularExpressionMatch>
32
#include "DlgCreateNewPreferencePackImp.h"
33
#include "ui_DlgCreateNewPreferencePack.h"
36
using namespace Gui::Dialog;
38
const auto TemplateRole = Qt::UserRole;
40
/* TRANSLATOR Gui::Dialog::DlgCreateNewPreferencePackImp */
43
* Constructs a Gui::Dialog::DlgCreateNewPreferencePackImp as a child of 'parent'
45
DlgCreateNewPreferencePackImp::DlgCreateNewPreferencePackImp(QWidget* parent)
47
, ui(new Ui_DlgCreateNewPreferencePack)
51
QRegularExpression validNames(QString::fromUtf8(R"([^/\\?%*:|"<>]+)"));
52
_nameValidator.setRegularExpression(validNames);
53
ui->lineEdit->setValidator(&_nameValidator);
54
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
55
connect(ui->treeWidget, &QTreeWidget::itemChanged, this, &DlgCreateNewPreferencePackImp::onItemChanged);
56
connect(ui->lineEdit, &QLineEdit::textEdited, this, &DlgCreateNewPreferencePackImp::onLineEditTextEdited);
60
DlgCreateNewPreferencePackImp::~DlgCreateNewPreferencePackImp() = default;
62
void DlgCreateNewPreferencePackImp::setPreferencePackTemplates(const std::vector<Gui::PreferencePackManager::TemplateFile>& availableTemplates)
64
ui->treeWidget->clear();
67
ui->treeWidget->header()->setDefaultSectionSize(250);
69
_templates = availableTemplates;
70
for (const auto &t : _templates) {
72
QTreeWidgetItem* group;
73
if (auto foundGroup = _groups.find(t.group); foundGroup != _groups.end()) {
74
group = foundGroup->second;
77
group = new QTreeWidgetItem(ui->treeWidget, QStringList(QString::fromStdString(t.group)));
78
group->setCheckState(0, Qt::Checked);
79
group->setExpanded(true);
80
_groups.insert(std::make_pair(t.group, group));
83
QStringList itemColumns;
84
itemColumns.push_back(QString::fromStdString(t.name));
85
auto newItem = new QTreeWidgetItem(group, itemColumns);
86
newItem->setCheckState(0, Qt::Checked);
87
if (group->checkState(0) != newItem->checkState(0))
88
group->setCheckState(0, Qt::PartiallyChecked);
89
newItem->setData(0, TemplateRole, QVariant::fromValue(t));
90
group->addChild(newItem);
94
void Gui::Dialog::DlgCreateNewPreferencePackImp::setPreferencePackNames(const std::vector<std::string>& usedNames)
96
_existingPackNames = usedNames;
99
std::vector<Gui::PreferencePackManager::TemplateFile> DlgCreateNewPreferencePackImp::selectedTemplates() const
101
std::vector<Gui::PreferencePackManager::TemplateFile> results;
103
for (const auto& group : _groups)
104
for (int childIndex = 0; childIndex < group.second->childCount(); ++childIndex)
105
if (auto child = group.second->child(childIndex); child->checkState(0) == Qt::Checked)
106
if (child->data(0, TemplateRole).canConvert<Gui::PreferencePackManager::TemplateFile>())
107
results.push_back(child->data(0, TemplateRole).value<Gui::PreferencePackManager::TemplateFile>());
112
std::string DlgCreateNewPreferencePackImp::preferencePackName() const
114
return ui->lineEdit->text().toStdString();
117
void DlgCreateNewPreferencePackImp::onItemChanged(QTreeWidgetItem* item, int column)
120
const QSignalBlocker blocker(ui->treeWidget);
121
if (auto group = item->parent(); group) {
123
bool firstItemChecked = false;
124
for (int childIndex = 0; childIndex < group->childCount(); ++childIndex) {
125
auto child = group->child(childIndex);
126
if (childIndex == 0) {
127
firstItemChecked = child->checkState(0) == Qt::Checked;
130
bool thisItemChecked = child->checkState(0) == Qt::Checked;
131
if (firstItemChecked != thisItemChecked) {
132
group->setCheckState(0, Qt::PartiallyChecked);
137
group->setCheckState(0, firstItemChecked ? Qt::Checked : Qt::Unchecked);
141
auto groupCheckState = item->checkState(0);
142
for (int childIndex = 0; childIndex < item->childCount(); ++childIndex) {
143
auto child = item->child(childIndex);
144
child->setCheckState(0, groupCheckState);
149
void DlgCreateNewPreferencePackImp::onLineEditTextEdited(const QString& text)
151
ui->buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty());
154
void Gui::Dialog::DlgCreateNewPreferencePackImp::accept()
156
// Ensure that the chosen name is either unique, or that the user actually wants to overwrite the old one
157
if (auto chosenName = ui->lineEdit->text().toStdString();
158
std::find(_existingPackNames.begin(), _existingPackNames.end(), chosenName) != _existingPackNames.end()) {
159
auto result = QMessageBox::warning(this, tr("Pack already exists"),
160
tr("A preference pack with that name already exists. Do you want to overwrite it?"),
161
QMessageBox::Yes | QMessageBox::Cancel);
162
if (result == QMessageBox::Cancel)
169
#include "moc_DlgCreateNewPreferencePackImp.cpp"