FreeCAD
303 строки · 11.7 Кб
1/***************************************************************************
2* Copyright (c) 2011 Werner Mayer <wmayer[at]users.sourceforge.net> *
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#ifndef _PreComp_
25#include <cfloat>
26#endif
27
28#include <App/Document.h>
29#include <App/DocumentObject.h>
30#include <Gui/Application.h>
31#include <Gui/Command.h>
32#include <Gui/Document.h>
33#include <Gui/MainWindow.h>
34#include <Gui/PrefWidgets.h>
35#include <Gui/ViewProvider.h>
36
37#include "VisualInspection.h"
38#include "ui_VisualInspection.h"
39
40
41using namespace InspectionGui;
42
43namespace InspectionGui
44{
45class SingleSelectionItem: public QTreeWidgetItem
46{
47public:
48explicit SingleSelectionItem(QTreeWidget* parent)
49: QTreeWidgetItem(parent)
50, _compItem(nullptr)
51{}
52
53explicit SingleSelectionItem(QTreeWidgetItem* parent)
54: QTreeWidgetItem(parent)
55, _compItem(nullptr)
56{}
57
58~SingleSelectionItem() override = default;
59
60SingleSelectionItem* getCompetitiveItem() const
61{
62return _compItem;
63}
64
65void setCompetitiveItem(SingleSelectionItem* item)
66{
67_compItem = item;
68}
69
70private:
71SingleSelectionItem* _compItem;
72};
73} // namespace InspectionGui
74
75/* TRANSLATOR InspectionGui::DlgVisualInspectionImp */
76
77/**
78* Constructs a VisualInspection as a child of 'parent', with the
79* name 'name' and widget flags set to 'f'.
80*/
81VisualInspection::VisualInspection(QWidget* parent, Qt::WindowFlags fl)
82: QDialog(parent, fl)
83, ui(new Ui_VisualInspection)
84{
85ui->setupUi(this);
86connect(ui->treeWidgetActual,
87&QTreeWidget::itemClicked,
88this,
89&VisualInspection::onActivateItem);
90connect(ui->treeWidgetNominal,
91&QTreeWidget::itemClicked,
92this,
93&VisualInspection::onActivateItem);
94connect(ui->buttonBox,
95&QDialogButtonBox::helpRequested,
96Gui::getMainWindow(),
97&Gui::MainWindow::whatsThis);
98
99// FIXME: Not used yet
100ui->textLabel2->hide();
101ui->thickness->hide();
102ui->searchRadius->setUnit(Base::Unit::Length);
103ui->searchRadius->setRange(0, DBL_MAX);
104ui->thickness->setUnit(Base::Unit::Length);
105ui->thickness->setRange(0, DBL_MAX);
106
107App::Document* doc = App::GetApplication().getActiveDocument();
108// disable Ok button and enable of at least one item in each view is on
109buttonOk = ui->buttonBox->button(QDialogButtonBox::Ok);
110buttonOk->setDisabled(true);
111
112if (!doc) {
113ui->treeWidgetActual->setDisabled(true);
114ui->treeWidgetNominal->setDisabled(true);
115return;
116}
117
118Gui::Document* gui = Gui::Application::Instance->getDocument(doc);
119
120std::vector<App::DocumentObject*> obj = doc->getObjects();
121Base::Type point = Base::Type::fromName("Points::Feature");
122Base::Type mesh = Base::Type::fromName("Mesh::Feature");
123Base::Type shape = Base::Type::fromName("Part::Feature");
124for (auto it : obj) {
125if (it->getTypeId().isDerivedFrom(point) || it->getTypeId().isDerivedFrom(mesh)
126|| it->getTypeId().isDerivedFrom(shape)) {
127Gui::ViewProvider* view = gui->getViewProvider(it);
128QIcon px = view->getIcon();
129SingleSelectionItem* item1 = new SingleSelectionItem(ui->treeWidgetActual);
130item1->setText(0, QString::fromUtf8(it->Label.getValue()));
131item1->setData(0, Qt::UserRole, QString::fromLatin1(it->getNameInDocument()));
132item1->setCheckState(0, Qt::Unchecked);
133item1->setIcon(0, px);
134
135SingleSelectionItem* item2 = new SingleSelectionItem(ui->treeWidgetNominal);
136item2->setText(0, QString::fromUtf8(it->Label.getValue()));
137item2->setData(0, Qt::UserRole, QString::fromLatin1(it->getNameInDocument()));
138item2->setCheckState(0, Qt::Unchecked);
139item2->setIcon(0, px);
140
141item1->setCompetitiveItem(item2);
142item2->setCompetitiveItem(item1);
143}
144}
145
146loadSettings();
147}
148
149/*
150* Destroys the object and frees any allocated resources
151*/
152VisualInspection::~VisualInspection()
153{
154// no need to delete child widgets, Qt does it all for us
155delete ui;
156}
157
158void VisualInspection::loadSettings()
159{
160ParameterGrp::handle handle = App::GetApplication().GetParameterGroupByPath(
161"User parameter:BaseApp/Preferences/Mod/Inspection/Inspection");
162
163double searchDistance = ui->searchRadius->value().getValue();
164searchDistance = handle->GetFloat("SearchDistance", searchDistance);
165ui->searchRadius->setValue(searchDistance);
166
167double thickness = ui->thickness->value().getValue();
168thickness = handle->GetFloat("Thickness", thickness);
169ui->thickness->setValue(thickness);
170}
171
172void VisualInspection::saveSettings()
173{
174ParameterGrp::handle handle = App::GetApplication().GetParameterGroupByPath(
175"User parameter:BaseApp/Preferences/Mod/Inspection/Inspection");
176double searchDistance = ui->searchRadius->value().getValue();
177handle->SetFloat("SearchDistance", searchDistance);
178
179double thickness = ui->thickness->value().getValue();
180handle->SetFloat("Thickness", thickness);
181}
182
183void VisualInspection::onActivateItem(QTreeWidgetItem* item)
184{
185if (item) {
186SingleSelectionItem* sel = static_cast<SingleSelectionItem*>(item);
187SingleSelectionItem* cmp = sel->getCompetitiveItem();
188if (cmp && cmp->checkState(0) == Qt::Checked) {
189cmp->setCheckState(0, Qt::Unchecked);
190}
191}
192
193bool ok = false;
194for (QTreeWidgetItemIterator it(ui->treeWidgetActual); *it; ++it) {
195SingleSelectionItem* sel = (SingleSelectionItem*)*it;
196if (sel->checkState(0) == Qt::Checked) {
197ok = true;
198break;
199}
200}
201
202if (ok) {
203ok = false;
204for (QTreeWidgetItemIterator it(ui->treeWidgetNominal); *it; ++it) {
205SingleSelectionItem* sel = (SingleSelectionItem*)*it;
206if (sel->checkState(0) == Qt::Checked) {
207ok = true;
208break;
209}
210}
211}
212
213buttonOk->setEnabled(ok);
214}
215
216void VisualInspection::accept()
217{
218onActivateItem(nullptr);
219if (buttonOk->isEnabled()) {
220QDialog::accept();
221saveSettings();
222
223// collect all nominal geometries
224QStringList nominalNames;
225for (QTreeWidgetItemIterator it(ui->treeWidgetNominal); *it; it++) {
226SingleSelectionItem* sel = (SingleSelectionItem*)*it;
227if (sel->checkState(0) == Qt::Checked) {
228nominalNames << sel->data(0, Qt::UserRole).toString();
229}
230}
231
232double searchRadius = ui->searchRadius->value().getValue();
233double thickness = ui->thickness->value().getValue();
234
235// open a new command
236Gui::Document* doc = Gui::Application::Instance->activeDocument();
237doc->openCommand(QT_TRANSLATE_NOOP("Command", "Visual Inspection"));
238
239// create a group
240Gui::Command::runCommand(Gui::Command::App,
241"App_activeDocument___InspectionGroup=App.ActiveDocument."
242"addObject(\"Inspection::Group\",\"Inspection\")");
243
244// for each actual geometry create an inspection feature
245for (QTreeWidgetItemIterator it(ui->treeWidgetActual); *it; it++) {
246SingleSelectionItem* sel = (SingleSelectionItem*)*it;
247if (sel->checkState(0) == Qt::Checked) {
248QString actualName = sel->data(0, Qt::UserRole).toString();
249Gui::Command::doCommand(Gui::Command::App,
250"App_activeDocument___InspectionGroup.newObject("
251"\"Inspection::Feature\",\"%s_Inspect\")",
252(const char*)actualName.toLatin1());
253Gui::Command::doCommand(
254Gui::Command::App,
255"App.ActiveDocument.ActiveObject.Actual=App.ActiveDocument.%s\n"
256"App_activeDocument___activeObject___Nominals=list()\n"
257"App.ActiveDocument.ActiveObject.SearchRadius=%.3f\n"
258"App.ActiveDocument.ActiveObject.Thickness=%.3f\n",
259(const char*)actualName.toLatin1(),
260searchRadius,
261thickness);
262for (const auto& it : nominalNames) {
263Gui::Command::doCommand(Gui::Command::App,
264"App_activeDocument___activeObject___Nominals.append("
265"App.ActiveDocument.%s)\n",
266(const char*)it.toLatin1());
267}
268Gui::Command::doCommand(Gui::Command::App,
269"App.ActiveDocument.ActiveObject.Nominals=App_"
270"activeDocument___activeObject___Nominals\n"
271"del App_activeDocument___activeObject___Nominals\n");
272}
273}
274
275Gui::Command::runCommand(Gui::Command::App, "del App_activeDocument___InspectionGroup\n");
276
277doc->commitCommand();
278doc->getDocument()->recompute();
279
280// hide the checked features
281for (QTreeWidgetItemIterator it(ui->treeWidgetActual); *it; it++) {
282SingleSelectionItem* sel = (SingleSelectionItem*)*it;
283if (sel->checkState(0) == Qt::Checked) {
284Gui::Command::doCommand(
285Gui::Command::App,
286"Gui.ActiveDocument.getObject(\"%s\").Visibility=False",
287(const char*)sel->data(0, Qt::UserRole).toString().toLatin1());
288}
289}
290
291for (QTreeWidgetItemIterator it(ui->treeWidgetNominal); *it; it++) {
292SingleSelectionItem* sel = (SingleSelectionItem*)*it;
293if (sel->checkState(0) == Qt::Checked) {
294Gui::Command::doCommand(
295Gui::Command::App,
296"Gui.ActiveDocument.getObject(\"%s\").Visibility=False",
297(const char*)sel->data(0, Qt::UserRole).toString().toLatin1());
298}
299}
300}
301}
302
303#include "moc_VisualInspection.cpp"
304