1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
|
/*****************************************************************************
* $CAMITK_LICENCE_BEGIN$
*
* CamiTK - Computer Assisted Medical Intervention ToolKit
* (c) 2001-2025 Univ. Grenoble Alpes, CNRS, Grenoble INP - UGA, TIMC, 38000 Grenoble, France
*
* Visit http://camitk.imag.fr for more information
*
* This file is part of CamiTK.
*
* CamiTK is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* CamiTK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with CamiTK. If not, see <http://www.gnu.org/licenses/>.
*
* $CAMITK_LICENCE_END$
****************************************************************************/
// -- Core stuff
#include "Component.h"
#include "Application.h"
#include "Viewer.h"
#include "Property.h"
#include "Action.h"
#include "PersistenceManager.h"
#include "TransformationManager.h"
#include "AbortException.h"
#include "Log.h"
// -- QT stuff
#include <QEvent>
#include <QMetaObject>
#include <QSet>
#include <utility>
// -- Vtk stuff
#include <vtkTextProperty.h>
#include <vtkCaptionActor2D.h>
#include <vtkTextActor.h>
#include <vtkProperty2D.h>
// -- Std stuff
#include <algorithm>
#include <cmath>
#include <utility> // as_const
namespace camitk {
// -------------------- constructor --------------------
Component::Component(QString file, QString n, Representation s, bool createDefaultFrame) : myParentNode(nullptr), myFileName(std::move(file)), myService(s) {
init(n, createDefaultFrame);
}
Component::Component(Component* parentComponent, const QString& n, Representation s, bool createDefaultFrame) : myParentNode(parentComponent), myService(s) {
if (myParentNode == nullptr) {
throw AbortException(tr("Inconsistency: cannot instantiate a sub component with a null parent, please use the parent component pointer as the first parameter of the constructor or use the top-level Component constructor.").toStdString());
}
init(n, createDefaultFrame);
myFileName = n;
// register as a parent child
myParentNode->addChild(this);
}
// -------------------- default destructor --------------------
Component::~Component() {
// delete all reference of this component in the application
Application::removeComponent(this);
// delete all its children Components
deleteChildren();
// unregister if I got a parent component
if (Component* parent = getParentComponent()) {
parent->removeChild(this);
}
// erase all the viewers
myViewers.clear();
// clear the adaptee
if (myGeometry) {
delete myGeometry;
}
myGeometry = nullptr;
if (mySlice) {
delete mySlice;
}
mySlice = nullptr;
// remove from selection
if (isSelectedFlag) {
// Beware that this calls the method 'Component::setSelected' (as define in this class)
// which bypass any redefined setSelected method in a derived class
Component::setSelected(false, false);
}
// delete all properties
for (auto prop : std::as_const(propertyMap)) {
delete prop;
}
propertyMap.clear();
if (actionsMenu != nullptr) {
actionsMenu->clear();
// deleteLater() is required instead of a simple delete to avoid crash
// Explanation:
// This menu is a context menu of the Explorer Viewer.
// At one point the user can right-click on this Component node representation in the explorer,
// which will run the exec() method of this actionsMenu.
// Now, during this exec() call, if the user selects the "Close" action, the processor
// will arrive in this destructor.
// If we leave a classic "delete" here, this will delete the currently opened
// context menu and free all the memory, while it is still being currently in its exec()
// This might crash the event loop (which runs in another thread) as it is still using the object.
// deleteLater() is safe as it will handle by the event loop later on.
actionsMenu->deleteLater();
actionsMenu = nullptr;
}
}
// -------------------- init --------------------
void Component::init(const QString& name, bool createDefaultFrame) {
modifiedFlag = false;
interfaceNodeModifiedFlag = false;
childrenComponent.clear();
// initialize representation
myGeometry = nullptr;
mySlice = nullptr;
actionsMenu = nullptr;
visibilityMenu = nullptr;
frameActors.clear();
frameVisibilities.clear();
// initialize name property
Component::addProperty(new Property("Name", name, tr("Name of the Component"), ""));
// initialize other state
isSelectedFlag = false;
// insert the Component in the list of top-level components as well as the full list
Application::addComponent(this);
// create the default Frame (does not depend on any representation, should be created for any Component)
if (createDefaultFrame) {
Component::setFrame(TransformationManager::addFrameOfReference(name, name));
}
else {
Component::setFrame(nullptr);
}
// install a filter to get the modification of the dynamic properties (see event method)
installEventFilter(this);
// by default, the 1st PropertyExplorer tab is displayed
setIndexOfPropertyExplorerTab(0);
}
// ---------------------- event --------------------
bool Component::event(QEvent* e) {
if (e->type() == QEvent::DynamicPropertyChange) {
e->accept();
QDynamicPropertyChangeEvent* changeEvent = dynamic_cast<QDynamicPropertyChangeEvent*>(e);
if (!changeEvent) {
return false;
}
// notify the instance that the corresponding property was changed by calling the propertyValueChanged method
// which should be overriden in the derived classes if needed
// warning: to avoid never ending notification loops, do this only if signals are not blocked
if (!signalsBlocked()) {
propertyValueChanged(changeEvent->propertyName());
}
return true;
}
return QObject::event(e);
}
// -------------------- setName --------------------
void Component::setName(const QString& name) {
// just modify the property, all the required updates are managed in propertyValueChanged
setPropertyValue("Name", name);
}
// -------------------- isTopLevel --------------------
bool Component::isTopLevel() const {
return (myParentNode == nullptr);
}
// -------------------- getParentComponent --------------------
Component* Component::getParentComponent() {
return (Component*)myParentNode;
}
// -------------------- getTopLevelComponent --------------------
Component* Component::getTopLevelComponent() {
if (!isTopLevel()) {
return getParentComponent()->getTopLevelComponent();
}
else {
return this;
}
}
// -------------------- setVisibility --------------------
void Component::setVisibility(QString viewerName, bool b) {
// first check if viewer is registered
Viewer* v = Application::getViewer(viewerName);
// only insert registered viewers
if (v != nullptr) {
auto it = myViewers.find(viewerName);
if (it == myViewers.end()) {
// insert the new viewer with the corresponding boolean
myViewers.insert(viewerName, b);
}
else {
it.value() = b;
}
}
}
// -------------------- getVisibility --------------------
bool Component::getVisibility(QString viewerName) const {
auto it = myViewers.constFind(viewerName);
if (it == myViewers.end()) {
return false;
}
else {
return it.value();
}
}
// -------------------- refresh --------------------
void Component::refresh() {
for (auto it = myViewers.begin(); it != myViewers.end(); it++) {
if (it.value()) {
Application::getViewer(it.key())->refresh();
}
}
setNodeModified(false);
}
// -------------------- getRepresentation --------------------
Component::Representation Component::getRepresentation() const {
static bool checked = false; // only check once for each Component
if (!checked) {
checked = true;
QString representationString;
QString instantiatedMember;
QString shouldInstantiateMember;
bool instantiationError = false;
// check the service
switch (myService) {
case GEOMETRY:
representationString = "GEOMETRY";
shouldInstantiateMember = "myGeometry";
instantiationError = (myGeometry == nullptr);
break;
case SLICE:
representationString = "SLICE";
shouldInstantiateMember = "mySlice";
instantiationError = (mySlice == nullptr);
break;
case NO_REPRESENTATION:
instantiatedMember = (myGeometry != nullptr) ? "myGeometry" : (mySlice != nullptr) ? "mySlice" : "<<INTERNAL_ERROR>>";
instantiationError = (mySlice != nullptr || myGeometry != nullptr);
break;
}
if (instantiationError) {
const QMetaObject* qmetaObject = metaObject();
QString classname = qmetaObject->className();
if (myService != NO_REPRESENTATION) {
// the Component has to instantiate
CAMITK_ERROR(tr("Component class \"%1\" implements service %2 but does not instantiate it!\n"
"\tPlease instantiate %3 in initRepresentation() or change the implemented representation in the constructor.").arg(classname, representationString, shouldInstantiateMember))
}
else {
// no representation should be instantiated!
CAMITK_ERROR(tr("Component class \"%1\" has no implemented representation (NO_REPRESENTATION) but %2 is instantiated. Please do not instantiate %3 in initRepresentation() or change the implemented service in the constructor.").arg(classname, instantiatedMember, instantiatedMember))
}
}
}
return myService;
}
// ---------------- actionLessThan ----------------
extern bool actionLessThan(const camitk::Action* a1, const camitk::Action* a2);
// -------------------- getActionMenu -----------------------
QMenu* Component::getActionMenu() {
ActionList allActions = Application::getActions(this);
if (actionsMenu == nullptr) {
actionsMenu = new QMenu();
//-- add all actions sorted by family
QMap<QString, ActionSet> familyMap;
// group actions by family
for (Action* action : std::as_const(allActions)) {
familyMap[action->getFamily().toLower()].insert(action);
}
//-- create the "File" and "View" submenu first
QMenu* fileMenu = actionsMenu->addMenu("File");
QMenu* viewMenu = actionsMenu->addMenu("View");
//-- create one sub menu per family (unless there is only one action)
for (const QSet<Action*>& familySet : std::as_const(familyMap)) {
// sort actions by name
ActionList familyList = familySet.values();
std::sort(familyList.begin(), familyList.end(), actionLessThan);
if (familyList.size() >= 1) {
QMenu* familyMenu;
if (familyList.first()->getFamily() == "File") {
familyMenu = fileMenu;
}
else {
if (familyList.first()->getFamily() == "View") {
familyMenu = viewMenu;
}
else {
// this is a new family
familyMenu = actionsMenu->addMenu(familyList.first()->getFamily());
}
}
for (Action* action : familyList) {
// ownership not taken here
familyMenu->addAction(action->getQAction(this));
}
}
else {
// ownership not taken here
actionsMenu->addAction(familyList.first()->getQAction(this));
}
}
// add the visibility submenu
visibilityMenu = viewMenu->addMenu("Viewer Visibility");
}
//-- update viewer visibility and all other actions that are checkable
for (Action* a : std::as_const(allActions)) {
a->getQAction(this);
}
visibilityMenu->clear();
for (const Viewer* viewer : Application::getViewers(this)) {
// show only embedded or stacked viewers
if (viewer->getType() != Viewer::DOCKED) {
// viewerAction is own by visibilityMenu, visibilityMenu.clear() will delete it
QAction* visibilityAction = visibilityMenu->addAction(viewer->getName());
visibilityAction->setCheckable(true);
visibilityAction->setChecked(getVisibility(viewer->getName()));
visibilityAction->setIcon(viewer->getIcon());
QString tipString = "Set the visibility of this component in the \"" + viewer->getName() + "\" viewer\n(note that it might not have effect depending on the viewer)";
visibilityAction->setStatusTip(tr(tipString.toStdString().c_str()));
visibilityAction->setWhatsThis(tr(tipString.toStdString().c_str()));
// add the toggle action slot using C++11 lambda so that everything is contained inside viewMenu
connect(visibilityAction, &QAction::toggled, [ this, viewer ](bool visible) {
setVisibility(viewer->getName(), visible);
Application::refresh();
});
}
}
// no menu if no action
if (actionsMenu->actions().size() == 0) {
delete actionsMenu;
actionsMenu = nullptr;
}
return actionsMenu;
}
// -------------------- setSelected --------------------
void Component::setSelected(const bool b, const bool recursive) {
isSelectedFlag = b;
// maintain the children selection state as well
if (recursive) {
for (Component* child : childrenComponent) {
child->setSelected(b, recursive);
}
}
//-- Now add to global selection
// do that only in the end, so that the last selected will be this Component (which is the one called first!)
Application::setSelected(this, b);
}
// -------------------- getFileName --------------------
const QString Component::getFileName() const {
return myFileName;
}
// -------------------- setFileName --------------------
void Component::setFileName(const QString& fName) {
myFileName = fName;
}
// -------------------------------------------------------
//
// InterfacePersistence methods
//
// -------------------------------------------------------
// -------------------- toVariant --------------------
QVariant Component::toVariant() const {
return QVariantMap{
{"filename", getFileName()},
{"properties", PersistenceManager::fromProperties(getPropertyObject())},
{"frame", getFrame()->getUuid()},
};
}
// -------------------- fromVariant --------------------
void Component::fromVariant(const QVariant& newValues) {
QVariantMap newValuesMap = newValues.toMap();
if (newValuesMap.contains("properties")) {
PersistenceManager::loadProperties(this, newValuesMap.value("properties"));
}
if (newValuesMap.contains("frame")) {
std::shared_ptr<FrameOfReference> fr = TransformationManager::getFrameOfReferenceOwnership(newValuesMap.value("frame").toUuid());
if (fr != nullptr) {
setFrame(fr); // To update potential child components
}
else {
CAMITK_WARNING_ALT("Could not load frame for Component")
}
}
}
// -------------------- getUuid --------------------
QUuid Component::getUuid() const {
return PersistenceManager::getUuidFromProperties(this);
}
// -------------------- setUuid --------------------
bool Component::setUuid(QUuid uuid) {
return PersistenceManager::setUuidInProperties(this, uuid);
}
// -------------------------------------------------------
//
// InterfaceProperty methods
//
// -------------------------------------------------------
// -------------------- getHierarchy --------------------
QStringList Component::getHierarchy() const {
const QMetaObject* qmetaObject = metaObject();
QStringList classnameList;
while (qmetaObject && !QString(qmetaObject->className()).contains("InterfaceProperty")) {
classnameList.append(QString(qmetaObject->className()).remove(0, QString(qmetaObject->className()).lastIndexOf(':') + 1));
if (qmetaObject->superClass()) {
qmetaObject = qmetaObject->superClass();
}
}
return classnameList;
}
// -------------------- isInstanceOf --------------------
bool Component::isInstanceOf(QString className) const {
if (getHierarchy().contains(className)) {
return true;
}
else {
return false;
}
}
// ---------------------- propertyValueChanged --------------------
void Component::propertyValueChanged(QString name) {
if (name == "Name") {
updateLabel(getPropertyValue(name).toString());
setNodeModified(true);
}
else {
CAMITK_INFO(tr("Component::propertyValueChanged: The value of property \"%1\" has changed to: \"%2\"\n"
"If you want to take this change/property into account, please redefine method \"propertyValueChanged(QString name)\" in class %3").arg(name, getPropertyValue(name).toString(), metaObject()->className()))
}
}
// ---------------------- getProperty --------------------
Property* Component::getProperty(QString name) {
return propertyMap.value(name);
}
// ---------------------- addProperty --------------------
bool Component::addProperty(Property* prop) {
// from QObject documentation, section "Detailed Description":
// "To avoid never ending notification loops you can temporarily block signals with blockSignals()."
blockSignals(true);
// add a dynamic Qt Meta Object property with the same name
bool returnStatus = setProperty(prop->getName().toStdString().c_str(), prop->getInitialValue());
// add to the map
propertyMap.insert(prop->getName(), prop);
blockSignals(false);
return returnStatus;
}
// -------------------- getPropertyValue --------------------
QVariant Component::getPropertyValue(const QString& name) const {
if (!propertyMap.contains(name)) {
CAMITK_WARNING(tr("Property \"%1\" undeclared. Check the spelling.").arg(name))
return QVariant(); // invalid QVariant
}
else {
return property(name.toStdString().c_str());
}
}
// -------------------- setPropertyValue --------------------
bool Component::setPropertyValue(const QString& name, QVariant newValue) {
if (!propertyMap.contains(name)) {
CAMITK_WARNING(tr("Property \"%1\" undeclared. Check the spelling. Cannot set value to %2").arg(name).arg(newValue.toString()))
return false;
}
else {
setProperty(name.toStdString().c_str(), newValue);
return true;
}
}
// -------------------------------------------------------
//
// InterfaceNode methods
//
// -------------------------------------------------------
// -------------------- addChild --------------------
void Component::addChild(InterfaceNode* childNode) {
attachChild(childNode);
childNode->setParent(this);
setNodeModified(true);
}
// -------------------- removeChild --------------------
void Component::removeChild(InterfaceNode* childNode) {
if (childrenComponent.removeAll((Component*)childNode)) {
// remove myself from the childNode's parent item list
childNode->setParent(nullptr);
setNodeModified(true);
}
}
// -------------------- deleteChildren --------------------
void Component::deleteChildren() {
while (!childrenComponent.empty()) {
Component* childComp = childrenComponent.takeLast();
if (Application::isAlive(childComp)) {
delete childComp;
}
}
setNodeModified(true);
}
// -------------------- attachChild --------------------
void Component::attachChild(InterfaceNode* childNode) {
auto* comp = dynamic_cast<Component*>(childNode);
if (comp != nullptr) {
// add a sub item
if (!childrenComponent.contains(comp)) {
// add it to the list
childrenComponent.append(comp);
}
setNodeModified(true);
}
}
// -------------------- setParent ------------------
void Component::setParent(InterfaceNode* pes) {
if (myParentNode == pes || pes == this) {
return;
}
if (myParentNode != nullptr) {
// tell my parent I have been adopted by someone else
myParentNode->removeChild(this);
// avoid infinite recursion
myParentNode = nullptr;
}
else {
// remove this from the top-level list if needed
Application::getTopLevelComponentList().removeAll(this);
}
myParentNode = pes;
setNodeModified(true);
}
// -------------------------------------------------------
//
// InterfaceGeometry methods
//
// -------------------------------------------------------
// -------------------- getRenderingModes --------------------
const InterfaceGeometry::RenderingModes Component::getRenderingModes() const {
if (myGeometry)
// return the Rendering mode of myGeometry, is exists
{
return myGeometry->getRenderingModes();
}
else {
// else return the added rendering mode of all children
if (childrenComponent.size() > 0) {
InterfaceGeometry::RenderingModes m = InterfaceGeometry::None;
for (Component* childComponent : childrenComponent) {
m |= childComponent->getRenderingModes();
}
return m;
}
}
// Should never comes to this extremity
return None;
}
// -------------------- getActorColor --------------------
void Component::getActorColor(InterfaceGeometry::RenderingModes m, double d[4], bool ignoreEnhancedModes) const {
if (myGeometry) {
// get the color of myGeometry, is exists, and finished
myGeometry->getActorColor(m, d, ignoreEnhancedModes);
}
else {
// return the first existing Color in my children
int i = 0;
bool found = false;
while (i < childrenComponent.size() && !found) {
childrenComponent[i]->getActorColor(m, d, ignoreEnhancedModes);
found = (d[0] != 0.0 || d[1] != 0.0 || d[2] != 0.0 || d[3] != 0.0);
i++;
}
if (!found) {
for (unsigned int j = 0; j < 4; j++) {
d[j] = 0.0;
}
}
}
}
// -------------------- getBounds --------------------
void Component::getBounds(double* bounds) {
invoke1(myGeometry, getBounds, bounds)
else {
bounds[0] = bounds[2] = bounds[4] = 0.0;
bounds[1] = bounds[3] = bounds[5] = 1.0;
// compute bounds using the children's
for (Component* childComponent : childrenComponent) {
double childBounds[6]; //xmin,xmax, ymin,ymax, zmin,zmax
// get child bounds
childComponent->getBounds(childBounds);
// check compared to global bound
for (int i = 0; i < 3; i++) {
// compare min
if (bounds[i * 2] > childBounds[i * 2]) {
bounds[i * 2] = childBounds[i * 2];
}
// compare max
if (bounds[i * 2 + 1] < childBounds[i * 2 + 1]) {
bounds[i * 2 + 1] = childBounds[i * 2 + 1];
}
}
}
}
}
// -------------------- getBoundingRadius --------------------
double Component::getBoundingRadius() {
if (myGeometry) {
return myGeometry->getBoundingRadius();
}
else {
// compute bounding radius using the children's
double radius = 0.0;
for (Component* childComponent : childrenComponent) {
double childRadius = childComponent->getBoundingRadius();
if (childRadius > radius) {
radius = childRadius;
}
}
return radius;
}
}
// -------------------- setGlyphType --------------------
void Component::setGlyphType(const camitk::InterfaceGeometry::GlyphTypes type, const double size) {
if (myGeometry) {
myGeometry->setGlyphType(type, size);
}
}
// -------------------------------------------------------
//
// InterfaceBitMap methods
//
// -------------------------------------------------------
// -------------------- getNumberOfSlices --------------------
int Component::getNumberOfSlices() const {
if (mySlice) {
return mySlice->getNumberOfSlices();
}
else {
return -1;
}
}
// -------------------- getSlice --------------------
int Component::getSlice() const {
if (mySlice) {
return mySlice->getSlice();
}
else {
return -1;
}
}
// -----------------------------------------------------------------------
//
// InterfaceFrame methods
//
// -----------------------------------------------------------------------
// -------------------- getAllFrames --------------------
QMultiMap<const FrameOfReference*, Component*> Component::getAllFrames(bool includeChildrenFrames) {
QMultiMap<const FrameOfReference*, Component*> allFrames;
allFrames.insert(this->getFrame(), this);
if (includeChildrenFrames) {
for (Component* child : getChildren()) {
allFrames = child->getAllFrames() + allFrames;
}
}
return allFrames;
}
// -------------------- getAllTransformations --------------------
QMultiMap<const Transformation*, Component*> Component::getAllTransformations(bool includeChildrenTransformations) {
QMultiMap<const Transformation*, Component*> allTransformations;
if (includeChildrenTransformations) {
for (Component* child : getChildren()) {
allTransformations = child->getAllTransformations() + allTransformations;
}
}
return allTransformations;
}
// -------------------- setFrameFrom --------------------
void Component::setFrameFrom(const InterfaceFrame* fr) {
setFrame(TransformationManager::getFrameOfReferenceOwnership(fr->getFrame()));
}
// -------------------- resetFrame --------------------
void Component::resetFrame() {
setFrame(TransformationManager::addFrameOfReference(getName(), getName()));
}
// -------------------- getFrameAxisActor --------------------
vtkSmartPointer<vtkAxesActor> Component::getFrameAxisActor(QString viewerName) {
vtkSmartPointer<vtkAxesActor> frameActor = frameActors.value(viewerName, nullptr);
if (frameActor == nullptr) {
frameActor = vtkSmartPointer<vtkAxesActor>::New();
frameActor->SetShaftTypeToCylinder();
frameActor->SetXAxisLabelText("x");
frameActor->SetYAxisLabelText("y");
frameActor->SetZAxisLabelText("z");
frameActor->SetTotalLength(1.0, 1.0, 1.0); // unit length
vtkSmartPointer<vtkTextProperty> axeXTextProp = vtkSmartPointer<vtkTextProperty>::New();
axeXTextProp->SetFontSize(10);
axeXTextProp->BoldOn();
axeXTextProp->ItalicOn();
axeXTextProp->ShadowOff();
axeXTextProp->SetFontFamilyToArial();
frameActor->GetXAxisCaptionActor2D()->SetCaptionTextProperty(axeXTextProp);
// remove the autoscaling so that the font size is smaller than default autoscale
frameActor->GetXAxisCaptionActor2D()->GetTextActor()->SetTextScaleModeToNone();
// set the color
frameActor->GetXAxisCaptionActor2D()->GetCaptionTextProperty()->SetColor(0.9, 0.4, 0.4);
// make sure the label can be hidden by any geometry, like the axes
frameActor->GetXAxisCaptionActor2D()->GetProperty()->SetDisplayLocationToBackground();
vtkSmartPointer<vtkTextProperty> axeYTextProp = vtkSmartPointer<vtkTextProperty>::New();
axeYTextProp->ShallowCopy(axeXTextProp);
frameActor->GetYAxisCaptionActor2D()->SetCaptionTextProperty(axeYTextProp);
frameActor->GetYAxisCaptionActor2D()->GetTextActor()->SetTextScaleModeToNone();
frameActor->GetYAxisCaptionActor2D()->GetCaptionTextProperty()->SetColor(0.4, 0.9, 0.4);
frameActor->GetYAxisCaptionActor2D()->GetProperty()->SetDisplayLocationToBackground();
vtkSmartPointer<vtkTextProperty> axeZTextProp = vtkSmartPointer<vtkTextProperty>::New();
axeZTextProp->ShallowCopy(axeXTextProp);
frameActor->GetZAxisCaptionActor2D()->SetCaptionTextProperty(axeZTextProp);
frameActor->GetZAxisCaptionActor2D()->GetTextActor()->SetTextScaleModeToNone();
frameActor->GetZAxisCaptionActor2D()->GetCaptionTextProperty()->SetColor(0.4, 0.4, 0.9);
frameActor->GetZAxisCaptionActor2D()->GetProperty()->SetDisplayLocationToBackground();
frameActors[viewerName] = frameActor;
}
// Resize frame actor according to the current Component size
double bounds[6];
if (myParentNode == nullptr) {
getBounds(bounds);
}
else {
getParentComponent()->getBounds(bounds);
}
// 10% of the maximum dimension of the component's bounds
double length = std::max({std::abs(bounds[1] - bounds[0]), std::abs(bounds[3] - bounds[2]), std::abs(bounds[5] - bounds[4])}) * 0.1;
if (length > 1.0) { // Avoid invisible size
frameActor->SetTotalLength(length, length, length);
}
else {
frameActor->SetTotalLength(1.0, 1.0, 1.0);
}
return frameActor;
}
// ------------------- getFrameVisibility --------------------------
bool Component::getFrameVisibility(QString viewerName) const {
return frameVisibilities.value(viewerName, false);
}
// ------------------- setFrameVisibility --------------------------
void Component::setFrameVisibility(QString viewerName, bool visibility) {
frameVisibilities[viewerName] = visibility;
}
} // namespace camitk
|