ctkDirectoryButtonTest1.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 <QFormLayout>
  17. #include <QSignalSpy>
  18. #include <QStyle>
  19. #include <QTimer>
  20. #include <QVBoxLayout>
  21. // CTK includes
  22. #include "ctkDirectoryButton.h"
  23. // STD includes
  24. #include <cstdlib>
  25. #include <iostream>
  26. //-----------------------------------------------------------------------------
  27. int ctkDirectoryButtonTest1(int argc, char * argv [] )
  28. {
  29. QApplication app(argc, argv);
  30. QWidget topLevel;
  31. ctkDirectoryButton button;
  32. QIcon defaultIcon = button.style()->standardIcon(QStyle::SP_DirIcon);
  33. QIcon icon = button.style()->standardIcon(QStyle::SP_MessageBoxQuestion);
  34. QIcon icon2 = button.style()->standardIcon(QStyle::SP_DesktopIcon);
  35. ctkDirectoryButton button2(".");
  36. ctkDirectoryButton button3(icon, "..");
  37. ctkDirectoryButton button4;
  38. button4.setAcceptMode(QFileDialog::AcceptSave);
  39. button4.setOptions(button4.options() | ctkDirectoryButton::DontUseNativeDialog);
  40. QFormLayout* layout = new QFormLayout;
  41. layout->addRow("Default button:", &button);
  42. layout->addRow("Current (.) directory: ", &button2);
  43. layout->addRow("Top (..) directory with icon:", &button3);
  44. layout->addRow("Writable directory only:", &button4);
  45. topLevel.setLayout(layout);
  46. button.setCaption("Select a directory");
  47. if (button.caption() != "Select a directory")
  48. {
  49. std::cerr << "ctkDirectoryButton::setCaption() failed." << std::endl;
  50. return EXIT_FAILURE;
  51. }
  52. button.setText("Click here");
  53. if (button.text() != "Click here")
  54. {
  55. std::cerr << "ctkDirectoryButton::setText() failed." << std::endl;
  56. return EXIT_FAILURE;
  57. }
  58. // Restore text to directory path
  59. button.setText(QString());
  60. if (button.text() != QString())
  61. {
  62. std::cerr << "ctkDirectoryButton::setText() failed." << std::endl;
  63. return EXIT_FAILURE;
  64. }
  65. if (button.icon().pixmap(20).toImage() !=
  66. defaultIcon.pixmap(20).toImage())
  67. {
  68. std::cerr << "ctkDirectoryButton::icon() failed." << std::endl;
  69. return EXIT_FAILURE;
  70. }
  71. button3.setIcon(icon2);
  72. if (button3.icon().pixmap(20).toImage() !=
  73. icon2.pixmap(20).toImage())
  74. {
  75. std::cerr << "ctkDirectoryButton::setIcon() failed." << std::endl;
  76. return EXIT_FAILURE;
  77. }
  78. #ifdef USE_QFILEDIALOG_OPTIONS
  79. button.setOptions(QFileDialog::ShowDirsOnly | QFileDialog::ReadOnly);
  80. if (button.options() != (QFileDialog::ShowDirsOnly |
  81. QFileDialog::ReadOnly))
  82. #else
  83. button.setOptions(ctkDirectoryButton::ShowDirsOnly |
  84. ctkDirectoryButton::ReadOnly);
  85. if (button.options() != (ctkDirectoryButton::ShowDirsOnly |
  86. ctkDirectoryButton::ReadOnly))
  87. #endif
  88. {
  89. std::cerr<< "ctkDirectoryButton::setOptions failed" << std::endl;
  90. return EXIT_FAILURE;
  91. }
  92. QSignalSpy spyDirectoryChanged(&button, SIGNAL(directoryChanged(QString)));
  93. QSignalSpy spyDirectorySelected(&button, SIGNAL(directorySelected(QString)));
  94. button.setDirectory(QDir::home().absolutePath());
  95. if ( QDir(button.directory()) != QDir::home() ||
  96. spyDirectoryChanged.count() != 1 ||
  97. spyDirectorySelected.count() != 1)
  98. {
  99. std::cerr<< "ctkDirectoryButton::setDirectory failed" << button.directory().toStdString() << std::endl;
  100. return EXIT_FAILURE;
  101. }
  102. spyDirectoryChanged.clear();
  103. spyDirectorySelected.clear();
  104. // set it again... just to check that it doesn't fire a new signal
  105. button.setDirectory(QDir::home().absolutePath());
  106. if ( QDir(button.directory()) != QDir::home() ||
  107. spyDirectoryChanged.count() != 0 ||
  108. spyDirectorySelected.count() != 1)
  109. {
  110. std::cerr<< "ctkDirectoryButton::setDirectory failed" << button.directory().toStdString() << std::endl;
  111. return EXIT_FAILURE;
  112. }
  113. topLevel.show();
  114. if (argc < 2 || QString(argv[1]) != "-I" )
  115. {
  116. QTimer::singleShot(400, &app, SLOT(quit()));
  117. }
  118. // If Qt uses the default native dialog, a nested event loop won't
  119. // be created and app.quit() will have no effect (as it solely quits
  120. // event loops).
  121. button.setOptions(button.options() | ctkDirectoryButton::DontUseNativeDialog);
  122. QTimer::singleShot(100, &button, SLOT(browse()));
  123. return app.exec();
  124. }