File size: 7,714 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | // SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
* Copyright (c) 2021 Chris Hennes <chennes@pioneerlibrarysystem.org> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include <QMessageBox>
#include <QPushButton>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include "Dialogs/DlgCreateNewPreferencePackImp.h"
#include "ui_DlgCreateNewPreferencePack.h"
#include "FileDialog.h"
using namespace Gui::Dialog;
const auto TemplateRole = Qt::UserRole;
/* TRANSLATOR Gui::Dialog::DlgCreateNewPreferencePackImp */
/**
* Constructs a Gui::Dialog::DlgCreateNewPreferencePackImp as a child of 'parent'
*/
DlgCreateNewPreferencePackImp::DlgCreateNewPreferencePackImp(QWidget* parent)
: QDialog(parent)
, ui(new Ui_DlgCreateNewPreferencePack)
{
ui->setupUi(this);
QRegularExpression validNames(QStringLiteral(R"([^/\\?%*:|"<>]+)"));
_nameValidator.setRegularExpression(validNames);
ui->lineEdit->setValidator(&_nameValidator);
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
connect(ui->treeWidget, &QTreeWidget::itemChanged, this, &DlgCreateNewPreferencePackImp::onItemChanged);
connect(
ui->lineEdit,
&QLineEdit::textEdited,
this,
&DlgCreateNewPreferencePackImp::onLineEditTextEdited
);
connect(
ui->pushButton,
&QPushButton::clicked,
this,
&DlgCreateNewPreferencePackImp::onBrowseButtonClicked
);
}
DlgCreateNewPreferencePackImp::~DlgCreateNewPreferencePackImp() = default;
void DlgCreateNewPreferencePackImp::setPreferencePackTemplates(
const std::vector<Gui::PreferencePackManager::TemplateFile>& availableTemplates
)
{
ui->treeWidget->clear();
_groups.clear();
ui->treeWidget->header()->setDefaultSectionSize(250);
_templates = availableTemplates;
for (const auto& t : _templates) {
QTreeWidgetItem* group;
if (auto foundGroup = _groups.find(t.group); foundGroup != _groups.end()) {
group = foundGroup->second;
}
else {
group = new QTreeWidgetItem(ui->treeWidget, QStringList(QString::fromStdString(t.group)));
group->setCheckState(0, Qt::Checked);
group->setExpanded(true);
_groups.insert(std::make_pair(t.group, group));
}
QStringList itemColumns;
itemColumns.push_back(QString::fromStdString(t.name));
auto newItem = new QTreeWidgetItem(group, itemColumns);
newItem->setCheckState(0, Qt::Checked);
if (group->checkState(0) != newItem->checkState(0)) {
group->setCheckState(0, Qt::PartiallyChecked);
}
newItem->setData(0, TemplateRole, QVariant::fromValue(t));
group->addChild(newItem);
}
}
void Gui::Dialog::DlgCreateNewPreferencePackImp::setPreferencePackNames(
const std::vector<std::string>& usedNames
)
{
_existingPackNames = usedNames;
}
std::vector<Gui::PreferencePackManager::TemplateFile> DlgCreateNewPreferencePackImp::selectedTemplates() const
{
std::vector<Gui::PreferencePackManager::TemplateFile> results;
for (const auto& group : _groups) {
for (int childIndex = 0; childIndex < group.second->childCount(); ++childIndex) {
if (auto child = group.second->child(childIndex); child->checkState(0) == Qt::Checked) {
if (child->data(0, TemplateRole).canConvert<Gui::PreferencePackManager::TemplateFile>()) {
results.push_back(
child->data(0, TemplateRole).value<Gui::PreferencePackManager::TemplateFile>()
);
}
}
}
}
return results;
}
std::string DlgCreateNewPreferencePackImp::preferencePackName() const
{
return ui->lineEdit->text().toStdString();
}
std::string Gui::Dialog::DlgCreateNewPreferencePackImp::preferencePackDirectory() const
{
return _cfgFileDirectory.toStdString();
}
void DlgCreateNewPreferencePackImp::onItemChanged(QTreeWidgetItem* item, int column)
{
Q_UNUSED(column);
const QSignalBlocker blocker(ui->treeWidget);
if (auto group = item->parent(); group) {
// Child clicked
bool firstItemChecked = false;
for (int childIndex = 0; childIndex < group->childCount(); ++childIndex) {
auto child = group->child(childIndex);
if (childIndex == 0) {
firstItemChecked = child->checkState(0) == Qt::Checked;
}
else {
bool thisItemChecked = child->checkState(0) == Qt::Checked;
if (firstItemChecked != thisItemChecked) {
group->setCheckState(0, Qt::PartiallyChecked);
return;
}
}
}
group->setCheckState(0, firstItemChecked ? Qt::Checked : Qt::Unchecked);
}
else {
// Group clicked:
auto groupCheckState = item->checkState(0);
for (int childIndex = 0; childIndex < item->childCount(); ++childIndex) {
auto child = item->child(childIndex);
child->setCheckState(0, groupCheckState);
}
}
}
void DlgCreateNewPreferencePackImp::onLineEditTextEdited(const QString& text)
{
ui->buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty());
}
void DlgCreateNewPreferencePackImp::onBrowseButtonClicked()
{
_cfgFileDirectory
= FileDialog::getExistingDirectory(this, tr("Export configuration"), _cfgFileDirectory);
}
void Gui::Dialog::DlgCreateNewPreferencePackImp::accept()
{
// Ensure that the chosen name is either unique, or that the user actually wants to overwrite
// the old one
if (const auto chosenName = ui->lineEdit->text().toStdString();
std::ranges::find(_existingPackNames, chosenName) != _existingPackNames.end()) {
const auto result = QMessageBox::warning(
this,
tr("Pack already exists"),
tr("A preference pack with that name already exists. Overwrite it?"),
QMessageBox::Yes | QMessageBox::Cancel
);
if (result == QMessageBox::Cancel) {
return;
}
}
QDialog::accept();
}
#include "moc_DlgCreateNewPreferencePackImp.cpp"
|