ctkConfirmExitDialogTest1.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 "ctkConfirmExitDialog.h"
  22. // STD includes
  23. #include <cstdlib>
  24. #include <iostream>
  25. //-----------------------------------------------------------------------------
  26. int ctkConfirmExitDialogTest1(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. ctkConfirmExitDialog confirmDialog;
  34. if (confirmDialog.dontShowAnymore() != false)
  35. {
  36. std::cerr << "ctkConfirmExitDialog::dontShowAnymore failed" << std::endl;
  37. return EXIT_FAILURE;
  38. }
  39. confirmDialog.setText("Are you sure you want to exit?");
  40. confirmDialog.setPixmap(confirmDialog.style()->standardPixmap(QStyle::SP_MessageBoxQuestion));
  41. QSettings settings;
  42. settings.setValue("DontShow", true);
  43. confirmDialog.setDontShowAnymoreSettingsKey("DontShow");
  44. if (confirmDialog.dontShowAnymoreSettingsKey() != "DontShow")
  45. {
  46. std::cerr << "ctkConfirmExitDialog::setDontShowAnymoreSettingsKey failed:"
  47. << confirmDialog.dontShowAnymoreSettingsKey().toStdString() << std::endl;
  48. return EXIT_FAILURE;
  49. }
  50. if (confirmDialog.dontShowAnymore() != true)
  51. {
  52. std::cerr << "ctkConfirmExitDialog::setDontShowAnymoreSettingsKey failed:"
  53. << confirmDialog.dontShowAnymore() << std::endl;
  54. return EXIT_FAILURE;
  55. }
  56. // exec() should return automatically because DontShowAnymore is true
  57. if (confirmDialog.exec() != QDialog::Accepted)
  58. {
  59. std::cerr << "ctkConfirmExitDialog::exec failed:" << std::endl;
  60. return EXIT_FAILURE;
  61. }
  62. // test the static version
  63. if (ctkConfirmExitDialog::confirmExit("DontShow") != true)
  64. {
  65. std::cerr << "ctkConfirmExitDialog::confirmExit failed:" << std::endl;
  66. return EXIT_FAILURE;
  67. }
  68. confirmDialog.setDontShowAnymore(false);
  69. // modal dialog
  70. confirmDialog.open();
  71. if (argc < 2 || QString(argv[1]) != "-I" )
  72. {
  73. QTimer::singleShot(200, &confirmDialog, SLOT(accept()));
  74. }
  75. return app.exec();
  76. }