1
/***************************************************************************
2
* Copyright (c) 2012 Werner Mayer <wmayer[at]users.sourceforge.net> *
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"
25
# include <QMessageBox>
28
#include <App/Application.h>
29
#include <App/Document.h>
30
#include <App/DocumentObject.h>
31
#include <Gui/Application.h>
32
#include <Gui/BitmapFactory.h>
33
#include <Gui/Command.h>
34
#include <Gui/CommandT.h>
35
#include <Gui/Document.h>
36
#include <Gui/Selection.h>
37
#include <Gui/SelectionFilter.h>
38
#include <Gui/SelectionObject.h>
39
#include <Gui/ViewProvider.h>
40
#include <Mod/Part/App/PartFeatures.h>
42
#include "TaskThickness.h"
43
#include "ui_TaskOffset.h"
46
using namespace PartGui;
48
class ThicknessWidget::Private
53
std::string selection;
54
Part::Thickness* thickness{nullptr};
56
class FaceSelection : public Gui::SelectionFilterGate
58
const App::DocumentObject* object;
60
explicit FaceSelection(const App::DocumentObject* obj)
61
: Gui::SelectionFilterGate(nullPointer()), object(obj)
64
bool allow(App::Document* /*pDoc*/, App::DocumentObject*pObj, const char*sSubName) override
66
if (pObj != this->object)
68
if (!sSubName || sSubName[0] == '\0')
70
std::string element(sSubName);
71
return element.substr(0,4) == "Face";
76
/* TRANSLATOR PartGui::ThicknessWidget */
78
ThicknessWidget::ThicknessWidget(Part::Thickness* thickness, QWidget* parent)
82
Gui::Command::runCommand(Gui::Command::App, "from FreeCAD import Base");
83
Gui::Command::runCommand(Gui::Command::App, "import Part");
85
d->thickness = thickness;
89
d->ui.labelOffset->setText(tr("Thickness"));
90
d->ui.fillOffset->hide();
92
QSignalBlocker blockOffset(d->ui.spinOffset);
93
d->ui.spinOffset->setRange(-INT_MAX, INT_MAX);
94
d->ui.spinOffset->setSingleStep(0.1);
95
d->ui.spinOffset->setValue(d->thickness->Value.getValue());
97
int mode = d->thickness->Mode.getValue();
98
d->ui.modeType->setCurrentIndex(mode);
100
int join = d->thickness->Join.getValue();
101
d->ui.joinType->setCurrentIndex(join);
103
QSignalBlocker blockIntSct(d->ui.intersection);
104
bool intsct = d->thickness->Intersection.getValue();
105
d->ui.intersection->setChecked(intsct);
107
QSignalBlocker blockSelfInt(d->ui.selfIntersection);
108
bool selfint = d->thickness->SelfIntersection.getValue();
109
d->ui.selfIntersection->setChecked(selfint);
111
d->ui.spinOffset->bind(d->thickness->Value);
114
ThicknessWidget::~ThicknessWidget()
117
Gui::Selection().rmvSelectionGate();
120
void ThicknessWidget::setupConnections()
123
connect(d->ui.spinOffset, qOverload<double>(&Gui::QuantitySpinBox::valueChanged),
124
this, &ThicknessWidget::onSpinOffsetValueChanged);
125
connect(d->ui.modeType, qOverload<int>(&QComboBox::activated),
126
this, &ThicknessWidget::onModeTypeActivated);
127
connect(d->ui.joinType, qOverload<int>(&QComboBox::activated),
128
this, &ThicknessWidget::onJoinTypeActivated);
129
connect(d->ui.intersection, &QCheckBox::toggled,
130
this, &ThicknessWidget::onIntersectionToggled);
131
connect(d->ui.selfIntersection, &QCheckBox::toggled,
132
this, &ThicknessWidget::onSelfIntersectionToggled);
133
connect(d->ui.facesButton, &QPushButton::toggled,
134
this, &ThicknessWidget::onFacesButtonToggled);
135
connect(d->ui.updateView, &QCheckBox::toggled,
136
this, &ThicknessWidget::onUpdateViewToggled);
140
Part::Thickness* ThicknessWidget::getObject() const
145
void ThicknessWidget::onSpinOffsetValueChanged(double val)
147
d->thickness->Value.setValue(val);
148
if (d->ui.updateView->isChecked())
149
d->thickness->getDocument()->recomputeFeature(d->thickness);
152
void ThicknessWidget::onModeTypeActivated(int val)
154
d->thickness->Mode.setValue(val);
155
if (d->ui.updateView->isChecked())
156
d->thickness->getDocument()->recomputeFeature(d->thickness);
159
void ThicknessWidget::onJoinTypeActivated(int val)
161
d->thickness->Join.setValue((long)val);
162
if (d->ui.updateView->isChecked())
163
d->thickness->getDocument()->recomputeFeature(d->thickness);
166
void ThicknessWidget::onIntersectionToggled(bool on)
168
d->thickness->Intersection.setValue(on);
169
if (d->ui.updateView->isChecked())
170
d->thickness->getDocument()->recomputeFeature(d->thickness);
173
void ThicknessWidget::onSelfIntersectionToggled(bool on)
175
d->thickness->SelfIntersection.setValue(on);
176
if (d->ui.updateView->isChecked())
177
d->thickness->getDocument()->recomputeFeature(d->thickness);
180
void ThicknessWidget::onFacesButtonToggled(bool on)
183
QList<QWidget*> c = this->findChildren<QWidget*>();
185
it->setEnabled(false);
186
d->ui.facesButton->setEnabled(true);
187
d->ui.labelFaces->setText(tr("Select faces of the source object and press 'Done'"));
188
d->ui.labelFaces->setEnabled(true);
189
d->text = d->ui.facesButton->text();
190
d->ui.facesButton->setText(tr("Done"));
192
Gui::Application::Instance->showViewProvider(d->thickness->Faces.getValue());
193
Gui::Application::Instance->hideViewProvider(d->thickness);
194
Gui::Selection().clearSelection();
195
Gui::Selection().addSelectionGate(new Private::FaceSelection(d->thickness->Faces.getValue()));
198
QList<QWidget*> c = this->findChildren<QWidget*>();
200
it->setEnabled(true);
201
d->ui.facesButton->setText(d->text);
202
d->ui.labelFaces->clear();
204
d->selection = Gui::Command::getPythonTuple
205
(d->thickness->Faces.getValue()->getNameInDocument(), d->thickness->Faces.getSubValues());
206
std::vector<Gui::SelectionObject> sel = Gui::Selection().getSelectionEx();
207
for (auto & it : sel) {
208
if (it.getObject() == d->thickness->Faces.getValue()) {
209
d->thickness->Faces.setValue(it.getObject(), it.getSubNames());
210
d->selection = it.getAsPropertyLinkSubString();
215
Gui::Selection().rmvSelectionGate();
216
Gui::Application::Instance->showViewProvider(d->thickness);
217
Gui::Application::Instance->hideViewProvider(d->thickness->Faces.getValue());
218
if (d->ui.updateView->isChecked())
219
d->thickness->getDocument()->recomputeFeature(d->thickness);
223
void ThicknessWidget::onUpdateViewToggled(bool on)
226
d->thickness->getDocument()->recomputeFeature(d->thickness);
230
bool ThicknessWidget::accept()
232
if (d->ui.facesButton->isChecked())
236
if (!d->selection.empty()) {
237
Gui::cmdAppObjectArgs(d->thickness, "Faces = %s", d->selection.c_str());
239
Gui::cmdAppObjectArgs(d->thickness, "Value = %f", d->ui.spinOffset->value().getValue());
240
Gui::cmdAppObjectArgs(d->thickness, "Mode = %d", d->ui.modeType->currentIndex());
241
Gui::cmdAppObjectArgs(d->thickness, "Join = %d", d->ui.joinType->currentIndex());
242
Gui::cmdAppObjectArgs(d->thickness, "Intersection = %s",
243
d->ui.intersection->isChecked() ? "True" : "False");
244
Gui::cmdAppObjectArgs(d->thickness, "SelfIntersection = %s",
245
d->ui.selfIntersection->isChecked() ? "True" : "False");
247
Gui::Command::doCommand(Gui::Command::Doc,"App.ActiveDocument.recompute()");
248
if (!d->thickness->isValid())
249
throw Base::CADKernelError(d->thickness->getStatusString());
250
Gui::Command::doCommand(Gui::Command::Gui,"Gui.ActiveDocument.resetEdit()");
251
Gui::Command::commitCommand();
253
catch (const Base::Exception& e) {
254
QMessageBox::warning(
255
this, tr("Input error"), QCoreApplication::translate("Exception", e.what()));
262
bool ThicknessWidget::reject()
264
if (d->ui.facesButton->isChecked())
267
// save this and check if the object is still there after the
268
// transaction is aborted
269
std::string objname = d->thickness->getNameInDocument();
270
App::DocumentObject* source = d->thickness->Faces.getValue();
272
// roll back the done things
273
Gui::Command::abortCommand();
274
Gui::Command::doCommand(Gui::Command::Gui,"Gui.ActiveDocument.resetEdit()");
275
Gui::Command::updateActive();
277
// Thickness object was deleted
278
if (source && !source->getDocument()->getObject(objname.c_str())) {
279
Gui::Application::Instance->getViewProvider(source)->show();
285
void ThicknessWidget::changeEvent(QEvent *e)
287
QWidget::changeEvent(e);
288
if (e->type() == QEvent::LanguageChange) {
289
d->ui.retranslateUi(this);
290
d->ui.labelOffset->setText(tr("Thickness"));
295
/* TRANSLATOR PartGui::TaskThickness */
297
TaskThickness::TaskThickness(Part::Thickness* offset)
299
widget = new ThicknessWidget(offset);
300
widget->setWindowTitle(ThicknessWidget::tr("Thickness"));
301
addTaskBox(Gui::BitmapFactory().pixmap("Part_Thickness"), widget);
304
Part::Thickness* TaskThickness::getObject() const
306
return widget->getObject();
309
void TaskThickness::open()
313
void TaskThickness::clicked(int)
317
bool TaskThickness::accept()
319
return widget->accept();
322
bool TaskThickness::reject()
324
return widget->reject();
327
#include "moc_TaskThickness.cpp"