Files
create/src/Mod/PartDesign/Gui/TaskPocketParameters.cpp
2016-04-12 18:12:17 +02:00

441 lines
17 KiB
C++

/***************************************************************************
* Copyright (c) 2011 Juergen Riegel <FreeCAD@juergen-riegel.net> *
* *
* 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_
# include <sstream>
# include <QRegExp>
# include <QTextStream>
# include <Precision.hxx>
#endif
#include "ui_TaskPocketParameters.h"
#include "TaskPocketParameters.h"
#include <App/Application.h>
#include <App/Document.h>
#include <Gui/Application.h>
#include <Gui/Document.h>
#include <Gui/BitmapFactory.h>
#include <Gui/ViewProvider.h>
#include <Gui/WaitCursor.h>
#include <Base/Console.h>
#include <Gui/Selection.h>
#include <Gui/Command.h>
#include <Mod/PartDesign/App/FeaturePocket.h>
#include <Mod/Sketcher/App/SketchObject.h>
#include "TaskSketchBasedParameters.h"
#include "ReferenceSelection.h"
using namespace PartDesignGui;
using namespace Gui;
/* TRANSLATOR PartDesignGui::TaskPocketParameters */
TaskPocketParameters::TaskPocketParameters(ViewProviderPocket *PocketView,QWidget *parent, bool newObj)
: TaskSketchBasedParameters(PocketView, parent, "PartDesign_Pocket",tr("Pocket parameters"))
{
// we need a separate container widget to add all controls to
proxy = new QWidget(this);
ui = new Ui_TaskPocketParameters();
ui->setupUi(proxy);
this->groupLayout()->addWidget(proxy);
// set the history path
ui->lengthEdit->setParamGrpPath(QByteArray("User parameter:BaseApp/History/PocketLength"));
ui->offsetEdit->setParamGrpPath(QByteArray("User parameter:BaseApp/History/PocketOffset"));
// Get the feature data
PartDesign::Pocket* pcPocket = static_cast<PartDesign::Pocket*>(vp->getObject());
Base::Quantity l = pcPocket->Length.getQuantityValue();
Base::Quantity off = pcPocket->Offset.getQuantityValue();
bool midplane = pcPocket->Midplane.getValue();
bool reversed = pcPocket->Reversed.getValue();
int index = pcPocket->Type.getValue(); // must extract value here, clear() kills it!
App::DocumentObject* obj = pcPocket->UpToFace.getValue();
std::vector<std::string> subStrings = pcPocket->UpToFace.getSubValues();
std::string upToFace;
int faceId = -1;
if ((obj != NULL) && !subStrings.empty()) {
upToFace = subStrings.front();
if (upToFace.substr(0,4) == "Face")
faceId = std::atoi(&upToFace[4]);
}
// Fill data into dialog elements
ui->lengthEdit->setValue(l);
ui->offsetEdit->setValue(off);
ui->checkBoxMidplane->setChecked(midplane);
ui->checkBoxReversed->setChecked(reversed);
if ((obj != NULL) && PartDesign::Feature::isDatum(obj))
ui->lineFaceName->setText(QString::fromAscii(obj->getNameInDocument()));
else if (faceId >= 0)
ui->lineFaceName->setText(QString::fromAscii(obj->getNameInDocument()) + QString::fromAscii(":") + tr("Face") +
QString::number(faceId));
else
ui->lineFaceName->setText(tr("No face selected"));
ui->lineFaceName->setProperty("FaceName", QByteArray(upToFace.c_str()));
ui->changeMode->clear();
ui->changeMode->insertItem(0, tr("Dimension"));
ui->changeMode->insertItem(1, tr("Through all"));
ui->changeMode->insertItem(2, tr("To first"));
ui->changeMode->insertItem(3, tr("Up to face"));
ui->changeMode->setCurrentIndex(index);
// Bind input fields to properties
ui->lengthEdit->bind(pcPocket->Length);
QMetaObject::connectSlotsByName(this);
connect(ui->lengthEdit, SIGNAL(valueChanged(double)),
this, SLOT(onLengthChanged(double)));
connect(ui->offsetEdit, SIGNAL(valueChanged(double)),
this, SLOT(onOffsetChanged(double)));
connect(ui->checkBoxMidplane, SIGNAL(toggled(bool)),
this, SLOT(onMidplaneChanged(bool)));
connect(ui->checkBoxReversed, SIGNAL(toggled(bool)),
this, SLOT(onReversedChanged(bool)));
connect(ui->changeMode, SIGNAL(currentIndexChanged(int)),
this, SLOT(onModeChanged(int)));
connect(ui->buttonFace, SIGNAL(clicked()),
this, SLOT(onButtonFace()));
connect(ui->lineFaceName, SIGNAL(textEdited(QString)),
this, SLOT(onFaceName(QString)));
connect(ui->checkBoxUpdateView, SIGNAL(toggled(bool)),
this, SLOT(onUpdateView(bool)));
// Due to signals attached after changes took took into effect we should update the UI now.
updateUI(index);
// if it is a newly created object use the last value of the history
// TODO: newObj doesn't supplied normally by any caller (2015-07-24, Fat-Zer)
if(newObj){
ui->lengthEdit->setToLastUsedValue();
ui->lengthEdit->selectNumber();
ui->offsetEdit->setToLastUsedValue();
ui->offsetEdit->selectNumber();
}
}
void TaskPocketParameters::updateUI(int index)
{
// disable/hide evrything unless we are sure we don't need it
bool isLengthEditVisable = false;
bool isOffsetEditVisable = false;
bool isOffsetEditEnabled = true;
bool isMidplateEnabled = false;
bool isReversedEnabled = false;
bool isFaceEditEnabled = false;
if (index == 0) { // dimension
isLengthEditVisable = true;
ui->lengthEdit->selectNumber();
// Make sure that the spin box has the focus to get key events
// Calling setFocus() directly doesn't work because the spin box is not
// yet visible.
QMetaObject::invokeMethod(ui->lengthEdit, "setFocus", Qt::QueuedConnection);
isMidplateEnabled = true;
// Reverse only makes sense if Midplane is not true
isReversedEnabled = !ui->checkBoxMidplane->isChecked();
} else if (index == 1) { // through all
isOffsetEditVisable = true;
isOffsetEditEnabled = false; // offset may have some meaning for through all but it doesn't work
isMidplateEnabled = true;
isReversedEnabled = !ui->checkBoxMidplane->isChecked();
} else if (index == 2) { // up to first
isOffsetEditVisable = true;
isReversedEnabled = true; // Will change the direction it seeks for its first face?
// It may work not quite as expected but usefull if sketch oriented upside down.
// (may happen in bodies)
// Fix probably lies somwhere in IF block on line 125 of FeaturePocket.cpp
} else if (index == 3) { // up to face
isOffsetEditVisable = true;
isFaceEditEnabled = true;
QMetaObject::invokeMethod(ui->lineFaceName, "setFocus", Qt::QueuedConnection);
// Go into reference selection mode if no face has been selected yet
if (ui->lineFaceName->text().isEmpty() || (ui->lineFaceName->text() == tr("No face selected")))
onButtonFace(true);
}
ui->lengthEdit->setVisible( isLengthEditVisable );
ui->lengthEdit->setEnabled( isLengthEditVisable );
ui->labelLength->setVisible( isLengthEditVisable );
ui->offsetEdit->setVisible( isOffsetEditVisable );
ui->offsetEdit->setEnabled( isOffsetEditVisable && isOffsetEditEnabled );
ui->labelOffset->setVisible( isOffsetEditVisable );
ui->checkBoxMidplane->setEnabled( isMidplateEnabled );
ui->checkBoxReversed->setEnabled( isReversedEnabled );
ui->buttonFace->setEnabled( isFaceEditEnabled );
ui->lineFaceName->setEnabled( isFaceEditEnabled );
if (!isFaceEditEnabled) {
onButtonFace(false);
}
}
void TaskPocketParameters::onSelectionChanged(const Gui::SelectionChanges& msg)
{
if (msg.Type == Gui::SelectionChanges::AddSelection) {
QString refText = onAddSelection(msg);
if (refText.length() > 0) {
ui->lineFaceName->blockSignals(true);
ui->lineFaceName->setText(refText);
ui->lineFaceName->setProperty("FaceName", QByteArray(msg.pSubName));
ui->lineFaceName->blockSignals(false);
// Turn off reference selection mode
onButtonFace(false);
} else {
ui->lineFaceName->blockSignals(true);
ui->lineFaceName->setText(tr("No face selected"));
ui->lineFaceName->setProperty("FaceName", QByteArray());
ui->lineFaceName->blockSignals(false);
}
} else if (msg.Type == Gui::SelectionChanges::ClrSelection) {
ui->lineFaceName->blockSignals(true);
ui->lineFaceName->setText(tr("No face selected"));
ui->lineFaceName->setProperty("FaceName", QByteArray());
ui->lineFaceName->blockSignals(false);
}
}
void TaskPocketParameters::onLengthChanged(double len)
{
PartDesign::Pocket* pcPocket = static_cast<PartDesign::Pocket*>(vp->getObject());
pcPocket->Length.setValue(len);
recomputeFeature();
}
void TaskPocketParameters::onOffsetChanged(double len)
{
PartDesign::Pocket* pcPocket = static_cast<PartDesign::Pocket*>(vp->getObject());
pcPocket->Offset.setValue(len);
recomputeFeature();
}
void TaskPocketParameters::onMidplaneChanged(bool on)
{
PartDesign::Pocket* pcPocket = static_cast<PartDesign::Pocket*>(vp->getObject());
pcPocket->Midplane.setValue(on);
ui->checkBoxReversed->setEnabled(!on);
recomputeFeature();
}
void TaskPocketParameters::onReversedChanged(bool on)
{
PartDesign::Pocket* pcPocket = static_cast<PartDesign::Pocket*>(vp->getObject());
pcPocket->Reversed.setValue(on);
recomputeFeature();
}
void TaskPocketParameters::onModeChanged(int index)
{
PartDesign::Pocket* pcPocket = static_cast<PartDesign::Pocket*>(vp->getObject());
switch (index) {
case 0:
// Why? See below for "UpToFace"
if (oldLength < Precision::Confusion())
oldLength = 5.0;
pcPocket->Length.setValue(oldLength);
ui->lengthEdit->setValue(oldLength);
break;
case 1:
oldLength = pcPocket->Length.getValue();
pcPocket->Type.setValue("ThroughAll");
break;
case 2:
oldLength = pcPocket->Length.getValue();
pcPocket->Type.setValue("UpToFirst");
break;
case 3:
// Because of the code at the begining of Pocket::execute() which is used to detect
// broken legacy parts, we must set the length to zero here!
oldLength = pcPocket->Length.getValue();
pcPocket->Type.setValue("UpToFace");
pcPocket->Length.setValue(0.0);
ui->lengthEdit->setValue(0.0);
break;
default: pcPocket->Type.setValue("Length"); break;
}
updateUI(index);
recomputeFeature();
}
void TaskPocketParameters::onButtonFace(const bool pressed) {
TaskSketchBasedParameters::onSelectReference(pressed, false, true, false);
// Update button if onButtonFace() is called explicitly
ui->buttonFace->setChecked(pressed);
}
void TaskPocketParameters::onFaceName(const QString& text)
{
ui->lineFaceName->setProperty("FaceName", TaskSketchBasedParameters::onFaceName(text));
}
double TaskPocketParameters::getLength(void) const
{
return ui->lengthEdit->value().getValue();
}
double TaskPocketParameters::getOffset(void) const
{
return ui->offsetEdit->value().getValue();
}
bool TaskPocketParameters::getReversed(void) const
{
return ui->checkBoxReversed->isChecked();
}
bool TaskPocketParameters::getMidplane(void) const
{
return ui->checkBoxMidplane->isChecked();
}
int TaskPocketParameters::getMode(void) const
{
return ui->changeMode->currentIndex();
}
QString TaskPocketParameters::getFaceName(void) const
{
// TODO Make it return None rather than empty string (2015-11-03, Fat-Zer)
if (getMode() == 3) {
QString faceName = ui->lineFaceName->property("FaceName").toString();
if (!faceName.isEmpty()) {
return getFaceReference(ui->lineFaceName->text(), faceName);
}
}
return QString ();
}
TaskPocketParameters::~TaskPocketParameters()
{
delete ui;
}
void TaskPocketParameters::changeEvent(QEvent *e)
{
TaskBox::changeEvent(e);
if (e->type() == QEvent::LanguageChange) {
ui->lengthEdit->blockSignals(true);
ui->offsetEdit->blockSignals(true);
ui->lineFaceName->blockSignals(true);
ui->changeMode->blockSignals(true);
int index = ui->changeMode->currentIndex();
ui->retranslateUi(proxy);
ui->changeMode->clear();
ui->changeMode->addItem(tr("Dimension"));
ui->changeMode->addItem(tr("Through all"));
ui->changeMode->addItem(tr("To first"));
ui->changeMode->addItem(tr("Up to face"));
ui->changeMode->setCurrentIndex(index);
QStringList parts = ui->lineFaceName->text().split(QChar::fromAscii(':'));
QByteArray upToFace = ui->lineFaceName->property("FaceName").toByteArray();
int faceId = -1;
bool ok = false;
if (upToFace.indexOf("Face") == 0) {
faceId = upToFace.remove(0,4).toInt(&ok);
}
#if QT_VERSION >= 0x040700
ui->lineFaceName->setPlaceholderText(tr("No face selected"));
#endif
ui->lineFaceName->setText(ok ?
parts[0] + QString::fromAscii(":") + tr("Face") + QString::number(faceId) :
tr(""));
ui->lengthEdit->blockSignals(false);
ui->offsetEdit->blockSignals(false);
ui->lineFaceName->blockSignals(false);
ui->changeMode->blockSignals(false);
}
}
void TaskPocketParameters::saveHistory(void)
{
// save the user values to history
ui->lengthEdit->pushToHistory();
ui->offsetEdit->pushToHistory();
}
void TaskPocketParameters::apply()
{
std::string name = vp->getObject()->getNameInDocument();
const char * cname = name.c_str();
ui->lengthEdit->apply();
Gui::Command::doCommand(Gui::Command::Doc,"App.ActiveDocument.%s.Type = %u", cname, getMode());
QString facename = getFaceName();
if (!facename.isEmpty()) {
Gui::Command::doCommand(Gui::Command::Doc,"App.ActiveDocument.%s.UpToFace = %s",
cname, facename.toLatin1().data());
} else {
Gui::Command::doCommand(Gui::Command::Doc,"App.ActiveDocument.%s.UpToFace = None", cname);
}
Gui::Command::doCommand(Gui::Command::Doc,"App.ActiveDocument.%s.Reversed = %i", cname, getReversed()?1:0);
Gui::Command::doCommand(Gui::Command::Doc,"App.ActiveDocument.%s.Midplane = %i", cname, getMidplane()?1:0);
Gui::Command::doCommand(Gui::Command::Doc,"App.ActiveDocument.%s.Offset = %f", name.c_str(), getOffset());
}
//**************************************************************************
//**************************************************************************
// TaskDialog
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
TaskDlgPocketParameters::TaskDlgPocketParameters(ViewProviderPocket *PocketView)
: TaskDlgSketchBasedParameters(PocketView)
{
assert(vp);
parameter = new TaskPocketParameters(static_cast<ViewProviderPocket*>(vp));
Content.push_back(parameter);
}
TaskDlgPocketParameters::~TaskDlgPocketParameters()
{
}
//==== calls from the TaskView ===============================================================
bool TaskDlgPocketParameters::accept()
{
parameter->apply();
// save the history
parameter->saveHistory();
return TaskDlgSketchBasedParameters::accept();
}
#include "moc_TaskPocketParameters.cpp"