ctkSettingsDialogTest1.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 <QDialogButtonBox>
  18. #include <QPushButton>
  19. #include <QSettings>
  20. #include <QTimer>
  21. // CTK includes
  22. #include "ctkSettingsPanel.h"
  23. #include "ctkSettingsDialog.h"
  24. // STD includes
  25. #include <cstdlib>
  26. #include <iostream>
  27. //-----------------------------------------------------------------------------
  28. int ctkSettingsDialogTest1(int argc, char * argv [] )
  29. {
  30. QApplication app(argc, argv);
  31. QSettings settings(QSettings::IniFormat, QSettings::UserScope, "Common ToolKit", "CTK");
  32. settings.remove("key 1");
  33. ctkSettingsDialog settingsDialog;
  34. // Get a reference to the DialogButtonBox
  35. QDialogButtonBox * buttonBox = settingsDialog.findChild<QDialogButtonBox*>("SettingsButtonBox");
  36. if (!buttonBox)
  37. {
  38. std::cerr << "Line " << __LINE__ << " - Failed to get a reference to the button box !" << std::endl;
  39. return EXIT_FAILURE;
  40. }
  41. // Reset button should be disabled by default
  42. if (buttonBox->button(QDialogButtonBox::Reset)->isEnabled())
  43. {
  44. std::cerr << "Line " << __LINE__ << " - Reset button should be disabled !" << std::endl;
  45. return EXIT_FAILURE;
  46. }
  47. settingsDialog.setSettings(&settings);
  48. // Reset button should be disabled after settings are set
  49. if (buttonBox->button(QDialogButtonBox::Reset)->isEnabled())
  50. {
  51. std::cerr << "Line " << __LINE__ << " - Reset button should be disabled !" << std::endl;
  52. return EXIT_FAILURE;
  53. }
  54. ctkSettingsPanel* panel1 = new ctkSettingsPanel;
  55. settingsDialog.addPanel("Panel 1", panel1);
  56. if (panel1->settings() != &settings)
  57. {
  58. std::cerr << "ctkSettingsDialog::addPanel settings failed" << panel1->settings() << std::endl;
  59. return EXIT_FAILURE;
  60. }
  61. settingsDialog.addPanel("Panel 2", new ctkSettingsPanel);
  62. settingsDialog.addPanel("Panel 3", new ctkSettingsPanel);
  63. ctkSettingsPanel* panel4 = new ctkSettingsPanel;
  64. settingsDialog.addPanel("Panel 4 with long title", panel4, panel1);
  65. QCheckBox* box = new QCheckBox(panel4);
  66. box->setChecked(false); // false by default but we just want to make sure
  67. panel4->registerProperty("key 1", box, "checked",
  68. SIGNAL(toggled(bool)));
  69. QVariant boxVal = settings.value("key 1");
  70. if (!boxVal.isValid() || boxVal.toBool() != false)
  71. {
  72. std::cerr << "Saving to settings failed" << std::endl;
  73. return EXIT_FAILURE;
  74. }
  75. box->setChecked(true);
  76. boxVal = settings.value("key 1");
  77. if (!boxVal.isValid() || boxVal.toBool() != true)
  78. {
  79. std::cerr << "Saving to settings failed" << std::endl;
  80. return EXIT_FAILURE;
  81. }
  82. // Reset button should be enabled after settings are modified
  83. if (!buttonBox->button(QDialogButtonBox::Reset)->isEnabled())
  84. {
  85. std::cerr << "Line " << __LINE__ << " - Reset button should be enabled !" << std::endl;
  86. return EXIT_FAILURE;
  87. }
  88. settingsDialog.resetSettings();
  89. // Reset button should be enabled after settings are reset
  90. if (buttonBox->button(QDialogButtonBox::Reset)->isEnabled())
  91. {
  92. std::cerr << "Line " << __LINE__ << " - Reset button should be disabled !" << std::endl;
  93. return EXIT_FAILURE;
  94. }
  95. boxVal = settings.value("key 1");
  96. if (!boxVal.isValid() || boxVal.toBool() != false)
  97. {
  98. std::cerr << "Reset failed" << std::endl;
  99. return EXIT_FAILURE;
  100. }
  101. // Reset button should be enabled after settings are modified
  102. box->setChecked(true);
  103. boxVal = settings.value("key 1");
  104. if (!boxVal.isValid() || boxVal.toBool() != true)
  105. {
  106. std::cerr << "Saving to settings failed" << std::endl;
  107. return EXIT_FAILURE;
  108. }
  109. if (!buttonBox->button(QDialogButtonBox::Reset)->isEnabled())
  110. {
  111. std::cerr << "Line " << __LINE__ << " - Reset button should be enabled !" << std::endl;
  112. return EXIT_FAILURE;
  113. }
  114. // .. and reset button should be disabled if new settings object is set
  115. QSettings settings2(QSettings::IniFormat, QSettings::UserScope, "Common ToolKit", "CTK");
  116. settingsDialog.setSettings(&settings2);
  117. if (buttonBox->button(QDialogButtonBox::Reset)->isEnabled())
  118. {
  119. std::cerr << "Line " << __LINE__ << " - Reset button should be disabled !" << std::endl;
  120. return EXIT_FAILURE;
  121. }
  122. settingsDialog.setCurrentPanel("Panel 4");
  123. settingsDialog.show();
  124. if (argc < 2 || QString(argv[1]) != "-I" )
  125. {
  126. QTimer::singleShot(200, &app, SLOT(quit()));
  127. }
  128. return app.exec();
  129. }