ctkCmdLineModuleUtils.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkCmdLineModuleUtils.h"
  16. #include "ctkCmdLineModuleRunException.h"
  17. #include <QApplication>
  18. #include <QMessageBox>
  19. #include <QUrl>
  20. #include <QString>
  21. //-----------------------------------------------------------------------------
  22. QString ctkCmdLineModuleUtils::errorMessagesFromModuleRegistration(
  23. const QList<ctkCmdLineModuleReferenceResult>& moduleRefs,
  24. ctkCmdLineModuleManager::ValidationMode validationMode
  25. )
  26. {
  27. QString errorMsg;
  28. QListIterator<ctkCmdLineModuleReferenceResult> listIter(moduleRefs);
  29. while(listIter.hasNext())
  30. {
  31. try
  32. {
  33. const ctkCmdLineModuleReferenceResult& moduleRefResult = listIter.next();
  34. if (!moduleRefResult.m_Reference)
  35. {
  36. errorMsg += (QObject::tr("Failed to register module:\n%1\n\ndue to:\n%2\n\n").arg(moduleRefResult.m_Url.toString()).arg(moduleRefResult.m_RuntimeError));
  37. }
  38. else if (!moduleRefResult.m_Reference.xmlValidationErrorString().isEmpty() &&
  39. validationMode == ctkCmdLineModuleManager::STRICT_VALIDATION)
  40. {
  41. errorMsg += (QObject::tr("Failed to register module:\n%1\n\ndue to xml validation error:\n%2\n\n").arg(moduleRefResult.m_Url.toString()).arg(moduleRefResult.m_Reference.xmlValidationErrorString()));
  42. }
  43. }
  44. // These exceptions should never happen, as at this point we are iterating over a fixed list of results, and not processing exceptions.
  45. catch (const ctkCmdLineModuleRunException& e)
  46. {
  47. errorMsg += QObject::tr("Failed to register module:\n") + e.location().toString() + "\n\ndue to:\n" + e.message() + "\n\n";
  48. }
  49. catch (const std::exception& e)
  50. {
  51. errorMsg += QObject::tr("Failed to register module:\n\n\ndue to:\n") + e.what() + "\n\n";
  52. }
  53. }
  54. return errorMsg;
  55. }
  56. //-----------------------------------------------------------------------------
  57. QString ctkCmdLineModuleUtils::errorMessagesFromModuleRegistration(
  58. const QFuture<ctkCmdLineModuleReferenceResult>& moduleRefsFuture,
  59. ctkCmdLineModuleManager::ValidationMode validationMode
  60. )
  61. {
  62. QList<ctkCmdLineModuleReferenceResult> moduleRefs;
  63. QFutureIterator<ctkCmdLineModuleReferenceResult> futureIter(moduleRefsFuture);
  64. while(futureIter.hasNext())
  65. {
  66. const ctkCmdLineModuleReferenceResult& moduleRefResult = futureIter.next();
  67. moduleRefs << moduleRefResult;
  68. }
  69. QString errorMsg = ctkCmdLineModuleUtils::errorMessagesFromModuleRegistration(moduleRefs, validationMode);
  70. return errorMsg;
  71. }
  72. //-----------------------------------------------------------------------------
  73. void ctkCmdLineModuleUtils::messageBoxForModuleRegistration(
  74. const QString& errorMsg
  75. )
  76. {
  77. if (!errorMsg.isEmpty())
  78. {
  79. QWidget* widget = QApplication::activeModalWidget();
  80. if (widget == NULL) widget = QApplication::activeWindow();
  81. QMessageBox::critical(widget, QObject::tr("Failed to register modules"), errorMsg);
  82. }
  83. }
  84. //-----------------------------------------------------------------------------
  85. void ctkCmdLineModuleUtils::messageBoxModuleRegistration(const QFuture<ctkCmdLineModuleReferenceResult> &moduleRefsFuture,
  86. ctkCmdLineModuleManager::ValidationMode validationMode)
  87. {
  88. const QString errorMsg = ctkCmdLineModuleUtils::errorMessagesFromModuleRegistration(moduleRefsFuture, validationMode);
  89. ctkCmdLineModuleUtils::messageBoxForModuleRegistration(errorMsg);
  90. }