void TaskGrooveParameters::onUpdateView(bool on)
{
    if (on) {
        PartDesign::Groove* pcGroove = static_cast<PartDesign::Groove*>(GrooveView->getObject());
        pcGroove->getDocument()->recomputeFeature(pcGroove);
    }
}
void TaskGrooveParameters::onReversed(bool on)
{
    PartDesign::Groove* pcGroove = static_cast<PartDesign::Groove*>(GrooveView->getObject());
    pcGroove->Reversed.setValue(on);
    if (updateView())
        pcGroove->getDocument()->recomputeFeature(pcGroove);
}
void TaskGrooveParameters::onAngleChanged(double len)
{
    PartDesign::Groove* pcGroove = static_cast<PartDesign::Groove*>(GrooveView->getObject());
    pcGroove->Angle.setValue(len);
    if (updateView())
        pcGroove->getDocument()->recomputeFeature(pcGroove);
}
bool ViewProviderGroove::setEdit(int ModNum)
{
    if (ModNum == ViewProvider::Default ) {
        PartDesign::Groove* pcGroove = static_cast<PartDesign::Groove*>(getObject());
        if (pcGroove->getSketchAxisCount() < 0) {
            QMessageBox msgBox;
            msgBox.setIcon(QMessageBox::Critical);
            msgBox.setWindowTitle(QObject::tr("Lost link to base sketch"));
            msgBox.setText(QObject::tr("The object can't be edited because the link to the the base sketch is lost."));
            msgBox.setStandardButtons(QMessageBox::Ok);
            msgBox.exec();
            return false;
        }
        // When double-clicking on the item for this pad the
        // object unsets and sets its edit mode without closing
        // the task panel
        Gui::TaskView::TaskDialog *dlg = Gui::Control().activeDialog();
        TaskDlgGrooveParameters *padDlg = qobject_cast<TaskDlgGrooveParameters *>(dlg);
        if (padDlg && padDlg->getGrooveView() != this)
            padDlg = 0; // another pad left open its task panel
        if (dlg && !padDlg) {
            QMessageBox msgBox;
            msgBox.setText(QObject::tr("A dialog is already open in the task panel"));
            msgBox.setInformativeText(QObject::tr("Do you want to close this dialog?"));
            msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
            msgBox.setDefaultButton(QMessageBox::Yes);
            int ret = msgBox.exec();
            if (ret == QMessageBox::Yes)
                Gui::Control().reject();
            else
                return false;
        }

        // clear the selection (convenience)
        Gui::Selection().clearSelection();
        //if (ModNum == 1)
        //    Gui::Command::openCommand("Change Groove parameters");

        // start the edit dialog
        if (padDlg)
            Gui::Control().showDialog(padDlg);
        else
            Gui::Control().showDialog(new TaskDlgGrooveParameters(this));

        return true;
    }
    else {
        return PartGui::ViewProviderPart::setEdit(ModNum);
    }
}
void TaskGrooveParameters::onAxisChanged(int num)
{
    PartDesign::Groove* pcGroove = static_cast<PartDesign::Groove*>(GrooveView->getObject());
    Sketcher::SketchObject *pcSketch = static_cast<Sketcher::SketchObject*>(pcGroove->Sketch.getValue());
    if (pcSketch) {
        int maxcount = pcSketch->getAxisCount()+2;
        if (num == 0)
            pcGroove->ReferenceAxis.setValue(pcSketch, std::vector<std::string>(1,"V_Axis"));
        else if (num == 1)
            pcGroove->ReferenceAxis.setValue(pcSketch, std::vector<std::string>(1,"H_Axis"));
        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<std::string>(1,str));
        }
        if (num < maxcount && ui->axis->count() > maxcount)
            ui->axis->setMaxCount(maxcount);
    }
    if (updateView())
        pcGroove->getDocument()->recomputeFeature(pcGroove);
}
void TaskGrooveParameters::onAxisChanged(int num)
{
    PartDesign::Groove* pcGroove = static_cast<PartDesign::Groove*>(GrooveView->getObject());
    Sketcher::SketchObject *pcSketch = static_cast<Sketcher::SketchObject*>(pcGroove->Sketch.getValue());
    if (pcSketch) {
        App::DocumentObject *oldRefAxis = pcGroove->ReferenceAxis.getValue();
        std::vector<std::string> oldSubRefAxis = pcGroove->ReferenceAxis.getSubValues();

        int maxcount = pcSketch->getAxisCount()+2;
        if (num == 0)
            pcGroove->ReferenceAxis.setValue(pcSketch, std::vector<std::string>(1,"V_Axis"));
        else if (num == 1)
            pcGroove->ReferenceAxis.setValue(pcSketch, std::vector<std::string>(1,"H_Axis"));
        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<std::string>(1,str));
        }
        if (num < maxcount && ui->axis->count() > maxcount)
            ui->axis->setMaxCount(maxcount);

        const std::vector<std::string> &newSubRefAxis = pcGroove->ReferenceAxis.getSubValues();
        if (oldRefAxis != pcSketch ||
            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);
            }
        }
    }
    if (updateView())
        pcGroove->getDocument()->recomputeFeature(pcGroove);
}
TaskGrooveParameters::TaskGrooveParameters(ViewProviderGroove *GrooveView,QWidget *parent)
    : TaskBox(Gui::BitmapFactory().pixmap("PartDesign_Groove"),tr("Groove parameters"),true, parent),GrooveView(GrooveView)
{
    // 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<PartDesign::Groove*>(GrooveView->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);

    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<std::string> &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);

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