ctkDirectoryButtonTest1.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.commontk.org/LICENSE
  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. if (button.icon().pixmap(20).toImage() !=
  48. defaultIcon.pixmap(20).toImage())
  49. {
  50. std::cerr << "ctkDirectoryButton::icon() failed." << std::endl;
  51. return EXIT_FAILURE;
  52. }
  53. button3.setIcon(icon2);
  54. if (button3.icon().pixmap(20).toImage() !=
  55. icon2.pixmap(20).toImage())
  56. {
  57. std::cerr << "ctkDirectoryButton::setIcon() failed." << std::endl;
  58. return EXIT_FAILURE;
  59. }
  60. #ifdef USE_QFILEDIALOG_OPTIONS
  61. button.setOptions(QFileDialog::ShowDirsOnly | QFileDialog::ReadOnly);
  62. if (button.options() != (QFileDialog::ShowDirsOnly |
  63. QFileDialog::ReadOnly))
  64. #else
  65. button.setOptions(ctkDirectoryButton::ShowDirsOnly |
  66. ctkDirectoryButton::ReadOnly);
  67. if (button.options() != (ctkDirectoryButton::ShowDirsOnly |
  68. ctkDirectoryButton::ReadOnly))
  69. #endif
  70. {
  71. std::cerr<< "ctkDirectoryButton::setOptions failed" << std::endl;
  72. return EXIT_FAILURE;
  73. }
  74. QSignalSpy spyDirectoryChanged(&button, SIGNAL(directoryChanged(const QString&)));
  75. QSignalSpy spyDirectorySelected(&button, SIGNAL(directorySelected(const QString&)));
  76. button.setDirectory(QDir::home().absolutePath());
  77. if ( QDir(button.directory()) != QDir::home() ||
  78. spyDirectoryChanged.count() != 1 ||
  79. spyDirectorySelected.count() != 1)
  80. {
  81. std::cerr<< "ctkDirectoryButton::setDirectory failed" << button.directory().toStdString() << std::endl;
  82. return EXIT_FAILURE;
  83. }
  84. spyDirectoryChanged.clear();
  85. spyDirectorySelected.clear();
  86. // set it again... just to check that it doesn't fire a new signal
  87. button.setDirectory(QDir::home().absolutePath());
  88. if ( QDir(button.directory()) != QDir::home() ||
  89. spyDirectoryChanged.count() != 0 ||
  90. spyDirectorySelected.count() != 1)
  91. {
  92. std::cerr<< "ctkDirectoryButton::setDirectory failed" << button.directory().toStdString() << std::endl;
  93. return EXIT_FAILURE;
  94. }
  95. topLevel.show();
  96. if (argc < 2 || QString(argv[1]) != "-I" )
  97. {
  98. QTimer::singleShot(400, &app, SLOT(quit()));
  99. }
  100. // If Qt uses the default native dialog, a nested event loop won't
  101. // be created and app.quit() will have no effect (as it solely quits
  102. // event loops).
  103. button.setOptions(button.options() | ctkDirectoryButton::DontUseNativeDialog);
  104. QTimer::singleShot(100, &button, SLOT(browse()));
  105. return app.exec();
  106. }