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

« back to all changes in this revision

Viewing changes to src/Gui/ViewProviderDocumentObject.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Teemu Ikonen
  • Date: 2009-07-16 18:37:41 UTC
  • Revision ID: james.westby@ubuntu.com-20090716183741-oww9kcxqrk991i1n
Tags: upstream-0.8.2237
Import upstream version 0.8.2237

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (c) 2004 J�rgen Riegel <juergen.riegel@web.de>              *
 
3
 *                                                                         *
 
4
 *   This file is part of the FreeCAD CAx development system.              *
 
5
 *                                                                         *
 
6
 *   This library is free software; you can redistribute it and/or         *
 
7
 *   modify it under the terms of the GNU Library General Public           *
 
8
 *   License as published by the Free Software Foundation; either          *
 
9
 *   version 2 of the License, or (at your option) any later version.      *
 
10
 *                                                                         *
 
11
 *   This library  is distributed in the hope that it will be useful,      *
 
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
14
 *   GNU Library General Public License for more details.                  *
 
15
 *                                                                         *
 
16
 *   You should have received a copy of the GNU Library General Public     *
 
17
 *   License along with this library; see the file COPYING.LIB. If not,    *
 
18
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
 
19
 *   Suite 330, Boston, MA  02111-1307, USA                                *
 
20
 *                                                                         *
 
21
 ***************************************************************************/
 
22
 
 
23
 
 
24
#include "PreCompiled.h"
 
25
 
 
26
#ifndef _PreComp_
 
27
# include <qpixmap.h>
 
28
# include <Inventor/nodes/SoDrawStyle.h>
 
29
# include <Inventor/nodes/SoMaterial.h>
 
30
# include <Inventor/nodes/SoSeparator.h>
 
31
#endif
 
32
 
 
33
/// Here the FreeCAD includes sorted by Base,App,Gui......
 
34
#include <Base/Console.h>
 
35
#include <App/Material.h>
 
36
#include <App/Feature.h>
 
37
#include "Application.h"
 
38
#include "Document.h"
 
39
#include "Selection.h"
 
40
#include "ViewProviderDocumentObject.h"
 
41
#include "ViewProviderDocumentObjectPy.h"
 
42
 
 
43
 
 
44
using namespace Gui;
 
45
 
 
46
 
 
47
PROPERTY_SOURCE(Gui::ViewProviderDocumentObject, Gui::ViewProvider)
 
48
 
 
49
ViewProviderDocumentObject::ViewProviderDocumentObject()
 
50
  : pcObject(0)
 
51
{
 
52
    ADD_PROPERTY(DisplayMode,((long)0));
 
53
    ADD_PROPERTY(Visibility,(true));
 
54
 
 
55
    sPixmap = "Feature";
 
56
}
 
57
 
 
58
 
 
59
ViewProviderDocumentObject::~ViewProviderDocumentObject()
 
60
{
 
61
    // Make sure that the property class does not destruct our string list
 
62
    DisplayMode.setEnums(0);
 
63
}
 
64
 
 
65
void ViewProviderDocumentObject::onChanged(const App::Property* prop)
 
66
{
 
67
    if (prop == &DisplayMode) {
 
68
        setActiveMode();
 
69
    }
 
70
    else if (prop == &Visibility) {
 
71
        Visibility.getValue() ? ViewProvider::show() : ViewProvider::hide();
 
72
    }
 
73
}
 
74
 
 
75
void ViewProviderDocumentObject::hide(void)
 
76
{
 
77
    Visibility.setValue(false);
 
78
}
 
79
 
 
80
void ViewProviderDocumentObject::show(void)
 
81
{
 
82
    Visibility.setValue(true);
 
83
}
 
84
 
 
85
void ViewProviderDocumentObject::updateView()
 
