ctkCmdLineModuleTour.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 <ctkCommandLineParser.h>
  16. #include <QApplication>
  17. #include <QTextStream>
  18. #include <QFile>
  19. #include <QPixmap>
  20. #include <QPainter>
  21. #include <cstdlib>
  22. int main(int argc, char* argv[])
  23. {
  24. // QPixmap (used below) requires QApplication (instead of QCoreApplication)
  25. QApplication app(argc, argv);
  26. // This is used by QSettings
  27. QCoreApplication::setOrganizationName("CommonTK");
  28. QCoreApplication::setApplicationName("CmdLineModuleTour");
  29. ctkCommandLineParser parser;
  30. // Use Unix-style argument names
  31. parser.setArgumentPrefix("--", "-");
  32. // Add command line argument names
  33. parser.addArgument("help", "h", QVariant::Bool, "Show this help text");
  34. parser.addArgument("xml", "", QVariant::Bool, "Print a XML description of this modules command line interface");
  35. parser.addArgument("integer", "i", QVariant::Int, "Show this help text",15);
  36. parser.addArgument("", "b", QVariant::String, "Show this help text");
  37. parser.addArgument("double", "d", QVariant::Double, "Show this help text");
  38. parser.addArgument("floatVector", "f", QVariant::String, "Show this help text");
  39. parser.addArgument("enumeration", "e", QVariant::String, "Show this help text");
  40. parser.addArgument("string_vector", "", QVariant::String, "Show this help text");
  41. parser.addArgument("", "p", QVariant::String, "Show this help text");
  42. // Parse the command line arguments
  43. bool ok = false;
  44. QHash<QString, QVariant> parsedArgs = parser.parseArguments(QCoreApplication::arguments(), &ok);
  45. if (!ok)
  46. {
  47. QTextStream(stderr, QIODevice::WriteOnly) << "Error parsing arguments: "
  48. << parser.errorString() << "\n";
  49. return EXIT_FAILURE;
  50. }
  51. // Show a help message
  52. if (parsedArgs.contains("help") || parsedArgs.contains("h"))
  53. {
  54. QTextStream(stdout, QIODevice::WriteOnly) << parser.helpText();
  55. return EXIT_SUCCESS;
  56. }
  57. if (parsedArgs.contains("xml"))
  58. {
  59. QFile xmlDescription(":/ctkCmdLineModuleTour.xml");
  60. xmlDescription.open(QIODevice::ReadOnly);
  61. QTextStream(stdout, QIODevice::WriteOnly) << xmlDescription.readAll();
  62. }
  63. if(parsedArgs.contains("p"))
  64. {
  65. QTextStream(stdout, QIODevice::WriteOnly) << parsedArgs["p"].toString();
  66. }
  67. if(parsedArgs.contains("double"))
  68. {
  69. QTextStream(stdout, QIODevice::WriteOnly) << parsedArgs["double"].toString();
  70. }
  71. // Do something
  72. // do we have enough information (input/output)?
  73. if(parser.unparsedArguments().count() >= 2)
  74. {
  75. QString input = parser.unparsedArguments().at(0);
  76. QString output = parser.unparsedArguments().at(1);
  77. QPixmap pix(input);
  78. QPainter painter(&pix);
  79. painter.setPen(Qt::white);
  80. painter.setFont(QFont("Arial", parsedArgs["integer"].toInt()));
  81. painter.drawText(pix.rect(),Qt::AlignBottom|Qt::AlignLeft,"Result image produced by ctkCLIModuleTour");
  82. pix.save(output, "JPEG");
  83. }
  84. return EXIT_SUCCESS;
  85. }