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
|
/*****************************************************************************
* $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$
****************************************************************************/
#include "Action.h"
#include "ActionExtension.h"
#include "Application.h"
#include "TransformationManager.h"
#include "Component.h"
#include "ImageComponent.h"
#include "Property.h"
#include "ActionWidget.h"
#include "HistoryItem.h"
#include "PersistenceManager.h"
#include "Log.h"
#include <utility> // as_const
namespace camitk {
// -------------------- constructor --------------------
Action::Action(ActionExtension* extension) : QObject() {
this->extension = extension;
componentClassName = "";
qAction = nullptr;
isEmbedded = true;
actionWidget = nullptr;
autoUpdateProperties = false;
historyItem = nullptr;
defaultWidgetButtonVisibility = true;
defaultWidgetApplyButtonText = tr("Apply");
}
// -------------------- destructor --------------------
Action::~Action() {
// delete all properties
for (auto prop : std::as_const(parameterMap)) {
delete prop;
}
parameterMap.clear();
//delete History item
if (historyItem) {
delete historyItem;
}
}
// -------------------- getStatusAsString --------------------
QString Action::getStatusAsString(ApplyStatus status) {
QString statusStr;
switch (status) {
case Action::ABORTED:
statusStr = "ABORTED";
break;
case Action::ERROR:
statusStr = "ERROR";
break;
case Action::SUCCESS:
statusStr = "SUCCESS";
break;
case Action::TRIGGERED:
statusStr = "TRIGGERED";
break;
case Action::WARNING:
statusStr = "WARNING";
break;
default:
statusStr = "UNKNOWN";
break;
}
return statusStr;
}
// -------------------- setName --------------------
void Action::setName(QString name) {
this->name = name;
setObjectName(name);
}
// -------------------- setDescription --------------------
void Action::setDescription(QString description) {
this->description = description;
}
// -------------------- setComponentClassName --------------------
void Action::setComponentClassName(QString componentClassName) {
this->componentClassName = componentClassName;
}
// -------------------- setFamily --------------------
void Action::setFamily(QString family) {
this->family = family;
}
// -------------------- setTag --------------------
void Action::addTag(QString tag) {
this->tags.append(tag);
}
// -------------------- setEmbedded --------------------
void Action::setEmbedded(bool isEmbedded) {
this->isEmbedded = isEmbedded;
}
// -------------------- setIcon --------------------
void Action::setIcon(QPixmap icon) {
this->icon = icon;
}
// -------------------- getExtensionName --------------------
QString Action::getExtensionName() const {
return extension->getName();
}
// -------------------- getExtension --------------------
const ActionExtension* Action::getExtension() const {
return extension;
}
// -------------------- refreshApplication --------------------
void Action::refreshApplication() {
TransformationManager::cleanupFramesAndTransformations();
Application::refresh();
}
// -------------------- getIcon --------------------
QPixmap Action::getIcon() {
return icon;
}
// -------------------- getTargets --------------------
const ComponentList Action::getTargets() const {
return targetComponents;
}
// -------------------- updateTargets --------------------
void Action::updateTargets() {
//-- build the list of valid targets
targetComponents.clear();
for (Component* comp : Application::getSelectedComponents()) {
// check compatibility
if (comp->isInstanceOf(this->getComponentClassName())) {
targetComponents.append(comp);
}
}
}
// -------------------- getWidget --------------------
QWidget* Action::getWidget() {
// build or update the widget
if (actionWidget == nullptr) {
// Setting the widget containing the parameters, using the default widget
actionWidget = new ActionWidget(this);
}
else {
// make sure the widget has updated targets
dynamic_cast<ActionWidget*>(actionWidget)->update();
}
// make sure the widget updates its parameters
dynamic_cast<ActionWidget*>(actionWidget)->setAutoUpdateProperty(autoUpdateProperties);
// update default UI
dynamic_cast<ActionWidget*>(actionWidget)->setApplyButtonText(defaultWidgetApplyButtonText);
dynamic_cast<ActionWidget*>(actionWidget)->setButtonVisibility(defaultWidgetButtonVisibility);
return actionWidget;
}
// -------------------- setDefaultWidgetButtonVisibility --------------------
void Action::setDefaultWidgetButtonVisibility(bool visible) {
defaultWidgetButtonVisibility = visible;
if (dynamic_cast<ActionWidget*>(actionWidget) != nullptr) {
dynamic_cast<ActionWidget*>(actionWidget)->setButtonVisibility(defaultWidgetButtonVisibility);
}
}
// -------------------- setDefaultWidgetApplyButtonText --------------------
void Action::setDefaultWidgetApplyButtonText(QString text) {
defaultWidgetApplyButtonText = text;
if (dynamic_cast<ActionWidget*>(actionWidget) != nullptr) {
dynamic_cast<ActionWidget*>(actionWidget)->setApplyButtonText(defaultWidgetApplyButtonText);
}
}
// -------------------- getQAction --------------------
QAction* Action::getQAction(Component* target) {
if (!qAction) {
// create the corresponding QAction (using the icon, name and descriptions)
qAction = new QAction(getIcon(), getName(), this);
qAction->setStatusTip(getDescription());
qAction->setWhatsThis(getName() + "\n" + getDescription());
// connect it to the trigger slot
connect(qAction, SIGNAL(triggered()), this, SLOT(trigger()));
}
return qAction;
}
// -------------------- trigger --------------------
Action::ApplyStatus Action::trigger(QWidget* parent) {
updateTargets();
//-- if there are some valid targets or if the action is generic
if (targetComponents.size() > 0 || getComponentClassName().isEmpty()) {
if (isEmbedded && getWidget() != nullptr) {
if (parent != nullptr) {
// set the widget in the given parent
getWidget()->setParent(parent);
getWidget()->show();
}
}
else {
QWidget* w = getWidget();
if (w) {
// non embedded, just show/raise the window
w->show();
}
else {
// non embedded, no widget -> call apply() directly
return applyAndRegister();
}
}
// tell the application this action is ready to show its widget
Application::setTriggeredAction(this);
// make sure that any viewer that wants to show something about it is refreshed
refreshApplication();
// now that everyone that wanted to be notified about this change of action selection,
// there is no need to do more, just empty the current selection
Application::setTriggeredAction(nullptr);
return TRIGGERED;
}
else {
// should never happened
CAMITK_WARNING(tr("Cannot apply this Action to currently selected component(s)"))
return ERROR;
}
}
// -------------------- applyAndRegister --------------------
Action::ApplyStatus Action::applyAndRegister() {
ApplyStatus status;
// call preprocess function to compute number of components before applying the action
this->preProcess();
//-- if there are some valid targets or if the action is generic
if (targetComponents.size() > 0 || getComponentClassName().isEmpty()) {
CAMITK_TRACE(tr("Applying..."))
status = apply();
CAMITK_TRACE(tr("Done. Status: %1").arg(getStatusAsString(status)))
this->postProcess();
}
else {
status = Action::ABORTED;
}
return status;
}
// -------------------- applyInPipeline --------------------
Action::ApplyStatus Action::applyInPipeline() {
ApplyStatus status;
// call preprocess function, such as in pipeline count components during the process
preProcessInPipeline();
//-- if there are some valid targets or if the action is generic
if (targetComponents.size() > 0 || getComponentClassName().isEmpty()) {
CAMITK_TRACE(tr("Applying..."))
status = apply();
CAMITK_TRACE(tr("Done. Status: %1").arg(getStatusAsString(status)))
postProcessInPipeline();
}
else {
status = Action::ABORTED;
CAMITK_TRACE(tr("Action requires targets, but none set. Status: %1").arg(getStatusAsString(status)))
}
return status;
}
// -------------------- setInputComponents --------------------
void Action::setInputComponents(ComponentList inputs) {
//-- build the list of valid targets
targetComponents.clear();
for (Component* comp : inputs) {
// check compatibility
if (comp->isInstanceOf(this->getComponentClassName())) {
targetComponents.append(comp);
}
}
}
// -------------------- setInputComponent --------------------
void Action::setInputComponent(Component* input) {
targetComponents.clear();
// check compatibility
if (input->isInstanceOf(this->getComponentClassName())) {
targetComponents.append(input);
}
}
// -------------------- getOutputComponents --------------------
ComponentList Action::getOutputComponents() {
return this->outputComponents;
}
// -------------------- getOutputComponent --------------------
Component* Action::getOutputComponent() {
if (outputComponents.isEmpty()) {
return nullptr;
}
else {
return outputComponents.first();
}
}
// -------------------- preProcess ------------------------------
void Action::preProcess() {
// Create a new entry in the history
this->historyItem = new HistoryItem(this->name);
// Get track of alive top level components before applying the action so
// that we can deduce from list of components after the action,
// the components created by the action.
this->aliveBeforeComponents = Application::getTopLevelComponents();
// We will select the one selected as input of the current action
this->topLevelSelectedComponents.clear();
QList<HistoryComponent> topLevelSelectedHistoryComponents;
for (Component* comp : this->aliveBeforeComponents) {
if (comp->isSelected()) {
this->topLevelSelectedComponents.append(comp);
topLevelSelectedHistoryComponents.append(HistoryComponent(comp));
}
}
// Save the input components information
this->historyItem->setInputHistoryComponents(topLevelSelectedHistoryComponents);
}
// -------------------- preProcessInPipeline --------------------
void Action::preProcessInPipeline() {
// get track of alive components before applying the action so
// that we can deduce from list of components after the action,
// the components created by the action.
this->aliveBeforeComponents = Application::getAllComponents();
}
// -------------------- postProcess ------------------------------
void Action::postProcess() {
// Deduce the number of top level components created during the action, from aliveBeforeComponents
ComponentList topLevelComponentCreated;
QList<HistoryComponent> topLevelHistoryComponentCreated;
topLevelHistoryComponentCreated.clear();
for (Component* comp : Application::getTopLevelComponents()) {
if (!this->aliveBeforeComponents.contains(comp)) {
topLevelComponentCreated.append(comp);
topLevelHistoryComponentCreated.append(HistoryComponent(comp));
}
}
// Save the output components information
historyItem->setOutputHistoryComponents(topLevelHistoryComponentCreated);
// Add the action's parameters to the history item
// get back all the properties dynamically added to the action using Qt meta object
for (QByteArray propertyName : dynamicPropertyNames()) {
historyItem->addProperty(propertyName, property(propertyName));
}
// Store the entry in the history.
Application::addHistoryItem(*historyItem);
}
// -------------------- postProcessInPipeline --------------------
void Action::postProcessInPipeline() {
outputComponents.clear();
for (Component* comp : Application::getAllComponents()) {
if (!aliveBeforeComponents.contains(comp) || comp->getModified()) {
outputComponents.append(comp);
}
}
}
// -------------------- getAutoUpdateProperties --------------------
bool Action::getAutoUpdateProperties() const {
return autoUpdateProperties;
}
// -------------------- setAutoUpdateProperties --------------------
void Action::setAutoUpdateProperties(bool autoUpdateProperties) {
this->autoUpdateProperties = autoUpdateProperties;
if (actionWidget != nullptr) {
dynamic_cast<ActionWidget*>(actionWidget)->setAutoUpdateProperty(autoUpdateProperties);
}
}
// -------------------- getProperty --------------------
Property* Action::getProperty(QString name) {
return parameterMap.value(name);
}
// -------------------- getParameterValue --------------------
QVariant Action::getParameterValue(const QString& name) const {
if (!parameterMap.contains(name)) {
CAMITK_WARNING(tr("Parameter \"%1\" undeclared. Check the spelling.").arg(name))
return QVariant(); // invalid QVariant
}
else {
return property(name.toStdString().c_str());
}
}
// -------------------- setParameterValue --------------------
bool Action::setParameterValue(const QString& name, QVariant newValue) {
if (!parameterMap.contains(name)) {
CAMITK_WARNING(tr("Parameter \"%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;
}
}
// -------------------- getParameterValueAsString --------------------
QString Action::getParameterValueAsString(const QString& name) const {
return Property::getValueAsString(getParameterValue(name));
}
// ---------------------- addParameter ----------------------------
bool Action::addParameter(Property* prop) {
// 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
parameterMap.insert(prop->getName(), prop);
return returnStatus;
}
// ---------------------- toVariant ----------------------------
QVariant Action::toVariant() const {
return PersistenceManager::fromProperties(this);
}
// ---------------------- fromVariant ----------------------------
void Action::fromVariant(const QVariant& variant) {
PersistenceManager::loadProperties(this, variant);
}
// ---------------------- getUuid ----------------------------
QUuid Action::getUuid() const {
return uuid;
}
// ---------------------- setUuid ----------------------------
bool Action::setUuid(QUuid id) {
if (uuid.isNull()) {
uuid = id;
return true;
}
else {
return false;
}
}
} //namespace camitk
|