ctkDateRangeWidgetTest1.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Isomics Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QApplication>
  16. #include <QSignalSpy>
  17. #include <QTimer>
  18. // CTK includes
  19. #include "ctkDateRangeWidget.h"
  20. // STD includes
  21. #include <cstdlib>
  22. #include <iostream>
  23. //-----------------------------------------------------------------------------
  24. int ctkDateRangeWidgetTest1(int argc, char * argv [] )
  25. {
  26. QApplication app(argc, argv);
  27. QDateTime today = QDateTime(QDate::currentDate());
  28. QDateTime tomorrow = today.addDays(1);
  29. QDateTime yesterday = today.addDays(-1);
  30. QDateTime lastWeek = today.addDays(-7);
  31. QDateTime lastMonth = today.addMonths(-1);
  32. ctkDateRangeWidget dateRange;
  33. if (dateRange.displayTime())
  34. {
  35. std::cerr << "ctkDateRangeWidget::ctkDateRangeWidget(): "
  36. << "wrong default displayTime()" << std::endl;
  37. return EXIT_FAILURE;
  38. }
  39. if (!dateRange.isAnyDate())
  40. {
  41. std::cerr << "ctkDateRangeWidget::ctkDateRangeWidget(): "
  42. << "default date range failed: "
  43. << qPrintable(dateRange.startDateTime().toString()) << " "
  44. << qPrintable(dateRange.endDateTime().toString()) << std::endl;
  45. return EXIT_FAILURE;
  46. }
  47. dateRange.setToday();
  48. if (dateRange.isAnyDate() ||
  49. dateRange.startDateTime() != today ||
  50. dateRange.endDateTime() != tomorrow)
  51. {
  52. std::cerr << "ctkDateRangeWidget::setToday(): "
  53. << qPrintable(dateRange.startDateTime().toString()) << " "
  54. << qPrintable(dateRange.endDateTime().toString()) << std::endl;
  55. return EXIT_FAILURE;
  56. }
  57. dateRange.setYesterday();
  58. if (dateRange.isAnyDate() ||
  59. dateRange.startDateTime() != yesterday ||
  60. dateRange.endDateTime() != today)
  61. {
  62. std::cerr << "ctkDateRangeWidget::setYesterday(): "
  63. << qPrintable(dateRange.startDateTime().toString()) << " "
  64. << qPrintable(dateRange.endDateTime().toString()) << std::endl;
  65. return EXIT_FAILURE;
  66. }
  67. dateRange.setLastWeek();
  68. if (dateRange.isAnyDate() ||
  69. dateRange.startDateTime() != lastWeek ||
  70. dateRange.endDateTime() != today)
  71. {
  72. std::cerr << "ctkDateRangeWidget::setLastWeek(): "
  73. << qPrintable(dateRange.startDateTime().toString()) << " "
  74. << qPrintable(dateRange.endDateTime().toString()) << std::endl;
  75. return EXIT_FAILURE;
  76. }
  77. dateRange.setLastMonth();
  78. if (dateRange.isAnyDate() ||
  79. dateRange.startDateTime() != lastMonth ||
  80. dateRange.endDateTime() != today)
  81. {
  82. std::cerr << "ctkDateRangeWidget::setLastMonth(): "
  83. << qPrintable(dateRange.startDateTime().toString()) << " "
  84. << qPrintable(dateRange.endDateTime().toString()) << std::endl;
  85. return EXIT_FAILURE;
  86. }
  87. dateRange.setAnyDate();
  88. if (!dateRange.isAnyDate() ||
  89. dateRange.startDateTime() == lastMonth ||
  90. dateRange.endDateTime() == today)
  91. {
  92. std::cerr << "ctkDateRangeWidget::setAnyDate(): "
  93. << qPrintable(dateRange.startDateTime().toString()) << " "
  94. << qPrintable(dateRange.endDateTime().toString()) << std::endl;
  95. return EXIT_FAILURE;
  96. }
  97. dateRange.setDateTimeRange(QDateTime(QDate(-2, -9,12345678)),
  98. QDateTime(QDate(2010, 15, 32)));
  99. if (!dateRange.isAnyDate() ||
  100. dateRange.startDateTime() == lastMonth ||
  101. dateRange.endDateTime() == today)
  102. {
  103. std::cerr << "ctkDateRangeWidget::setDateTimeRange(): "
  104. << qPrintable(dateRange.startDateTime().toString()) << " "
  105. << qPrintable(dateRange.endDateTime().toString()) << std::endl;
  106. return EXIT_FAILURE;
  107. }
  108. dateRange.setDateTimeRange(today, tomorrow);
  109. if (dateRange.isAnyDate() ||
  110. !dateRange.startDateTime().isValid() ||
  111. !dateRange.endDateTime().isValid())
  112. {
  113. std::cerr << "ctkDateRangeWidget::setDateTimeRange(): "
  114. << qPrintable(dateRange.startDateTime().toString()) << " "
  115. << qPrintable(dateRange.endDateTime().toString()) << std::endl;
  116. return EXIT_FAILURE;
  117. }
  118. QDateTime oldStart = dateRange.startDateTime();
  119. QDateTime oldEnd = dateRange.endDateTime();
  120. dateRange.setSelectRange();
  121. if (dateRange.startDateTime() != oldStart ||
  122. dateRange.endDateTime() != oldEnd)
  123. {
  124. std::cerr << "ctkDateRangeWidget::setSelectRange(): "
  125. << qPrintable(dateRange.startDateTime().toString()) << " "
  126. << qPrintable(dateRange.endDateTime().toString()) << std::endl;
  127. return EXIT_FAILURE;
  128. }
  129. dateRange.setDisplayTime(true);
  130. if (!dateRange.displayTime())
  131. {
  132. std::cerr << "ctkDateRangeWidget::setSelectRange(): "
  133. << qPrintable(dateRange.startDateTime().toString()) << " "
  134. << qPrintable(dateRange.endDateTime().toString()) << std::endl;
  135. return EXIT_FAILURE;
  136. }
  137. QSignalSpy startSpy(&dateRange, SIGNAL(startDateTimeChanged(QDateTime)));
  138. QSignalSpy endSpy(&dateRange, SIGNAL(endDateTimeChanged(QDateTime)));
  139. dateRange.setLastWeek();
  140. if (startSpy.count() != 1 || endSpy.count() != 1)
  141. {
  142. std::cerr << "ctkDateRangeWidget::setLastWeek(): "
  143. << startSpy.count() << " " << endSpy.count() << std::endl;
  144. return EXIT_FAILURE;
  145. }
  146. startSpy.clear();
  147. endSpy.clear();
  148. dateRange.setLastMonth();
  149. if (startSpy.count() != 1 || endSpy.count() != 0)
  150. {
  151. std::cerr << "ctkDateRangeWidget::setLastMonth(): "
  152. << startSpy.count() << " " << endSpy.count() << std::endl;
  153. return EXIT_FAILURE;
  154. }
  155. dateRange.show();
  156. if (argc < 2 || QString(argv[1]) != "-I" )
  157. {
  158. QTimer::singleShot(200, &app, SLOT(quit()));
  159. }
  160. return app.exec();
  161. }