/****************************************************************************** * Copyright (c)2012 Jan Rheinlaender * * * * 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 "PreCompiled.h" #ifndef _PreComp_ #endif #include "ui_TaskGrooveParameters.h" #include "TaskGrooveParameters.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "Workbench.h" #include "ReferenceSelection.h" #include "TaskSketchBasedParameters.h" using namespace PartDesignGui; using namespace Gui; /* TRANSLATOR PartDesignGui::TaskGrooveParameters */ TaskGrooveParameters::TaskGrooveParameters(ViewProviderGroove *GrooveView,QWidget *parent) : TaskSketchBasedParameters(GrooveView, parent, "PartDesign_Groove",tr("Groove parameters")) { // we need a separate container widget to add all controls to proxy = new QWidget(this); ui = new Ui_TaskGrooveParameters(); ui->setupUi(proxy); QMetaObject::connectSlotsByName(this); connect(ui->grooveAngle, SIGNAL(valueChanged(double)), this, SLOT(onAngleChanged(double))); connect(ui->axis, SIGNAL(activated(int)), this, SLOT(onAxisChanged(int))); connect(ui->checkBoxMidplane, SIGNAL(toggled(bool)), this, SLOT(onMidplane(bool))); connect(ui->checkBoxReversed, SIGNAL(toggled(bool)), this, SLOT(onReversed(bool))); connect(ui->checkBoxUpdateView, SIGNAL(toggled(bool)), this, SLOT(onUpdateView(bool))); this->groupLayout()->addWidget(proxy); // Temporarily prevent unnecessary feature updates ui->grooveAngle->blockSignals(true); ui->axis->blockSignals(true); ui->checkBoxMidplane->blockSignals(true); ui->checkBoxReversed->blockSignals(true); PartDesign::Groove* pcGroove = static_cast(vp->getObject()); double l = pcGroove->Angle.getValue(); bool mirrored = pcGroove->Midplane.getValue(); bool reversed = pcGroove->Reversed.getValue(); ui->grooveAngle->setValue(l); ui->grooveAngle->bind(pcGroove->Angle); <<<<<<< f0798a82fe8f03db57aca4f634d2123486daaea0 int count=pcGroove->getSketchAxisCount(); for (int i=ui->axis->count()-1; i >= count+2; i--) ui->axis->removeItem(i); for (int i=ui->axis->count(); i < count+2; i++) ui->axis->addItem(QString::fromLatin1("Sketch axis %1").arg(i-2)); int pos=-1; App::DocumentObject *pcReferenceAxis = pcGroove->ReferenceAxis.getValue(); const std::vector &subReferenceAxis = pcGroove->ReferenceAxis.getSubValues(); if (pcReferenceAxis && pcReferenceAxis == pcGroove->Sketch.getValue()) { assert(subReferenceAxis.size()==1); if (subReferenceAxis[0] == "V_Axis") pos = 0; else if (subReferenceAxis[0] == "H_Axis") pos = 1; else if (subReferenceAxis[0].size() > 4 && subReferenceAxis[0].substr(0,4) == "Axis") pos = 2 + std::atoi(subReferenceAxis[0].substr(4,4000).c_str()); } if (pos < 0 || pos >= ui->axis->count()) { ui->axis->addItem(QString::fromLatin1("Undefined")); pos = ui->axis->count()-1; } ui->axis->setCurrentIndex(pos); ======= blockUpdate = false; updateUI(); >>>>>>> Enable edges and datum lines as rotation axis for Groove and Revolution features ui->checkBoxMidplane->setChecked(mirrored); ui->checkBoxReversed->setChecked(reversed); ui->grooveAngle->blockSignals(false); ui->axis->blockSignals(false); ui->checkBoxMidplane->blockSignals(false); ui->checkBoxReversed->blockSignals(false); setFocus (); } void TaskGrooveParameters::updateUI() { if (blockUpdate) return; blockUpdate = true; PartDesign::Groove* pcGroove = static_cast(vp->getObject()); App::DocumentObject* pcReferenceAxis = pcGroove->ReferenceAxis.getValue(); std::vector sub = pcGroove->ReferenceAxis.getSubValues(); // Add user-defined sketch axes to the reference selection combo box Sketcher::SketchObject *pcSketch = static_cast(pcGroove->Sketch.getValue()); int maxcount=2; if (pcSketch) maxcount += pcSketch->getAxisCount(); for (int i=ui->axis->count()-1; i >= 2; i--) ui->axis->removeItem(i); for (int i=ui->axis->count(); i < maxcount; i++) ui->axis->addItem(QString::fromAscii("Sketch axis %1").arg(i-5)); bool undefined = false; if (pcReferenceAxis != NULL && !sub.empty()) { if (sub.front() == "H_Axis") ui->axis->setCurrentIndex(0); else if (sub.front() == "V_Axis") ui->axis->setCurrentIndex(1); else if (sub.front().size() > 4 && sub.front().substr(0,4) == "Axis") { int pos = 2 + std::atoi(sub.front().substr(4,4000).c_str()); if (pos <= maxcount) ui->axis->setCurrentIndex(pos); else undefined = true; } else { ui->axis->addItem(getRefStr(pcReferenceAxis, sub)); ui->axis->setCurrentIndex(maxcount); } } else { undefined = true; } ui->axis->addItem(tr("Select reference...")); blockUpdate = false; } void TaskGrooveParameters::onSelectionChanged(const Gui::SelectionChanges& msg) { if (msg.Type == Gui::SelectionChanges::AddSelection) { PartDesign::Groove* pcGroove = static_cast(vp->getObject()); exitSelectionMode(); if (!blockUpdate) { std::vector axis; App::DocumentObject* selObj; getReferencedSelection(pcGroove, msg, selObj, axis); pcGroove->ReferenceAxis.setValue(selObj, axis); recomputeFeature(); updateUI(); } else { Sketcher::SketchObject *pcSketch = static_cast(pcGroove->Sketch.getValue()); int maxcount=2; if (pcSketch) maxcount += pcSketch->getAxisCount(); for (int i=ui->axis->count()-1; i >= maxcount; i--) ui->axis->removeItem(i); std::vector sub; App::DocumentObject* selObj; getReferencedSelection(pcGroove, msg, selObj, sub); ui->axis->addItem(getRefStr(selObj, sub)); ui->axis->setCurrentIndex(maxcount); ui->axis->addItem(tr("Select reference...")); } } } void TaskGrooveParameters::onAngleChanged(double len) { PartDesign::Groove* pcGroove = static_cast(vp->getObject()); pcGroove->Angle.setValue(len); exitSelectionMode(); recomputeFeature(); } void TaskGrooveParameters::onAxisChanged(int num) { if (blockUpdate) return; PartDesign::Groove* pcGroove = static_cast(vp->getObject()); Sketcher::SketchObject *pcSketch = static_cast(pcGroove->Sketch.getValue()); if (pcSketch) { App::DocumentObject *oldRefAxis = pcGroove->ReferenceAxis.getValue(); std::vector oldSubRefAxis = pcGroove->ReferenceAxis.getSubValues(); int maxcount = pcSketch->getAxisCount()+2; if (num == 0) { pcGroove->ReferenceAxis.setValue(pcSketch, std::vector(1,"H_Axis")); exitSelectionMode(); } else if (num == 1) { pcGroove->ReferenceAxis.setValue(pcSketch, std::vector(1,"V_Axis")); exitSelectionMode(); } else if (num >= 2 && num < maxcount) { QString buf = QString::fromUtf8("Axis%1").arg(num-2); std::string str = buf.toStdString(); pcGroove->ReferenceAxis.setValue(pcSketch, std::vector(1,str)); exitSelectionMode(); } else if (num == ui->axis->count() - 1) { // enter reference selection mode TaskSketchBasedParameters::onSelectReference(true, true, false, true); } else if (num == maxcount) exitSelectionMode(); App::DocumentObject *newRefAxis = pcGroove->ReferenceAxis.getValue(); const std::vector &newSubRefAxis = pcGroove->ReferenceAxis.getSubValues(); if (oldRefAxis != newRefAxis || oldSubRefAxis.size() != newSubRefAxis.size() || oldSubRefAxis[0] != newSubRefAxis[0]) { bool reversed = pcGroove->suggestReversed(); if (reversed != pcGroove->Reversed.getValue()) { pcGroove->Reversed.setValue(reversed); ui->checkBoxReversed->blockSignals(true); ui->checkBoxReversed->setChecked(reversed); ui->checkBoxReversed->blockSignals(false); } } } updateUI(); recomputeFeature(); } void TaskGrooveParameters::onMidplane(bool on) { PartDesign::Groove* pcGroove = static_cast(vp->getObject()); pcGroove->Midplane.setValue(on); recomputeFeature(); } void TaskGrooveParameters::onReversed(bool on) { PartDesign::Groove* pcGroove = static_cast(vp->getObject()); pcGroove->Reversed.setValue(on); recomputeFeature(); } double TaskGrooveParameters::getAngle(void) const { return ui->grooveAngle->value().getValue(); } void TaskGrooveParameters::getReferenceAxis(App::DocumentObject*& obj, std::vector& sub) const { // get the support and Sketch PartDesign::Groove* pcGroove = static_cast(vp->getObject()); obj = static_cast(pcGroove->Sketch.getValue()); sub = std::vector(1,""); int maxcount=2; if (obj) maxcount += static_cast(obj)->getAxisCount(); if (obj) { int num = ui->axis->currentIndex(); if (num == 0) sub[0] = "H_Axis"; else if (num == 1) sub[0] = "V_Axis"; else if (num >= 2 && num < maxcount) { QString buf = QString::fromUtf8("Axis%1").arg(num-2); sub[0] = buf.toStdString(); } else if (num == maxcount && ui->axis->count() == maxcount + 2) { QStringList parts = ui->axis->currentText().split(QChar::fromAscii(':')); obj = vp->getObject()->getDocument()->getObject(parts[0].toStdString().c_str()); if (parts.size() > 1) sub[0] = parts[1].toStdString(); } else { obj = NULL; } } else obj = NULL; } bool TaskGrooveParameters::getMidplane(void) const { return ui->checkBoxMidplane->isChecked(); } bool TaskGrooveParameters::getReversed(void) const { return ui->checkBoxReversed->isChecked(); } TaskGrooveParameters::~TaskGrooveParameters() { delete ui; } void TaskGrooveParameters::changeEvent(QEvent *e) { TaskBox::changeEvent(e); if (e->type() == QEvent::LanguageChange) { ui->retranslateUi(proxy); } } void TaskGrooveParameters::apply() { App::DocumentObject* groove = vp->getObject(); std::string name = groove->getNameInDocument(); // retrieve sketch and its support object App::DocumentObject* sketch = 0; App::DocumentObject* support = 0; if (groove->getTypeId().isDerivedFrom(PartDesign::Groove::getClassTypeId())) { sketch = static_cast(groove)->Sketch.getValue(); if (sketch) { support = static_cast(sketch)->Support.getValue(); } } //Gui::Command::openCommand("Groove changed"); ui->grooveAngle->apply(); std::vector sub; App::DocumentObject* obj; parameter->getReferenceAxis(obj, sub); std::string axis = getPythonStr(obj, sub); Gui::Command::doCommand(Gui::Command::Doc,"App.ActiveDocument.%s.ReferenceAxis = %s",name.c_str(),axis.c_str()); Gui::Command::doCommand(Gui::Command::Doc,"App.ActiveDocument.%s.Midplane = %i",name.c_str(), getMidplane() ? 1 : 0); Gui::Command::doCommand(Gui::Command::Doc,"App.ActiveDocument.%s.Reversed = %i",name.c_str(), getReversed() ? 1 : 0); Gui::Command::doCommand(Gui::Command::Doc,"App.ActiveDocument.recompute()"); if (groove->isValid()) { if (sketch) Gui::Command::doCommand(Gui::Command::Gui,"Gui.activeDocument().hide(\"%s\")",sketch->getNameInDocument()); if (support) Gui::Command::doCommand(Gui::Command::Gui,"Gui.activeDocument().hide(\"%s\")",support->getNameInDocument()); } Gui::Command::doCommand(Gui::Command::Gui,"Gui.activeDocument().resetEdit()"); Gui::Command::commitCommand(); } //************************************************************************** //************************************************************************** // TaskDialog //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ TaskDlgGrooveParameters::TaskDlgGrooveParameters(ViewProviderGroove *GrooveView) : TaskDlgSketchBasedParameters(GrooveView) { assert(vp); parameter = new TaskGrooveParameters(static_cast(vp)); Content.push_back(parameter); } TaskDlgGrooveParameters::~TaskDlgGrooveParameters() { } //==== calls from the TaskView =============================================================== void TaskDlgGrooveParameters::open() { // a transaction is already open at creation time of the groove if (!Gui::Command::hasPendingCommand()) { QString msg = QObject::tr("Edit groove"); Gui::Command::openCommand((const char*)msg.toUtf8()); } } void TaskDlgGrooveParameters::clicked(int) { } bool TaskDlgGrooveParameters::accept() { parameter->apply(); return true; } bool TaskDlgGrooveParameters::reject() { // get the support and Sketch PartDesign::Groove* pcGroove = static_cast(vp->getObject()); Sketcher::SketchObject *pcSketch = 0; if (pcGroove->Sketch.getValue()) { pcSketch = static_cast(pcGroove->Sketch.getValue()); } // role back the done things Gui::Command::abortCommand(); Gui::Command::doCommand(Gui::Command::Gui,"Gui.activeDocument().resetEdit()"); // if abort command deleted the object the support is visible again if (!Gui::Application::Instance->getViewProvider(pcGroove)) { if (pcSketch && Gui::Application::Instance->getViewProvider(pcSketch)) Gui::Application::Instance->getViewProvider(pcSketch)->show(); } // Body housekeeping if (ActivePartObject != NULL) { // Make the new Tip and the previous solid feature visible again App::DocumentObject* tip = ActivePartObject->Tip.getValue(); App::DocumentObject* prev = ActivePartObject->getPrevSolidFeature(); if (tip != NULL) { Gui::Application::Instance->getViewProvider(tip)->show(); if ((tip != prev) && (prev != NULL)) Gui::Application::Instance->getViewProvider(prev)->show(); } } return true; } #include "moc_TaskGrooveParameters.cpp"