| 1 |
/**************************************************************************** |
| 2 |
* Copyright (c) 2022 Zheng Lei (realthunder) <[email protected]> * |
| 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/details/SoDetail.h> |
| 25 |
#include <Inventor/nodes/SoMaterial.h> |
| 26 |
#include <Inventor/nodes/SoPickStyle.h> |
| 27 |
#include <Inventor/nodes/SoSeparator.h> |
| 28 |
|
| 29 |
|
| 30 |
#include "Application.h" |
| 31 |
#include "Document.h" |
| 32 |
#include "SoFCUnifiedSelection.h" |
| 33 |
#include "View3DInventorSelection.h" |
| 34 |
#include "ViewProviderDocumentObject.h" |
| 35 |
#include <App/Document.h> |
| 36 |
#include <App/GeoFeature.h> |
| 37 |
#include <App/GeoFeatureGroupExtension.h> |
| 38 |
#include <Base/Console.h> |
| 39 |
|
| 40 |
FC_LOG_LEVEL_INIT("3DViewerSelection", true, true) |
| 41 |
|
| 42 |
using namespace Gui; |
| 43 |
|
| 44 |
View3DInventorSelection::View3DInventorSelection(SoFCUnifiedSelection* root) |
| 45 |
: selectionRoot(root) |
| 46 |
{ |
| 47 |
selectionRoot->ref(); |
| 48 |
|
| 49 |
pcGroupOnTop = new SoSeparator; |
| 50 |
pcGroupOnTop->ref(); |
| 51 |
pcGroupOnTop->setName("GroupOnTop"); |
| 52 |
root->addChild(pcGroupOnTop); |
| 53 |
|
| 54 |
auto pcGroupOnTopPickStyle = new SoPickStyle; |
| 55 |
pcGroupOnTopPickStyle->style = SoPickStyle::UNPICKABLE; |
| 56 |
pcGroupOnTopPickStyle->setOverride(true); |
| 57 |
pcGroupOnTopPickStyle->setName("GroupOnTopPickStyle"); |
| 58 |
pcGroupOnTop->addChild(pcGroupOnTopPickStyle); |
| 59 |
|
| 60 |
coin_setenv("COIN_SEPARATE_DIFFUSE_TRANSPARENCY_OVERRIDE", "1", TRUE); |
| 61 |
auto pcGroupOnTopMaterial = new SoMaterial; |
| 62 |
pcGroupOnTopMaterial->transparency = 0.5; |
| 63 |
pcGroupOnTopMaterial->diffuseColor.setIgnored(true); |
| 64 |
pcGroupOnTopMaterial->setOverride(true); |
| 65 |
pcGroupOnTopMaterial->setName("GroupOnTopMaterial"); |
| 66 |
pcGroupOnTop->addChild(pcGroupOnTopMaterial); |
| 67 |
|
| 68 |
{ |
| 69 |
auto selRoot = new SoFCSelectionRoot; |
| 70 |
selRoot->selectionStyle = SoFCSelectionRoot::PassThrough; |
| 71 |
pcGroupOnTopSel = selRoot; |
| 72 |
pcGroupOnTopSel->setName("GroupOnTopSel"); |
| 73 |
pcGroupOnTopSel->ref(); |
| 74 |
pcGroupOnTop->addChild(pcGroupOnTopSel); |
| 75 |
} |
| 76 |
|
| 77 |
{ |
| 78 |
auto selRoot = new SoFCSelectionRoot; |
| 79 |
selRoot->selectionStyle = SoFCSelectionRoot::PassThrough; |
| 80 |
pcGroupOnTopPreSel = selRoot; |
| 81 |
pcGroupOnTopPreSel->setName("GroupOnTopPreSel"); |
| 82 |
pcGroupOnTopPreSel->ref(); |
| 83 |
pcGroupOnTop->addChild(pcGroupOnTopPreSel); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
View3DInventorSelection::~View3DInventorSelection() |
| 88 |
{ |
| 89 |
selectionRoot->unref(); |
| 90 |
pcGroupOnTop->unref(); |
| 91 |
pcGroupOnTopPreSel->unref(); |
| 92 |
pcGroupOnTopSel->unref(); |
| 93 |
} |
| 94 |
|
| 95 |
void View3DInventorSelection::checkGroupOnTop(const SelectionChanges& Reason) |
| 96 |
{ |
| 97 |
if (Reason.Type == SelectionChanges::SetSelection |
| 98 |
|| Reason.Type == SelectionChanges::ClrSelection) { |
| 99 |
clearGroupOnTop(); |
| 100 |
if (Reason.Type == SelectionChanges::ClrSelection) { |
| 101 |
return; |
| 102 |
} |
| 103 |
} |
| 104 |
if (Reason.Type == SelectionChanges::RmvPreselect |
| 105 |
|| Reason.Type == SelectionChanges::RmvPreselectSignal) { |
| 106 |
SoSelectionElementAction action(SoSelectionElementAction::None, true); |
| 107 |
action.apply(pcGroupOnTopPreSel); |
| 108 |
coinRemoveAllChildren(pcGroupOnTopPreSel); |
| 109 |
objectsOnTopPreSel.clear(); |
| 110 |
return; |
| 111 |
} |
| 112 |
if (!getDocument() || !Reason.pDocName || !Reason.pDocName[0] || !Reason.pObjectName) { |
| 113 |
return; |
| 114 |
} |
| 115 |
auto obj = getDocument()->getDocument()->getObject(Reason.pObjectName); |
| 116 |
if (!obj || !obj->isAttachedToDocument()) { |
| 117 |
return; |
| 118 |
} |
| 119 |
std::string key(obj->getNameInDocument()); |
| 120 |
key += '.'; |
| 121 |
auto subname = Reason.pSubName; |
| 122 |
App::ElementNamePair element; |
| 123 |
App::GeoFeature::resolveElement(obj, Reason.pSubName, element); |
| 124 |
if (Data::isMappedElement(subname) && !element.oldName.empty()) { // If we have a shortened |
| 125 |
// element name |
| 126 |
subname = element.oldName.c_str(); // use if |
| 127 |
} |
| 128 |
if (subname) { |
| 129 |
key += subname; |
| 130 |
} |
| 131 |
if (Reason.Type == SelectionChanges::RmvSelection) { |
| 132 |
auto& objs = objectsOnTop; |
| 133 |
auto pcGroup = pcGroupOnTopSel; |
| 134 |
auto it = objs.find(key.c_str()); |
| 135 |
if (it == objs.end()) { |
| 136 |
return; |
| 137 |
} |
| 138 |
int index = pcGroup->findChild(it->second); |
| 139 |
if (index >= 0) { |
| 140 |
auto node = static_cast<SoFCPathAnnotation*>(it->second); |
| 141 |
SoSelectionElementAction action( |
| 142 |
node->getDetail() ? SoSelectionElementAction::Remove : SoSelectionElementAction::None, |
| 143 |
true |
| 144 |
); |
| 145 |
auto path = node->getPath(); |
| 146 |
SoTempPath tmpPath(2 + (path ? path->getLength() : 0)); |
| 147 |
tmpPath.ref(); |
| 148 |
tmpPath.append(pcGroup); |
| 149 |
tmpPath.append(node); |
| 150 |
tmpPath.append(node->getPath()); |
| 151 |
action.setElement(node->getDetail()); |
| 152 |
action.apply(&tmpPath); |
| 153 |
tmpPath.unrefNoDelete(); |
| 154 |
pcGroup->removeChild(index); |
| 155 |
FC_LOG("remove annotation " << Reason.Type << " " << key); |
| 156 |
} |
| 157 |
else { |
| 158 |
FC_LOG("remove annotation object " << Reason.Type << " " << key); |
| 159 |
} |
| 160 |
objs.erase(it); |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
auto& objs = Reason.Type == SelectionChanges::SetPreselect ? objectsOnTopPreSel : objectsOnTop; |
| 165 |
auto pcGroup = Reason.Type == SelectionChanges::SetPreselect ? pcGroupOnTopPreSel |
| 166 |
: pcGroupOnTopSel; |
| 167 |
|
| 168 |
if (objs.find(key.c_str()) != objs.end()) { |
| 169 |
return; |
| 170 |
} |
| 171 |
auto vp = freecad_cast<ViewProviderDocumentObject*>(Application::Instance->getViewProvider(obj)); |
| 172 |
if (!vp || !vp->isSelectable() || !vp->isShow()) { |
| 173 |
return; |
| 174 |
} |
| 175 |
auto svp = vp; |
| 176 |
if (subname && *subname) { |
| 177 |
auto sobj = obj->getSubObject(subname); |
| 178 |
if (!sobj || !sobj->isAttachedToDocument()) { |
| 179 |
return; |
| 180 |
} |
| 181 |
if (sobj != obj) { |
| 182 |
svp = freecad_cast<ViewProviderDocumentObject*>( |
| 183 |
Application::Instance->getViewProvider(sobj) |
| 184 |
); |
| 185 |
if (!svp || !svp->isSelectable()) { |
| 186 |
return; |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
int onTop; |
| 191 |
// onTop==2 means on top only if whole object is selected, |
| 192 |
// onTop==3 means on top only if some sub-element is selected |
| 193 |
// onTop==1 means either |
| 194 |
if (vp->OnTopWhenSelected.getValue()) { |
| 195 |
onTop = vp->OnTopWhenSelected.getValue(); |
| 196 |
} |
| 197 |
else { |
| 198 |
onTop = svp->OnTopWhenSelected.getValue(); |
| 199 |
} |
| 200 |
if (Reason.Type == SelectionChanges::SetPreselect) { |
| 201 |
SoHighlightElementAction action; |
| 202 |
action.setHighlighted(true); |
| 203 |
action.setColor(selectionRoot->colorHighlight.getValue()); |
| 204 |
action.apply(pcGroupOnTopPreSel); |
| 205 |
if (!onTop) { |
| 206 |
onTop = 2; |
| 207 |
} |
| 208 |
} |
| 209 |
else { |
| 210 |
if (!onTop) { |
| 211 |
return; |
| 212 |
} |
| 213 |
SoSelectionElementAction action(SoSelectionElementAction::All); |
| 214 |
action.setColor(selectionRoot->colorHighlight.getValue()); |
| 215 |
action.apply(pcGroupOnTopSel); |
| 216 |
} |
| 217 |
if (onTop == 2 || onTop == 3) { |
| 218 |
if (subname && *subname) { |
| 219 |
size_t len = strlen(subname); |
| 220 |
if (subname[len - 1] == '.') { |
| 221 |
// ending with '.' means whole object selection |
| 222 |
if (onTop == 3) { |
| 223 |
return; |
| 224 |
} |
| 225 |
} |
| 226 |
else if (onTop == 2) { |
| 227 |
return; |
| 228 |
} |
| 229 |
} |
| 230 |
else if (onTop == 3) { |
| 231 |
return; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
std::vector<ViewProvider*> groups; |
| 236 |
auto grpVp = vp; |
| 237 |
std::set<ViewProvider*> visited; |
| 238 |
for (auto childVp = vp;; childVp = grpVp) { |
| 239 |
auto grp = App::GeoFeatureGroupExtension::getGroupOfObject(childVp->getObject()); |
| 240 |
if (!grp || !grp->isAttachedToDocument()) { |
| 241 |
break; |
| 242 |
} |
| 243 |
|
| 244 |
grpVp = freecad_cast<ViewProviderDocumentObject*>(Application::Instance->getViewProvider(grp)); |
| 245 |
if (!grpVp) { |
| 246 |
break; |
| 247 |
} |
| 248 |
|
| 249 |
// avoid endless-loops |
| 250 |
if (!visited.insert(childVp).second) { |
| 251 |
break; |
| 252 |
} |
| 253 |
|
| 254 |
auto childRoot = grpVp->getChildRoot(); |
| 255 |
auto modeSwitch = grpVp->getModeSwitch(); |
| 256 |
auto idx = modeSwitch->whichChild.getValue(); |
| 257 |
if (idx < 0 || idx >= modeSwitch->getNumChildren() || modeSwitch->getChild(idx) != childRoot) { |
| 258 |
FC_LOG( |
| 259 |
"skip " << obj->getFullName() << '.' << (subname ? subname : "") |
| 260 |
<< ", hidden inside geo group" |
| 261 |
); |
| 262 |
return; |
| 263 |
} |
| 264 |
if (childRoot->findChild(childVp->getRoot()) < 0) { |
| 265 |
FC_LOG( |
| 266 |
"cannot find '" << childVp->getObject()->getFullName() << "' in geo group '" |
| 267 |
<< grp->getNameInDocument() << "'" |
| 268 |
); |
| 269 |
break; |
| 270 |
} |
| 271 |
groups.push_back(grpVp); |
| 272 |
} |
| 273 |
|
| 274 |
SoTempPath path(10); |
| 275 |
path.ref(); |
| 276 |
|
| 277 |
for (auto it = groups.rbegin(); it != groups.rend(); ++it) { |
| 278 |
auto grpVp = *it; |
| 279 |
path.append(grpVp->getRoot()); |
| 280 |
path.append(grpVp->getModeSwitch()); |
| 281 |
path.append(grpVp->getChildRoot()); |
| 282 |
} |
| 283 |
|
| 284 |
SoDetail* det = nullptr; |
| 285 |
if (vp->getDetailPath(subname, &path, true, det) && path.getLength()) { |
| 286 |
auto node = new SoFCPathAnnotation; |
| 287 |
node->setPath(&path); |
| 288 |
pcGroup->addChild(node); |
| 289 |
if (det) { |
| 290 |
SoSelectionElementAction action(SoSelectionElementAction::Append, true); |
| 291 |
action.setElement(det); |
| 292 |
SoTempPath tmpPath(path.getLength() + 2); |
| 293 |
tmpPath.ref(); |
| 294 |
tmpPath.append(pcGroup); |
| 295 |
tmpPath.append(node); |
| 296 |
tmpPath.append(&path); |
| 297 |
action.apply(&tmpPath); |
| 298 |
tmpPath.unrefNoDelete(); |
| 299 |
node->setDetail(det); |
| 300 |
det = nullptr; |
| 301 |
} |
| 302 |
FC_LOG("add annotation " << Reason.Type << " " << key); |
| 303 |
objs[key.c_str()] = node; |
| 304 |
} |
| 305 |
delete det; |
| 306 |
path.unrefNoDelete(); |
| 307 |
} |
| 308 |
|
| 309 |
void View3DInventorSelection::clearGroupOnTop() |
| 310 |
{ |
| 311 |
if (!objectsOnTop.empty() || !objectsOnTopPreSel.empty()) { |
| 312 |
objectsOnTop.clear(); |
| 313 |
objectsOnTopPreSel.clear(); |
| 314 |
SoSelectionElementAction action(SoSelectionElementAction::None, true); |
| 315 |
action.apply(pcGroupOnTopPreSel); |
| 316 |
action.apply(pcGroupOnTopSel); |
| 317 |
coinRemoveAllChildren(pcGroupOnTopSel); |
| 318 |
coinRemoveAllChildren(pcGroupOnTopPreSel); |
| 319 |
FC_LOG("clear annotation"); |
| 320 |
} |
| 321 |
} |