ctkMessageBoxDontShowAgainTest.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware 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 <QCheckBox>
  17. #include <QSettings>
  18. #include <QStyle>
  19. #include <QTimer>
  20. // CTK includes
  21. #include "ctkMessageBox.h"
  22. // STD includes
  23. #include <cstdlib>
  24. #include <iostream>
  25. //-----------------------------------------------------------------------------
  26. int ctkMessageBoxDontShowAgainTest(int argc, char * argv [] )
  27. {
  28. QApplication app(argc, argv);
  29. /// set the names for QSettings to work
  30. app.setOrganizationName("CommonToolKit");
  31. app.setOrganizationDomain("www.commontk.org");
  32. app.setApplicationName("CTK");
  33. ctkMessageBox confirmDialog;
  34. // Test default values.
  35. if (confirmDialog.isDontShowAgainVisible() != false ||
  36. confirmDialog.dontShowAgain() != false ||
  37. confirmDialog.dontShowAgainSettingsKey().isEmpty() != true)
  38. {
  39. std::cerr << "ctkMessageBox default values failed" << std::endl;
  40. return EXIT_FAILURE;
  41. }
  42. confirmDialog.setText("Are you sure you want to exit?");
  43. confirmDialog.setIcon(QMessageBox::Question);
  44. confirmDialog.setDontShowAgainVisible(true);
  45. QSettings settings;
  46. settings.setValue("DontShow", true);
  47. confirmDialog.setDontShowAgainSettingsKey("DontShow");
  48. if (confirmDialog.dontShowAgainSettingsKey() != "DontShow")
  49. {
  50. std::cerr << "ctkMessageBox::setDontShowAgainSettingsKey failed:"
  51. << confirmDialog.dontShowAgainSettingsKey().toStdString() << std::endl;
  52. return EXIT_FAILURE;
  53. }
  54. if (confirmDialog.dontShowAgain() != true)
  55. {
  56. std::cerr << "ctkMessageBox::setDontShowAgainSettingsKey failed:"
  57. << confirmDialog.dontShowAgain() << std::endl;
  58. return EXIT_FAILURE;
  59. }
  60. // exec() should return automatically because DontShowAgain is true
  61. if (confirmDialog.exec() != QDialog::Accepted)
  62. {
  63. std::cerr << "ctkMessageBox::exec failed:" << std::endl;
  64. return EXIT_FAILURE;
  65. }
  66. // test the static version
  67. if (ctkMessageBox::confirmExit("DontShow") != true)
  68. {
  69. std::cerr << "ctkMessageBox::confirmExit failed:" << std::endl;
  70. return EXIT_FAILURE;
  71. }
  72. confirmDialog.setDontShowAgain(false);
  73. // modal dialog
  74. confirmDialog.open();
  75. if (argc < 2 || QString(argv[1]) != "-I" )
  76. {
  77. QTimer::singleShot(200, &confirmDialog, SLOT(accept()));
  78. }
  79. return app.exec();
  80. }