ctkDirectoryButtonTest1.cpp 4.5 KB

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