~ubuntu-branches/ubuntu/maverick/freecad/maverick

« back to all changes in this revision

Viewing changes to src/Gui/ViewProviderInventorObject.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam C. Powell, IV
  • Date: 2010-01-11 08:48:33 UTC
  • mfrom: (3.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20100111084833-4g9vgdqbkw8u34zb
Tags: 0.9.2646.5-1
* New upstream version (closes: #561696).
* Added swig to Build-Depends (closes: #563523, #563772).
* Removed python-opencv from Build-Depends and Recommends (closes: #560768).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/***************************************************************************
2
 
 *   Copyright (c) 2007 Werner Mayer <werner.wm.mayer@gmx.de>              *
 
2
 *   Copyright (c) 2007 Werner Mayer <wmayer[at]users.sourceforge.net>     *
3
3
 *                                                                         *
4
4
 *   This file is part of the FreeCAD CAx development system.              *
5
5
 *                                                                         *
23
23
 
24
24
#include "PreCompiled.h"
25
25
 
26
 
#ifndef _PreComp_
 
26
#ifndef _PreComp_
27
27
# include <Inventor/SoDB.h>
28
28
# include <Inventor/SoInput.h>
29
29
# include <Inventor/nodes/SoSeparator.h>
 
30
# include <QFile>
30
31
#endif
31
32
 
32
33
#include "ViewProviderInventorObject.h"
33
 
#include <App/InventorObject.h>
 
34
#include <Gui/SoFCSelection.h>
 
35
#include <App/InventorObject.h>
 
36
#include <App/Document.h>
34
37
#include <Base/FileInfo.h>
35
38
#include <Base/Stream.h>
36
39
#include <sstream>
40
43
PROPERTY_SOURCE(Gui::ViewProviderInventorObject, Gui::ViewProviderDocumentObject)
41
44
 
42
45
ViewProviderInventorObject::ViewProviderInventorObject()
43
 
{
44
 
    pcBuffer = new SoSeparator();
 
46
{
 
47
    pcBuffer = new SoSeparator();
45
48
    pcBuffer->ref();
46
 
    pcFile = new SoSeparator();
 
49
    pcFile = new SoSeparator();
47
50
    pcFile->ref();
48
51
}
49
52
 
50
53
ViewProviderInventorObject::~ViewProviderInventorObject()
51
 
{
 
54
{
52
55
    pcBuffer->unref();
53
56
    pcFile->unref();
54
57
}
56
59
void ViewProviderInventorObject::attach(App::DocumentObject *pcObj)
57
60
{
58
61
    ViewProviderDocumentObject::attach(pcObj);
59
 
    SoGroup* pcFileBuf = new SoGroup();
60
 
    pcFileBuf->addChild(pcBuffer);
61
 
    pcFileBuf->addChild(pcFile);
62
 
    addDisplayMaskMode(pcFileBuf, "FileBuffer");
63
 
    addDisplayMaskMode(pcBuffer, "Buffer");
64
 
    addDisplayMaskMode(pcFile, "File");
65
 
}
66
 
 
67
 
void ViewProviderInventorObject::setDisplayMode(const char* ModeName)
68
 
{
69
 
  if ( strcmp("File+Buffer",ModeName)==0 )
70
 
    setDisplayMaskMode("FileBuffer");
71
 
  else if ( strcmp("Buffer",ModeName)==0 )
72
 
    setDisplayMaskMode("Buffer");
73
 
  else if ( strcmp("File",ModeName)==0 )
74
 
    setDisplayMaskMode("File");
75
 
  ViewProviderDocumentObject::setDisplayMode( ModeName );
76
 
}
77
 
 
78
 
std::vector<std::string> ViewProviderInventorObject::getDisplayModes(void) const
79
 
{
80
 
  std::vector<std::string> StrList;
81
 
  StrList.push_back("File+Buffer");
82
 
  StrList.push_back("Buffer");
83
 
  StrList.push_back("File");
84
 
  return StrList;
85
 
}
 
62
    SoGroup* pcFileBuf = new SoGroup();
 
63
    pcFileBuf->addChild(pcBuffer);
 
64
    pcFileBuf->addChild(pcFile);
 
65
    addDisplayMaskMode(pcFileBuf, "FileBuffer");
 
66
    addDisplayMaskMode(pcBuffer, "Buffer");
 
67
    addDisplayMaskMode(pcFile, "File");
 
68
}
 
69
 
 
70
void ViewProviderInventorObject::setDisplayMode(const char* ModeName)
 
71
{
 
72
    if (strcmp("File+Buffer",ModeName)==0)
 
73
        setDisplayMaskMode("FileBuffer");
 
74
    else if (strcmp("Buffer",ModeName)==0)
 
75
        setDisplayMaskMode("Buffer");
 
76
    else if (strcmp("File",ModeName)==0)
 
77
        setDisplayMaskMode("File");
 
78
    ViewProviderDocumentObject::setDisplayMode(ModeName);
 
79
}
 
80
 
 
81
std::vector<std::string> ViewProviderInventorObject::getDisplayModes(void) const
 
82
{
 
83
    std::vector<std::string> StrList;
 
84
    StrList.push_back("File+Buffer");
 
85
    StrList.push_back("Buffer");
 
86
    StrList.push_back("File");
 
87
    return StrList;
 
88
}
86
89
 
87
90
void ViewProviderInventorObject::updateData(const App::Property* prop)
88
91
{
95
98
        if (buffer.empty()) return;
96
99
        in.setBuffer((void *)buffer.c_str(), buffer.size());
97
100
        SoSeparator * node = SoDB::readAll(&in);
98
 
        if (node) pcBuffer->addChild(node);
 
101
        if (node) {
 
102
            const char* doc = this->pcObject->getDocument()->getName();
 
103
            const char* obj = this->pcObject->getNameInDocument();
 
104
            adjustSelectionNodes(node, doc, obj);
 
105
            pcBuffer->addChild(node);
 
106
        }
99
107
    }
100
108
    if (prop == &ivObj->FileName) {
101
 
        // read also from file
102
 
        const char* filename = ivObj->FileName.getValue();
103
 
        QFile file(QString::fromUtf8(filename));
 
109
        // read also from file
 
110
        const char* filename = ivObj->FileName.getValue();
 
111
        QString fn = QString::fromUtf8(filename);
 
112
        QFile file(fn);
104
113
        SoInput in;
105
114
        pcFile->removeAllChildren();
106
 
        if (file.open(QFile::ReadOnly)) {
 
115
        if (!fn.isEmpty() && file.open(QFile::ReadOnly)) {
107
116
            QByteArray buffer = file.readAll();
108
 
            in.setBuffer((void *)buffer.constData(), buffer.length());
 
117
            in.setBuffer((void *)buffer.constData(), buffer.length());
109
118
            SoSeparator * node = SoDB::readAll(&in);
110
 
            if (node) pcFile->addChild(node);
 
119
            if (node) {
 
120
                const char* doc = this->pcObject->getDocument()->getName();
 
121
                const char* obj = this->pcObject->getNameInDocument();
 
122
                adjustSelectionNodes(node, doc, obj);
 
123
                pcFile->addChild(node);
 
124
            }
 
125
        }
 
126
    }
 
127
}
 
128
 
 
129
void ViewProviderInventorObject::adjustSelectionNodes(SoNode* child, const char* docname,
 
130
                                                      const char* objname)
 
131
{
 
132
    if (child->getTypeId().isDerivedFrom(SoFCSelection::getClassTypeId())) {
 
133
        static_cast<SoFCSelection*>(child)->documentName = docname;
 
134
        static_cast<SoFCSelection*>(child)->objectName = objname;
 
135
    }
 
136
    else if (child->getTypeId().isDerivedFrom(SoGroup::getClassTypeId())) {
 
137
        SoGroup* group = static_cast<SoGroup*>(child);
 
138
        for (int i=0; i<group->getNumChildren(); i++) {
 
139
            SoNode* subchild = group->getChild(i);
 
140
            adjustSelectionNodes(subchild, docname, objname);
111
141
        }
112
142
    }
113
143
}