FreeCAD

Форк
0
/
DlgCreateNewPreferencePackImp.cpp 
169 строк · 6.9 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2021 Chris Hennes <chennes@pioneerlibrarysystem.org>    *
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 "PreCompiled.h"
24

25
#ifndef _PreComp_
26
# include <QMessageBox>
27
# include <QPushButton>
28
# include <QRegularExpression>
29
# include <QRegularExpressionMatch>
30
#endif
31

32
#include "DlgCreateNewPreferencePackImp.h"
33
#include "ui_DlgCreateNewPreferencePack.h"
34

35

36
using namespace Gui::Dialog;
37

38
const auto TemplateRole = Qt::UserRole;
39

40
/* TRANSLATOR Gui::Dialog::DlgCreateNewPreferencePackImp */
41

42
/**
43
 *  Constructs a Gui::Dialog::DlgCreateNewPreferencePackImp as a child of 'parent'
44
 */
45
DlgCreateNewPreferencePackImp::DlgCreateNewPreferencePackImp(QWidget* parent)
46
    : QDialog(parent)
47
    , ui(new Ui_DlgCreateNewPreferencePack)
48
{
49
    ui->setupUi(this);
50

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);
57
}
58

59

60
DlgCreateNewPreferencePackImp::~DlgCreateNewPreferencePackImp() = default;
61

62
void DlgCreateNewPreferencePackImp::setPreferencePackTemplates(const std::vector<Gui::PreferencePackManager::TemplateFile>& availableTemplates)
63
{
64
    ui->treeWidget->clear();
65
    _groups.clear();
66

67
    ui->treeWidget->header()->setDefaultSectionSize(250);
68

69
    _templates = availableTemplates;
70
    for (const auto &t : _templates) {
71

72
        QTreeWidgetItem* group;
73
        if (auto foundGroup = _groups.find(t.group); foundGroup != _groups.end()) {
74
            group = foundGroup->second;
75
        }
76
        else {
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));
81
        }
82

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);
91
    }
92
}
93

94
void Gui::Dialog::DlgCreateNewPreferencePackImp::setPreferencePackNames(const std::vector<std::string>& usedNames)
95
{
96
    _existingPackNames = usedNames;
97
}
98

99
std::vector<Gui::PreferencePackManager::TemplateFile> DlgCreateNewPreferencePackImp::selectedTemplates() const
100
{
101
    std::vector<Gui::PreferencePackManager::TemplateFile> results;
102

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>());
108

109
    return results;
110
}
111

112
std::string DlgCreateNewPreferencePackImp::preferencePackName() const
113
{
114
    return ui->lineEdit->text().toStdString();
115
}
116

117
void DlgCreateNewPreferencePackImp::onItemChanged(QTreeWidgetItem* item, int column)
118
{
119
    Q_UNUSED(column);
120
    const QSignalBlocker blocker(ui->treeWidget);
121
    if (auto group = item->parent(); group) {
122
        // Child clicked
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;
128
            }
129
            else {
130
                bool thisItemChecked = child->checkState(0) == Qt::Checked;
131
                if (firstItemChecked != thisItemChecked) {
132
                    group->setCheckState(0, Qt::PartiallyChecked);
133
                    return;
134
                }
135
            }
136
        }
137
        group->setCheckState(0, firstItemChecked ? Qt::Checked : Qt::Unchecked);
138
    }
139
    else {
140
        // Group clicked:
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);
145
        }
146
    }
147
}
148

149
void DlgCreateNewPreferencePackImp::onLineEditTextEdited(const QString& text)
150
{
151
    ui->buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty());
152
}
153

154
void Gui::Dialog::DlgCreateNewPreferencePackImp::accept()
155
{
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)
163
            return;
164
    }
165
    QDialog::accept();
166
}
167

168

169
#include "moc_DlgCreateNewPreferencePackImp.cpp"
170

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.