| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2012 Jürgen Riegel <[email protected]> * |
| 3 |
* Copyright (c) 2015 Alexander Golubev (Fat-Zer) <[email protected]> * |
| 4 |
* * |
| 5 |
* This file is part of the FreeCAD CAx development system. * |
| 6 |
* * |
| 7 |
* This library is free software; you can redistribute it and/or * |
| 8 |
* modify it under the terms of the GNU Library General Public * |
| 9 |
* License as published by the Free Software Foundation; either * |
| 10 |
* version 2 of the License, or (at your option) any later version. * |
| 11 |
* * |
| 12 |
* This library is distributed in the hope that it will be useful, * |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 15 |
* GNU Library General Public License for more details. * |
| 16 |
* * |
| 17 |
* You should have received a copy of the GNU Library General Public * |
| 18 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 19 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 20 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 21 |
* * |
| 22 |
***************************************************************************/ |
| 23 |
|
| 24 |
#include <Inventor/nodes/SoAsciiText.h> |
| 25 |
#include <Inventor/nodes/SoCoordinate3.h> |
| 26 |
#include <Inventor/nodes/SoFaceSet.h> |
| 27 |
#include <Inventor/nodes/SoIndexedLineSet.h> |
| 28 |
#include <Inventor/nodes/SoMaterial.h> |
| 29 |
#include <Inventor/nodes/SoPickStyle.h> |
| 30 |
#include <Inventor/nodes/SoSeparator.h> |
| 31 |
#include <Inventor/nodes/SoShapeHints.h> |
| 32 |
#include <Inventor/nodes/SoTranslation.h> |
| 33 |
#include <Inventor/nodes/SoSwitch.h> |
| 34 |
#include <Inventor/SbColor.h> |
| 35 |
|
| 36 |
#include <App/Datums.h> |
| 37 |
#include <App/Document.h> |
| 38 |
#include <Gui/ViewParams.h> |
| 39 |
|
| 40 |
#include "ViewProviderPlane.h" |
| 41 |
#include "ViewProviderCoordinateSystem.h" |
| 42 |
|
| 43 |
#include <Utilities.h> |
| 44 |
#include <Base/Tools.h> |
| 45 |
#include <Inventor/SoPickedPoint.h> |
| 46 |
#include <Inventor/events/SoEvent.h> |
| 47 |
#include <Inventor/nodes/SoFontStyle.h> |
| 48 |
|
| 49 |
|
| 50 |
using namespace Gui; |
| 51 |
|
| 52 |
PROPERTY_SOURCE(Gui::ViewProviderPlane, Gui::ViewProviderDatum) |
| 53 |
|
| 54 |
|
| 55 |
ViewProviderPlane::ViewProviderPlane() |
| 56 |
: SelectionObserver(true) |
| 57 |
{ |
| 58 |
sPixmap = "Std_Plane"; |
| 59 |
lineThickness = 1.0; |
| 60 |
|
| 61 |
pLabel = new SoAsciiText(); |
| 62 |
} |
| 63 |
|
| 64 |
ViewProviderPlane::~ViewProviderPlane() = default; |
| 65 |
|
| 66 |
void ViewProviderPlane::attach(App::DocumentObject* obj) |
| 67 |
{ |
| 68 |
ViewProviderDatum::attach(obj); |
| 69 |
|
| 70 |
std::string role = getRole(); |
| 71 |
|
| 72 |
SoSeparator* sep = getDatumRoot(); |
| 73 |
|
| 74 |
// Setup colors |
| 75 |
// Can't use transparency because of https://github.com/FreeCAD/FreeCAD/issues/18395 |
| 76 |
// When this issue is fixed then we can use the below and remove the material here |
| 77 |
// and faceSeparator... |
| 78 |
// ShapeAppearance.setTransparency(0.8); |
| 79 |
auto material = new SoMaterial(); |
| 80 |
material->transparency.setValue(0.85f); |
| 81 |
|
| 82 |
if (!role.empty()) { |
| 83 |
ShapeAppearance.setDiffuseColor(getColor(role)); |
| 84 |
} |
| 85 |
|
| 86 |
SbColor color = ShapeAppearance.getDiffuseColor().asValue<SbColor>(); |
| 87 |
material->ambientColor.setValue(color); |
| 88 |
material->diffuseColor.setValue(color); |
| 89 |
|
| 90 |
auto ps = new SoPickStyle(); |
| 91 |
ps->style.setValue(SoPickStyle::SHAPE_ON_TOP); |
| 92 |
|
| 93 |
pCoords = new SoCoordinate3(); |
| 94 |
sep->addChild(pCoords); |
| 95 |
sep->addChild(material); |
| 96 |
sep->addChild(ps); |
| 97 |
|
| 98 |
auto lineSeparator = new SoSeparator(); |
| 99 |
auto pLines = new SoIndexedLineSet(); |
| 100 |
static const int32_t lines[6] = {0, 1, 2, 3, 0, -1}; |
| 101 |
pLines->coordIndex.setNum(6); |
| 102 |
pLines->coordIndex.setValues(0, 6, lines); |
| 103 |
|
| 104 |
lineSeparator->addChild(pLines); |
| 105 |
sep->addChild(lineSeparator); |
| 106 |
|
| 107 |
// add semi transparent face |
| 108 |
auto faceSeparator = new SoSeparator(); |
| 109 |
sep->addChild(faceSeparator); |
| 110 |
|
| 111 |
// disable backface culling and render with two-sided lighting |
| 112 |
auto shapeHints = new SoShapeHints(); |
| 113 |
shapeHints->vertexOrdering = SoShapeHints::COUNTERCLOCKWISE; |
| 114 |
shapeHints->shapeType = SoShapeHints::UNKNOWN_SHAPE_TYPE; |
| 115 |
faceSeparator->addChild(shapeHints); |
| 116 |
|
| 117 |
auto faceSet = new SoFaceSet(); |
| 118 |
auto vertexProperty = new SoVertexProperty(); |
| 119 |
vertexProperty->vertex.connectFrom(&pCoords->point); |
| 120 |
faceSet->vertexProperty.setValue(vertexProperty); |
| 121 |
faceSeparator->addChild(faceSet); |
| 122 |
|
| 123 |
pTextTranslation = new SoTranslation(); |
| 124 |
sep->addChild(pTextTranslation); |
| 125 |
|
| 126 |
pLabel->string.setValue(getLabelText(role).c_str()); |
| 127 |
pLabel->justification = SoAsciiText::RIGHT; |
| 128 |
labelSwitch = new SoSwitch(); |
| 129 |
setLabelVisibility(false); |
| 130 |
|
| 131 |
auto font = new SoFontStyle(); |
| 132 |
font->size = 10.0; |
| 133 |
font->style = SoFontStyle::BOLD; |
| 134 |
font->family = SoFontStyle::SANS; |
| 135 |
|
| 136 |
auto labelMaterial = static_cast<SoMaterial*>(material->copy()); |
| 137 |
labelMaterial->transparency = 0.0; |
| 138 |
|
| 139 |
labelSwitch->addChild(font); |
| 140 |
labelSwitch->addChild(labelMaterial); |
| 141 |
labelSwitch->addChild(pTextTranslation); |
| 142 |
labelSwitch->addChild(pLabel); |
| 143 |
|
| 144 |
updatePlaneSize(); |
| 145 |
|
| 146 |
sep->addChild(labelSwitch); |
| 147 |
|
| 148 |
handlers.addDelayedHandler( |
| 149 |
ViewParams::instance()->getHandle(), |
| 150 |
{"DatumLineSize", "DatumScale"}, |
| 151 |
[this](ParameterGrp::handle) { updatePlaneSize(); } |
| 152 |
); |
| 153 |
|
| 154 |
updatePlaneSize(); |
| 155 |
} |
| 156 |
|
| 157 |
void ViewProviderPlane::setLabelVisibility(bool val) |
| 158 |
{ |
| 159 |
labelSwitch->whichChild = val ? SO_SWITCH_ALL : SO_SWITCH_NONE; |
| 160 |
} |
| 161 |
|
| 162 |
void ViewProviderPlane::onSelectionChanged(const SelectionChanges& msg) |
| 163 |
{ |
| 164 |
bool isSelectedOrHoveredBefore = isSelected || isHovered; |
| 165 |
|
| 166 |
if (msg.Type == Gui::SelectionChanges::ClrSelection) { |
| 167 |
isSelected = false; |
| 168 |
} |
| 169 |
|
| 170 |
auto obj = getObject(); |
| 171 |
if (!obj) { |
| 172 |
return; |
| 173 |
} |
| 174 |
auto doc = obj->getDocument(); |
| 175 |
if (!doc) { |
| 176 |
return; |
| 177 |
} |
| 178 |
const char* nameInDoc = getObject()->getNameInDocument(); |
| 179 |
|
| 180 |
if (nameInDoc && strcmp(msg.pDocName, doc->getName()) == 0 |
| 181 |
&& strcmp(msg.pObjectName, nameInDoc) == 0) { |
| 182 |
isSelected = Gui::Selection().isSelected(getObject()); |
| 183 |
isHovered = Gui::Selection().getPreselection().Object.getSubObject() == getObject() |
| 184 |
|| Gui::Selection().getPreselection().Object.getObject() == getObject(); |
| 185 |
} |
| 186 |
|
| 187 |
bool isSelectedOrHovered = isSelected || isHovered; |
| 188 |
|
| 189 |
if (isSelectedOrHoveredBefore != isSelectedOrHovered) { |
| 190 |
updatePlaneSize(); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
void ViewProviderPlane::updatePlaneSize() |
| 195 |
{ |
| 196 |
if (!pcObject->isAttachedToDocument()) { |
| 197 |
return; |
| 198 |
} |
| 199 |
|
| 200 |
const auto params = ViewParams::instance(); |
| 201 |
|
| 202 |
const float size = params->getDatumPlaneSize() * Base::fromPercent(params->getDatumScale()); |
| 203 |
const float offset = 8.0F; |
| 204 |
|
| 205 |
SbVec3f verts[4]; |
| 206 |
|
| 207 |
bool isSelectedOrHovered = isSelected || isHovered; |
| 208 |
|
| 209 |
if (!getRole().empty() && !isSelectedOrHovered) { |
| 210 |
verts[0] = SbVec3f(size, size, 0); |
| 211 |
verts[1] = SbVec3f(size, offset, 0); |
| 212 |
verts[2] = SbVec3f(offset, offset, 0); |
| 213 |
verts[3] = SbVec3f(offset, size, 0); |
| 214 |
} |
| 215 |
else { |
| 216 |
verts[0] = SbVec3f(size, size, 0); |
| 217 |
verts[1] = SbVec3f(size, -size, 0); |
| 218 |
verts[2] = SbVec3f(-size, -size, 0); |
| 219 |
verts[3] = SbVec3f(-size, size, 0); |
| 220 |
} |
| 221 |
|
| 222 |
pTextTranslation->translation.setValue(verts[0] / 2 - SbVec3f(2, 6, 0)); // NOLINT |
| 223 |
pCoords->point.setNum(4); |
| 224 |
pCoords->point.setValues(0, 4, verts); |
| 225 |
} |
| 226 |
|
| 227 |
unsigned long ViewProviderPlane::getColor(const std::string& role) const |
| 228 |
{ |
| 229 |
auto planesRoles = App::LocalCoordinateSystem::PlaneRoles; |
| 230 |
if (role == planesRoles[0]) { |
| 231 |
return ViewParams::instance()->getAxisZColor(); // XY-plane |
| 232 |
} |
| 233 |
else if (role == planesRoles[1]) { |
| 234 |
return ViewParams::instance()->getAxisYColor(); // XZ-plane |
| 235 |
} |
| 236 |
else if (role == planesRoles[2]) { |
| 237 |
return ViewParams::instance()->getAxisXColor(); // YZ-plane |
| 238 |
} |
| 239 |
return 0; |
| 240 |
} |
| 241 |
|
| 242 |
std::string ViewProviderPlane::getLabelText(const std::string& role) const |
| 243 |
{ |
| 244 |
auto planesRoles = App::LocalCoordinateSystem::PlaneRoles; |
| 245 |
if (role == planesRoles[0]) { |
| 246 |
return "XY"; |
| 247 |
} |
| 248 |
else if (role == planesRoles[1]) { |
| 249 |
return "XZ"; |
| 250 |
} |
| 251 |
else if (role == planesRoles[2]) { |
| 252 |
return "YZ"; |
| 253 |
} |
| 254 |
return getObject()->getNameInDocument(); |
| 255 |
} |
| 256 |
|
| 257 |
std::string ViewProviderPlane::getRole() const |
| 258 |
{ |
| 259 |
// Note: Role property of App::Plane is not set yet when attaching. |
| 260 |
const char* name = pcObject->getNameInDocument(); |
| 261 |
if (!name) { |
| 262 |
return ""; |
| 263 |
} |
| 264 |
|
| 265 |
auto planesRoles = App::LocalCoordinateSystem::PlaneRoles; |
| 266 |
if (strncmp(name, planesRoles[0], strlen(planesRoles[0])) == 0) { |
| 267 |
return planesRoles[0]; |
| 268 |
} |
| 269 |
else if (strncmp(name, planesRoles[1], strlen(planesRoles[1])) == 0) { |
| 270 |
return planesRoles[1]; |
| 271 |
} |
| 272 |
else if (strncmp(name, planesRoles[2], strlen(planesRoles[2])) == 0) { |
| 273 |
return planesRoles[2]; |
| 274 |
} |
| 275 |
|
| 276 |
return ""; |
| 277 |
} |