| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2010 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 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 <Inventor/nodes/SoMaterial.h> |
| 25 |
|
| 26 |
|
| 27 |
#include <App/PropertyStandard.h> |
| 28 |
|
| 29 |
#include "ViewProviderBuilder.h" |
| 30 |
#include "SoFCSelection.h" |
| 31 |
#include "Window.h" |
| 32 |
|
| 33 |
|
| 34 |
using namespace Gui; |
| 35 |
|
| 36 |
std::map<Base::Type, Base::Type> ViewProviderBuilder::_prop_to_view; |
| 37 |
|
| 38 |
ViewProviderBuilder::ViewProviderBuilder() = default; |
| 39 |
|
| 40 |
ViewProviderBuilder::~ViewProviderBuilder() = default; |
| 41 |
|
| 42 |
void ViewProviderBuilder::add(const Base::Type& prop, const Base::Type& view) |
| 43 |
{ |
| 44 |
_prop_to_view[prop] = view; |
| 45 |
} |
| 46 |
|
| 47 |
ViewProvider* ViewProviderBuilder::create(const Base::Type& type) |
| 48 |
{ |
| 49 |
std::map<Base::Type, Base::Type>::iterator it = _prop_to_view.find(type); |
| 50 |
if (it != _prop_to_view.end()) { |
| 51 |
return static_cast<ViewProvider*>(it->second.createInstance()); |
| 52 |
} |
| 53 |
return nullptr; |
| 54 |
} |
| 55 |
|
| 56 |
Gui::SoFCSelection* ViewProviderBuilder::createSelection() |
| 57 |
{ |
| 58 |
auto sel = new Gui::SoFCSelection(); |
| 59 |
|
| 60 |
float transparency; |
| 61 |
ParameterGrp::handle hGrp = Gui::WindowParameter::getDefaultParameter()->GetGroup("View"); |
| 62 |
bool enablePre = hGrp->GetBool("EnablePreselection", true); |
| 63 |
bool enableSel = hGrp->GetBool("EnableSelection", true); |
| 64 |
if (!enablePre) { |
| 65 |
sel->preselectionMode = Gui::SoFCSelection::OFF; |
| 66 |
} |
| 67 |
else { |
| 68 |
// Search for a user defined value with the current color as default |
| 69 |
SbColor highlightColor = sel->colorHighlight.getValue(); |
| 70 |
auto highlight = (unsigned long)(highlightColor.getPackedValue()); |
| 71 |
highlight = hGrp->GetUnsigned("HighlightColor", highlight); |
| 72 |
highlightColor.setPackedValue((uint32_t)highlight, transparency); |
| 73 |
sel->colorHighlight.setValue(highlightColor); |
| 74 |
} |
| 75 |
if (!enableSel) { |
| 76 |
sel->selectionMode = Gui::SoFCSelection::SEL_OFF; |
| 77 |
} |
| 78 |
else { |
| 79 |
// Do the same with the selection color |
| 80 |
SbColor selectionColor = sel->colorSelection.getValue(); |
| 81 |
auto selection = (unsigned long)(selectionColor.getPackedValue()); |
| 82 |
selection = hGrp->GetUnsigned("SelectionColor", selection); |
| 83 |
selectionColor.setPackedValue((uint32_t)selection, transparency); |
| 84 |
sel->colorSelection.setValue(selectionColor); |
| 85 |
} |
| 86 |
|
| 87 |
return sel; |
| 88 |
} |
| 89 |
|
| 90 |
// -------------------------------------- |
| 91 |
|
| 92 |
ViewProviderColorBuilder::ViewProviderColorBuilder() = default; |
| 93 |
|
| 94 |
ViewProviderColorBuilder::~ViewProviderColorBuilder() = default; |
| 95 |
|
| 96 |
void ViewProviderColorBuilder::buildNodes(const App::Property* prop, std::vector<SoNode*>& node) const |
| 97 |
{ |
| 98 |
const auto color = static_cast<const App::PropertyColorList*>(prop); |
| 99 |
const std::vector<Base::Color>& val = color->getValues(); |
| 100 |
unsigned long i = 0; |
| 101 |
|
| 102 |
auto material = new SoMaterial(); |
| 103 |
material->diffuseColor.setNum(val.size()); |
| 104 |
|
| 105 |
SbColor* colors = material->diffuseColor.startEditing(); |
| 106 |
for (const auto& it : val) { |
| 107 |
colors[i].setValue(it.r, it.g, it.b); |
| 108 |
i++; |
| 109 |
} |
| 110 |
material->diffuseColor.finishEditing(); |
| 111 |
node.push_back(material); |
| 112 |
} |