CMS 3D CMS Logo

IgMatEdMainWidget.cc

Go to the documentation of this file.
00001 //<<<<<< INCLUDES                                                       >>>>>>
00002 
00003 #include "Iguana/Controls/interface/IgMatEdMainWidget.h"
00004 #include "Iguana/Controls/src/IgMatEdSlider.h"
00005 #include "Iguana/Controls/src/IgMatEdColorBox.h"
00006 #include "Iguana/Controls/src/IgMatEdModificationAction.h"
00007 #include "Iguana/Controls/src/IgMatEdMainMenu.h"
00008 #include "Iguana/Controls/interface/IgMatEdViewer.h"
00009 #include "Iguana/Controls/interface/IgSoMaterialMap.h"
00010 #include "Iguana/Controls/interface/IgG4ToSoMaterialMap.h"
00011 
00012 #include <iostream>
00013 #include <fstream>
00014 
00015 #include <qhbox.h>
00016 #include <qvbox.h>
00017 #include <qlistview.h>
00018 #include <qlayout.h>
00019 #include <qslider.h>
00020 #include <qcombobox.h>
00021 #include <qpopupmenu.h>
00022 #include <qpushbutton.h>
00023 #include <qinputdialog.h>
00024 #include <qmessagebox.h>
00025 #include <qframe.h>
00026 
00027 #include <Inventor/nodes/SoNode.h>
00028 #include <Inventor/nodes/SoBaseColor.h>
00029 #include <Inventor/nodes/SoSeparator.h>
00030 #include <Inventor/nodes/SoSphere.h>
00031 #include <Inventor/nodes/SoMaterial.h>
00032 #include <Inventor/actions/SoWriteAction.h>
00033 #include <Inventor/SoOutput.h>
00034 
00035 #include <classlib/iobase/Filename.h>
00036 
00037 //<<<<<< PRIVATE DEFINES                                                >>>>>>
00038 //<<<<<< PRIVATE CONSTANTS                                              >>>>>>
00039 //<<<<<< PRIVATE TYPES                                                  >>>>>>
00040 //<<<<<< PRIVATE VARIABLE DEFINITIONS                                   >>>>>>
00041 //<<<<<< PUBLIC VARIABLE DEFINITIONS                                    >>>>>>
00042 //<<<<<< CLASS STRUCTURE INITIALIZATION                                 >>>>>>
00043 //<<<<<< PRIVATE FUNCTION DEFINITIONS                                   >>>>>>
00044 //<<<<<< PUBLIC FUNCTION DEFINITIONS                                    >>>>>>
00045 //<<<<<< MEMBER FUNCTION DEFINITIONS                                    >>>>>>
00046 
00047 IgMatEdMainWidget::IgMatEdMainWidget (QWidget *parent, IgMatEdMainMenu *menu)
00048     :QWidget (parent),
00049      m_parent (parent),
00050      m_g4ListView (0),
00051      m_menu (menu),
00052      m_layout (0),
00053      m_mainHBox (0),
00054      m_viewer (0),
00055      m_rootNode (0),
00056      m_sliderBox (0)
00057 {    
00058     ASSERT (m_parent);
00059 
00060     m_layout = new QVBoxLayout (this);    
00061     m_layout->setAutoAdd (TRUE);    
00062 
00063     m_mainHBox = new QHBox (this);
00064     m_mainHBox->setMargin(5);
00065     m_mainHBox->setSpacing(20);
00066     m_mainHBox->setEnabled (true);
00067     
00068     // Create a listview with all the materials
00069     m_g4ListView = new QListView (m_mainHBox);
00070     m_g4ListView->addColumn ("G4 material");    
00071     m_g4ListView->addColumn ("OIV material");    
00072 
00073     m_g4ToSoMap = IgG4ToSoMaterialMapSingleton::instance ();
00074 
00075     for (IgG4ToSoMaterialMap::MaterialMap::iterator i = m_g4ToSoMap->begin ();i != m_g4ToSoMap->end () ; i++)
00076     {
00077         QListViewItem *item = new QListViewItem (m_g4ListView);
00078         item->setText (0, i->first);
00079         item->setText (1, i->second);   
00080     }        
00081 
00082     m_soMaterialBox = new QHBox (this);
00083     m_soMaterialBox->setMargin (10);
00084     m_soMaterialBox->setSpacing (5);
00085 
00086     m_sliderBox = new QVBox (this);
00087 
00088     m_sliderBox->setMargin (10);
00089     m_sliderBox->setSpacing (5);    
00090     
00091     //Makes the sphere scenegraph.
00092     m_rootNode = new SoSeparator;
00093     m_rootNode->ref ();
00094 
00095     m_soMaterialMap = IgSoMaterialMapSingleton::instance ();
00096     
00097     ASSERT (m_soMaterialMap);
00098     ASSERT ((*m_soMaterialMap)["default"]);
00099     
00100     m_rootNode->insertChild ((*m_soMaterialMap)["default"], 0);
00101     m_rootNode->insertChild (new SoSphere, 1);
00102     
00103     m_colorBox = new QHBox (this);    
00104     
00105     m_createMaterialButton = new QPushButton ("Create material", m_soMaterialBox);    
00106     connect (m_createMaterialButton, SIGNAL (clicked()), this, SLOT (createSoMaterial()));
00107     
00108     m_comboBox = new QComboBox (m_soMaterialBox);
00109 
00110     for (IgSoMaterialMap::MaterialMap::iterator i = m_soMaterialMap->begin (); 
00111          i != m_soMaterialMap->end (); 
00112          i++)
00113     {
00114         m_comboBox->insertItem (i->first);      
00115     }
00116 
00117     connect (m_comboBox, SIGNAL (activated (const QString &)), this, SLOT (newMaterial (const QString &)));
00118     connect (m_comboBox, SIGNAL (highlighted (const QString &)), this, SLOT (newMaterial (const QString &)));
00119     connect (m_g4ListView, SIGNAL (selectionChanged (QListViewItem *)), this, SLOT (newG4Material (QListViewItem *)));
00120     
00121     if (m_menu != 0)
00122     {
00123         QPopupMenu *tmpMenu = m_menu->m_fileMenu;
00124         ASSERT (tmpMenu);    
00125         connect (tmpMenu, SIGNAL (activated (int)), this, SLOT (fileMenuActivated (int)));
00126     }
00127     
00128     m_shininessSlider = new IgMatEdSlider (m_sliderBox, "Shininess", &IgMatEdModificationAction::m_shininess, this);
00129     m_transparencySlider = new IgMatEdSlider (m_sliderBox, "Transparency", &IgMatEdModificationAction::m_transparency, this);
00130 
00131     m_diffuseColorBox = new IgMatEdColorBox (m_colorBox, "Diffuse", IgMatEdModificationAction::m_diffuseColor, this);
00132     m_ambientColorBox = new IgMatEdColorBox (m_colorBox, "Ambient", IgMatEdModificationAction::m_ambientColor, this);
00133     m_specularColorBox = new IgMatEdColorBox (m_colorBox, "Specular", IgMatEdModificationAction::m_specularColor, this);
00134     m_emissiveColorBox = new IgMatEdColorBox (m_colorBox, "Emissive", IgMatEdModificationAction::m_emissiveColor, this);
00135     
00136     // Use one of the convenient SoQt viewer classes.
00137     QHBox *dummyBox = new QHBox (m_mainHBox);
00138     QFrame *dummyFrame = new QFrame (dummyBox);
00139     dummyFrame->setMinimumSize (200,200);
00140     
00141     m_viewer = new IgMatEdViewer (dummyFrame, m_rootNode);
00142     m_g4ListView->show ();
00143     m_mainHBox->show ();
00144     m_g4ListView->setSelected (m_g4ListView->firstChild (), true);    
00145 }
00146 
00147 IgMatEdMainWidget::~IgMatEdMainWidget (void)
00148 {
00149     ASSERT (m_viewer);
00150     ASSERT (m_rootNode);
00151     ASSERT (m_g4ListView);
00152     
00153     m_rootNode->unref (); 
00154     
00155     delete m_viewer;    
00156     delete m_g4ListView;    
00157 }
00158 
00159 void
00160 IgMatEdMainWidget::setCurrentG4Material (const QString &name)
00161 {
00162     QListViewItem *item = m_g4ListView->findItem (name,0);
00163     //If the material name matches one of the list, then select it
00164     //in the list, otherwise create and select a new entry for it.
00165     if (!item)
00166     {
00167         item = new QListViewItem (m_g4ListView, name, "default");       
00168         m_g4ListView->insertItem (item);
00169         (*m_g4ToSoMap)[name.ascii ()] = "default";
00170     }    
00171     m_g4ListView->setCurrentItem (item);    
00172     m_g4ListView->ensureItemVisible (item);    
00173 }
00174 
00175 void
00176 IgMatEdMainWidget::newG4Material (QListViewItem *item)
00177 {    
00178     std::cerr << "newG4Material called" << std::endl;    
00179     newMaterial (item->text (1));    
00180     m_comboBox->setCurrentText (item->text (1));    
00181 }
00182 
00183 void 
00184 IgMatEdMainWidget::newMaterial (const QString &text)
00185 {
00186     std::cerr << "newMaterial called with argument " << text << std::endl;    
00187     ASSERT (m_soMaterialMap);
00188 
00189     if (text != QString::null)
00190     {
00191         /*Set the new name for the SoMaterial*/
00192         ASSERT (m_g4ListView);  
00193         ASSERT (m_comboBox);
00194         QListViewItem *item = m_g4ListView->selectedItem ();
00195         item->setText (1, text);        
00196 
00197         /*Updates the G4->So mapping map*/
00198         (*m_g4ToSoMap) [item->text (0).ascii ()] = text.ascii ();               
00199 
00200         /*Updates the display material using the selected one*/
00201         SoMaterial *m_selectedMaterial = (*m_soMaterialMap) [text.ascii ()];
00202 
00203         if (m_selectedMaterial == 0)
00204         {
00205             QMessageBox::warning (0, "Warning!", "Open Inventor material "+text+" not found. Using a default one for it.", "OK");
00206             m_selectedMaterial = new SoMaterial;
00207             m_soMaterialMap->addEntry (text.ascii (), m_selectedMaterial);
00208         }       
00209 
00210         ASSERT (m_selectedMaterial);
00211         m_rootNode->removeChild (0);
00212         m_rootNode->insertChild (m_selectedMaterial, 0);
00213         std::cerr << m_selectedMaterial->ambientColor[0][0] << " " << m_selectedMaterial->ambientColor[0][1] << " " << m_selectedMaterial->ambientColor[0][2] << std::endl;
00214         
00215         /*Updates the position of the sliders*/
00216         ASSERT (m_diffuseColorBox);
00217         ASSERT (m_ambientColorBox);
00218         ASSERT (m_specularColorBox);
00219         ASSERT (m_emissiveColorBox);
00220         ASSERT (m_transparencySlider);
00221         ASSERT (m_shininessSlider);
00222         
00223         m_diffuseColorBox->updatePosition (m_selectedMaterial->diffuseColor);    
00224         m_ambientColorBox->updatePosition (m_selectedMaterial->ambientColor);
00225         m_specularColorBox->updatePosition (m_selectedMaterial->specularColor);
00226         m_emissiveColorBox->updatePosition (m_selectedMaterial->emissiveColor);
00227         m_transparencySlider->updatePosition ((int) (m_selectedMaterial->transparency[0]*100));
00228         m_shininessSlider->updatePosition ((int) (m_selectedMaterial->shininess[0]*100));
00229     }
00230 }
00231 
00232 void 
00233 IgMatEdMainWidget::saveMaterialMap (void)
00234 {
00235     //Get material map name, use default one "material.map" if not set.
00236     std::string filename;
00237     char *fname = getenv ("MATERIAL_CONFIG");    
00238         
00239     if (fname == 0)
00240     {
00241         filename = "./materials.map";   
00242     }
00243     else
00244     {
00245         filename = fname;    
00246     }
00247     
00248     std::ofstream output;
00249     output.open (filename.c_str ());
00250 
00251     if (output)
00252     {
00253         QListViewItem *item = m_g4ListView->firstChild ();
00254     
00255         while (item!=0)
00256         {
00257             output << item->text (0) << "= " << item->text (1) << std::endl;
00258             item = item->nextSibling ();        
00259         }
00260         output.close();    
00261     }
00262 }
00263 
00264 void
00265 IgMatEdMainWidget::saveSoMaterials (void)
00266 {
00267     SoOutput out;
00268     std::string filename;
00269     
00270     char *fn = getenv ("SO_MATERIALS_IV");        
00271     if (!fn)
00272         filename = "./materials.iv";
00273     else
00274         filename = fn;
00275     
00276     out.openFile (filename.c_str ());
00277     SoWriteAction writer (&out);
00278     for (IgSoMaterialMap::MaterialMap::iterator i = m_soMaterialMap->begin (); 
00279          i != m_soMaterialMap->end (); 
00280          i++)
00281     {
00282         writer.apply (i->second);
00283     }
00284 }
00285 
00286 void
00287 IgMatEdMainWidget::fileMenuActivated (int item)
00288 {
00289     switch (item)
00290     {
00291     case IgMatEdMainMenu::SAVE_G4_MATERIAL_MAP:
00292         saveMaterialMap ();
00293         break;
00294     case IgMatEdMainMenu::LOAD_G4_MATERIAL_MAP:
00295         break;
00296     case IgMatEdMainMenu::SAVE_MATERIALS:
00297         saveSoMaterials ();     
00298         break;  
00299     }    
00300 }
00301 
00302 void
00303 IgMatEdMainWidget::createSoMaterial (void)
00304 {
00305     bool ok;    
00306     QString text = QInputDialog::getText (
00307         "Create new material", "Enter material name:", QLineEdit::Normal,
00308         QString::null, &ok, this );
00309     if (text != QString::null)
00310     {
00311         SoMaterial *entry= new SoMaterial;
00312         entry->ref ();
00313         entry->setName (text.ascii ());
00314 
00315         m_soMaterialMap->addEntry (text.ascii (), entry);       
00316         m_comboBox->insertItem (text);
00317     }    
00318 }

Generated on Tue Jun 9 17:38:12 2009 for CMSSW by  doxygen 1.5.4