ViewProviderInventorObject.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
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
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 }