ViewProviderInventorObject.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2007 Werner Mayer <wmayer[at]users.sourceforge.net>     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 
00026 #ifndef _PreComp_
00027 # include <Inventor/SoDB.h>
00028 # include <Inventor/SoInput.h>
00029 # include <Inventor/nodes/SoSeparator.h>
00030 # include <QFile>
00031 #endif
00032 
00033 #include "ViewProviderInventorObject.h"
00034 #include <Gui/SoFCSelection.h>
00035 #include <App/InventorObject.h>
00036 #include <App/Document.h>
00037 #include <Base/FileInfo.h>
00038 #include <Base/Stream.h>
00039 #include <sstream>
00040 
00041 using namespace Gui;
00042 
00043 PROPERTY_SOURCE(Gui::ViewProviderInventorObject, Gui::ViewProviderDocumentObject)
00044 
00045 ViewProviderInventorObject::ViewProviderInventorObject()
00046 {
00047     pcBuffer = new SoSeparator();
00048     pcBuffer->ref();
00049     pcFile = new SoSeparator();
00050     pcFile->ref();
00051 }
00052 
00053 ViewProviderInventorObject::~ViewProviderInventorObject()
00054 {
00055     pcBuffer->unref();
00056     pcFile->unref();
00057 }
00058 
00059 void ViewProviderInventorObject::attach(App::DocumentObject *pcObj)
00060 {
00061     ViewProviderDocumentObject::attach(pcObj);
00062     SoGroup* pcFileBuf = new SoGroup();
00063     pcFileBuf->addChild(pcBuffer);
00064     pcFileBuf->addChild(pcFile);
00065     addDisplayMaskMode(pcFileBuf, "FileBuffer");
00066     addDisplayMaskMode(pcBuffer, "Buffer");
00067     addDisplayMaskMode(pcFile, "File");
00068 }
00069 
00070 void ViewProviderInventorObject::setDisplayMode(const char* ModeName)
00071 {
00072     if (strcmp("File+Buffer",ModeName)==0)
00073         setDisplayMaskMode("FileBuffer");
00074     else if (strcmp("Buffer",ModeName)==0)
00075         setDisplayMaskMode("Buffer");
00076     else if (strcmp("File",ModeName)==0)
00077         setDisplayMaskMode("File");
00078     ViewProviderDocumentObject::setDisplayMode(ModeName);
00079 }
00080 
00081 std::vector<std::string> ViewProviderInventorObject::getDisplayModes(void) const
00082 {
00083     std::vector<std::string> StrList;
00084     StrList.push_back("File+Buffer");
00085     StrList.push_back("Buffer");
00086     StrList.push_back("File");
00087     return StrList;
00088 }
00089 
00090 void ViewProviderInventorObject::updateData(const App::Property* prop)
00091 {
00092     App::InventorObject* ivObj = static_cast<App::InventorObject*>(pcObject);
00093     if (prop == &ivObj->Buffer) {
00094         // read from buffer
00095         SoInput in;
00096         std::string buffer = ivObj->Buffer.getValue();
00097         pcBuffer->removeAllChildren();
00098         if (buffer.empty()) return;
00099         in.setBuffer((void *)buffer.c_str(), buffer.size());
00100         SoSeparator * node = SoDB::readAll(&in);
00101         if (node) {
00102             const char* doc = this->pcObject->getDocument()->getName();
00103             const char* obj = this->pcObject->getNameInDocument();
00104             adjustSelectionNodes(node, doc, obj);
00105             pcBuffer->addChild(node);
00106         }
00107     }
00108     if (prop == &ivObj->FileName) {
00109         // read also from file
00110         const char* filename = ivObj->FileName.getValue();
00111         QString fn = QString::fromUtf8(filename);
00112         QFile file(fn);
00113         SoInput in;
00114         pcFile->removeAllChildren();
00115         if (!fn.isEmpty() && file.open(QFile::ReadOnly)) {
00116             QByteArray buffer = file.readAll();
00117             in.setBuffer((void *)buffer.constData(), buffer.length());
00118             SoSeparator * node = SoDB::readAll(&in);
00119             if (node) {
00120                 const char* doc = this->pcObject->getDocument()->getName();
00121                 const char* obj = this->pcObject->getNameInDocument();
00122                 adjustSelectionNodes(node, doc, obj);
00123                 pcFile->addChild(node);
00124             }
00125         }
00126     }
00127 }
00128 
00129 void ViewProviderInventorObject::adjustSelectionNodes(SoNode* child, const char* docname,
00130                                                       const char* objname)
00131 {
00132     if (child->getTypeId().isDerivedFrom(SoFCSelection::getClassTypeId())) {
00133         static_cast<SoFCSelection*>(child)->documentName = docname;
00134         static_cast<SoFCSelection*>(child)->objectName = objname;
00135     }
00136     else if (child->getTypeId().isDerivedFrom(SoGroup::getClassTypeId())) {
00137         SoGroup* group = static_cast<SoGroup*>(child);
00138         for (int i=0; i<group->getNumChildren(); i++) {
00139             SoNode* subchild = group->getChild(i);
00140             adjustSelectionNodes(subchild, docname, objname);
00141         }
00142     }
00143 }

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