86
{
 
87
    std::map<std::string, App::Property*> Map;
 
88
    pcObject->getPropertyMap(Map);
 
89
 
 
90
    // Hide the object temporarily to speed up the update
 
91
    bool vis = this->isShow();
 
92
    if (vis) hide();
 
93
    for (std::map<std::string, App::Property*>::iterator it = Map.begin(); it != Map.end(); ++it) {
 
94
        updateData(it->second);
 
95
    }
 
96
    if (vis) show();
 
97
}
 
98
 
 
99
void ViewProviderDocumentObject::attach(App::DocumentObject *pcObj)
 
100
{
 
101
    // save Object pointer
 
102
    pcObject = pcObj;
 
103
 
 
104
    // Retrieve the supported display modes of the view provider
 
105
    aDisplayModesArray = this->getDisplayModes();
 
106
 
 
107
    if( aDisplayModesArray.empty() )
 
108
        aDisplayModesArray.push_back("");
 
109
 
 
110
    // We must collect the const char* of the strings and give it to PropertyEnumeration,
 
111
    // but we are still responsible for them, i.e. the property class must not delete the literals.
 
112
    for ( std::vector<std::string>::iterator it = aDisplayModesArray.begin(); it != aDisplayModesArray.end(); ++it ) {
 
113
        aDisplayEnumsArray.push_back( it->c_str() );
 
114
    }
 
115
    aDisplayEnumsArray.push_back(0); // null termination
 
116
    DisplayMode.setEnums(&(aDisplayEnumsArray[0]));
 
117
 
 
118
    // set the active mode
 
119
    const char* defmode = this->getDefaultDisplayMode();
 
120
    if (defmode)
 
121
        DisplayMode.setValue(defmode);
 
122
}
 
123
 
 
124
SoSeparator* ViewProviderDocumentObject::findFrontRootOfType( const SoType& type) const
 
125
{
 
126
    // first get the document this object is part of and get its GUI counterpart
 
127
    App::Document* pAppDoc = pcObject->getDocument();
 
128
    Gui::Document* pGuiDoc = Gui::Application::Instance->getDocument(pAppDoc);
 
129
 
 
130
    // search in all view providers for the node type
 
131
    std::vector<App::DocumentObject*> obj = pAppDoc->getObjects();
 
132
    for (std::vector<App::DocumentObject*>::iterator it = obj.begin(); it != obj.end(); ++it) {
 
133
        const ViewProvider* vp = pGuiDoc->getViewProvider(*it);
 
134
        // Ignore 'this' view provider. It could also happen that vp is 0, e.g. when
 
135
        // several objects have been added to the App::Document before notifying the
 
136
        // Gui::Document
 
137
        if (!vp || vp == this)
 
138
            continue;
 
139
        SoSeparator* front = vp->getFrontRoot();
 
140
        if (front && front->getTypeId() == type)
 
141
            return front;
 
142
    }
 
143
 
 
144
    return 0;
 
145
}
 
146
 
 
147
void ViewProviderDocumentObject::setActiveMode()
 
148
{
 
149
    if (DisplayMode.getEnums()) {
 
150
        const char* mode = DisplayMode.getValueAsString();
 
151
        if (mode)
 
152
            setDisplayMode(mode);
 
153
    }
 
154
    if (!Visibility.getValue())
 
155
        ViewProvider::hide();
 
156
}
 
157
 
 
158
const char* ViewProviderDocumentObject::getDefaultDisplayMode() const
 
159
{
 
160
    // We use the first item then
 
161
    return 0;
 
162
}
 
163
 
 
164
std::vector<std::string> ViewProviderDocumentObject::getDisplayModes(void) const
 
165
{
 
166
    // empty
 
167
    return std::vector<std::string>();
 
168
}
 
169
 
 
170
PyObject* ViewProviderDocumentObject::getPyObject()
 
171
{
 
172
    if (!pyViewObject)
 
173
        pyViewObject = new ViewProviderDocumentObjectPy(this);
 
174
    pyViewObject->IncRef();
 
175
    return pyViewObject;
 
176
}