| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2008 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/SoSeparator.h> |
| 25 |
#include <Inventor/nodes/SoDrawStyle.h> |
| 26 |
#include <Inventor/nodes/SoPickStyle.h> |
| 27 |
#include <Inventor/nodes/SoCoordinate3.h> |
| 28 |
#include <QHeaderView> |
| 29 |
#include <QTextStream> |
| 30 |
|
| 31 |
|
| 32 |
#include <Inventor/actions/SoGetBoundingBoxAction.h> |
| 33 |
|
| 34 |
#include "SceneInspector.h" |
| 35 |
#include "ui_SceneInspector.h" |
| 36 |
#include "Application.h" |
| 37 |
#include "Document.h" |
| 38 |
#include "View3DInventor.h" |
| 39 |
#include "View3DInventorViewer.h" |
| 40 |
#include "ViewProviderDocumentObject.h" |
| 41 |
#include "MainWindow.h" |
| 42 |
|
| 43 |
#include <limits> |
| 44 |
|
| 45 |
|
| 46 |
using namespace Gui::Dialog; |
| 47 |
|
| 48 |
/* TRANSLATOR Gui::Dialog::SceneModel */ |
| 49 |
|
| 50 |
SceneModel::SceneModel(QObject* parent) |
| 51 |
: QStandardItemModel(parent) |
| 52 |
{} |
| 53 |
|
| 54 |
enum class Column : std::int8_t |
| 55 |
{ |
| 56 |
INVENTOR_TREE = 0, |
| 57 |
NAME = 1, |
| 58 |
MEMORY_ADDRESS = 2, |
| 59 |
DATA = 3, |
| 60 |
RENDER_CACHING = 4, |
| 61 |
BOUNDING_BOX_CACHING = 5, |
| 62 |
BOUNDING_BOX = 6, |
| 63 |
COUNT = 7, |
| 64 |
}; |
| 65 |
|
| 66 |
SceneModel::~SceneModel() = default; |
| 67 |
|
| 68 |
int SceneModel::columnCount(const QModelIndex& parent) const |
| 69 |
{ |
| 70 |
Q_UNUSED(parent); |
| 71 |
return static_cast<int>(Column::COUNT); |
| 72 |
} |
| 73 |
|
| 74 |
Qt::ItemFlags SceneModel::flags(const QModelIndex& index) const |
| 75 |
{ |
| 76 |
return QAbstractItemModel::flags(index); |
| 77 |
} |
| 78 |
|
| 79 |
QVariant SceneModel::headerData(int section, Qt::Orientation orientation, int role) const |
| 80 |
{ |
| 81 |
if (orientation == Qt::Horizontal) { |
| 82 |
if (role != Qt::DisplayRole) { |
| 83 |
return {}; |
| 84 |
} |
| 85 |
|
| 86 |
switch ((Column)section) { |
| 87 |
case Column::INVENTOR_TREE: |
| 88 |
return tr("Inventor Tree"); |
| 89 |
case Column::NAME: |
| 90 |
return tr("Name"); |
| 91 |
case Column::MEMORY_ADDRESS: |
| 92 |
return tr("Address"); |
| 93 |
case Column::DATA: |
| 94 |
return tr("Data"); |
| 95 |
case Column::RENDER_CACHING: |
| 96 |
return tr("Render Cache"); |
| 97 |
case Column::BOUNDING_BOX_CACHING: |
| 98 |
return tr("Bounds Cache"); |
| 99 |
case Column::BOUNDING_BOX: |
| 100 |
return tr("Bounds"); |
| 101 |
default: |
| 102 |
assert(0 && "Not handled yet"); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
return {}; |
| 107 |
} |
| 108 |
|
| 109 |
bool SceneModel::setHeaderData(int, Qt::Orientation, const QVariant&, int) |
| 110 |
{ |
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
void SceneModel::setNode(SoNode* node) |
| 115 |
{ |
| 116 |
this->clear(); |
| 117 |
this->setHeaderData(0, Qt::Horizontal, tr("Nodes"), Qt::DisplayRole); |
| 118 |
|
| 119 |
this->insertColumns(0, static_cast<int>(Column::COUNT)); |
| 120 |
this->insertRows(0, 1); |
| 121 |
setNode(this->index(0, 0), node); |
| 122 |
} |
| 123 |
|
| 124 |
static std::string_view formatSoSwitchValue(int32_t value) |
| 125 |
{ |
| 126 |
switch (value) { |
| 127 |
case SO_SWITCH_NONE: |
| 128 |
return {"None"}; |
| 129 |
case SO_SWITCH_INHERIT: |
| 130 |
return {"Inherit"}; |
| 131 |
case SO_SWITCH_ALL: |
| 132 |
return {"All"}; |
| 133 |
default: |
| 134 |
return {"Child"}; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
static std::string_view formatSoSeparatorCacheEnabled(int32_t value) |
| 139 |
{ |
| 140 |
switch (value) { |
| 141 |
case SoSeparator::OFF: |
| 142 |
return {"Off"}; |
| 143 |
case SoSeparator::ON: |
| 144 |
return {"On"}; |
| 145 |
case SoSeparator::AUTO: |
| 146 |
return {"Auto"}; |
| 147 |
default: |
| 148 |
throw Base::ValueError(); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
static std::string_view formatSoDrawStyleElement(int32_t value) |
| 153 |
{ |
| 154 |
switch (value) { |
| 155 |
case SoDrawStyleElement::FILLED: |
| 156 |
return {"Filled"}; |
| 157 |
case SoDrawStyleElement::LINES: |
| 158 |
return {"Lines"}; |
| 159 |
case SoDrawStyleElement::POINTS: |
| 160 |
return {"Points"}; |
| 161 |
case SoDrawStyleElement::INVISIBLE: |
| 162 |
return {"Invisible"}; |
| 163 |
default: |
| 164 |
throw Base::ValueError(); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
static std::string_view formatSoPickStyleElement(int32_t value) |
| 169 |
{ |
| 170 |
switch (value) { |
| 171 |
case SoPickStyleElement::SHAPE: |
| 172 |
return {"Shape"}; |
| 173 |
case SoPickStyleElement::BOUNDING_BOX: |
| 174 |
return {"BoundingBox"}; |
| 175 |
case SoPickStyleElement::UNPICKABLE: |
| 176 |
return {"Unpickable"}; |
| 177 |
case SoPickStyleElement::SHAPE_ON_TOP: |
| 178 |
return {"ShapeOnTop"}; |
| 179 |
case SoPickStyleElement::BOUNDING_BOX_ON_TOP: |
| 180 |
return {"BoundingBoxOnTop"}; |
| 181 |
case SoPickStyleElement::SHAPE_FRONTFACES: |
| 182 |
return {"ShapeFrontFaces"}; |
| 183 |
default: |
| 184 |
throw Base::ValueError(); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
static inline bool isAtFloatValueLimit(float v) |
| 189 |
{ |
| 190 |
return v == std::numeric_limits<float>::lowest() || v == std::numeric_limits<float>::max(); |
| 191 |
} |
| 192 |
|
| 193 |
static bool isInfinite(const SbVec3f& vec) |
| 194 |
{ |
| 195 |
float x, y, z; // NOLINT |
| 196 |
vec.getValue(x, y, z); |
| 197 |
return isAtFloatValueLimit(x) && isAtFloatValueLimit(y) && isAtFloatValueLimit(z); |
| 198 |
} |
| 199 |
|
| 200 |
static std::string formatSbXfBox3f(const SbXfBox3f& box) |
| 201 |
{ |
| 202 |
SbVec3f minpoint; |
| 203 |
SbVec3f maxpoint; |
| 204 |
box.getBounds(minpoint, maxpoint); |
| 205 |
|
| 206 |
if (isInfinite(minpoint) && isInfinite(maxpoint)) { |
| 207 |
return {"Infinite"}; |
| 208 |
} |
| 209 |
|
| 210 |
float minx, miny, minz; // NOLINT |
| 211 |
minpoint.getValue(minx, miny, minz); |
| 212 |
|
| 213 |
float maxx, maxy, maxz; // NOLINT |
| 214 |
maxpoint.getValue(maxx, maxy, maxz); |
| 215 |
|
| 216 |
return fmt::format( |
| 217 |
"Min: ({:.3},{:.3},{:.3}), Max: ({:.3},{:.3},{:.3})", |
| 218 |
minx, |
| 219 |
miny, |
| 220 |
minz, |
| 221 |
maxx, |
| 222 |
maxy, |
| 223 |
maxz |
| 224 |
); |
| 225 |
} |
| 226 |
|
| 227 |
void SceneModel::setNode(QModelIndex index, SoNode* node) |
| 228 |
{ |
| 229 |
this->setData( |
| 230 |
index.siblingAtColumn(static_cast<int>(Column::INVENTOR_TREE)), |
| 231 |
QVariant(QString::fromLatin1(QByteArray(node->getTypeId().getName()))) |
| 232 |
); |
| 233 |
|
| 234 |
QHash<SoNode*, QString>::iterator it = nodeNames.find(node); |
| 235 |
const QString name { |
| 236 |
(it != nodeNames.end()) ? it.value() : QString::fromLatin1(QByteArray(node->getName())) |
| 237 |
}; |
| 238 |
this->setData(index.siblingAtColumn(static_cast<int>(Column::NAME)), QVariant(name)); |
| 239 |
|
| 240 |
this->setData( |
| 241 |
index.siblingAtColumn(static_cast<int>(Column::MEMORY_ADDRESS)), |
| 242 |
QVariant(QString::fromStdString(fmt::format("{}", (void*)node))) |
| 243 |
); |
| 244 |
|
| 245 |
auto view = qobject_cast<View3DInventor*>(getMainWindow()->activeWindow()); |
| 246 |
auto vp = view->getViewer()->getSoRenderManager()->getViewportRegion(); |
| 247 |
|
| 248 |
QString data; |
| 249 |
QTextStream stream(&data); |
| 250 |
if (static_cast<bool>(node->isOfType(SoSwitch::getClassTypeId()))) { |
| 251 |
auto pcSwitch = static_cast<SoSwitch*>(node); |
| 252 |
auto value = pcSwitch->whichChild.getValue(); |
| 253 |
stream << fmt::format("Which: {} ({})", formatSoSwitchValue(value), value).c_str(); |
| 254 |
} |
| 255 |
else if (static_cast<bool>(node->isOfType(SoSeparator::getClassTypeId()))) { |
| 256 |
auto pcSeparator = static_cast<SoSeparator*>(node); |
| 257 |
|
| 258 |
auto renderCaching = pcSeparator->renderCaching.getValue(); |
| 259 |
this->setData( |
| 260 |
index.siblingAtColumn(static_cast<int>(Column::RENDER_CACHING)), |
| 261 |
QVariant(QString::fromStdString(std::string {formatSoSeparatorCacheEnabled(renderCaching)})) |
| 262 |
); |
| 263 |
|
| 264 |
auto boundingBoxCaching = pcSeparator->boundingBoxCaching.getValue(); |
| 265 |
this->setData( |
| 266 |
index.siblingAtColumn(static_cast<int>(Column::BOUNDING_BOX_CACHING)), |
| 267 |
QVariant( |
| 268 |
QString::fromStdString(std::string {formatSoSeparatorCacheEnabled(boundingBoxCaching)}) |
| 269 |
) |
| 270 |
); |
| 271 |
|
| 272 |
SoGetBoundingBoxAction getBBox(vp); |
| 273 |
getBBox.apply(pcSeparator); |
| 274 |
this->setData( |
| 275 |
index.siblingAtColumn(static_cast<int>(Column::BOUNDING_BOX)), |
| 276 |
QVariant(QString::fromStdString(formatSbXfBox3f(getBBox.getXfBoundingBox()))) |
| 277 |
); |
| 278 |
} |
| 279 |
else if (static_cast<bool>(node->isOfType(SoDrawStyle::getClassTypeId()))) { |
| 280 |
auto pcDrawStyle = static_cast<SoDrawStyle*>(node); |
| 281 |
auto value = pcDrawStyle->style.getValue(); |
| 282 |
stream << fmt::format("Style: {} ({})", formatSoDrawStyleElement(value), value).c_str(); |
| 283 |
} |
| 284 |
else if (static_cast<bool>(node->isOfType(SoPickStyle::getClassTypeId()))) { |
| 285 |
auto pcPickStyle = static_cast<SoPickStyle*>(node); |
| 286 |
auto value = pcPickStyle->style.getValue(); |
| 287 |
stream << fmt::format("Style: {} ({})", formatSoPickStyleElement(value), value).c_str(); |
| 288 |
} |
| 289 |
else if (static_cast<bool>(node->isOfType(SoCoordinate3::getClassTypeId()))) { |
| 290 |
auto pcCoords = static_cast<SoCoordinate3*>(node); |
| 291 |
auto values = pcCoords->point.getValues(0); |
| 292 |
if (values) { |
| 293 |
float x {0}; |
| 294 |
float y {0}; |
| 295 |
float z {0}; |
| 296 |
values->getValue(x, y, z); |
| 297 |
stream << fmt::format("XYZ: {}, {}, {}", x, y, z).c_str(); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
this->setData(index.siblingAtColumn((int)Column::DATA), QVariant(data)); |
| 302 |
|
| 303 |
if (static_cast<bool>(node->getTypeId().isDerivedFrom(SoGroup::getClassTypeId()))) { |
| 304 |
auto group = static_cast<SoGroup*>(node); |
| 305 |
this->insertColumns(0, static_cast<int>(Column::COUNT), index); |
| 306 |
this->insertRows(0, group->getNumChildren(), index); |
| 307 |
for (int i = 0; i < group->getNumChildren(); i++) { |
| 308 |
SoNode* child = group->getChild(i); |
| 309 |
setNode(this->index(i, 0, index), child); |
| 310 |
} |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
void SceneModel::setNodeNames(const QHash<SoNode*, QString>& names) |
| 315 |
{ |
| 316 |
nodeNames = names; |
| 317 |
} |
| 318 |
|
| 319 |
// -------------------------------------------------------- |
| 320 |
|
| 321 |
/* TRANSLATOR Gui::Dialog::DlgInspector */ |
| 322 |
|
| 323 |
DlgInspector::DlgInspector(QWidget* parent, Qt::WindowFlags fl) |
| 324 |
: QDialog(parent, fl) |
| 325 |
, ui(new Ui_SceneInspector()) |
| 326 |
{ |
| 327 |
ui->setupUi(this); |
| 328 |
connect(ui->refreshButton, &QPushButton::clicked, this, &DlgInspector::onRefreshButtonClicked); |
| 329 |
setWindowTitle(tr("Scene Inspector")); |
| 330 |
|
| 331 |
auto model = new SceneModel(this); |
| 332 |
ui->treeView->setModel(model); |
| 333 |
ui->treeView->setRootIsDecorated(true); |
| 334 |
} |
| 335 |
|
| 336 |
/* |
| 337 |
* Destroys the object and frees any allocated resources |
| 338 |
*/ |
| 339 |
DlgInspector::~DlgInspector() |
| 340 |
{ |
| 341 |
// no need to delete child widgets, Qt does it all for us |
| 342 |
delete ui; |
| 343 |
} |
| 344 |
|
| 345 |
void DlgInspector::setDocument(Gui::Document* doc) |
| 346 |
{ |
| 347 |
setNodeNames(doc); |
| 348 |
|
| 349 |
auto view = qobject_cast<View3DInventor*>(doc->getActiveView()); |
| 350 |
if (view) { |
| 351 |
View3DInventorViewer* viewer = view->getViewer(); |
| 352 |
setNode(viewer->getSceneGraph()); |
| 353 |
ui->treeView->expandToDepth(3); |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
void DlgInspector::setNode(SoNode* node) |
| 358 |
{ |
| 359 |
auto model = static_cast<SceneModel*>(ui->treeView->model()); |
| 360 |
model->setNode(node); |
| 361 |
|
| 362 |
QHeaderView* header = ui->treeView->header(); |
| 363 |
header->setSectionResizeMode(static_cast<int>(Column::INVENTOR_TREE), QHeaderView::Interactive); |
| 364 |
header->resizeSection(static_cast<int>(Column::INVENTOR_TREE), 300); |
| 365 |
header->setSectionResizeMode(static_cast<int>(Column::NAME), QHeaderView::Interactive); |
| 366 |
header->resizeSection(static_cast<int>(Column::NAME), 200); |
| 367 |
header->setSectionResizeMode(static_cast<int>(Column::MEMORY_ADDRESS), QHeaderView::Interactive); |
| 368 |
header->resizeSection(static_cast<int>(Column::MEMORY_ADDRESS), 140); |
| 369 |
header->setSectionResizeMode(static_cast<int>(Column::DATA), QHeaderView::Interactive); |
| 370 |
header->resizeSection(static_cast<int>(Column::DATA), 200); |
| 371 |
|
| 372 |
header->setSectionsMovable(false); |
| 373 |
} |
| 374 |
|
| 375 |
void DlgInspector::setNodeNames(Gui::Document* doc) |
| 376 |
{ |
| 377 |
std::vector<Gui::ViewProvider*> vps = doc->getViewProvidersOfType( |
| 378 |
Gui::ViewProviderDocumentObject::getClassTypeId() |
| 379 |
); |
| 380 |
QHash<SoNode*, QString> nodeNames; |
| 381 |
for (const auto& it : vps) { |
| 382 |
auto vp = static_cast<Gui::ViewProviderDocumentObject*>(it); |
| 383 |
App::DocumentObject* obj = vp->getObject(); |
| 384 |
if (obj) { |
| 385 |
QString label = QString::fromUtf8(obj->Label.getValue()); |
| 386 |
nodeNames[vp->getRoot()] = label; |
| 387 |
} |
| 388 |
|
| 389 |
std::vector<std::string> modes = vp->getDisplayMaskModes(); |
| 390 |
for (const auto& mode : modes) { |
| 391 |
SoNode* node = vp->getDisplayMaskMode(mode.c_str()); |
| 392 |
if (node) { |
| 393 |
nodeNames[node] = QString::fromStdString(mode); |
| 394 |
} |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
auto model = static_cast<SceneModel*>(ui->treeView->model()); |
| 399 |
model->setNodeNames(nodeNames); |
| 400 |
} |
| 401 |
|
| 402 |
void DlgInspector::changeEvent(QEvent* e) |
| 403 |
{ |
| 404 |
if (e->type() == QEvent::LanguageChange) { |
| 405 |
ui->retranslateUi(this); |
| 406 |
setWindowTitle(tr("Scene Inspector")); |
| 407 |
} |
| 408 |
QDialog::changeEvent(e); |
| 409 |
} |
| 410 |
|
| 411 |
void DlgInspector::onRefreshButtonClicked() |
| 412 |
{ |
| 413 |
Gui::Document* doc = Application::Instance->activeDocument(); |
| 414 |
if (doc) { |
| 415 |
setNodeNames(doc); |
| 416 |
|
| 417 |
auto view = qobject_cast<View3DInventor*>(doc->getActiveView()); |
| 418 |
if (view) { |
| 419 |
View3DInventorViewer* viewer = view->getViewer(); |
| 420 |
setNode(viewer->getSceneGraph()); |
| 421 |
ui->treeView->expandToDepth(3); |
| 422 |
} |
| 423 |
} |
| 424 |
else { |
| 425 |
auto model = static_cast<SceneModel*>(ui->treeView->model()); |
| 426 |
model->clear(); |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
#include "moc_SceneInspector.cpp" |