EditDatumDialog.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                                                         *
00003  *   This file is part of the FreeCAD CAx development system.              *
00004  *                                                                         *
00005  *   This library is free software; you can redistribute it and/or         *
00006  *   modify it under the terms of the GNU Library General Public           *
00007  *   License as published by the Free Software Foundation; either          *
00008  *   version 2 of the License, or (at your option) any later version.      *
00009  *                                                                         *
00010  *   This library  is distributed in the hope that it will be useful,      *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU Library General Public License for more details.                  *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU Library General Public     *
00016  *   License along with this library; see the file COPYING.LIB. If not,    *
00017  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00018  *   Suite 330, Boston, MA  02111-1307, USA                                *
00019  *                                                                         *
00020  ***************************************************************************/
00021 
00022 #include "PreCompiled.h"
00023 
00024 #ifndef _PreComp_
00025 # include <Standard_math.hxx>
00027 # include <QApplication>
00028 # include <QDialog>
00029 # include <QMessageBox>
00030 #endif
00031 
00032 # include <Inventor/sensors/SoSensor.h>
00033 
00034 #include <Base/Tools.h>
00035 #include <Gui/Application.h>
00036 #include <Gui/Command.h>
00037 #include <Gui/Document.h>
00038 #include <Gui/View3DInventor.h>
00039 #include <Gui/View3DInventorViewer.h>
00040 #include <Mod/Sketcher/App/SketchObject.h>
00041 
00042 #include "ViewProviderSketch.h"
00043 #include "ui_InsertDatum.h"
00044 #include "EditDatumDialog.h"
00045 
00046 using namespace SketcherGui;
00047 
00048 EditDatumDialog::EditDatumDialog(ViewProviderSketch* vp, int ConstrNbr) : vp(vp), ConstrNbr(ConstrNbr)
00049 {
00050     const std::vector<Sketcher::Constraint *> &Constraints = vp->getSketchObject()->Constraints.getValues();
00051     Constr = Constraints[ConstrNbr];
00052 }
00053 
00054 EditDatumDialog::~EditDatumDialog(){}
00055 
00056 void EditDatumDialog::run(void * data, SoSensor * sensor)
00057 {
00058     EditDatumDialog* self = reinterpret_cast<EditDatumDialog*>(data);
00059     self->exec();
00060     delete self;
00061     delete sensor;
00062 }
00063 
00064 void EditDatumDialog::exec(bool atCursor)
00065 {
00066     // Return if constraint doesn't have editable value
00067     if (Constr->Type == Sketcher::Distance ||
00068         Constr->Type == Sketcher::DistanceX || Constr->Type == Sketcher::DistanceY ||
00069         Constr->Type == Sketcher::Radius || Constr->Type == Sketcher::Angle) {
00070 
00071         if (vp->getSketchObject()->hasConflicts()) {
00072             QMessageBox::critical(qApp->activeWindow(), QObject::tr("Distance constraint"),
00073                                   QObject::tr("Not allowed to edit the datum because the sketch contains conflicting constraints"));
00074             return;
00075         }
00076 
00077         double datum = Constr->Value;
00078         if (Constr->Type == Sketcher::Angle)
00079             datum = Base::toDegrees<double>(datum);
00080 
00081         Gui::MDIView *mdi = Gui::Application::Instance->activeDocument()->getActiveView();
00082         Gui::View3DInventorViewer *viewer = static_cast<Gui::View3DInventor *>(mdi)->getViewer();
00083 
00084         QDialog dlg(viewer->getGLWidget());
00085 
00086         Ui::InsertDatum ui_ins_datum;
00087         ui_ins_datum.setupUi(&dlg);
00088 
00089         double init_val;
00090         if (Constr->Type == Sketcher::Angle ||
00091             ((Constr->Type == Sketcher::DistanceX || Constr->Type == Sketcher::DistanceY) &&
00092              Constr->FirstPos == Sketcher::none || Constr->Second != Sketcher::Constraint::GeoUndef))
00093             // hide negative sign
00094             init_val = std::abs(datum);
00095         else // show negative sign
00096             init_val = datum;
00097 
00098         ui_ins_datum.lineEdit->setText(QLocale::system().toString(init_val,'g',6));
00099         ui_ins_datum.lineEdit->selectAll();
00100 
00101         if (atCursor)
00102             dlg.setGeometry(QCursor::pos().x() - dlg.geometry().width() / 2, QCursor::pos().y(), dlg.geometry().width(), dlg.geometry().height());
00103 
00104         if (dlg.exec()) {
00105             bool ok;
00106             double newDatum = ui_ins_datum.lineEdit->text().toDouble(&ok);
00107             if (ok) {
00108                 if (Constr->Type == Sketcher::Angle)
00109                     newDatum = Base::toRadians<double>(newDatum);
00110 
00111                 if (Constr->Type == Sketcher::Angle ||
00112                     ((Constr->Type == Sketcher::DistanceX || Constr->Type == Sketcher::DistanceY) &&
00113                      Constr->FirstPos == Sketcher::none || Constr->Second != Sketcher::Constraint::GeoUndef)) {
00114                     // Permit negative values to flip the sign of the constraint
00115                     if (newDatum >= 0) // keep the old sign
00116                         newDatum = ((datum >= 0) ? 1 : -1) * std::abs(newDatum);
00117                     else // flip sign
00118                         newDatum = ((datum >= 0) ? -1 : 1) * std::abs(newDatum);
00119                 }
00120 
00121                 try {
00122                     Gui::Command::openCommand("Modify sketch constraints");
00123                     Gui::Command::doCommand(Gui::Command::Doc,"App.ActiveDocument.%s.setDatum(%i,%f)",
00124                                 vp->getObject()->getNameInDocument(),
00125                                 ConstrNbr, newDatum);
00126                     Gui::Command::commitCommand();
00127                 }
00128                 catch (const Base::Exception& e) {
00129                     QMessageBox::critical(qApp->activeWindow(), QObject::tr("Dimensional constraint"), QString::fromUtf8(e.what()));
00130                     Gui::Command::abortCommand();
00131                 }
00132             }
00133         }
00134     }
00135 }

Generated on Wed Nov 23 19:00:10 2011 for FreeCAD by  doxygen 1.6.1