00001
00002
00003 #include "Iguana/GLBrowsers/interface/IgIVView.h"
00004 #include "Iguana/Framework/interface/IgArgsElement.h"
00005 #include "Iguana/Framework/interface/IgBrowserDB.h"
00006 #include "Iguana/Framework/interface/IgDocumentDataRoot.h"
00007 #include "Iguana/Framework/interface/IgSelectionService.h"
00008 #include "Iguana/GLBrowsers/interface/Ig3DBaseWindow.h"
00009 #include "Iguana/GLModels/interface/IgSoTwig.h"
00010 #include "Iguana/GLModels/interface/Ig3DBaseRep.h"
00011 #include "Iguana/GLModels/interface/Ig3DModel.h"
00012 #include "Iguana/Studio/interface/IgDocumentData.h"
00013 #include "Iguana/Studio/interface/IgFileFetcher.h"
00014 #include "Iguana/Studio/interface/IgModelManager.h"
00015 #include "Iguana/Studio/interface/IgPage.h"
00016 #include "Iguana/Studio/interface/IgQtAppWindowService.h"
00017 #include "Iguana/Studio/interface/IgQWidgetStackSite.h"
00018 #include "Iguana/Studio/interface/IgViewDB.h"
00019 #include <Inventor/SoInput.h>
00020 #include <Inventor/nodes/SoGroup.h>
00021 #include <qfiledialog.h>
00022 #include <qmainwindow.h>
00023 #include <qmessagebox.h>
00024 #include <qstring.h>
00025 #undef emit
00026 #include <classlib/utils/Log.h>
00027 #include <iostream>
00028
00029
00030
00031 lat::logflag LFivview = { 0, "ivview", true, -1 };
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 IgIVView::IgIVView (IgPage *page)
00043 :IgView (page),
00044 m_state (0),
00045 m_browser (0),
00046 m_bt (0)
00047 {
00048 QString filename;
00049 QString workingDirectory ("./");
00050 QWidget *mainWindow = dynamic_cast<QMainWindow *> (IgQtAppWindowService::get (page->state ())->mainWindow ());
00051
00052 IgArgsElement *args = IgArgsElement::get (page->state ());
00053 std::string fname;
00054 std::vector<std::string> arg;
00055 arg.push_back ("--ivfile");
00056 arg.push_back ("-iv");
00057
00058 if (args->find (arg, fname, true)) filename = fname.c_str ();
00059
00060 if (filename.isEmpty ())
00061 filename = QFileDialog::getOpenFileName (workingDirectory,
00062 "Open Inventor Scenegraph (*.iv)",
00063 mainWindow,
00064 QString::null,
00065 "Select Scenegraph to View");
00066
00067 if (! filename.isEmpty ())
00068 {
00069 IgDocumentDataManager *dm = IgDocumentDataManager::get (page->state ());
00070 IgState *s = dm->lookup (0, filename.latin1 ());
00071
00072 if (s)
00073 {
00074
00075 IgDocumentDataRoot::currentRoot (filename.latin1 ());
00076 return;
00077 }
00078
00079
00080 IgFileFetcher *fetcher = IgFileFetcher::get (page->state ());
00081 if (! fetcher)
00082 fetcher = new IgFileFetcher (page->state ());
00083
00084 std::string local = fetcher->fetch (filename.ascii ());
00085 if (local.empty ())
00086 {
00087 LOG (0, trace, LFivview, "SoReader: " << filename.ascii ()
00088 << ": error: failed to access file.\n");
00089 QMessageBox::warning (mainWindow,
00090 "File open error",
00091 "\""+ filename + "\" file not found.", "Ok");
00092 return;
00093 }
00094
00095 SoNode *node = 0;
00096 SoInput file;
00097 if (! file.openFile (local.c_str ())
00098 || ! SoDB::read (&file, node)
00099 || ! node)
00100 {
00101 LOG (0, trace, LFivview, "SoReader: " << filename.ascii ()
00102 << ": error: failed to read\n");
00103 QMessageBox::warning (mainWindow,
00104 "File Access Error",
00105 "Can not open file \""+ filename + "\" for reading.", "Ok");
00106 return;
00107 }
00108 IgDocumentDataRoot::currentRoot (filename.latin1 ());
00109
00110
00111
00112
00113 IgSoTwig *twigs = IgSoTwig::buildFrom (node);
00114 dump (0, twigs);
00115 std::cout << std::flush;
00116
00117 IgDocumentData::get (page->state ())->root (dynamic_cast<IgSoTwig*>(twigs->child(0)));
00118
00119 m_state = new IgState (page->state ());
00120 new IgSelectionService (m_state);
00121
00122 Ig3DModel *model = 0;
00123 IgModelManager::get (m_state)->lookup (m_state, &model);
00124
00125 model->attachPoint ()->magic (twigs->magic ());
00126 model->attachPoint ()->node (twigs->node ());
00127
00128 m_browser = dynamic_cast<Ig3DBaseWindow *> (IgViewDB::get ()->create ("3D Window", page ));
00129
00130 IgQWidgetStackSite *stackArea = dynamic_cast<IgQWidgetStackSite *> (page->stackArea ());
00131 m_bt = IgBrowserDB::get()->create ("Twig", m_state,
00132 stackArea->hostWithName (filename.latin1 ()));
00133 m_bt->browse (twigs->child (0));
00134 }
00135 else
00136 {
00137 QMessageBox::warning (mainWindow,
00138 "No file selected",
00139 "No OpenInventor file has been selected", "Ok");
00140 }
00141 }
00142
00143 IgIVView::~IgIVView (void)
00144 {
00145 if (m_browser)
00146 delete m_browser;
00147 if (m_bt)
00148 delete m_bt;
00149 if (m_state)
00150 delete m_state;
00151 }
00152
00153 void
00154 IgIVView::dump (unsigned level, IgTwig *twig)
00155 {
00156 ASSERT (twig);
00157 LOG (0, trace, LFivview, std::string(level, ' ') << "Twig " << twig->name ()
00158 << (twig->selfVisible () ? " visible" : " invisible")
00159 << (twig->cascade () ? " cascade; " : " no cascade; ")
00160 << ((IgSoTwig *) twig)->node ()->getNumChildren ()
00161 << " OIV children\n" );
00162 for (unsigned n = 0; n < twig->children (); ++n)
00163 dump (level + 2, twig->child (n));
00164 }