| 405 | } |
| 406 | |
| 407 | bool Command::SetHandler(const IHandler::Pointer handler) |
| 408 | { |
| 409 | if (handler == this->handler) |
| 410 | { |
| 411 | return false; |
| 412 | } |
| 413 | |
| 414 | // Swap the state around. |
| 415 | const QList<QString> stateIds(this->GetStateIds()); |
| 416 | for (int i = 0; i < stateIds.size(); ++i) |
| 417 | { |
| 418 | const QString stateId = stateIds[i]; |
| 419 | if (IObjectWithState::Pointer stateHandler = this->handler.Cast<IObjectWithState>()) |
| 420 | { |
| 421 | stateHandler->RemoveState(stateId); |
| 422 | } |
| 423 | if (IObjectWithState::Pointer stateHandler = handler.Cast<IObjectWithState>()) |
| 424 | { |
| 425 | const State::Pointer stateToAdd(this->GetState(stateId)); |
| 426 | stateHandler->AddState(stateId, stateToAdd); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | bool enabled = this->IsEnabled(); |
| 431 | if (this->handler) |
| 432 | { |
| 433 | this->handler->RemoveHandlerListener(this->GetHandlerListener()); |
| 434 | } |
| 435 | |
| 436 | // Update the handler, and flush the string representation. |
| 437 | this->handler = handler; |
| 438 | if (this->handler) |
| 439 | { |
| 440 | this->handler->AddHandlerListener(this->GetHandlerListener()); |
| 441 | } |
| 442 | this->str = ""; |
| 443 | |
| 444 | // Debugging output |
| 445 | if ((DEBUG_HANDLERS) |
| 446 | && ((DEBUG_HANDLERS_COMMAND_ID.isEmpty()) || (DEBUG_HANDLERS_COMMAND_ID == id))) |
| 447 | { |
| 448 | QString buffer("Command('"); |
| 449 | buffer += id + "') has changed to "; |
| 450 | if (!handler) { |
| 451 | buffer += "no handler"; |
| 452 | } else { |
| 453 | buffer += "\'" + handler->ToString() + "' as its handler"; |
| 454 | } |
| 455 | CommandTracing::PrintTrace("HANDLERS", buffer); |
| 456 | } |
| 457 | |
| 458 | // Send notification |
| 459 | CommandEvent::Pointer cmdEvent(new CommandEvent(Command::Pointer(this), false, false, false, true, |
| 460 | false, false, false, false, enabled != this->IsEnabled())); |
| 461 | this->FireCommandChanged(cmdEvent); |
| 462 | |
| 463 | return true; |
| 464 | } |
no test coverage detected