ctkPopupWidgetEventTranslatorPlayerTest1.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 <QAction>
  16. #include <QApplication>
  17. #include <QComboBox>
  18. #include <QDebug>
  19. #include <QMenu>
  20. #include <QPushButton>
  21. #include <QSignalSpy>
  22. #include <QStandardItemModel>
  23. #include <QTimer>
  24. #include <QToolButton>
  25. #include <QTreeView>
  26. #include <QVBoxLayout>
  27. // CTK includes
  28. #include "ctkCallback.h"
  29. #include "ctkCollapsibleButton.h"
  30. #include "ctkConfig.h"
  31. #include "ctkPopupWidget.h"
  32. #include "ctkPopupWidgetEventPlayer.h"
  33. #include "ctkPopupWidgetEventTranslator.h"
  34. #include "ctkEventTranslatorPlayerWidget.h"
  35. // QtTesting includes
  36. #include "pqTestUtility.h"
  37. // STD includes
  38. #include <cstdlib>
  39. #include <iostream>
  40. QWidget* createPanelTest(const QString& title, QList<ctkPopupWidget*>& popups);
  41. namespace
  42. {
  43. QSignalSpy *Spy1;
  44. QSignalSpy *Spy2;
  45. QSignalSpy *Spy3;
  46. QSignalSpy *Spy4;
  47. //-----------------------------------------------------------------------------
  48. void checkFinalWidgetState(void* data)
  49. {
  50. QWidget* parentWidget = reinterpret_cast<QWidget*>(data);
  51. QList<ctkPopupWidget*> widget = parentWidget->findChildren<ctkPopupWidget*>();
  52. QList<QComboBox*> widget2 = parentWidget->findChildren<QComboBox*>();
  53. if(widget.count())
  54. {
  55. CTKCOMPARE(Spy1->count(), 1);
  56. CTKCOMPARE(Spy2->count(), 1);
  57. CTKCOMPARE(Spy3->count(), 1);
  58. CTKCOMPARE(Spy4->count(), 1);
  59. }
  60. else if(widget2.count())
  61. {
  62. CTKCOMPARE(widget2[0]->currentIndex(), 2);
  63. }
  64. else
  65. {
  66. QApplication::exit(EXIT_FAILURE);
  67. }
  68. }
  69. }
  70. //-----------------------------------------------------------------------------
  71. int ctkPopupWidgetEventTranslatorPlayerTest1(int argc, char * argv [] )
  72. {
  73. QApplication app(argc, argv);
  74. QString xmlDirectory = CTK_SOURCE_DIR "/Libs/Widgets/Testing/Cpp/";
  75. // ------------------------
  76. ctkEventTranslatorPlayerWidget etpWidget;
  77. pqTestUtility* testUtility = new pqTestUtility(&etpWidget);
  78. etpWidget.setTestUtility(testUtility);
  79. etpWidget.addWidgetEventTranslator(new ctkPopupWidgetEventTranslator);
  80. etpWidget.addWidgetEventPlayer(new ctkPopupWidgetEventPlayer);
  81. // Test case 1
  82. QList<ctkPopupWidget*> popups;
  83. QWidget* widget = createPanelTest("Window opacity", popups);
  84. foreach(ctkPopupWidget* popup, popups)
  85. {
  86. popup->setAnimationEffect(ctkPopupWidget::WindowOpacityFadeEffect);
  87. }
  88. QSignalSpy spy1(popups[0], SIGNAL(popupOpened(bool)));
  89. QSignalSpy spy2(popups[1], SIGNAL(popupOpened(bool)));
  90. QSignalSpy spy3(popups[2], SIGNAL(popupOpened(bool)));
  91. QSignalSpy spy4(popups[3], SIGNAL(popupOpened(bool)));
  92. Spy1 = &spy1;
  93. Spy2 = &spy2;
  94. Spy3 = &spy3;
  95. Spy4 = &spy4;
  96. etpWidget.addTestCase(widget,
  97. xmlDirectory + "ctkPopupWidgetEventTranslatorPlayerTest1.xml",
  98. &checkFinalWidgetState);
  99. // ------------------------
  100. if (!app.arguments().contains("-I"))
  101. {
  102. QTimer::singleShot(0, &etpWidget, SLOT(play()));
  103. }
  104. etpWidget.show();
  105. return app.exec();
  106. }
  107. //-----------------------------------------------------------------------------
  108. QWidget* createPanelTest(const QString& title, QList<ctkPopupWidget*>& popups)
  109. {
  110. QWidget* topLevel = new QWidget(0);
  111. topLevel->setObjectName("topLevelWidget");
  112. topLevel->setWindowTitle(title);
  113. ctkCollapsibleButton* button = new ctkCollapsibleButton;
  114. QComboBox* focusComboBox = new QComboBox;
  115. focusComboBox->setObjectName("focusComboBox");
  116. focusComboBox->addItem("Focus popup");
  117. focusComboBox->addItem("Focus popup");
  118. focusComboBox->addItem("Focus popup");
  119. focusComboBox->addItem("Focus popup");
  120. QPushButton* openButton = new QPushButton("Open popup");
  121. openButton->setObjectName("openButton");
  122. QPushButton* toggleButton = new QPushButton("Toggle popup");
  123. toggleButton->setObjectName("toggleButton");
  124. toggleButton->setCheckable(true);
  125. QToolButton* pinButton = new QToolButton(0);
  126. pinButton->setCheckable(true);
  127. QVBoxLayout* collapsibleLayout = new QVBoxLayout;
  128. collapsibleLayout->addWidget(focusComboBox);
  129. button->setLayout(collapsibleLayout);
  130. QVBoxLayout* vlayout = new QVBoxLayout;
  131. vlayout->addWidget(button);
  132. vlayout->addWidget(openButton);
  133. vlayout->addWidget(toggleButton);
  134. vlayout->addWidget(pinButton);
  135. topLevel->setLayout(vlayout);
  136. ctkPopupWidget* focusPopup = new ctkPopupWidget(focusComboBox);
  137. focusPopup->setObjectName("focusPopup");
  138. focusPopup->setAutoShow(true);
  139. focusPopup->setAutoHide(true);
  140. QPushButton* focusPopupContent = new QPushButton("button");
  141. focusPopupContent->setObjectName("focusPopupContent");
  142. QToolButton* popupToolButton = new QToolButton;
  143. popupToolButton->setObjectName("popupToolButton");
  144. QMenu* menu = new QMenu(popupToolButton);
  145. menu->setObjectName("menu");
  146. menu->addAction("first menu item");
  147. menu->addAction("second menu item");
  148. menu->addAction("third menu item");
  149. menu->addAction("fourth menu item");
  150. popupToolButton->setPopupMode(QToolButton::InstantPopup);
  151. popupToolButton->setMenu(menu);
  152. QHBoxLayout* focusLayout = new QHBoxLayout;
  153. focusLayout->addWidget(focusPopupContent);
  154. focusLayout->addWidget(popupToolButton);
  155. focusPopup->setLayout(focusLayout);
  156. focusLayout->setContentsMargins(0,0,0,0);
  157. QPalette palette = focusPopup->palette();
  158. QLinearGradient linearGradient(QPointF(0.f, 0.f), QPointF(0.f, 0.666f));
  159. linearGradient.setSpread(QGradient::PadSpread);
  160. linearGradient.setCoordinateMode(QGradient::StretchToDeviceMode);
  161. linearGradient.setColorAt(0, palette.color(QPalette::Window));
  162. linearGradient.setColorAt(1, palette.color(QPalette::Dark));
  163. palette.setBrush(QPalette::Window, linearGradient);
  164. focusPopup->setPalette(palette);
  165. ctkPopupWidget* openPopup = new ctkPopupWidget(openButton);
  166. openPopup->setObjectName("openPopup");
  167. openPopup->setFrameStyle(QFrame::Box);
  168. openPopup->setLineWidth(1);
  169. openPopup->setAutoShow(false);
  170. openPopup->setAutoHide(false);
  171. openPopup->setWindowOpacity(0.7);
  172. QPushButton* openPopupContent = new QPushButton("Close popup");
  173. openPopupContent->setObjectName("openPopupContent");
  174. QVBoxLayout* openLayout = new QVBoxLayout;
  175. openLayout->addWidget(openPopupContent);
  176. openPopup->setLayout(openLayout);
  177. QObject::connect(openButton, SIGNAL(clicked()),
  178. openPopup, SLOT(showPopup()));
  179. QObject::connect(openPopupContent, SIGNAL(clicked()),
  180. openPopup, SLOT(hidePopup()));
  181. ctkPopupWidget* togglePopup = new ctkPopupWidget(toggleButton);
  182. togglePopup->setObjectName("togglePopup");
  183. togglePopup->setAutoShow(false);
  184. togglePopup->setAutoHide(false);
  185. QPushButton* togglePopupContent = new QPushButton("tooltip button");
  186. togglePopupContent->setObjectName("togglePopupContent");
  187. togglePopupContent->setToolTip("tooltip");
  188. QVBoxLayout* toggleLayout = new QVBoxLayout;
  189. toggleLayout->addWidget(togglePopupContent);
  190. togglePopup->setLayout(toggleLayout);
  191. QObject::connect(toggleButton, SIGNAL(toggled(bool)),
  192. togglePopup, SLOT(showPopup(bool)));
  193. togglePopup->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  194. ctkPopupWidget* pinPopup = new ctkPopupWidget(pinButton);
  195. pinPopup->setObjectName("pinPopup");
  196. QPushButton* pinPopupContent = new QPushButton("pin button");
  197. pinPopupContent->setCheckable(true);
  198. QObject::connect(pinPopupContent, SIGNAL(toggled(bool)),
  199. pinButton, SLOT(setChecked(bool)));
  200. QObject::connect(pinButton, SIGNAL(toggled(bool)),
  201. pinPopupContent, SLOT(setChecked(bool)));
  202. pinPopupContent->setObjectName("pinPopupContent");
  203. QVBoxLayout* pinLayout = new QVBoxLayout;
  204. pinLayout->addWidget(pinPopupContent);
  205. pinPopup->setLayout(pinLayout);
  206. QObject::connect(pinButton, SIGNAL(toggled(bool)),
  207. pinPopup, SLOT(pinPopup(bool)));
  208. popups << focusPopup << openPopup << togglePopup << pinPopup;
  209. return topLevel;
  210. }