AddObject.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This program is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU General Public License as published by the Free  *
00007 * Software Foundation; either version 2 of the License, or (at your option)   *
00008 * any later version.                                                          *
00009 *                                                                             *
00010 * This program is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for    *
00013 * more details.                                                               *
00014 *                                                                             *
00015 * You should have received a copy of the GNU General Public License along     *
00016 * with this program; if not, write to the Free Software Foundation, Inc., 51  *
00017 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.                   *
00018 *******************************************************************************
00019 *                            SOFA :: Applications                             *
00020 *                                                                             *
00021 * Authors: M. Adam, J. Allard, B. Andre, P-J. Bensoussan, S. Cotin, C. Duriez,*
00022 * H. Delingette, F. Falipou, F. Faure, S. Fonteneau, L. Heigeas, C. Mendoza,  *
00023 * M. Nesme, P. Neumann, J-P. de la Plata Alcade, F. Poyer and F. Roy          *
00024 *                                                                             *
00025 * Contact information: contact@sofa-framework.org                             *
00026 ******************************************************************************/
00027 
00028 #include "AddObject.h"
00029 #include "RealGUI.h"
00030 
00031 #include <iostream> 
00032 #include <sstream>
00033 
00034 #ifdef SOFA_QT4
00035 #include <Q3FileDialog>   
00036 #include <QLineEdit>
00037 #include <QLabel>
00038 #include <QRadioButton>
00039 #include <QPushButton>
00040 #include <Q3ButtonGroup>
00041 #include <QGridLayout>
00042 #else
00043 #include <qfiledialog.h>
00044 #include <qlineedit.h>
00045 #include <qlabel.h>
00046 #include <qradiobutton.h>
00047 #include <qpushbutton.h>
00048 #include <qbuttongroup.h>
00049 #include <qlayout.h>
00050 #endif
00051 
00052 namespace sofa
00053 {
00054 
00055   namespace gui
00056   {
00057 
00058     namespace qt
00059     {
00060 
00061 
00062 #ifndef SOFA_QT4
00063       typedef QFileDialog  Q3FileDialog;
00064       typedef QButtonGroup Q3ButtonGroup;
00065 #endif
00066 
00067   
00068       AddObject::AddObject( std::vector< std::string > *list_object_, QWidget* parent , const char* name, bool , Qt::WFlags ):  DialogAddObject(parent, name), list_object(list_object_)
00069       {
00070     //At the creation of the dialog window, we enable the custom object
00071     custom->setChecked(true);
00072       
00073 
00074     //Creation of the list of radio button corresponding to the preset objects: they are specified in the sofa/scenes/object.txt file 
00075     if (list_object != NULL)
00076       {
00077         QRadioButton *button;
00078         std::string current_name;
00079 
00080         for (int i=0;i<(int)list_object->size();i++)
00081           {
00082         std::ostringstream ofilename;
00083         current_name = (*list_object)[i];
00084         std::string::size_type pos=current_name.rfind('/');
00085       
00086         if (pos != std::string::npos)
00087           {     
00088             current_name = current_name.substr(pos+1, current_name.size()-pos-5);
00089           }
00090         button = new QRadioButton( buttonGroup, QString(current_name.c_str()) );
00091         button->setText(current_name.c_str());
00092         
00093 #ifdef SOFA_QT4             
00094         gridLayout1->addWidget( button, i+1, 0 );       
00095 #else
00096         buttonGroupLayout->addWidget( button, i+1, 0 );
00097 #endif
00098           }
00099       }
00100 
00101     positionX->setText("0");
00102     positionY->setText("0");
00103     positionZ->setText("0");
00104     
00105     rotationX->setText("0");
00106     rotationY->setText("0");
00107     rotationZ->setText("0");
00108     
00109     scaleValue->setText("1");
00110     //Option still experimental : disabled
00111     scaleValue->hide();
00112     scaleText->hide();
00113 
00114     openFilePath->setText(NULL);
00115 
00116     //Make the connection between this widget and the parent
00117     connect( this, SIGNAL(loadObject(std::string, double, double, double, double, double, double,double)), parent, SLOT(loadObject(std::string, double, double, double,double, double, double, double)));
00118     //For tje Modifications of the state of the radio buttons
00119     connect( buttonGroup, SIGNAL( clicked(int) ), this, SLOT (buttonUpdate(int)));
00120       }
00121 
00122       //**************************************************************************************
00123       //When the Ok Button is clicked, this method is called: we just have to emit a signal to the parent, with the information on the object
00124       void AddObject::accept()
00125       {
00126     std::string position[3];
00127     std::string rotation[3];
00128     std::string scale;
00129 #ifdef SOFA_QT4                     
00130     std::string object_fileName(openFilePath->text().toStdString());
00131     position[0] = positionX->text().toStdString();
00132     position[1] = positionY->text().toStdString();
00133     position[2] = positionZ->text().toStdString();
00134     
00135     rotation[0] = rotationX->text().toStdString();
00136     rotation[1] = rotationY->text().toStdString();
00137     rotation[2] = rotationZ->text().toStdString();
00138     
00139     scale       = scaleValue->text().toStdString();
00140 #else
00141     std::string object_fileName(openFilePath->text().latin1());
00142     position[0] = positionX->text().latin1();
00143     position[1] = positionY->text().latin1();
00144     position[2] = positionZ->text().latin1();
00145     
00146     rotation[0] = rotationX->text().latin1();
00147     rotation[1] = rotationY->text().latin1();
00148     rotation[2] = rotationZ->text().latin1();
00149     
00150     scale       = scaleValue->text().latin1();
00151 #endif
00152     emit( loadObject(object_fileName, atof(position[0].c_str()),atof(position[1].c_str()),atof(position[2].c_str()),
00153            atof(rotation[0].c_str()),atof(rotation[1].c_str()),atof(rotation[2].c_str()),
00154             atof(scale.c_str())));
00155     setPath(object_fileName);
00156     QDialog::accept();
00157       }
00158 
00159       //**************************************************************************************
00160       //Set the default file
00161       void AddObject::setPath(const std::string path)
00162       {
00163     fileName = path;
00164     openFilePath->setText(QString(fileName.c_str()));
00165       }
00166 
00167       //**************************************************************************************
00168       //Open a file Dialog and set the path of the selected path in the text field.
00169       void AddObject::fileOpen()
00170       {
00171     QString s  = getOpenFileName(this, QString(fileName.c_str()), "Sofa Element (*.xml *.scn)", "open file dialog",  "Choose a file to open" );
00172     
00173     if (s.isNull() ) return;
00174 #ifdef SOFA_QT4                     
00175     std::string object_fileName(s.toStdString());
00176 #else
00177     std::string object_fileName(s.latin1());
00178 #endif
00179     
00180     openFilePath->setText(QString(object_fileName.c_str()));
00181       }
00182 
00183       //**************************************************************************************
00184       //The state of the radio buttons has been modified
00185       //we update the content of the dialog window
00186       void AddObject::buttonUpdate(int Id)
00187       {
00188     //Id = 0 : custom radio button clicked: we need to show the selector of the file
00189     if (Id == 0)
00190       {
00191         openFilePath->setText(fileName.c_str());
00192         openFileText->show();
00193         openFilePath->show();
00194         openFileButton->show();
00195       }
00196     else
00197       {
00198         openFilePath->setText((*list_object)[Id-1].c_str());
00199         openFileText->hide();
00200         openFilePath->hide();
00201         openFileButton->hide();
00202       }
00203       }
00204     } // namespace qt
00205 
00206   } // namespace gui
00207 
00208 } // namespace sofa
00209 

Generated on Sun Nov 23 02:28:35 2008 for Sofa by  doxygen 1.5.7.1